/
Main.py
105 lines (89 loc) · 2.86 KB
/
Main.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
from User import User
from threading import Thread
from Event import Event
from Calendar import Calendar
import pika, sys, time
def callback(channel,method_frame,header_frame,body):
calName = method_frame.exchange[:-1]
# print "calendarname",calName
# create event
m = body.split("%%")
newEv = Event(m[0],m[1],m[2],m[3],m[4])
u.addEvent(calName,newEv)
print "INFO: New Event Received"
def startListening():
print 'Listening to calendar \''+calName+'\' ...'
global keepListening
channel.start_consuming()
if __name__ == "__main__":
tmp = sys.argv[1]
global u
u = User(tmp)
print "username",u.getName()
# CONSTANT
hostname = 'localhost'
connection = pika.BlockingConnection(pika.ConnectionParameters(host = hostname))
global channel
channel = connection.channel()
# gimana cara pastiin queue name unik
result = channel.queue_declare(exclusive = True)
queue_name = result.method.queue
while 1:
cmd = raw_input('> ')
param = cmd.split(' ')
# comm /EXIT
if(param[0].strip() == '/EXIT'):
print "INFO: Successfully closed program"
channel.stop_consuming()
connection.close()
break
# comm /NAME <username>
elif(param[0].strip() == '/NAME'):
if(param.__len__()>1):
u.setName(param[1])
print "INFO: Successfully change name to",u.getName()
else:
print "WARNING: You must enter new name to change"
# comm /CAL <calendar-name>
elif(param[0].strip() == '/CAL'):
if(param.__len__()>1):
calName = param[1].strip()
newCal = Calendar(calName)
u.addCal(newCal)
x = calName + 'X'
# make sure the calendar exists
channel.exchange_declare(exchange=x,type='fanout')
channel.queue_bind(exchange=x,queue=u.getQueueName())
channel.basic_consume(callback,queue=u.getQueueName(),no_ack=False)
# run listening thread
temp = Thread(target = startListening, args = ())
temp.start()
u.addListener(temp)
print "Successfully added Calendar",calName
else:
print "ERROR: Format /CAL <calendar-name>"
# comm /EVT <calendar-name> <event-name> <event-place> <event-start-hour> <event-end-hour> <event-description>
# hour consists of 4 character e.g. 04pm, 05am
# no space character for description
elif(param[0].strip() == '/EVT'):
if (param.__len__() == 7):
calName = param[1].strip()
if u.hasCalendar(calName):
EvtName = param[2]
EvtPlace = param[3]
EvtStart = param[4]
EvtEnd = param[5]
EvtDesc = param[6]
ev = Event(EvtName,EvtPlace,EvtStart,EvtEnd,EvtDesc)
x = calName + 'X'
channel.exchange_declare(exchange=x,type='fanout')
message = ev.toString()
# print "message:",message
channel.basic_publish(exchange=x,routing_key='',body=message)
print "INFO: Event sent to calendar",calName
else:
print "INFO: You are not in this calendar"
else:
print "ERROR: Parameter Error"
elif(param[0].strip()=='/ALL'):
u.showAllEvents()