This repository was archived by the owner on Sep 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageProcessor.py
More file actions
88 lines (76 loc) · 3.3 KB
/
MessageProcessor.py
File metadata and controls
88 lines (76 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#***************************************
# Filename: MessageProcessor.py
#
# Description:
# This module processes all of the sent and
# recieved Message packets.
#
#***************************************
#Import Constants
from Constants import *
#Lib imports:
from threading import Thread
import string
class MessageProcessor(Thread):
def __init__(self, ViewControllerPlug, DataStoragePlug, PacketManagerPlug):
Thread.__init__(self)
self.ViewControllerPlug = ViewControllerPlug
self.DataStoragePlug = DataStoragePlug
self.PacketManagerPlug = PacketManagerPlug
def run(self):
if verbose: print "MessageProcessor Start"
myAddress = None
theNodeisOn = False
Exit = False
while(not Exit):
#check the view plug for messages
if not self.ViewControllerPlug.isEmpty(__name__):
viewControllerMessage = self.ViewControllerPlug.get(__name__)
if viewControllerMessage != None:
if viewControllerMessage == ViewExit:
Exit = True
elif isReturnNewMessage(viewControllerMessage):
recievedMessage = viewControllerMessage[1]
if verbose: print "MessPross got message: " + str(recievedMessage)
if recievedMessage[0] != '' and recievedMessage[1] != '' and theNodeisOn and myAddress != None:
#Here, we build the message to send.
if verbose: print "MessPross building mess"
payload = str(ttl_value) + "/" + str(recievedMessage[0]) + "/" + myAddress + "/" + str(recievedMessage[1])
finalDest = str(recievedMessage[0])
#Ask the dataStorage who we should send this message to:
self.DataStoragePlug.put((MessMessage, finalDest, payload), __name__)
#check the data plug for messages
if not self.DataStoragePlug.isEmpty(__name__):
dataStorageMessage = self.DataStoragePlug.get(__name__)
if dataStorageMessage != None:
if isMessMessage(dataStorageMessage):
bestNext = dataStorageMessage[1]
payload = dataStorageMessage[2]
if verbose: print "HEY GOT THE MESS BACK FROM DATA: " + str(bestNext)
self.PacketManagerPlug.put((MessMessage, myAddress, bestNext, payload), __name__)
#check the packetmanager plug for messages
if not self.PacketManagerPlug.isEmpty(__name__):
packetManagerMessage = self.PacketManagerPlug.get(__name__)
if packetManagerMessage != None:
if packetManagerMessage == NodeOn:
theNodeisOn = True
elif packetManagerMessage == NodeOff:
theNodeisOn = False
elif isAddressMessage(packetManagerMessage):
myAddress = packetManagerMessage[1]
elif isMessMessage(packetManagerMessage):
print "Recieved message! MESSAGE PROCESSOR : " + str(packetManagerMessage[1:])
recievedMessagePayload = packetManagerMessage[2]
listFromPayload = recievedMessagePayload.split("/")
ttl_fromPayload = listFromPayload[0]
theFinalDest = listFromPayload[1]
theOriginator = listFromPayload[2]
theMessage = listFromPayload[3]
if theFinalDest == myAddress:
self.ViewControllerPlug.put((ReturnNewMessage, (theOriginator, theMessage)), __name__)
else:
if ttl_fromPayload > 0:
listFromPayload[0] = str(int(ttl_fromPayload)-1)
newMessage = string.join(listFromPayload, "/")
self.DataStoragePlug.put((MessMessage, theFinalDest, newMessage), __name__)
if verbose: print "MessageProcessor Exit"