-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmessageDaemon.py
152 lines (136 loc) · 4.61 KB
/
messageDaemon.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
import optparse
import socket
import sys
class Server:
def __init__(self,port):
self.host = ""
self.port = port
self.client = None
self.cache = ''
self.messages = {}
self.size = 1024
self.parse_options()
self.open_socket()
self.run()
def parse_options(self):
parser = optparse.OptionParser(usage = "%prog [options]",
version = "%prog 0.1")
parser.add_option("-p","--port",type="int",dest="port",
default=5000,
help="port to listen on")
(options,args) = parser.parse_args()
self.port = options.port
def open_socket(self):
""" Setup the socket for incoming clients """
try:
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
self.server.bind((self.host,self.port))
self.server.listen(5)
except socket.error, (value,message):
if self.server:
self.server.close()
print "Could not open socket: " + message
sys.exit(1)
def run(self):
while True:
try:
(client,address) = self.server.accept()
except:
break
self.client = client
self.handle_client()
def handle_client(self):
while True:
data = self.client.recv(self.size)
if not data:
return
self.cache += data
message = self.read_message()
if not message:
continue
self.handle_message(message)
def read_message(self):
index = self.cache.find("\n")
if index == "-1" or index == -1:
return None
message = self.cache[0:index+1]
self.cache = self.cache[index+1:]
return message
def handle_message(self,message):
response = self.parse_message(message)
self.send_response(response)
def parse_message(self,message):
fields = message.split()
if not fields:
return('error invalid message\n')
if fields[0] == 'reset':
self.messages = {}
return "OK\n"
if fields[0] == 'put':
try:
name = fields[1]
subject = fields[2]
length = int(fields[3])
except:
return('error invalid message\n')
data = self.read_put(length)
if data == None:
return 'error could not read entire message\n'
self.store_message(name,subject,data)
return "OK\n"
if fields[0] == 'list':
try:
name = fields[1]
except:
return('error invalid message\n')
subjects,length = self.get_subjects(name)
response = "list %d\n" % length
response += subjects
return response
if fields[0] == 'get':
try:
name = fields[1]
index = int(fields[2])
except:
return('error invalid message\n')
subject,data = self.get_message(name,index)
if not subject:
return "error no such message for that user\n"
response = "message %s %d\n" % (subject,len(data))
response += data
return response
return('error invalid message\n')
def store_message(self,name,subject,data):
if name not in self.messages:
self.messages[name] = []
self.messages[name].append((subject,data))
def get_subjects(self,name):
if name not in self.messages:
return "",0
response = ["%d %s\n" % (index+1,message[0]) for index,message in enumerate(self.messages[name])]
return "".join(response),len(response)
def get_message(self,name,index):
if index <= 0:
return None,None
try:
return self.messages[name][index-1]
except:
return None,None
def read_put(self,length):
data = self.cache
while len(data) < length:
d = self.client.recv(self.size)
if not d:
return None
data += d
if len(data) > length:
self.cache = data[length:]
data = data[:length]
else:
self.cache = ''
return data
def send_response(self,response):
self.client.sendall(response)
if __name__ == '__main__':
s = Server(5000)