2018-03-08 11:06:22 +00:00
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
Author: freezed <freezed@users.noreply.github.com> 2018-03-06
|
|
|
|
|
Version: 0.1
|
|
|
|
|
Licence: `GNU GPL v3` GNU GPL v3: http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
|
|
This file is part or roboc project. View `readme.md`
|
|
|
|
|
"""
|
|
|
|
|
import socket
|
2018-03-08 17:02:13 +00:00
|
|
|
|
import select
|
2018-03-08 11:06:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ConnectSocket:
|
|
|
|
|
"""
|
|
|
|
|
ConnectSocket
|
|
|
|
|
=======
|
|
|
|
|
|
|
|
|
|
Provide network connection and management methods
|
|
|
|
|
|
|
|
|
|
:Example:
|
|
|
|
|
>>> c0 = ConnectSocket(False)
|
|
|
|
|
Server is running, listening on port 5555
|
|
|
|
|
|
|
|
|
|
>>> c0.list_sockets(False)
|
|
|
|
|
|
|
|
|
|
>>> c0.list_sockets()
|
|
|
|
|
0: (localhost-5555)
|
|
|
|
|
|
|
|
|
|
>>> c0.count_clients()
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
|
>>> c0.close()
|
|
|
|
|
Server stop
|
|
|
|
|
|
|
|
|
|
>>> c0.list_sockets()
|
|
|
|
|
|
|
|
|
|
"""
|
2018-03-08 17:08:43 +00:00
|
|
|
|
# default connection parameters
|
2018-03-08 11:06:22 +00:00
|
|
|
|
_HOST = 'localhost'
|
|
|
|
|
_PORT = 5555
|
|
|
|
|
_BUFFER = 1024
|
2018-03-08 17:08:43 +00:00
|
|
|
|
|
|
|
|
|
# Template messages
|
2018-03-09 13:10:14 +00:00
|
|
|
|
_BROADCAST_MSG = "{}~ {}\n"
|
2018-03-08 11:06:22 +00:00
|
|
|
|
_MSG_DISCONNECTED = "<gone away to infinity, and beyond>"
|
2018-03-08 17:08:43 +00:00
|
|
|
|
_MSG_SALUTE = "Hi, {}, wait for other players\n"
|
2018-03-08 11:06:22 +00:00
|
|
|
|
_MSG_SERVER_STOP = "Server stop"
|
|
|
|
|
_MSG_START_SERVER = "Server is running, listening on port {}"
|
2018-03-08 17:08:43 +00:00
|
|
|
|
_MSG_USER_IN = "<entered the network>"
|
|
|
|
|
_MSG_WELCOME = "Welcome. First do something usefull and type your name: "
|
|
|
|
|
_SERVER_LOG = "{}:{}|{name}|{msg}"
|
2018-03-08 11:06:22 +00:00
|
|
|
|
|
2018-03-08 17:20:57 +00:00
|
|
|
|
def __init__(self, host=_HOST, port=_PORT):
|
|
|
|
|
"""
|
|
|
|
|
Set up the server connection using a socket object
|
2018-03-08 11:06:22 +00:00
|
|
|
|
|
2018-03-08 17:20:57 +00:00
|
|
|
|
:param str _HOST: domain or IPV4 address
|
|
|
|
|
:param int _PORT: port number
|
|
|
|
|
:return obj: socket
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# Setting up the connection
|
2018-03-08 11:06:22 +00:00
|
|
|
|
self._CONNECTION = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
|
self._CONNECTION.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
|
|
|
self._CONNECTION.bind((host, port))
|
|
|
|
|
self._CONNECTION.listen(5)
|
2018-03-08 17:20:57 +00:00
|
|
|
|
|
|
|
|
|
# Init connection list
|
|
|
|
|
self._inputs = []
|
2018-03-08 11:06:22 +00:00
|
|
|
|
self._inputs.append(self._CONNECTION)
|
|
|
|
|
|
2018-03-08 17:20:57 +00:00
|
|
|
|
# Init username list
|
|
|
|
|
self._user_name = []
|
|
|
|
|
self._user_name.append("CONNECTION")
|
|
|
|
|
|
|
|
|
|
# Print server's activity on console
|
|
|
|
|
print(self._MSG_START_SERVER.format(port))
|
2018-03-08 11:06:22 +00:00
|
|
|
|
|
2018-03-08 17:02:13 +00:00
|
|
|
|
def broadcast(self, sender, name, message):
|
|
|
|
|
"""
|
2018-03-09 13:10:14 +00:00
|
|
|
|
Send a message to all clients but the sender
|
2018-03-08 17:02:13 +00:00
|
|
|
|
|
2018-03-09 13:10:14 +00:00
|
|
|
|
:param obj sender: socket_object of tne sender
|
|
|
|
|
:param str name: name of tne sender
|
|
|
|
|
:param str message: message to send
|
2018-03-08 17:02:13 +00:00
|
|
|
|
"""
|
2018-03-09 13:10:14 +00:00
|
|
|
|
message = self._BROADCAST_MSG.format(name, message)
|
2018-03-08 17:02:13 +00:00
|
|
|
|
for sckt in self._inputs:
|
|
|
|
|
if sckt != self._CONNECTION and sckt != sender:
|
|
|
|
|
try:
|
|
|
|
|
sckt.send(message.encode())
|
|
|
|
|
except:
|
|
|
|
|
sckt.close()
|
|
|
|
|
self._inputs.remove(sckt)
|
|
|
|
|
|
2018-03-08 11:06:22 +00:00
|
|
|
|
def close(self):
|
|
|
|
|
""" Cleanly closes each socket (clients) of the network """
|
|
|
|
|
self._CONNECTION = self._inputs.pop(0)
|
|
|
|
|
i = 1
|
|
|
|
|
for sckt in self._inputs:
|
|
|
|
|
print(self._SERVER_LOG.format(
|
|
|
|
|
*sckt.getpeername(),
|
|
|
|
|
name=self._user_name[i],
|
|
|
|
|
msg="closed client sckt")
|
|
|
|
|
)
|
|
|
|
|
sckt.close()
|
|
|
|
|
i += 1
|
|
|
|
|
self._inputs.clear()
|
|
|
|
|
self._CONNECTION.close()
|
|
|
|
|
print(self._MSG_SERVER_STOP)
|
|
|
|
|
|
2018-03-08 17:08:43 +00:00
|
|
|
|
def count_clients(self):
|
2018-03-08 23:01:54 +00:00
|
|
|
|
"""
|
|
|
|
|
Count connected and named clients
|
|
|
|
|
|
|
|
|
|
(to avoid playing with a unamed player)
|
|
|
|
|
"""
|
|
|
|
|
connect = 1 # the 1st entry is the main connection
|
|
|
|
|
unnamed = self._user_name.count(False) # unnamed clients
|
|
|
|
|
total = len(self._user_name) # All sockets
|
|
|
|
|
return total - unnamed - connect
|
2018-03-08 17:08:43 +00:00
|
|
|
|
|
2018-03-08 11:06:22 +00:00
|
|
|
|
def list_sockets(self, print_it=True):
|
|
|
|
|
""" List connected sckts """
|
|
|
|
|
if print_it:
|
|
|
|
|
for idx, sckt in enumerate(self._inputs):
|
|
|
|
|
if idx == 0:
|
|
|
|
|
print("{}: ({}-{})".format(
|
|
|
|
|
idx, self._HOST, self._PORT)
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
print("{}: {}".format(idx, sckt))
|
|
|
|
|
else:
|
|
|
|
|
return self._inputs
|
|
|
|
|
|
2018-03-08 17:02:13 +00:00
|
|
|
|
def listen(self):
|
|
|
|
|
"""
|
|
|
|
|
Listen sockets activity
|
|
|
|
|
|
|
|
|
|
Get the list of sockets which are ready to be read and apply:
|
|
|
|
|
- connect a new client
|
|
|
|
|
- return data sended by client
|
|
|
|
|
|
2018-03-09 13:44:08 +00:00
|
|
|
|
This object only processes data specific to network connections,
|
|
|
|
|
other data (username and message sent) are stored in `u_name` &
|
|
|
|
|
`message` attributes to be used by parent script
|
|
|
|
|
|
2018-03-08 17:02:13 +00:00
|
|
|
|
:return str, str: user_name, data
|
|
|
|
|
"""
|
2018-03-09 13:44:08 +00:00
|
|
|
|
self.u_name = ""
|
|
|
|
|
self.message = ""
|
2018-03-08 17:02:13 +00:00
|
|
|
|
|
2018-03-09 13:44:08 +00:00
|
|
|
|
# listennig…
|
|
|
|
|
rlist, [], [] = select.select(self._inputs, [], [], 0.05)
|
2018-03-08 17:02:13 +00:00
|
|
|
|
for sckt in rlist:
|
|
|
|
|
# Listen for new client connection
|
|
|
|
|
if sckt == self._CONNECTION:
|
|
|
|
|
sckt_object, sckt_addr = sckt.accept()
|
|
|
|
|
self._inputs.append(sckt_object)
|
|
|
|
|
self._user_name.append(False)
|
|
|
|
|
print(self._SERVER_LOG.format(
|
|
|
|
|
*sckt_addr,
|
|
|
|
|
name="unknow",
|
|
|
|
|
msg="connected")
|
|
|
|
|
)
|
|
|
|
|
sckt_object.send(self._MSG_WELCOME.encode())
|
|
|
|
|
|
|
|
|
|
else: # receiving data
|
|
|
|
|
data = sckt.recv(self._BUFFER).decode().strip()
|
|
|
|
|
peername = sckt.getpeername()
|
|
|
|
|
s_idx = self._inputs.index(sckt)
|
|
|
|
|
uname = self._user_name[s_idx]
|
|
|
|
|
|
|
|
|
|
if self._user_name[s_idx] is False: # setting username
|
|
|
|
|
# insert username naming rule here
|
|
|
|
|
# verify if name is already used TODO17
|
|
|
|
|
self._user_name[s_idx] = data
|
|
|
|
|
sckt.send(self._MSG_SALUTE.format(
|
|
|
|
|
self._user_name[s_idx]).encode()
|
|
|
|
|
)
|
|
|
|
|
print(self._SERVER_LOG.format(
|
|
|
|
|
*peername,
|
|
|
|
|
name=data,
|
|
|
|
|
msg="set user name")
|
|
|
|
|
)
|
|
|
|
|
self.broadcast(
|
|
|
|
|
sckt, self._user_name[s_idx], self._MSG_USER_IN
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
elif data.upper() == "QUIT": # client quit network
|
|
|
|
|
print(self._SERVER_LOG.format(
|
|
|
|
|
*peername,
|
|
|
|
|
name=uname,
|
|
|
|
|
msg="disconnected")
|
|
|
|
|
)
|
|
|
|
|
self.broadcast(sckt, uname, self._MSG_DISCONNECTED)
|
|
|
|
|
self._inputs.remove(sckt)
|
|
|
|
|
self._user_name.pop(s_idx)
|
|
|
|
|
sckt.close()
|
|
|
|
|
|
|
|
|
|
elif data:
|
|
|
|
|
print(self._SERVER_LOG.format(
|
|
|
|
|
*peername, name=uname, msg=data)
|
|
|
|
|
)
|
2018-03-09 13:44:08 +00:00
|
|
|
|
self.u_name, self.message = uname, data
|
2018-03-08 17:02:13 +00:00
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
msg = "uncommon transmission:«{}»".format(data)
|
|
|
|
|
print(self._SERVER_LOG.format(
|
|
|
|
|
*peername, name=uname, msg=msg)
|
|
|
|
|
)
|
|
|
|
|
sckt.send(("server do not transmit: {}\n".format(
|
|
|
|
|
msg
|
|
|
|
|
)).encode())
|
|
|
|
|
|
2018-03-08 11:06:22 +00:00
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
""" Starting doctests """
|
|
|
|
|
|
|
|
|
|
import doctest
|
|
|
|
|
doctest.testmod()
|