| 44 | # The message object represents a FreakWAN message, and is also responsible |
| 45 | # of the decoding and encoding of the messages to be sent to the "wire". |
| 46 | class Message: |
| 47 | def __init__(self, nick="", text="", media_type=255, media_data=False, uid=False, ttl=15, mtype=MessageTypeData, sender=False, flags=0, rssi=0, ack_type=0, seen=0, key_name=None, pinger=None, ping_rssi=0, ping_t0_ms=0): |
| 48 | self.ctime = time.ticks_ms() # To evict old messages |
| 49 | |
| 50 | # send_time is only useful for sending, to introduce a random delay. |
| 51 | self.send_time = self.ctime |
| 52 | |
| 53 | # Number of times to transmit this message. Each time the message |
| 54 | # is transmitted, this value is reduced by one. When it reaches |
| 55 | # zero, the message is removed from the send queue. |
| 56 | self.num_tx = 1 |
| 57 | self.acks = {} # Device IDs we received ACKs from |
| 58 | self.type = mtype |
| 59 | self.flags = flags |
| 60 | self.nick = nick |
| 61 | self.text = text |
| 62 | self.media_type = media_type |
| 63 | self.media_data = media_data |
| 64 | self.uid = uid if uid != False else self.gen_uid() |
| 65 | self.sender = sender if sender != False else self.get_this_sender() |
| 66 | self.ttl = ttl # Only DATA |
| 67 | self.ack_type = ack_type # Only ACK |
| 68 | self.seen = seen # Only HELLO |
| 69 | self.pinger = pinger # Only PONG (target of the pong) |
| 70 | self.ping_rssi = ping_rssi # Only PONG (RSSI of original PING) |
| 71 | self.ping_t0_ms = ping_t0_ms # PING/PONG (timestamp for RTT) |
| 72 | self.rssi = rssi |
| 73 | self.key_name = key_name |
| 74 | self.no_key = False # True if it was not possible to decrypt. |
| 75 | |
| 76 | # If key_name is set, encoded messages will be encrypted, too. |
| 77 | # When messages are decoded, key_name is set to the key that |
| 78 | # decrypted the message, if any. |
| 79 | |
| 80 | # Sometimes we want to supporess sending of packets that may |
| 81 | # already be inside the TX queue. Instead of scanning the queue |
| 82 | # to look for the message, we just set this flag to True. |
| 83 | self.send_canceled = False |
| 84 | |
| 85 | # Generate a 32 bit unique message ID. |
| 86 | def gen_uid(self): |
| 87 | return urandom.getrandbits(32) |
| 88 | |
| 89 | # Get the sender address for this device. We just take 6 bytes |
| 90 | # of the device unique ID. |
| 91 | def get_this_sender(self): |
| 92 | return machine.unique_id()[-6:] |
| 93 | |
| 94 | # Return the sender as a printable hex string. |
| 95 | def sender_to_str(self): |
| 96 | if self.sender: |
| 97 | s = self.sender |
| 98 | return "%02x%02x%02x%02x%02x%02x" % (s[0],s[1],s[2],s[3],s[4],s[5]) |
| 99 | else: |
| 100 | return "ffffffffffff" |
| 101 | |
| 102 | # Turn the message into its binary representation. |
| 103 | def encode(self,keychain=None): |
no outgoing calls
no test coverage detected