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 79 @cvar activeTransports: A simple list of the transports that are currently active 80 @cvar links: A dict, keys are the node id of the directly connected neighbor and the values are 81 the associated transport 82 @cvar nodeId: The local node id. This must be set to the node's integer id before the spoon transport hub is started. 83 The nodeId is just a network wide, unique int. How this is determined is left as an excercise for the implementation. 84 In most cases, it should probably be something that is constant for the host/program between instances. 85 @type nodeId: int 86 """ 87 activeTransports = [] 88 links = {} 89 nodeId = None 90 _log = NullLogger() 91 92 @staticmethod15194 """ 95 Sets a logger object for SpoonRPC to use. 96 97 This can be a python logger object, or just anything that supports that general protocol. 98 It defaults to NullLogger which does nothing with the messages. 99 """ 100 TransportHub._log = logger101 102 @staticmethod104 """ 105 Must be called after a transport is initialized to initiate the spoonRPC protocol. 106 107 @param t: The transport being initialized 108 @return: Nothing 109 @raise TransportException: If the protocol initialization fails for some reason. 110 """ 111 # This is our protocol initilization, just a simple exchange of nodeId 112 hello = SpoonRPCHello() 113 hello.nodeId = TransportHub.nodeId 114 lmhello = LinkMessage(LMTYPE_INIT, hello) 115 t.spoon.write(lmhello) 116 remoteLmHello = t.spoon.read() 117 remoteHello = remoteLmHello.attach 118 if (type(remoteLmHello) != LinkMessage) or (type(remoteHello) != SpoonRPCHello): 119 TransportHub._log.error("Did not get proper Hello message from neighbor across transport "+repr(t)) 120 raise TransportException("Did not get proper Hello message from neighbor across transport "+repr(t)) 121 122 # TODO Implement something to check that the transport authenticated as the proper node if it supports such a thing 123 if TransportHub.links.has_key(remoteHello.nodeId): 124 # Try to remove any transports we currently have to this nodeid 125 try: 126 TransportHub.activeTransports.remove(TransportHub.links[remoteHello.nodeId]) 127 except: 128 pass 129 del TransportHub.links[remoteHello.nodeId] 130 t.nodeId = remoteHello.nodeId 131 TransportHub.links[remoteHello.nodeId] = t 132 TransportHub.activeTransports.append(t) 133 # TODO possibly refactor this to handle other message types, but we'll figure that out later. 134 tNetwork = t.getNetwork() 135 if tNetwork: 136 tNetwork.addTransport(t, t.nodeId)137 138 @staticmethod140 """ 141 Must be called after a transport has been made inactive. 142 143 """ 144 if not TransportHub.links.has_key(t.nodeId): 145 TransportHub._log.warn("Removing transport for nodeId %d and it wasn't in the links dict."%t.nodeId) 146 raise TransportException("Removing transport for nodeId %d and it wasn't in the links dict."%t.nodeId) 147 del TransportHub.links[t.nodeId] 148 TransportHub.activeTransports.remove(t) 149 # TODO possibly refactor this to handle other message types, but we'll figure that out later. 150 t.getNetwork().removeTransport(t)
Home | Trees | Indices | Help |
---|
Generated by Epydoc 3.0alpha3 on Mon Nov 27 16:05:22 2006 | http://epydoc.sourceforge.net |