def serialize(self): # create outer packet message packetMessage = protoPackets_pb2.PacketMessage() packetMessage.type = protoPackets_pb2.PacketMessage.BID packetMessage.transactionID = self.transactionID # add Bid-specific content packetMessage.bidMessage.bid = self.bid return packetMessage.SerializeToString()
def serialize(self): # create outer packet message packetMessage = protoPackets_pb2.PacketMessage() packetMessage.type = protoPackets_pb2.PacketMessage.BIDWIN packetMessage.transactionID = self.transactionID # add Bid-specific content packetMessage.bidWinMessage.winnerIP = self.winnerIP packetMessage.bidWinMessage.winningBid = self.winningBid packetMessage.bidWinMessage.fine = self.fine return packetMessage.SerializeToString()
def serialize(self): # create outer packet message packetMessage = protoPackets_pb2.PacketMessage() packetMessage.type = protoPackets_pb2.PacketMessage.DATA packetMessage.transactionID = self.transactionID # add Data-specific content packetMessage.dataMessage.finalDestinationIP = self.finalDestinationIP packetMessage.dataMessage.deadline = self.deadline packetMessage.dataMessage.fine = self.fine packetMessage.dataMessage.initialBudget = self.initialBudget packetMessage.dataMessage.payload = self.payload return packetMessage.SerializeToString()
def serialize(self): # create outer packet message packetMessage = protoPackets_pb2.PacketMessage() packetMessage.type = protoPackets_pb2.PacketMessage.ADVERT packetMessage.transactionID = self.transactionID # add advert-specific content packetMessage.advertMessage.finalDestinationIP = self.finalDestinationIP packetMessage.advertMessage.ceil = self.ceil packetMessage.advertMessage.deadline = self.deadline packetMessage.advertMessage.fine = self.fine packetMessage.advertMessage.initialBudget = self.initialBudget return packetMessage.SerializeToString()
def serializeDelimited(self): # create outer packet message packetMessage = protoPackets_pb2.PacketMessage() packetMessage.type = protoPackets_pb2.PacketMessage.CHECK packetMessage.transactionID = self.transactionID # add check-specific content packetMessage.checkMessage.newBalance = self.newBalance for (transactionID, amount) in self.balanceUpdates: x = packetMessage.checkMessage.balanceUpdates.add() x.transactionID = transactionID x.amount = amount print self serializedMessage = packetMessage.SerializeToString() delimiter = encoder._VarintBytes(len(serializedMessage)) return delimiter + serializedMessage
class Packet: packetMessage = protoPackets_pb2.PacketMessage() PACKET_PORT = 8765 transactionID = sourceIP = destinationIP = "" def buildPayload(self): raise NotImplementedError( "Packet does not implement this." " Please use Bid, BidWin, Advert, Check or Data") def toJSON(self): raise NotImplementedError("don't forget to code this!") # returns the port the packets we be send to. def getPort(self): return self.PACKET_PORT # returns the destination this packet will be sent to # or the one it has been sent to. def getDestinationIP(self): return self.destinationIP # returns the source of this packet (or null if it hasn't been sent). def getSource(self): return self.sourceIP # returns the transaction ID of this packet. def getTransactionID(self): return self.transactionID # determine the packet type, then parse accordingly. # returns Advert/Bid/BiwdWin/Chcek/Data packet if type is kown, None otherwise. def parse(self, payload, sourceIP): try: self.packetMessage.ParseFromString(payload) packet_type = self.packetMessage.type if (packet_type == 0): p = Advert() elif (packet_type == 1): p = Bid() elif (packet_type == 2): p = BidWin() elif (packet_type == 3): p = Check() elif (packet_type == 4): p = Data() elif (packet_type == 5): p = GeneralPurposePacket() else: return None p.parse(self.packetMessage) p.sourceIP = sourceIP return p except: print payload, payload[1] #traceback.print_exc(file=sys.stdout) return None