Skip to content

multithreaded bluetooth server #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 49 additions & 11 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(self, sock, address):
self.receivingFile = False
self.fileBuilder = ""
self.DELIMETER = " cnysetomer"
self.processes = []

def send_msg(self, message):
self._logger.info("%s S - %s" % (self.address[0][12:], message))
Expand All @@ -48,6 +49,7 @@ def send_msg(self, message):
def get_msg(self):
data = str(self.sock.recv(1024).decode("utf-8"))
if len(data) == 0:
self._logger.debug("Data is empty")
self.stopped = True
self._logger.info("%s R %s" % (self.address[0][12:], data))
return data
Expand All @@ -60,18 +62,10 @@ def handle_request(self, msg):
self.fileBuilder = msg.split(' ', 1)[1]
elif self.receivingFile:
self.fileBuilder += str(msg)
elif str(msg).find('killall') != -1:
self.killAllProcesses()
else:
try:
#self.send_msg("::start::")
result = subprocess.check_output(msg, stderr=subprocess.STDOUT, shell=True).decode('utf-8').strip()
if not len(result):
self.send_msg("the command '%s' returns nothing " % msg)
for line in result.splitlines():
self.send_msg(line + " ")
except subprocess.CalledProcessError as e:
self.send_msg(e.output.decode("utf-8"))
#finally:
#self.send_msg("::end::")
self.sendToCLI(msg)
if self.fileBuilder.find(self.DELIMETER) != -1:
now = datetime.datetime.now()
copyfile(sys.argv[0], sys.argv[0] + now.strftime("%Y%m%d%H%M"))
Expand All @@ -93,6 +87,50 @@ def run(self):
self.sock.close()
self._logger.info("Disconnected from %s" % self.address[0])

def killAllProcesses(self):
self._logger.info("Killing %d processes..." % len(self.processes))
for process in self.processes:
process.kill()
self.processes = []
self._logger.info("Killed all processes")

def startProcess(self, process, message):
self._logger.info("Starting Process...")
try:
stdout, stderr = process.communicate()
self._logger.info("Finish Communicating")
except subprocess.TimeoutExpired:
process.kill()
stdout, stderr = process.communicate()
self.send_msg("Timeout when trying to send %s" % message)
except Exception as e: # Including KeyboardInterrupt, communicate handled that.
self._logger.error("ERROR...%s" % e)
process.kill()
return
retcode = process.poll()
if retcode: # ERROR OCCURRED
if (process in self.processes):
self.send_msg("ERROR: %s %s" % (stdout.decode("utf-8"), stderr.decode("utf-8")))
else:
self._logger.info("Interrupted %s" % message)
else:
self._logger.info("Result")
result = stdout.decode("utf-8")
if not len(result):
self.send_msg("the command '%s' returns nothing " % msg)
else:
self.send_msg(result)

if (process in self.processes):
self.processes.remove(process)

def sendToCLI(self, message):
process = subprocess.Popen(message, stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True)
self.processes.append(process)
thread = threading.Thread(target = self.startProcess, args=(process, message, ))
thread.start()


class Server():
def __init__(self):
self.q_len = 3
Expand Down