Last active
April 27, 2021 10:42
-
-
Save egemenertugrul/192aed9f98f30c9972484226f9b3a2c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# listen.py | |
from threading import Thread | |
import socket | |
import time | |
import sys | |
VERBOSE = False # Enables/Disables printing of debug messages | |
IP_PORT = 22000 # the port that tinkerboard listens to | |
TIME_INTERVAL = 10 # sec | |
def debug(text): | |
if VERBOSE: | |
print ("Debug:---", text) | |
# ---------------------- class SocketHandler ------------------------ | |
class SocketHandler(Thread): | |
def __init__(self, conn): | |
Thread.__init__(self) | |
self.conn = conn | |
def run(self): | |
global isConnected | |
debug("SocketHandler started") | |
while True: | |
cmd = "" | |
try: | |
debug("Calling blocking conn.recv()") | |
cmd = self.conn.recv(1024) | |
except: | |
debug("exception in conn.recv()") | |
# happens when connection is reset from the peer | |
break | |
debug("Received cmd: " + cmd + " len: " + str(len(cmd))) | |
if len(cmd) == 0: | |
break | |
self.executeCommand(cmd) | |
conn.close() | |
print ("Client disconnected. Waiting for next client...") | |
isConnected = False | |
debug("SocketHandler terminated") | |
def executeCommand(self, cmd): | |
print(cmd) | |
if cmd == "getWeight" : # removed the trailing "\0" | |
print("commandReceived: " + "getWeight") | |
def reply(self, msg): | |
print("Sent message to all: " + msg) | |
self.conn.sendall(msg + "\0") | |
# ----------------- End of SocketHandler ----------------------- | |
# setup() | |
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
# close port when process exits: | |
serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
debug("Socket created") | |
HOSTNAME = "" # Symbolic name meaning all available interfaces | |
try: | |
serverSocket.bind((HOSTNAME, IP_PORT)) | |
except socket.error as msg: | |
print ("Bind failed", msg[0], msg[1]) | |
sys.exit() | |
serverSocket.listen(10) # 10 clients? | |
print ("Waiting for a connecting client...") | |
isConnected = False | |
while True: | |
debug("Calling blocking accept()...") | |
conn, addr = serverSocket.accept() | |
print ("Connected with client at " + addr[0]) | |
print(addr) | |
isConnected = True | |
socketHandler = SocketHandler(conn) | |
# necessary to terminate it at program termination: | |
socketHandler.setDaemon(True) | |
socketHandler.start() | |
t = 0 | |
while isConnected: # check connection status every TIME_INTERVAL secs | |
debug ("Server connected at" + str(t) + "s") | |
socketHandler.reply("This is a message from the server") # broadcast to all | |
time.sleep(10) | |
t += TIME_INTERVAL | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment