-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.py
executable file
·213 lines (165 loc) · 6.63 KB
/
controller.py
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import unittest
from model import Calendar
from promptHelper import addEventParsing
from promptHelper import deleteEventParsing
from cyrusbus import Bus
from EventTypeEnum import EventType
import pickle
import os
import algHelper
__author__ = 'Mark Plagge'
#UserEvent: THESE are what go into the logs. Store a type, the local time of the user that submitted it stored in T_i[i,i],
# the event itself and the id of the user
# CONTROLLLER NEEDS NEW FEATURES
class process(object):
def __init__(self, uid, uRank, nprocs):
self.lamportClock = 0
self.uid = uid
self.theCalendar = Calendar(uid)
self.tt = [[0 for j in range(nprocs)]for i in range(nprocs)]
self.log = []
self.uRank = uRank
self.nprocs = nprocs
# self.pid = pid
def prompt(self):
input() #THIS GETS RID OF THE FIRST THING THAT THE USER INPUT TO BRING UP THE PROMPT FROM THREADSERVER
prompt1 = "1. View your calendar\n"
prompt2 = "2. Add an event to your local calendar\n"
prompt3 = "3. Delete an event from your calendar\n"
prompt0 = "0. Test Event Send\n"
prompt4 = "4. Print Your TimeTable\n"
prompt5 = "5. Print your Log\n"
print(prompt0 + prompt1 + prompt2 + prompt3 + prompt4 + prompt5)
choice = int(input("What would you like to do?\n"))
if choice == 1: #Print the events in the calendar
print('This calendar has the following events in it:')
numEvents = len(self.theCalendar.cal)
for i in range(0,numEvents):
print('\t%i. '%(i) + str(self.theCalendar.cal[i]))
print('------------\n\nPress Return for Prompt')
elif choice == 2: #Add event to calendar
newEvent = addEventParsing(self)
parts = newEvent.participants
outboundEvents = []
algHelper.alginsert(self,newEvent)
for i in parts:
if i is not self.uid:
algHelper.algsend(self,i) #Notify of the new insert that concerns them
print('------------\n\nPress Return for Prompt')
return outboundEvents
elif choice == 3: #Remove event from calendar
eventToDelete = deleteEventParsing(self.theCalendar)
parts = eventToDelete.participants
algHelper.algdelete(self,eventToDelete)
#move to algdelete
# for i in range(0,len(parts)):
# if i is not self.uid:
# algHelper.algsend(self,parts[i]) #Notify of the new delete that concerns them
print('------------\n\nPress Return for Prompt')
elif choice == 0:
newEvent = addEventParsing(self,True)
parts = newEvent.participants
outboundEvents = []
algHelper.alginsert(self,newEvent)
for i in parts:
if i is not self.uid:
print('about to algsend')
algHelper.algsend(self,i) #Notify of the new insert that concerns them
print('------------\n\nPress Return for Prompt')
return outboundEvents
elif choice == 4:
print('The current Time Table for YOU is:')
print(self.tt)
print('------------\n\nPress Return for Prompt')
elif choice == 5:
print('Your local log is:')
for eR in self.log:
print(eR)
# def sendMsg(self, msg):
# otherIP = '127.0.0.1'
# PORT = 9999
#
# sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#
# sock.sendto(msg, (otherIP,PORT))
def recvMsg(self,msg):
algHelper.algrec(self,msg)
# #CONFLICT RESOLUTION - WRONG - COMMENTING OUT FOR NOW OCT-23
# if msg.type == EventType.insert:
# #update TT
# #insert event into cal from each partial log:
# print("Here we are")
# did_insert, conflicts, removed = self.theCalendar.insertEvent(msg.event)
# if did_insert and len(removed) is not 0:
# print("Conflicting entries were removed.")
# elif did_insert:
# print("inserted event")
# else:
# print("Did not insert. Conflicts with event ", list(map(lambda conflict:str(conflict),conflicts)))
#
# elif msg.type == EventType.delete:
# print("Delete event")
# self.theCalendar.deleteEvent(msg.event)
#
# else:
# print("This shouldn't happen, an eventType was left Unassigned")
return True
def updateLamportClock(self):
self.lamportClock += 1
self.tt[self.uid][self.uid] += 1
self.saveProc()
def updateTT(self,msg):
remID = msg.sourceID
i = self.uid
for j in range(0,self.nprocs):
self.tt[i][j] = max(self.tt[i][j], msg.tt[remID][j])
self.tt = list(map(lambda x,y:max(x,y),self.tt,msg.tt))
self.saveProc()
def updateLog(self,msg):
i = self.uid
newLogEntry = algHelper.UserEvent(msg.type, self.tt[i][i],msg.event,i)
self.log.append(newLogEntry)
self.saveProc()
# self.log.append(msg.type, msg.event)
def hasrec(self,record,k):
# print('k=',k,'recuid=',record.uid)
# print(self.tt)
return self.tt[k][record.uid] >= record.Tiii
def saveProc(self):
#bin
with open("controller_state.bin", mode='wb') as file:
pickle.dump(self,file)
with open("human_readable_state.txt",mode='w') as file:
dat = "Lamport Clock:" + str(self.lamportClock) + "\n"
dat += "--TT--\n" + str(self.tt)
dat += "\nLog:\n"
for logEntry in self.log:
dat += str(logEntry) + "\n"
file.writelines(dat)
def loadProcFromFile():
proc = False
if os.path.isfile('controller_state.bin'):
with open('controller_state.bin', mode='rb') as file:
proc = pickle.load(file)
return proc
# def eventHandler(self, msg):
# self.updateLamportClock()
#
# # if msg.sourceID == self.uid or msg.sourceID == self.uid:
# if msg.sourceID == self.uid:
# localEvent(msg)
# else:
# remoteEvent(msg)
class TestController(unittest.TestCase):
def setUp(self):
self.process1 = process(1, 2)
self.process2 = process(2,2)
pass
def testJava(self):
pass
def busTester(self, text):
print(str(text))
if __name__ == "__main__":
bus = Bus()
bus.subscribe("tester",busTester)
bus.publish("tester",text="Hi there!")