- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.py
More file actions
Latest commit
179 lines (130 loc) · 5.9 KB
/
Copy pathserver.py
File metadata and controls
179 lines (130 loc) · 5.9 KB
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
# Server implementation
importsocket
importthreading
fromenumimportEnum
importcv2ascamera_vision
importpickle
# WINDOW DIMENSIONS
WINDOW_WIDTH=50
WINDOW_HEIGHT=50
# WEB CAMERA PROPERTIES
WEB_CAMERA_WIDTH_PROPERTY=3
WEB_CAMERA_HEIGHT_PROPERTY=4
WEB_CAMERA_DEVICE_ID_PROPERTY=0
WEB_CAMERA_BRIGHTNESS_PROPERTY=10
# SETUP CAPTURED VIDEO STREAM
captured_video_stream=camera_vision.VideoCapture(WEB_CAMERA_DEVICE_ID_PROPERTY)
captured_video_stream.set(WEB_CAMERA_WIDTH_PROPERTY, WINDOW_WIDTH)
captured_video_stream.set(WEB_CAMERA_HEIGHT_PROPERTY, WINDOW_HEIGHT)
captured_video_stream.set(WEB_CAMERA_BRIGHTNESS_PROPERTY, 50)
classSocketConnectionStatus(Enum):
PENDING=0
RUNNING=1
STOPPED=2
classServer:
# connection port
__SERVER_PORT=1989
# local server's IPv4 ADDRESS
__SERVER_IP_ADDRESS=socket.gethostbyname(socket.gethostname())
# Server Socket
__server_socket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Server Connection BINDER
__SERVER_CONNECTION_BINDER= (__SERVER_IP_ADDRESS, __SERVER_PORT)
# Bind server_socket
__server_socket.bind(__SERVER_CONNECTION_BINDER)
# Protocol Bytes Accepted.
__PROTOCOL_BYTES_ACCEPTED=4096# 1 Kilo Byte of information, is accepted; by this Server.
# Protocol Communication Format.
__PROTOCOL_FORMAT="unicode_escape"# "utf-8"
# Disconnect Message
__DISCONNECT_MESSAGE="!DISCONNECT_MESSAGE"
def__init__(self):
self.display_server_connection_info()
self.server_socket_connection_status=SocketConnectionStatus.PENDING
self.display_socket_connection_status()
pass
def__set_server_socket_connection_status(self, to_socket_connection_status):
self.server_socket_connection_status=to_socket_connection_status
pass
defget_server_socket_connection_status(self):
returnself.server_socket_connection_status
defdisplay_socket_connection_status(self):
print()
print(f"[SERVER SOCKET CONNECTION STATUS] {self.get_server_socket_connection_status().name.lower()}...")
pass
defdisplay_server_connection_info(self):
print()
print(f"[SERVER CONNECTION INFO] {self.__SERVER_IP_ADDRESS}:{self.__SERVER_PORT}")
pass
def__initiate_client_connection(self, client_connection, client_address):
print()
print(f"[NEW CLIENT CONNECTED TO SERVER] {client_address}:{client_connection}")
self.display_active_client_connection_count()
print()
is_client_connected=True
# Server listens, to incoming client Messages.
whileis_client_connected:
print()
client_message_length=int(
client_connection.recv(self.__PROTOCOL_BYTES_ACCEPTED).decode(self.__PROTOCOL_FORMAT))
ifclient_message_length:
client_message=client_connection.recv(client_message_length).decode(self.__PROTOCOL_FORMAT)
print(f"[CLIENT {client_address}, SENT A MESSAGE TO SERVER; THAT READS] {client_message}")
client_connection.send(f"eho, {client_message}".encode(self.__PROTOCOL_FORMAT))
# When the client sends the self.__DISCONNECT_MESSAGE,
# Server must stop listening; to client's messages.
ifclient_message==self.__DISCONNECT_MESSAGE:
is_client_connected=False
client_connection.close()
elifclient_message=="s":
try:
success, image_stream=captured_video_stream.read()
ifsuccess:
fordatainimage_stream:
client_connection.send(pickle.dumps(data).decode(encoding=bytes))
finally:
ifcamera_vision.waitKey(1) &0xFF==ord('z'):
break
pass
pass
defstart_server_connection_to_clients(self):
# set server_socket_connection_status to SocketConnectionStatus.RUNNING
self.__set_server_socket_connection_status(SocketConnectionStatus.RUNNING)
# display_socket_connection_status
self.display_socket_connection_status()
# Start listening to clients; caution, this operation is Blocking.
self.__server_socket.listen()
# Server will remain connected to its Clients; until Connection is Stopped.
whileself.get_server_socket_connection_status() ==SocketConnectionStatus.RUNNING:
try:
# Server will now accept, new Client Connection.
client_connection, client_address=self.__server_socket.accept()
ifclient_connectionandclient_address:
# Spawn a thread, that connects a client; to this Server.
threading.Thread(target=self.__initiate_client_connection(client_connection, client_address),
args=(client_connection, client_address)).start()
exceptKeyboardInterrupt:
self.stop_server_connection_to_clients()
pass
defdisplay_active_client_connection_count(self):
print()
ifthreading:
active_client_count=threading.activeCount()
ifactive_client_count>=1:
print(f"[ACTIVE CLIENTS ON SERVER SOCKET CONNECTION] {active_client_count}")
else:
print("[NO ACTIVE CLIENT ON SERVER SOCKET CONNECTION]")
pass
defstop_server_connection_to_clients(self):
self.__set_server_socket_connection_status(SocketConnectionStatus.STOPPED)
self.display_active_client_connection_count()
self.display_socket_connection_status()
pass
pass
classServerApp:
def__init__(self):
self.server=Server()
self.server.start_server_connection_to_clients()
pass
pass
serverApp=ServerApp()