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 gossip(self): """ Gossip procedure. This is basic. Hope to improve later. """ if connections.connectToNeighbors(): debug("Connections in process. deferred gossip", info=True) return # Get my neighbors recipients = connections.getNeighbors() if len(recipients) > 0: shortids = [] for uid in recipients: shortids.append(connections.lookupNode(uid).getShortUid()) debug("Gossipping with: [ " + " ][ ".join(shortids) + " ]", info=True) else: debug("No neighbors to gossip with this interval.", error=True, threshold=1) return # Put all messages in a list. gossipMessages = [] # Get a vector clock message vcMessage = message.VectorMessage.createVectorClockMessage() vcMessage.setRecipients(recipients) gossipMessages.append(vcMessage) # Put in each aggreggation. Tae out for now. for aggName in aggregation.STATISTICS: agg = aggregation.STATISTICS[aggName] aggMessage = message.AggregateMessage.createAggregateMessage(agg) aggMessage.setRecipients(recipients) gossipMessages.append(aggMessage) """ agg = random.choice(aggregation.STATISTICS.values()) aggMessage = message.AggregateMessage.createAggregateMessage(agg) aggMessage.setRecipients(recipients) gossipMessages.append(aggMessage) """ # Get a network message gossipmsg = gossipPrepare() while gossipmsg: gossipmsg.setRecipients(recipients) toAppend = copy.deepcopy(gossipmsg) gossipMessages.append(toAppend) gossipmsg = gossipPrepare() debug("There are " \ + str(len(gossipMessages)) + " to send.", threshold=2, info=True) # Send out the messages for msg in gossipMessages: try: msg.send() except ConnectionError as ce: debug(ce.__str__(), error=True) except GeneralError as ge: debug(ge.__str__(), error=True)