def init(): """ Initialize the connections. Let's get it started...in here! Developer note: the advantage of putting it here is that me.py doesn't need to import anything. """ global neighborStrategy me.init(nodes.CurrentNode()) debug("Init called. Node is " + me.getUid(), info=True) debug("#".join(["New", me.getMe().getShortUid(), me.getUid()]), monitor=True) neighborStrategy = neighbors.neighborStrategyFactory( config.GOSSIP_NEIGHBOR_STRATEGY)
def network_load_single_stat(): """ Give back a single number to represent network load. """ receivedPackets, sentPackets, receivedBytes, sentBytes = network_load() debug("Load#" + me.getUid() + "#" + str(receivedPackets + sentPackets), monitor=True) return receivedBytes + sentBytes
def getNeighbors(self): """ Get a single neighbor. """ sortedkeys = self._universeUids() ix = sortedkeys.index(me.getUid()) if ix == (len(sortedkeys)-1): self.neighborSet = sortedkeys[0] else: self.neighborSet = sortedkeys[ix+1] return self.neighborSet
def getNeighbors(self): """ Get a single neighbor. """ sortedkeys = self._universeUids() ix = sortedkeys.index(me.getUid()) if ix == (len(sortedkeys) - 1): self.neighborSet = sortedkeys[0] else: self.neighborSet = sortedkeys[ix + 1] return self.neighborSet
def connectionMade(self): """ Callback when a connection is made. """ debug("Client Protocol connection made", success=True, threshold=1) connections.clientConnectionMade(self.transport) ### Send a message to respond. #response = message.MeMessage() #response.send(self.transport) self.transport.write(me.getUid())
def connectionMade(self): """ Callback called once a connection is made with a client. """ debug("Server protocol connection made!", success=True, threshold=1) self.factory.clientConnectionMade(self) # Send a response #response = message.MeMessage() #response.send(self.transport) self.transport.write(me.getUid())
def getNeighbors(self): """ Get Neighbors """ unilength = len(self._universeUids()) if unilength > 2: if not self.count: idealcount = int(math.ceil(math.log10(unilength))) self.count = max(2, idealcount) samplespace = self._universeUids() if me.getUid() in samplespace: samplespace.remove(me.getMe().getUid()) self.neighborSet = random.sample(samplespace, self.count) return set(self.neighborSet) else: return set([])
def log(typ, msg, level, ordered=True): """ Log a received piece of data from the api. Can be ordered or not. """ try: item = {"type":typ,"message":msg,"level":level,"time":time.time()} if ordered: item["machine"] = me.getUid() item["clock"] = me.getMe().getVectorClock().getClocks() insert(item) debug("logged item", success=True) except Exception, e: debug(e, error=True) debug("did not log message (ordered:" + str(ordered) + ")", error=True)
def membersRefresh(): """ Run members refresh operation """ global members try: oldKeys = set(members.keys()) for key in members: members_to_delete.add(key) members.clear() newMembersDict = simpledb.getSet(ITEMKEY) if (newMembersDict != None) and (len(newMembersDict) > 0): #newmembers = joined.split(GLUE) for memberUid in newMembersDict: if len(memberUid) > 6: memberNode = nodes.buildNode(newMembersDict[memberUid]) if not me.getMe().__eq__(memberNode): result = True # TODO Really should send a noop. if result: members[memberNode.getUid()] = memberNode else: debug("Noop failed. Node removed.", info=True) else: pass members[me.getUid()] = me.getMe().getBaseData() for key in members: if key in members_to_delete: members_to_delete.remove(key) debug("Members refresh procedure ran.", success=True) debug("There are " + str(len(members)) + " members in the system.", info=True) if set(members.keys()) == oldKeys: """ We have reached stable state """ gossip.quitMembersRefresh() connections.informAlive() except Exception as e: debug(e) traceback.print_exc(file=sys.stdout) persistSet()
def send(self, transport=None): """ Send the message. Assume we don't have multicast, so just do point to point for now. """ if not self._recipients or len(self.getRecipients()) == 0: debug("No recipients specified.", error=True) raise GeneralError("No recipients specified.") if len(self.getRecipients()) == 1 and transport: node = connections.lookupNode(self.getRecipients()[0]) hissclient = connections.HissConnection(node, transport) hissclient.dispatchMessage(self) recs = [] for recipient in self.getRecipients(): uid = recipient if not isinstance(recipient, str): # Handles exception case if this were a node instead of a UID. uid = recipient.getUid() if uid in connections.universe: node = connections.lookupNode(uid) if not node.hasTCPConnection(): debug("No connection to " + node.getShortUid(), error=True) else: # Ok, stop messing around and send the message! try: tcpConn = node.getTCPConnection().dispatchMessage( self) recs.append(node.getShortUid()) debug("#".join( ["Msg", me.getUid(), uid, self.getCode()]), monitor=True) except: debug("Failed to send message to [ " \ + node.getShortUid() + " ]", error=True) else: raise GeneralError( "recipient " + uid + " not found.") if self.getCode() != 'AG': debug("(" + self.getCode() + ") message sent to [ " \ + " ][ ".join(recs) + " ]", success=True)
def send(self, transport=None): """ Send the message. Assume we don't have multicast, so just do point to point for now. """ if not self._recipients or len(self.getRecipients()) == 0: debug("No recipients specified.", error=True) raise GeneralError("No recipients specified.") if len(self.getRecipients()) == 1 and transport: node = connections.lookupNode(self.getRecipients()[0]) hissclient = connections.HissConnection(node, transport) hissclient.dispatchMessage(self) recs = [] for recipient in self.getRecipients(): uid = recipient if not isinstance(recipient, str): # Handles exception case if this were a node instead of a UID. uid = recipient.getUid() if uid in connections.universe: node = connections.lookupNode(uid) if not node.hasTCPConnection(): debug("No connection to " + node.getShortUid(), error=True) else: # Ok, stop messing around and send the message! try: tcpConn = node.getTCPConnection().dispatchMessage(self) recs.append(node.getShortUid()) debug("#".join( ["Msg", me.getUid(), uid, self.getCode()]), monitor=True) except: debug("Failed to send message to [ " \ + node.getShortUid() + " ]", error=True) else: raise GeneralError("recipient " + uid + " not found.") if self.getCode() != 'AG': debug("(" + self.getCode() + ") message sent to [ " \ + " ][ ".join(recs) + " ]", success=True)
def createAggregateMessage(agg): """ Build an aggregate message for the aggregation in the param. """ return AggregateMessage(agg, me.getUid())