123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- #! /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 <http://www.gnu.org/licenses/>.
- 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_class=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
- }
- async 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)
- task = None
- if func == False:
- await self.respond("Unknown command")
- return
- try:
- coro = func(**kwargs)
- if hasattr(coro,"__await__"):
- task = await coro
- except Exception as e:
- lserver.error("api_func raised an error:",exc_info=e)
- tb = traceback.format_exc()
- await self.respond(tb,log_msg="traceback of '{}'"
- .format(e.__cause__))
- finally:
- pass
- if task:
- lserver.debug("exec: "+task.exception.__cause__)
- # task joins iself to suppress the "task not joined" warning
- cur_task = await curio.current_task()
- await curio.ignore_after(0,cur_task.wait)
- async def respond(self,text,*args,log_msg=False):
- await self.send(text,log_msg)
- # async def wait_for_func(self,coro):
- # try:
- # task = await curio.spawn(coro)
- # try:
- # await task.join()
- # except Exception as e:
- # lserver.error("error during api_func execution",exc_info=e)
- # # we do this so we don't get an "task was not joined" error
- # cur_task = await curio.current_task()
- # await curio.ignore_after(0,cur_task.wait)
- # except Exception as e:
- # lserver.error("error",exc_info=e)
- async def welcome_client(self):
- await self.send("this is the server speaking, hello new client!")
- #
|