Home | Trees | Indices | Help |
|
---|
|
1 # 2 # Copyright (C) 2006, Matt Sullivan <matts@zarrf.com> 3 # 4 # Permission is hereby granted, free of charge, to any person obtaining 5 # a copy of this software and associated documentation files (the 6 # "Software"), to deal in the Software without restriction, including 7 # without limitation the rights to use, copy, modify, merge, publish, 8 # distribute, sublicense, and/or sell copies of the Software, and to 9 # permit persons to whom the Software is furnished to do so, subject to 10 # the following conditions: 11 # 12 # The above copyright notice and this permission notice shall be included 13 # in all copies or substantial portions of the Software. 14 # 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 18 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 19 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 20 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 21 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 # 23 24 from spoon import Serial, SpoonStream, serialprop, __SPOONLINKMSG_TAG__ 25 from spoon import NullLogger, LMTYPE_INIT, LMTYPE_NETWORK, LMTYPE_NETWORK_PROTO 26 from spoon import ber 27 28 from threading import Thread 29 30 3133 """ 34 This object is sent as a very simple form of initial negotiation. 35 It contains the nodeId and the protocol version (currently always 36 1.) 37 """ 38 nodeId = serialprop() 39 version = serialprop(1)4042 """ 43 Link message is the wrapper which will contain messages sent between two directly connected nodes. 44 This message just consists of a msg type (an int) and some arbitrary attachment. The msgtype 45 determines what system will deal with the message. 46 Since this is strictly for use between directly connected nodes, there's no need for src or destination 47 fields. 48 """52 53 @ber.encoder(LinkMessage)50 self.msgtype = msgtype 51 self.attach = attach55 ber.Tag.from_tag(__SPOONLINKMSG_TAG__, None).write(fd) 56 b = ber.BERStream(fd) 57 b.add(obj.msgtype) 58 b.add(obj.attach) 59 b._add_eof()60 @ber.decoder(__SPOONLINKMSG_TAG__)62 out = LinkMessage() 63 b = ber.BERStream(fd) 64 out.msgtype = b.next() 65 out.attach = b.next() 66 # Pull the EOF off the stream 67 if b.has_next(): 68 pass 69 return out7072 pass7375 """ 76 Where all of your transports connect to form your glorious new node. 77 78 B{Important} you must set the nodeId on this object to something unique for the networks you are 79 joining in order for things to work. 80 81 @cvar activeTransports: A simple list of the transports that are currently active 82 @cvar links: A dict, keys are the node id of the directly connected neighbor and the values are 83 the associated transport 84 @cvar nodeId: The local node id. This must be set to the node's integer id before the spoon transport hub is started. 85 The nodeId is just a network wide, unique int. How this is determined is left as an excercise for the implementation. 86 In most cases, it should probably be something that is constant for the host/program between instances. 87 @type nodeId: int 88 """ 89 activeTransports = [] 90 links = {} 91 nodeId = None 92 _log = NullLogger() 93 94 @staticmethod15696 """ 97 Sets a logger object for SpoonRPC to use. 98 99 This can be a python logger object, or just anything that supports that general protocol. 100 It defaults to NullLogger which does nothing with the messages. 101 """ 102 TransportHub._log = logger103 104 @staticmethod106 """ 107 Must be called after a transport is initialized to initiate the spoonRPC protocol. 108 109 @param t: The transport being initialized 110 @return: Nothing 111 @raise TransportException: If the protocol initialization fails for some reason. 112 """ 113 if TransportHub.nodeId == None: 114 raise TransportException("Cannot initialize transport until nodeId is set on TransportHub. \ 115 Use spoon.transports.TransportHub.nodeId = someint") 116 # This is our protocol initilization, just a simple exchange of nodeId 117 hello = SpoonRPCHello() 118 hello.nodeId = TransportHub.nodeId 119 lmhello = LinkMessage(LMTYPE_INIT, hello) 120 t.spoon.write(lmhello) 121 remoteLmHello = t.spoon.read() 122 remoteHello = remoteLmHello.attach 123 if (type(remoteLmHello) != LinkMessage) or (type(remoteHello) != SpoonRPCHello): 124 TransportHub._log.error("Did not get proper Hello message from neighbor across transport "+repr(t)) 125 raise TransportException("Did not get proper Hello message from neighbor across transport "+repr(t)) 126 127 # TODO Implement something to check that the transport authenticated as the proper node if it supports such a thing 128 if TransportHub.links.has_key(remoteHello.nodeId): 129 # Try to remove any transports we currently have to this nodeid 130 try: 131 TransportHub.activeTransports.remove(TransportHub.links[remoteHello.nodeId]) 132 except: 133 pass 134 del TransportHub.links[remoteHello.nodeId] 135 t.nodeId = remoteHello.nodeId 136 TransportHub.links[remoteHello.nodeId] = t 137 TransportHub.activeTransports.append(t) 138 # TODO possibly refactor this to handle other message types, but we'll figure that out later. 139 tNetwork = t.getNetwork() 140 if tNetwork: 141 tNetwork.addTransport(t, t.nodeId)142 143 @staticmethod145 """ 146 Must be called after a transport has been made inactive. 147 148 """ 149 if not TransportHub.links.has_key(t.nodeId): 150 TransportHub._log.warn("Removing transport for nodeId %d and it wasn't in the links dict."%t.nodeId) 151 raise TransportException("Removing transport for nodeId %d and it wasn't in the links dict."%t.nodeId) 152 del TransportHub.links[t.nodeId] 153 TransportHub.activeTransports.remove(t) 154 # TODO possibly refactor this to handle other message types, but we'll figure that out later. 155 t.getNetwork().removeTransport(t)
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0alpha3 on Mon Nov 27 18:56:25 2006 | http://epydoc.sourceforge.net |