-
Notifications
You must be signed in to change notification settings - Fork 1
/
routexClient.py
executable file
·189 lines (140 loc) · 4.25 KB
/
routexClient.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
from socket import *
import time
import select
class RoutexClient:
STATUS = 'S'
NUMBER= 'N'
TEXT = 'T'
NO_PARAM = 'B'
PARAM = 'I'
def __init__(self):
self.routexAddress = ''
self.routexPort = 5100
self.clientPort = 6000
self.deviceName = ''
self.sock = None
self.lastCommand = ''
self.lastService = ''
self.lastArgument = ''
self.acceptSock = socket(AF_INET, SOCK_STREAM)
self.acceptSock.bind(('', 5000))
self.acceptSock.listen(3)
def setRoutexAddress(self, address):
self.routexAddress = address
def setRoutexPort(self, port):
self.routexPort = port
def setClientPort(self, port):
self.clientPort = port
def sendMessage(self, message):
if self.sock == None:
self.sock = socket(AF_INET, SOCK_STREAM)
self.sock.bind(('', 0))
self.sock.connect((self.routexAddress, self.routexPort))
self.sock.send(message)
return True
def receiveResponse(self):
if self.sock == None:
print "Socket is None"
return False
read_list = [self.sock]
readable, writable, errored = select.select(read_list, [], [], 2)
for sock in readable:
msg = sock.recv(50)
if msg[-1] == '\n':
return msg[0:(len(msg)-1)]
else:
return msg
return None
def registerClient(self, name):
if name == None or name == '':
print "--- Registered Failed - No Name"
return False
self.deviceName = name
self.sendMessage("J!" + self.deviceName + "!A")
ack = self.receiveResponse()
if ack == None:
print "Failed"
return False
if ack == "J!Ok":
print "Registered Successfully"
self.sock.close()
self.sock = None
return True
def registerService(self, name, serviceType):
if name == None or name == '':
return False
self.sendMessage("S!A!" + serviceType + "!" + name)
ack = self.receiveResponse()
#print ack
if ack == None:
print "Failed"
return False
if ack == "S!G!Ok":
print "Service declared successfully"
return True
def addCommandToService(self, name, commandType):
self.sendMessage("S!" + commandType + "!" + name)
ack = self.receiveResponse()
#print ack
if ack == None:
print "Failed"
return False
if ack == "C!Added":
print "Command added successfully"
return True
def doneService(self):
self.sendMessage("S!DONE")
ack = self.receiveResponse()
#print ack
if ack == None:
print "Failed"
return False
if ack == "S!DONE!Ok":
print "Service registered successfully"
return True
def checkServiceRequest(self):
read_list = [self.acceptSock]
readable, writable, errored = select.select(read_list, [], [], 1)
for sock in readable:
#print "---Accepting"
self.sock, (dev_address, dev_port) = sock.accept()
#print "---Receiving input from : " + str(dev_address) + str(dev_port)
ready = select.select([self.sock], [], [], 4)
if ready[0]:
msg = self.sock.recv(200)
if msg[-1] == '\n':
msg = msg[0:(len(msg)-1)]
l = len(msg)
#print "Received: " + msg
separatorIndex = msg.find('!')
if separatorIndex == -1:
print "Bad Format Request"
return False
self.lastService = msg[0:separatorIndex]
if msg[-1] == '*':
self.lastCommand = msg[separatorIndex+1:len(msg)-1]
readable, writable, errored = select.select(read_list, [], [], 1)
for sock in readable:
self.sock, (dev_address, dev_port) = sock.accept()
argready = select.select([self.sock], [], [], 4)
if argready[0]:
msg = self.sock.recv(50)
if msg[-1] == '\n':
msg = msg[0:(len(msg)-1)]
if msg[0] != '*':
return False
self.lastArgument = msg[2:]
else:
self.lastCommand = msg[separatorIndex+1:]
return True
return False
def serviceResponse(self, response):
self.sendMessage("R!" + response)
self.sock.close()
self.sock = None
def getRequestedServiceName(self):
return self.lastService
def getRequestedServiceCommand(self):
return self.lastCommand
def getRequestedServiceArgument(self):
return self.lastArgument