-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·140 lines (115 loc) · 3.19 KB
/
server.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
import socket
import sys
import time
from thread import *
import car
def stop():
print "stop"
car.stop()
def forward(on):
print "forward: %s" % on
if on:
car.forward()
else:
car.stop()
def back(on):
print "back: %s" % on
if on:
car.back()
else:
car.stop()
def left(on):
print "left: %s" % on
if on:
car.left()
else:
car.straight()
def right(on):
print "right: %s" % on
if on:
car.right()
else:
car.straight()
# Set mapping between socket command name and function name
armoptions = {'FF' : forward,
'BB' : back,
'LL' : left,
'RR' : right}
HOST = '' # Symbolic name meaning all available interfaces
PORT = 5000 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Allow socket reuse
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
s.bind((HOST,PORT))
except socket.error , msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
s.listen(10)
def clientthread(conn):
# Keep running until quit command executed
while 1:
# Get the data
data = conn.recv(1024)
print data
if not data:
print data
print "Socket closed"
break
# Strip off any whitespace if present
data = ''.join(data.split())
# Loop through the recieved buffer a character at a time building up commands
i = 0;
cmd = ""
while i < len(data):
# Look for the special case of a stop
if data[i] == '.':
stop()
i += 1
continue
# Build up command
cmd += data[i]
# If enough characters for a command has been found then interpret and call function
if len(cmd) == 2:
param = True
if len(data) > i+1:
# on/off
if data[i+1] == '+':
param = True
i += 1
elif data[i+1] == '-':
param = False
i += 1
elif data[i+1].isdigit():
param = data[i+1]
i += 1
# Get option and call function
try:
armoptions[cmd](param)
except KeyError:
print "Bad command: " + cmd
# Clear command once issued
cmd = ""
# Move onto next character
i += 1
##
## Main starting piont of server
##
running = True
try:
while running:
print "Waiting ..."
# Wait to accept a connection from client control app
conn, addr = s.accept()
print 'Connected with ' + addr[0] + ':' + str(addr[1])
# Start listening to new socket on seperate thread
start_new_thread(clientthread ,(conn,))
finally:
print "Exiting"
try:
car.stop() # Stop the car if we get here in error
except:
pass
s.close()
time.sleep(0.1) # wait a little for threads to finish
car.cleanup()