#! /usr/bin/env python3.5 # Copyright 2017 Digital # # This file is part of BeeWatch. # # BeeWatch is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # BeeWatch is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with BeeWatch. If not, see . import curio import blinker import logging import logging.handlers import digilib.network import os import queue import select import socket import sys import threading import time import traceback import digilib.pin import digilib.network import beewatch import beewatch.pinapi log = logging.getLogger(__name__+"") lapi = logging.getLogger(__name__+".api") lchat = logging.getLogger(__name__+".chat") lserver = logging.getLogger(__name__+".server") class BeeWatchServer(digilib.network.Server): """docstring for Server.""" def __init__(self,*args,**kwargs): super(BeeWatchServer, self).__init__( *args, **kwargs, handler=beewatch.server.ConnHandlerBeeWatch, ) def make_socket(self): s = super(BeeWatchServer,self).make_socket() s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) return s class ConnHandlerBeeWatch(digilib.network.ConnHandlerBase): def __init__(self, socket, addr, server): super(ConnHandlerBeeWatch, self).__init__(socket, addr, server) self.engines_ctrl = beewatch._controllers["engines_ctrl"] # digilib.pin.EnginesController( # left=[4,17], # right=[27,22], # ) self.text_to_func = { "turn":self.engines_ctrl.turn } def handle(self, data): data = data.strip() data = data.split(" ") cmd,*args = data kwargs = {"args":args,"command":cmd,"respond":self.respond} func = beewatch._commands.get(cmd,False) if func == False: self.respond("Unknown command") return try: retval = func(**kwargs) if hasattr(retval,"__await__"): lserver.debug("curio.spawn started") task = curio.spawn(retval) lserver.debug("curio.spawn finished") except Exception as e: lserver.error("Error while calling api func",exc_info=e) tb = traceback.format_exc() self.respond(tb) def respond(self,text,*args): self.send(text) def welcome_client(self): self.send("this is the server speaking, hello new client!") #