def set(self, name, value): """ Reception of a SET order. Modification of a characteristic of the node. E.g. the pseudo set('Pseudo', 'john')""" factory = EventFactory.getInstance(ControlEvent.TYPE) set = factory.createSET(name, value) self.incoming.put(set) return self.ok
def addService(self, srvId, srvDesc, srvConnectionString): """ add a new service to the node. """ factory = EventFactory.getInstance(ControlEvent.TYPE) addSrv = factory.createADDSERVICE(srvId, srvDesc, srvConnectionString) self.incoming.put(addSrv) return self.ok
def TIMER(self, event): manager = self.node.getPeersManager() nbSeeds = len(self.portList) nbPeers = manager.getNumberOfPeers() # we have connected to all other seeds if nbPeers == nbSeeds -1 : ar = manager.computeAwarenessRadius() self.node.setAwarenessRadius(ar) #self.node.setExpectedPeers(nbPeers) self.node.startPeriodicTasks() self.sendUpdates() self.node.setState(state.TooManyPeers()) else: self.logger.debug('Not connected to all seeds: %d/%d', nbPeers, nbSeeds -1) # relaunch timer self.startTimer() connectedPorts = [] for peer in manager.enumeratePeers(): connectedPorts.append(peer.getAddress().getPort()) #self.logger.debug('ports: %s' , str(connectedPorts)) for port in self.portList: if (port <> self.node.getAddress().getPort()) and \ ( port not in connectedPorts): f = EventFactory.getInstance(PeerEvent.TYPE) hello = f.createHELLO() hello.setRecipientAddress(Address(self.node.host, port)) self.node.dispatch(hello)
def disconnect(self): """ Disconnect node Return OK """ factory = EventFactory.getInstance(ControlEvent.TYPE) controlEvent = factory.createDISCONNECT self.incoming.put(controlEvent) return self.ok
def getNodeInfo(self): """ Return all the caracteristics of the node [id, positionX, positionY, AR, CA, pseudo, orientation ] """ factory = EventFactory.getInstance(ControlEvent.TYPE) controlEvent = factory.createGETNODEINFO() self.incoming.put(controlEvent) return self.ok
def delService(self, srvId): """ Remove a service srvId : id of the service to remove Return : OK """ factory = EventFactory.getInstance(ControlEvent.TYPE) rmSrv = factory.createDELSERVICE(srvId) self.incoming.put(rmSrv) return self.ok
def getPeerInfo(self, id): """ Return all the caracteristics of a peer [id, positionX, positionY, AR, CA, pseudo, orientation ] id : id of the peer """ factory = EventFactory.getInstance(ControlEvent.TYPE) controlEvent = factory.createGETPEERINFO(id) self.incoming.put(controlEvent) return self.ok
def move(self, x, y, z): """ Reception of a move order x,y,z : target coordinates. These parameters are passed as string to avoid int overflow problems """ assert z == 0 pos = Position(long(x), long(y), long(z)) factory = EventFactory.getInstance(ControlEvent.TYPE) move = factory.createMOVE(pos) self.incoming.put(move) return self.ok
def kill(self): """ Kill the node and stop connection betwwen navigator and node """ # send a KILL order to the node factory = EventFactory.getInstance(ControlEvent.TYPE) kill = factory.createKILL() self.incoming.put(kill) # stop this connector self.connector.stop() return self.ok
def __init__(self, params): """ All informations about this node: host --> ip if the node, network_port, GUI_port, posX, posY, ar, ca, ori --> a point, exp --> expected number of neighbour, pseudo""" self.params=params # event queue, used for inter-thread communication self.events = NotificationQueue() self.connectors = [] EventFactory.init(self) EventFactory.register(PeerEventFactory.TYPE, PeerEventFactory()) EventFactory.register(ControlEventFactory.TYPE, ControlEventFactory()) # network communication with peers is handled by this object self.peerConnector = UDPConnector(PeerEventParser(), self.events, params) self.connectors.append(self.peerConnector) if (not params.bot): self.controlConnector = XMLRPCConnector(ControlEventParser(), self.events, params) self.connectors.append(self.controlConnector) else: self.controlConnector = None id_ = self.createId() position = Position(params.pos_x, params.pos_y) address = Address(params.host, params.port) # call parent class constructor Entity.__init__(self, id_, position, params.orientation, params.awareness_radius, params.calibre, params.pseudo, address) # maximum expected number of neighbours. self.exp = params.expected_neighbours # our IP address or 'localhost' if not specified in config file self.host = params.host self.alive = True self.logger = logging.getLogger('root') self.logger.debug('node started') # manage all peers #peersParams = params.getPeersParams() self.peersManager = PeersManager(self, params) # set world size in Geometry class Geometry.SIZE = params.world_size # periodic tasks self.periodic = [] self.state = None self.setState(state.NotConnected())