Ejemplo n.º 1
0
class SchedServer(object):
    def __init__(self, ip="localhost", port=8000):
        self.sched = scheduler.Scheduler()
        self.server = SimpleXMLRPCServer((ip, port), requestHandler=RequestHandler)
        self.server.register_introspection_functions()
        self.serving = 0
        self.server.register_function(self.push)
        self.server.register_function(self.shutdown)
        self.sched.run()
        self.server.serve_forever()

    def push(self, username, policy_number, code):
        user_id = db_session.query(User.id).filter(User.username == username).one()[0]
        db_task = Task(user_id, policy_number)

        t = task.Task(program.Program(code), username, 3, 1000)
        index = self.sched.add(t)
        if index == -1:
            print("Shutting down")
            return -1
        db_session.add(db_task)
        db_session.commit()
        ret_val = self.sched.get(index)
        while ret_val is None:
            ret_val = self.sched.get(index)
            sleep(0.2)
        return ret_val

    def shutdown(self):
        self.sched.join()
Ejemplo n.º 2
0
class KeyValueServer:
    _rpc_methods_ = ['get', 'set', 'delete', 'exists', 'keys']
    def __init__(self, address):
        self._data = {}
        self._serv = SimpleXMLRPCServer(address, allow_none=True)
        for name in self._rpc_methods_:
            self._serv.register_function(getattr(self, name))

    def get(self, name):
        return self._data[name]

    def set(self, name, value):
        self._data[name] = value

    def delete(self, name):
        del self._data[name]

    def exists(self, name):
        return name in self._data

    def keys(self):
        return list(self._data)

    def serve_forever(self):
        self._serv.serve_forever()
Ejemplo n.º 3
0
def start_server(obj):
	"""
		Purpose:
		Start the XML-RPC server and register the "obj" class.
	"""
	try:
		commandServer = SimpleXMLRPCServer(('', 9000), allow_none=True)

		commandServer.register_introspection_functions()
		commandServer.register_instance(obj)

		print("{} - Starting to server forever".format(time.asctime()))
		#Now keep the server alive till killed or system reboots
		commandServer.serve_forever()

	except KeyboardInterrupt as err:
		print("{} - Keyboard Interrupt. Exiting.".format(time.asctime()))
		return

	except Exception as err:
		print(err)
		raise err

	except System.Exception as err:
		print(err)
		raise err
Ejemplo n.º 4
0
class NetServer(Thread):

    def __init__(self, port=8080, logRequests=True):
        Thread.__init__(self)
        self.__port = port
        self.__logRequests = logRequests
        self.__server = None
        self.__running = False
        self.State = None
        self.start()
        while not self.__running:
            sleep(0)

    def stop(self):
        if self.State is not None:
            self.State.set_update_period(0)
        self.__server.shutdown()
        self.__server.server_close()
        self.join()

    def run(self):
        #register API
        self.State = NetServerState() # accessor for state object for tests and shutting down, etc
        self.__server = SimpleXMLRPCServer(("127.0.0.1", self.__port), CrossDomainXMLRPCRequestHandler, logRequests=self.__logRequests)
        self.__server.register_instance(self.State)
        self.__server.register_introspection_functions()

        #start server
        self.__running = True
        self.__server.serve_forever()
Ejemplo n.º 5
0
def main():
    # Parse CLI options
    import argparse
    cli_parser = argparse.ArgumentParser(
        description="XML-RPC server for filtering recommendations."
        )

    # Add verbosity option
    cli_parser.add_argument('-v', '--verbose', action='store_true',
                            help='Be more verbose')
    args = cli_parser.parse_args()

    if args.verbose:
        logging.basicConfig(level=logging.INFO)

    filterServer = RecFilter()
    server = SimpleXMLRPCServer(
        (config.filter_server_hostname, config.filter_server_hostport),
        allow_none=True)

    server.register_introspection_functions()
    server.register_function(filterServer.getRecs, 'getrecs')
    print("Filter-server is running...")

    # Run the server's main loop
    server.serve_forever()
Ejemplo n.º 6
0
def server_main():
    global spv

    logging_level = pyspv.WARNING
    if '-v' in sys.argv:
        logging_level = pyspv.INFO
    if '-vv' in sys.argv or '-vvv' in sys.argv:
        logging_level = pyspv.DEBUG

    spv = pyspv.pyspv('pyspv-simple-wallet', logging_level=logging_level, peer_goal=4, testnet=True, listen=('0.0.0.0', 8336))
                #relay_tx=False,

    rpc_server = SimpleXMLRPCServer((RPC_LISTEN_ADDRESS, RPC_LISTEN_PORT), allow_none=True)
    rpc_server.register_function(getnewaddress)
    rpc_server.register_function(getnewstealthaddress)
    rpc_server.register_function(getnewpubkey)
    rpc_server.register_function(getbalance)
    rpc_server.register_function(sendtoaddress)
    rpc_server.register_function(sendspendtoaddress)
    rpc_server.register_function(getinfo)
    rpc_server.register_function(listspends)
    rpc_server.register_function(dumppubkey)
    rpc_server.register_function(dumpprivkey)
    rpc_server.register_function(genmultisig)
    rpc_server.register_function(sendrawtransaction)

    try:
        rpc_server.serve_forever()
    except KeyboardInterrupt:
        pass

    spv.shutdown() # Async shutdown
    spv.join()     # Wait for shutdown to complete
Ejemplo n.º 7
0
 def _start(self):
     """
     Used internally to start the XML-RPC server.
     """
     s = SimpleXMLRPCServer(("", getPort(self.url)), logRequests=False)
     s.register_instance(self)
     s.serve_forever()
Ejemplo n.º 8
0
class Server:

    def __init__(self, address):
        self._rpc_methods_ = ['get', 'put', 'put_back', 'tick', 'keys']
        self._data = {}
        self._serv = SimpleXMLRPCServer(address, allow_none=True)
        for name in self._rpc_methods_:
            self._serv.register_function(getattr(self, name))

    def get(self, key):
        return self._data[key]

    def put(self, key, value):
        self._data[key] = value

    def put_back(self, key, value):
        pass

    def tick(self):
        pass

    def keys(self):
        return list(self._data)

    def serve_forever(self):
        self._serv.serve_forever()
Ejemplo n.º 9
0
class KeyValueServer:
    _rpc_methods = ["get", "set", "exists", "delete", "keys"]

    def __init__(self, address):
        self._data = {}
        self._serv = SimpleXMLRPCServer(address, allow_none=True)
        for method in self._rpc_methods:
            self._serv.register_function(getattr(self, method), method)

    def get(self, name):
        return self._data["name"]

    def set(self, name, value):
        self._data["name"] = value

    def exists(self, name):
        return name in self._data

    def delete(self, name):
        del self._data["name"]

    def keys(self):
        # dict_keys is not supported
        return list(self._data.keys())

    def serve_forever(self):
        self._serv.serve_forever()
def run_xmlrpc_server():
  port = 1210
  oo_proxy = OOProxy()
  #server = SimpleXMLRPCServer.SimpleXMLRPCServer(("localhost", port))
  server = SimpleXMLRPCServer(("localhost", port))
  server.register_instance(oo_proxy)
  #Go into the main listener loop
  server.serve_forever()
Ejemplo n.º 11
0
def rpcserver():
    server = SimpleXMLRPCServer(('localhost', 8000), requestHandler=TestRequestHandler)
    server.register_introspection_functions()
    server.register_function(pow)
    server.register_function(adder_func, 'add')
    server.register_instance(TestRPCClass())

    server.serve_forever()
Ejemplo n.º 12
0
def main():
    server = SimpleXMLRPCServer(('127.0.0.1', 7001))
    server.register_introspection_functions()
    server.register_multicall_functions()
    server.register_function(addtogether)
    server.register_function(quadratic)
    server.register_function(remote_repr)
    print("Server ready")
    server.serve_forever()
Ejemplo n.º 13
0
def main():
    server = SimpleXMLRPCServer(("localhost", PORT))
    print("We've got a connection and are listening on port {}...huzzah".format(PORT))

    # 注册函数,这样它可以被即将创建的客户端代码使用
    server.register_function(square, "square")

    # 启动服务器
    server.serve_forever()
Ejemplo n.º 14
0
def buildServer():
    """build an XMLRPC Server and serve forever \
    waiting for problem to submit.
    """
    global config
    server = SimpleXMLRPCServer((config.get("XMLRPCServer","host"), \
		    config.getint("XMLRPCServer","port")))
    server.register_function(receive, "send")
    server.serve_forever()
Ejemplo n.º 15
0
class DebugStubComm(object):

    def __init__(self, stub, port):
        self.stub = stub
        addr = "0.0.0.0"
        self.server = SimpleXMLRPCServer((addr, port))
        self.server.register_function(self.handshake)
        self.server.register_function(self.get_agent_list)
        self.server.register_function(self.track_agent)
        self.server.register_function(self.get_agent_track)


    def start_service(self):
        self.server.serve_forever()

    ###########################################################################
    # client side api
    ###########################################################################
    def get_agent_list(self):
        self.stub.lock.acquire()
        l = [i for i in self.stub.agents]
        self.stub.lock.release()
        return l

    def handshake(self):
        return True

    def track_agent(self, agent_id, on_off):
        self.stub.lock.acquire()
        if agent_id not in self.stub.agents:
            self.stub.lock.release()
            return False
        agent = self.stub.agents[agent_id]
        agent.tracked = on_off
        self.stub.lock.release()
        return True

    def get_agent_track(self, agent_id):
        self.stub.lock.acquire()
        if agent_id not in self.stub.agents:
            self.stub.lock.release()
            return False
        agent = self.stub.agents[agent_id]
        ret = agent.history
        agent.history = []
        self.stub.lock.release()
        return ret

    def set_breakpoint(self, agent_id, location_info):
        pass

    def remove_breakpoint(self, agent_id, location_info):
        pass

    def continue_agent(self, agent_id):
        pass
Ejemplo n.º 16
0
def start():
    #NOTE: bots directory should always be on PYTHONPATH - otherwise it will not start.
    #***command line arguments**************************
    usage = '''
    This is "%(name)s" version %(version)s, part of Bots open source edi translator (http://bots.sourceforge.net).
    Server program that ensures only a single bots-engine runs at any time, and no engine run requests are
    lost/discarded. Each request goes to a queue and is run in sequence when the previous run completes.
    Use of the job queue is optional and must be configured in bots.ini (jobqueue section, enabled = True).
    Usage:
        %(name)s  -c<directory>
    Options:
        -c<directory>   directory for configuration files (default: config).

    ''' % {'name': os.path.basename(sys.argv[0]), 'version': 3.3}
    configdir = 'config'
    for arg in sys.argv[1:]:
        if arg.startswith('-c'):
            configdir = arg[2:]
            if not configdir:
                print('Error: configuration directory indicated, but no directory name.')
                sys.exit(1)
        else:
            print(usage)
            sys.exit(0)
    #***end handling command line arguments**************************
    #~ botsinit.generalinit(configdir)     #find locating of bots, configfiles, init paths etc.
    #~ if not botsglobal.ini.getboolean('jobqueue','enabled',False):
        #~ print('Error: bots jobqueue cannot start; not enabled in %s/bots.ini'%(configdir))
        #~ sys.exit(1)
    nr_threads = 2  # botsglobal.ini.getint('jobqueue','nr_threads')
    process_name = 'jobqueue'
    #~ logger = botsinit.initserverlogging(process_name)
    #~ logger.log(25,'Bots %(process_name)s started.',{'process_name':process_name})
    #~ logger.log(25,'Bots %(process_name)s configdir: "%(configdir)s".',{'process_name':process_name,'configdir':botsglobal.ini.get('directories','config')})
    port = 28082  # botsglobal.ini.getint('jobqueue','port',28082)
    #~ logger.log(25,'Bots %(process_name)s listens for xmlrpc at port: "%(port)s".',{'process_name':process_name,'port':port})

    #start launcher thread
    lauchfrequency = 5  # botsglobal.ini.getint('jobqueue','lauchfrequency',5)
    maxruntime = 60  # botsglobal.ini.getint('settings','maxruntime',60)
    for thread in range(nr_threads)
        launcher_thread = threading.Thread(name='launcher', target=launcher,
                                           args=(logger, queue, lauchfrequency, maxruntime))
        launcher_thread.start()
        #~ logger.info('Jobqueue launcher started.')

    #the main thread is the xmlrpc server: all adding, getting etc for jobqueue is done via xmlrpc.
    #~ logger.info('Jobqueue server started.')
    server = SimpleXMLRPCServer(('localhost', port), logRequests=False)
    server.register_instance(Jobqueue(logger))
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        pass

    sys.exit(0)
Ejemplo n.º 17
0
def serve_agent(agent, address, port):
    """Serve agent on specified bind address and port number."""
    from xmlrpc.server import SimpleXMLRPCServer
    server = SimpleXMLRPCServer((address, port), allow_none=True)
    server.register_instance(agent)
    print("Listening on ", address, ":", port, sep="")
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        pass
Ejemplo n.º 18
0
def serve_player(player, address, port):
    """Serve player on specified bind address and port number."""
    from xmlrpc.server import SimpleXMLRPCServer
    server = SimpleXMLRPCServer((address, port))
    server.register_instance(player)
    print("Listening on " + address + ":" + str(port))
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        pass
Ejemplo n.º 19
0
def main():
    """ Parse argument list and execute command. """

    global CONNECTION
    global SERVER
    global HEADERS
    global XMLRPC_URL
    global TENANT_ID

    usage = "%s [options]" % sys.argv[0]

    parser = ArgumentParser(usage=usage)

    parser.add_argument("-x", "--xmlrpc", dest="xmlrpc",
                        default="http://127.0.0.1:8000/RPC2",
                        help="Specify the XML-RPC server URL")

    parser.add_argument("-a", "--hostname", dest="host", default="127.0.0.1",
                        help="EmPOWER REST address; default='127.0.0.1'")

    parser.add_argument("-p", "--port", dest="port", default="8888",
                        help="EmPOWER REST port; default=8888")

    parser.add_argument("-u", "--user", dest="user", default="root",
                        help="EmPOWER admin user; default='root'")

    parser.add_argument("-n", "--no-passwd", action="store_true",
                        dest="no_passwd", default=False,
                        help="Run without password; default false")

    parser.add_argument("-f", "--passwd-file", dest="passwdfile",
                        default=None, help="Password file; default=none")

    parser.add_argument("-i", "--tenant-id", dest="tenant_id",
                        default=None, help="Tenant id; default=none")

    parser.add_argument("-t", "--transport", dest="transport", default="http",
                        help="Specify the transport; default='http'")

    (args, _) = parser.parse_known_args(sys.argv[1:])

    CONNECTION, HEADERS = get_connection(args)

    SERVER = SimpleXMLRPCServer(("localhost", 8000))
    XMLRPC_URL = args.xmlrpc
    TENANT_ID = args.tenant_id

    # synch state
    synch_callback(url='/api/v1/tenants/%s/cppup' % args.tenant_id)

    # register callback
    cpp_up(callback=cpp_up_callback)

    # Start xml-rpc server
    SERVER.serve_forever()
Ejemplo n.º 20
0
Archivo: server.py Proyecto: 12z/Raft
    def start_receiver(self, port):
        server = SimpleXMLRPCServer(('localhost', port), requestHandler=RequestHandler)
        # listen messages to this node

        # the messages that this receiver serves
        server.register_function(append_entries)
        server.register_function(request_vote)
        server.register_function(ping)

        print('serving on ', self.port)
        server.serve_forever()
Ejemplo n.º 21
0
def main():
    logging.basicConfig(format="%(asctime)s %(levelname)s "
                               "%(filename)s:%(lineno)d %(message)s")

    LOG.setLevel(logging.INFO)

    LOG.info("psutil version %s at %s", psutil.__version__, psutil.__file__)

    server = SimpleXMLRPCServer(("0.0.0.0", 9002))
    server.register_introspection_functions()
    server.register_instance(MonHelperRPCInterface())
    server.serve_forever()
Ejemplo n.º 22
0
 def _start(self):
     '''
     Start the server part of client.
     '''
     global PORT
     global ADDRESS
     try:
         server = SimpleXMLRPCServer((ADDRESS, PORT))
         server.register_instance(self)
         server.serve_forever()
     except:
         PORT+=1
         self._start()
Ejemplo n.º 23
0
def main():
    host = "localhost"
    port = 7500

    data_handler = DatabaseHandler()
    
    try:
        server = SimpleXMLRPCServer((host, port))
        server.register_instance(data_handler)
        server.serve_forever()
    except KeyboardInterrupt:
        print("Caught control-c, closing database connection")
        data_handler.close()
Ejemplo n.º 24
0
class RemoteController(metaclass=Singleton):
    """
        Provide control over a SimpleXMLRPCServer.
    """

    def __init__(self, ip='localhost', port=8070):
        try:
            self.server = SimpleXMLRPCServer((ip, port), allow_none=True,
                                             logRequests=False)
            self._announcer = Announcer()
        except OSError as error:
            # If address already in use
            if error.errno == 98:
                raise Exception(
                    "Only one application instance can use this module")
            else:
                raise error

        self.server.register_introspection_functions()
        self.server.register_instance(RemoteDispatcher())

    @async
    def start(self):
        logging.info('REMOTE: Session started at ' +
                     str(self.server.server_address))

        self._announcer.start()
        self.server.serve_forever()  # Blocking
        self._announcer.stop()

        logging.info('REMOTE: Session ended')

    def stop(self):
        self.server.shutdown()

    @staticmethod
    def connect_to(uri):
        proxy = ServerProxy(uri, transport=TimeoutTransport())

        try:
            # Call a fictive method.
            proxy._()
        except Fault:
            # Connected, the method doesn't exist, which is expected.
            pass
        except socket.error:
            # Not connected, socket error mean that the service is unreachable.
            raise OSError('Session at ' + uri + ' is unreachable')

        # Just in case the method is registered in the XmlRPC server
        return proxy
Ejemplo n.º 25
0
def main():
    opt = parse_cmdline()
    logging.basicConfig(format="%(asctime)s %(levelname)s "
                               "%(filename)s:%(lineno)d %(message)s")

    LOG.setLevel(logging.INFO)

    LOG.info("psutil version %s at %s", psutil.__version__, psutil.__file__)

    server = SimpleXMLRPCServer((opt.ip, opt.port))
    LOG.info("listening at %s:%s", opt.ip, opt.port)
    server.register_introspection_functions()
    server.register_instance(MonHelperRPCInterface())
    server.serve_forever()
Ejemplo n.º 26
0
def start_server(obj):
	"""
		Purpose:
		Create the XML-RPC Server to allow CPython (and other tools) to interact with
		the dksikuli instance and its Sikuli functions.

		Similar to remotesrv.py's "start_server" function.
	"""
	guiServer = SimpleXMLRPCServer(('', 8080), allow_none=True)

	guiServer.register_introspection_functions()
	guiServer.register_instance(obj)

	#Now keep the server alive till the end of the run
	guiServer.serve_forever()
 def _start(self):
     '''
     Start the main server
     '''
     # Start the server.
     try:
         server = SimpleXMLRPCServer((gethostbyname(gethostname()),self.port))
         server.register_instance(self)
         print ('Start the main server successfully')
         print('The URL of this server is '+gethostbyname(gethostname())+':'+str(self.port))
         print('Copy this address to client.')
         server.serve_forever()
     except:
         self.port+=1
         self._start()
Ejemplo n.º 28
0
def startServer(box): 

    # Restrict to a particular path.
    class RequestHandler(SimpleXMLRPCRequestHandler):
        rpc_paths = ('/RPC2',)

    # Create server
    server = SimpleXMLRPCServer(("0.0.0.0", 8000),
                                requestHandler=RequestHandler, allow_none=True)
    server.register_introspection_functions()
    
    server.register_instance(box)
    
    # Run the server's main loop
    server.serve_forever()
Ejemplo n.º 29
0
def start(configdir):
    """Server program that ensures only a single bots-engine runs at any time,
    and no engine run requests are lost/discarded.

    Each request goes to a queue and is run in sequence when the previous run completes.
    Use of the job queue is optional and must be configured in bots.ini (jobqueue section, enabled = True).
    """

    botsinit.generalinit(configdir)
    if not botsglobal.ini.getboolean('jobqueue', 'enabled', False):
        print('Error: bots jobqueue cannot start; not enabled in {}/bots.ini'.format(configdir))
        sys.exit(1)
    nr_threads = 2  # botsglobal.ini.getint('jobqueue','nr_threads')
    process_name = 'jobqueue'

    logger = botsinit.initserverlogging(process_name)
    logger.log(25, 'Bots %(process_name)s started.', {'process_name': process_name})
    logger.log(25, 'Bots %(process_name)s configdir: "%(configdir)s".', {
               'process_name': process_name, 'configdir': botsglobal.ini.get('directories', 'config')})
    port = botsglobal.ini.getint('jobqueue', 'port', 28082)
    logger.log(25, 'Bots %(process_name)s listens for xmlrpc at port: "%(port)s".',
               {'process_name': process_name, 'port': port})

    # start launcher thread
    q = queue.Queue()

    lauchfrequency = botsglobal.ini.getint('jobqueue', 'lauchfrequency', 5)
    maxruntime = botsglobal.ini.getint('settings', 'maxruntime', 60)
    for thread in range(nr_threads):
        launcher_thread = threading.Thread(
            name='launcher',
            target=launcher,
            args=(logger, q, lauchfrequency, maxruntime),
            )
        launcher_thread.start()

    # the main thread is the xmlrpc server:
    # all adding, getting etc for jobqueue is done via xmlrpc.
    logger.info('Jobqueue server started.')
    server = SimpleXMLRPCServer(('localhost', port), logRequests=False)
    server.register_instance(Jobqueue(logger))

    try:
        server.serve_forever()
    except (KeyboardInterrupt, SystemExit) as e:  # noqa
        pass

    sys.exit(0)
Ejemplo n.º 30
0
def start_server():
    print('Intializing NLP engine... (this may take a while)')

    nlp.init()

    print('Starting HTTP server at <http://localhost:{port}>.'.format(port=PORT))
    if not fork_and_daemonize():
        exit(0)

    server = SimpleXMLRPCServer(('localhost', PORT), allow_none=True)
    server.register_function(ping)
    server.register_function(query)
    try:
        server.serve_forever()
    except:
        server.server_close()
# import SimpleXMLRPCServer bagian server
from xmlrpc.server import SimpleXMLRPCServer

# buat fungsi bernama file_upload()
def file_upload(filedata):
    
    # buka file 
    with open("hasil_upload.txt",'wb') as handle:
        #convert from byte to binary IMPORTANT!
        data1=filedata.data
        
        # tulis file tersebut
        handle.write(data1)
        return True  #IMPORTANT
        
# must have return value
# else error messsage: "cannot marshal None unless allow_none is enabled"

# buat server
port = 9999
server = SimpleXMLRPCServer(('',port))

# tulis pesan server telah berjalan
print ("Listening on port 9999")

# register fungsi 
server.register_function(file_upload, 'file_upload')

# jalankan server
server.serve_forever()
Ejemplo n.º 32
0
class Operate(object):
    def __init__(self, engine_name):
        threading.Thread(target=self.Run, daemon=True).start()
        threading.Thread(target=self._Call, daemon=True).start()

        self.loaded_engiens = {
            'Buffers': buffer,
            'Rg': file_content_rg,
            'All': all,
            'Lines': buffer_line
        }

        self.fzf_rpc = xmlrpc.client.ServerProxy("http://127.0.0.1:%s/" %
                                                 '4562',
                                                 allow_none=True)

    def StringToBase64(self, s):
        return str(base64.b64encode(s.encode('utf-8')), encoding='utf-8')

    def Run(self):
        self.fzf_port = 4563

        self.HOST = '127.0.0.1'
        self.client = SimpleXMLRPCServer((self.HOST, self.fzf_port),
                                         allow_none=True,
                                         logRequests=False)
        self.client.register_function(self.Call, "callback")
        self.client.register_function(self.Preview, "preview")
        self.client.serve_forever()

    def _show_msg(self, msg):
        if type(msg) is str:
            msg = msg.split('\n')
        res = ['[ECY_FZF]']
        res.extend(msg)
        rpc.DoCall('ECY#utils#echo', [res])

    def OpenFZF(self, context):
        engine = context['params']['engines']
        if engine not in self.loaded_engiens:
            self._show_msg("Wrong engine_name '%s'" % engine)
            return
        self.fzf_rpc.new(self.New(self.loaded_engiens[engine].Operate,
                                  context))
        rpc.DoCall('OpenPopupWindows')
        return context

    def CloseFZF(self, context):
        self.fzf_rpc.close_fzf({})
        return context

    def _handle_preview(self, event):
        event_id = int(event['id'])
        if event_id not in g_context:
            return ""

        obj = g_context[event_id]['obj']
        if not hasattr(obj, 'items'):
            logger.error("engine should has items attribution.")
            return ""

        index = ''
        for item in event['line']:
            if item == ' ':
                break
            index += item

        index = int(index)

        if len(obj.items) < index or len(obj.items) == 0:
            event['res'] = {}
        else:
            event['res'] = obj.items[index]
        return event

    def _Call(self):
        while True:
            try:
                event = g_call_queue.get()
                self._handler(event)
            except Exception as e:
                logger.exception(e)

    def _handler(self, event):
        callback_name = event['callback_name']
        event_id = int(event['id'])
        if event_id not in g_context:
            return "event_id not in g_context"
        obj = g_context[event_id]['obj']
        if not hasattr(obj, callback_name):
            return "has no " + callback_name
        if callback_name == 'Closed' and 'key' in event and 'key_bind' in g_context[
                event_id]:
            key = event['key']
            key_bind = g_context[event_id]['key_bind']
            if key in key_bind:
                fuc = key_bind[key]
                logger.debug(fuc)
                fuc(event)
        fuc = getattr(obj, callback_name)
        return fuc(event)

    def Preview(self, event):
        try:
            event = self._handle_preview(event)
            res = self._handler(event)
            if type(res) is not str:
                return "not str"
        except Exception as e:
            logger.exception(e)
            res = str(e)
        res = self.StringToBase64(res)
        return res

    def Call(self, event):
        g_call_queue.put(event)

    def New(self, new_obj, context):
        global event_id
        event_id += 1
        new_obj = new_obj(event_id)
        try:
            context['source'] = new_obj.GetSource(context)
            context['id'] = event_id
            context['engine_name'] = new_obj.engine_name
            key_bind = new_obj.RegKeyBind()
            if type(context['source']) != list or type(key_bind) is not dict:
                return

            g_context[event_id] = {
                'obj': new_obj,
                'context': context,
                'key_bind': key_bind,
                'id': event_id
            }
            return g_context[event_id]
        except Exception as e:
            logger.exception(e)
Ejemplo n.º 33
0
 def _start(self):
     s = SimpleXMLRPCServer(('', get_port(self.url)), logRequests=False)
     s.register_instance(self)
     s.serve_forever()
Ejemplo n.º 34
0
def server(host, port):
    from xmlrpc.server import SimpleXMLRPCServer
    server = SimpleXMLRPCServer((host, port))
    server.register_function(encrypt)
    server.serve_forever()
Ejemplo n.º 35
0
class RPC:
    _metodos = [
        'prefijo', 'infijo', 'posfijo', 'infijo_a_posfijo', 'evaluar_posfijo',
        'evaluar_prefijo'
    ]

    def __init__(self, direccion):
        self._servidor = SimpleXMLRPCServer(direccion, allow_none=True)

        for metodo in self._metodos:
            self._servidor.register_function(getattr(self, metodo))

    def infijo_a_posfijo(self, expresion):
        pila = []
        posfijo = ""
        numeros = '0123456789'
        especial = 'sctl'
        i = 0

        while (i < len(expresion)):
            char = expresion[i]

            # Si es un número
            if (char in numeros):
                posfijo = posfijo + char

            # Si es un paréntesis que abre
            elif (char == '('):
                pila.append('(')

            # Si es un paréntesis que cierra
            elif (char == ')'):

                # Buscas el paréntesis que abre
                aux = pila.pop()

                if (aux != '('):
                    posfijo = posfijo + aux

                while (aux != '('):
                    aux = pila.pop()
                    # No agregamos los (
                    if (aux != '('):
                        posfijo = posfijo + aux

            # Si es del tipo +-
            elif (char in '+-'):
                # Si la pila está vacía
                if (len(pila) == 0 or pila[-1] == '('):
                    pila.append(char)

                # Si la pila no está vacía
                else:
                    # Si el tope es de misma priori
                    if (pila[-1] in '+-'):
                        posfijo = posfijo + pila.pop()
                        pila.append(char)

                    # Si el tope es de mayor priori
                    elif (pila[-1] in '*/' or pila[-1] in '^'):
                        for i in range(len(pila)):
                            posfijo = posfijo + pila.pop()

                        pila.append(char)

            # Si es del tipo */
            elif (char in '*/'):
                # Si la pila está vacía
                if (len(pila) == 0 or pila[-1] == '('):
                    pila.append(char)

                # Si la pila no está vacía
                else:
                    # Si el tope es de misma priori
                    if (pila[-1] in '*/'):
                        posfijo = posfijo + pila.pop()
                        pila.append(char)

                    # Si el tope es de mayor priori
                    elif (pila[-1] in '^'):
                        for i in range(len(pila)):
                            posfijo = posfijo + pila.pop()

                        pila.append(char)

                    # Si el tope es de menor prioridad
                    elif (pila[-1] in '+-'):
                        pila.append(char)

            # Si es del tipo ^
            elif (char in '^' or pila[-1] == '('):
                # Si la pila está vacía
                if (len(pila) == 0):
                    pila.append(char)

                # Si la pila no está vacía
                else:
                    # Si el tope es de misma priori
                    if (pila[-1] in '^'):
                        posfijo = posfijo + pila.pop()
                        pila.append(char)

                    # Si el tope es de menor prioridad
                    elif (pila[-1] in '*/' or pila[-1] in '+-'):
                        pila.append(char)

            # Si es una funcion trigonometrica
            elif (char in especial):

                # Si es una funcion seno
                if (expresion[i:i + 3] == 'sen'):
                    pila.append('sen')

                elif (expresion[i:i + 3] == 'cos'):
                    pila.append('cos')

                elif (expresion[i:i + 3] == 'tan'):
                    pila.append('tan')

                elif (expresion[i:i + 3] == 'cot'):
                    pila.append('cot')

                elif (expresion[i:i + 3] == 'sec'):
                    pila.append('sec')

                elif (expresion[i:i + 3] == 'csc'):
                    pila.append('csc')

                elif (expresion[i:i + 3] == 'log'):
                    pila.append('log')

                i = i + 2

            i = i + 1

        # Si la pila no está vacía
        if (len(pila) > 0):
            for i in range(len(pila)):
                posfijo = posfijo + pila.pop()

        print(posfijo)
        return posfijo

    def evaluar_posfijo(self, expresion):
        pila = []
        numeros = '0123456789'
        especial = 'sctl'
        i = 0

        while (i < len(expresion)):
            #for char in expresion:
            char = expresion[i]

            # Si encontramos un número
            if (char in numeros):
                pila.append(char)

            # Si encontramos un operador
            elif (char in '+-*/^'):
                aux1 = pila.pop()
                aux2 = pila.pop()

                if (char == '+'):
                    pila.append(float(aux1) + float(aux2))

                elif (char == '-'):
                    pila.append(float(aux2) - float(aux1))

                elif (char == '/'):
                    pila.append(float(aux2) / float(aux1))

                elif (char == '*'):
                    pila.append(float(aux2) * float(aux1))

            elif (char in especial):

                # Si es una funcion seno
                if (expresion[i:i + 3] == 'sen'):
                    valor = float(pila.pop())
                    pila.append(np.sin(valor))

                elif (expresion[i:i + 3] == 'cos'):
                    valor = float(pila.pop())
                    pila.append(np.cos(valor))

                elif (expresion[i:i + 3] == 'tan'):
                    valor = float(pila.pop())
                    sen = np.sin(valor)
                    cos = np.cos(valor)
                    pila.append(sen / cos)

                elif (expresion[i:i + 3] == 'cot'):
                    valor = float(pila.pop())
                    sen = np.sin(valor)
                    cos = np.cos(valor)
                    pila.append(cos / sen)

                elif (expresion[i:i + 3] == 'sec'):
                    valor = float(pila.pop())
                    cos = np.cos(valor)
                    pila.append(1 / cos)

                elif (expresion[i:i + 3] == 'csc'):
                    valor = float(pila.pop())
                    sen = np.sin(valor)
                    pila.append(1 / sen)

                elif (expresion[i:i + 3] == 'log'):
                    valor = float(pila.pop())
                    pila.append(np.log10(valor))

                i = i + 2

            i = i + 1

        return pila.pop()

    def evaluar_prefijo(self, expresion):
        numeros = '0123456789'
        operadores = '+-*/^'
        pila = []

        # Recorremos la expresion al reves
        for char in expresion[::-1]:
            if (char in numeros):
                pila.append(char)

            elif (char in operadores):
                aux1 = pila.pop()
                aux2 = pila.pop()

                if (char == '+'):
                    pila.append(float(aux1) + float(aux2))

                elif (char == '-'):
                    pila.append(float(aux1) - float(aux2))

                elif (char == '*'):
                    pila.append(float(aux1) * float(aux2))

                elif (char == '/'):
                    pila.append(float(aux1) / float(aux2))

                elif (char == '^'):
                    pila.append(float(aux1) + float(aux2))

        return pila.pop()

    def prefijo(self, expresion):
        return self.evaluar_prefijo(expresion)

    def infijo(self, expresion):
        return self.evaluar_posfijo(self.infijo_a_posfijo(expresion))

    def posfijo(self, expresion):
        return self.evaluar_posfijo(expresion)

    def iniciar_servidor(self):
        self._servidor.serve_forever()
Ejemplo n.º 36
0
def start_server(ip, port=8301):
    server = SimpleXMLRPCServer((ip, port))
    rpc = RpcServer(server)
    server.register_instance(rpc)
    server.serve_forever()
Ejemplo n.º 37
0
class ServerThread(threading.Thread):
    """XML-RPC server thread to handle messages from CCU / Homegear"""
    def __init__(self,
                 local=LOCAL,
                 localport=LOCALPORT,
                 remotes=REMOTES,
                 devicefile=DEVICEFILE,
                 interface_id=INTERFACE_ID,
                 eventcallback=False,
                 systemcallback=False,
                 resolveparamsets=False):
        LOG.debug("ServerThread.__init__")
        threading.Thread.__init__(self)

        # Member
        self._interface_id = interface_id
        self._local = local
        self._localport = int(localport)
        self._devicefile = devicefile
        self.remotes = remotes
        self.eventcallback = eventcallback
        self.systemcallback = systemcallback
        self.resolveparamsets = resolveparamsets
        self.proxies = {}

        # Create proxies to interact with CCU / Homegear
        LOG.debug("__init__: Creating proxies")
        for remote, host in self.remotes.items():
            # Initialize XML-RPC
            try:
                socket.inet_pton(socket.AF_INET, host['ip'])
            except Exception as err:
                LOG.warning("Skipping proxy: %s" % str(err))
                continue
            if 'path' not in host:
                host['path'] = ""
            LOG.info("Creating proxy %s. Connecting to http://%s:%i%s" %
                     (remote, host['ip'], host['port'], host['path']))
            host['id'] = "%s-%s" % (self._interface_id, remote)
            try:
                self.proxies[host['id']] = LockingServerProxy(
                    "http://%s:%i%s" %
                    (host['ip'], host['port'], host['path']),
                    callbackip=host.get('callbackip', None),
                    callbackport=host.get('callbackport', None),
                    skipinit=not host.get('connect', True))
            except Exception as err:
                LOG.warning("Failed connecting to proxy at http://%s:%i%s" %
                            (host['ip'], host['port'], host['path']))
                LOG.debug("__init__: Exception: %s" % str(err))
                raise Exception
            try:
                host['type'] = BACKEND_UNKNOWN
                #if "Homegear" in self.proxies[host['id']].getVersion():
                #    LOG.debug("__init__: Host is Homegear")
                #    host['type'] = BACKEND_HOMEGEAR
                #else:
                #    LOG.debug("__init__: Host is CCU")
                #    host['type'] = BACKEND_CCU
            except Exception as err:
                LOG.warning("__init__: Failed to detect backend type: %s" %
                            str(err))
                host['type'] = BACKEND_UNKNOWN

        if not self.proxies:
            LOG.warning("No proxies available. Aborting.")
            raise Exception

        self._rpcfunctions = RPCFunctions(
            devicefile=self._devicefile,
            proxies=self.proxies,
            remotes=self.remotes,
            eventcallback=self.eventcallback,
            systemcallback=self.systemcallback,
            resolveparamsets=self.resolveparamsets)

        # Setup server to handle requests from CCU / Homegear
        LOG.debug("ServerThread.__init__: Setting up server")
        self.server = SimpleXMLRPCServer((self._local, self._localport),
                                         requestHandler=RequestHandler,
                                         logRequests=False)
        self._localport = self.server.socket.getsockname()[1]
        self.server.register_introspection_functions()
        self.server.register_multicall_functions()
        LOG.debug("ServerThread.__init__: Registering RPC functions")
        self.server.register_instance(self._rpcfunctions,
                                      allow_dotted_names=True)

    def run(self):
        LOG.info("Starting server at http://%s:%i" %
                 (self._local, self._localport))
        self.server.serve_forever()

    def proxyInit(self):
        """
        To receive events the proxy has to tell the CCU / Homegear where to send the events. For that we call the init-method.
        """
        # Call init() with local XML RPC config and interface_id (the name of
        # the receiver) to receive events. XML RPC server has to be running.
        for interface_id, proxy in self.proxies.items():
            if proxy._skipinit:
                continue
            if proxy._callbackip and proxy._callbackport:
                callbackip = proxy._callbackip
                callbackport = proxy._callbackport
            else:
                callbackip = proxy._localip
                callbackport = self._localport
            LOG.debug("ServerThread.proxyInit: init('http://%s:%i', '%s')" %
                      (callbackip, callbackport, interface_id))
            try:
                proxy.init("http://%s:%i" % (callbackip, callbackport),
                           interface_id)
                LOG.info("Proxy initialized")
            except Exception as err:
                LOG.debug("proxyInit: Exception: %s" % str(err))
                LOG.warning("Failed to initialize proxy")
                raise Exception

    def stop(self):
        """To stop the server we de-init from the CCU / Homegear, then shut down our XML-RPC server."""
        stopped = []
        for _, proxy in self.proxies.items():
            if proxy._callbackip and proxy._callbackport:
                callbackip = proxy._callbackip
                callbackport = proxy._callbackport
            else:
                callbackip = proxy._localip
                callbackport = self._localport
            remote = "http://%s:%i" % (callbackip, callbackport)
            LOG.debug("ServerThread.stop: init('%s')" % remote)
            if not callbackip in stopped:
                try:
                    proxy.init(remote)
                    stopped.append(callbackip)
                    LOG.info("Proxy de-initialized: %s" % remote)
                except Exception as err:
                    LOG.debug("proxyInit: Exception: %s" % str(err))
                    LOG.warning("Failed to de-initialize proxy")
                    raise Exception
        self.proxies.clear()
        LOG.info("Shutting down server")
        self.server.shutdown()
        LOG.debug("ServerThread.stop: Stopping ServerThread")
        self.server.server_close()
        LOG.info("Server stopped")

    def parseCCUSysVar(self, data):
        """Helper to parse type of system variables of CCU"""
        if data['type'] == 'LOGIC':
            return data['name'], data['value'] == 'true'
        elif data['type'] == 'NUMBER':
            return data['name'], float(data['value'])
        elif data['type'] == 'LIST':
            return data['name'], int(data['value'])
        else:
            return data['name'], data['value']

    def jsonRpcLogin(self, remote):
        """Login to CCU and return session"""
        session = False
        try:
            params = {
                "username": self.remotes[remote]['username'],
                "password": self.remotes[remote]['password']
            }
            response = self._rpcfunctions.jsonRpcPost(
                self.remotes[remote]['ip'], "Session.login", params)
            if response['error'] is None and response['result']:
                session = response['result']

            if not session:
                LOG.warning(
                    "ServerThread.jsonRpcLogin: Unable to open session.")
        except Exception as err:
            LOG.debug(
                "ServerThread.jsonRpcLogin: Exception while logging in via JSON-RPC: %s"
                % str(err))
        return session

    def jsonRpcLogout(self, remote, session):
        """Logout of CCU"""
        logout = False
        try:
            params = {"_session_id_": session}
            response = self._rpcfunctions.jsonRpcPost(
                self.remotes[remote]['ip'], "Session.logout", params)
            if response['error'] is None and response['result']:
                logout = response['result']
        except Exception as err:
            LOG.debug(
                "ServerThread.jsonRpcLogout: Exception while logging in via JSON-RPC: %s"
                % str(err))
        return logout

    def getAllSystemVariables(self, remote):
        """Get all system variables from CCU / Homegear"""
        variables = {}
        if self.remotes[remote]['username'] and self.remotes[remote][
                'password']:
            LOG.debug(
                "ServerThread.getAllSystemVariables: Getting all System variables via JSON-RPC"
            )
            session = self.jsonRpcLogin(remote)
            if not session:
                return
            try:
                params = {"_session_id_": session}
                response = self._rpcfunctions.jsonRpcPost(
                    self.remotes[remote]['ip'], "SysVar.getAll", params)
                if response['error'] is None and response['result']:
                    for var in response['result']:
                        key, value = self.parseCCUSysVar(var)
                        variables[key] = value

                self.jsonRpcLogout(remote, session)
            except Exception as err:
                self.jsonRpcLogout(remote, session)
                LOG.warning(
                    "ServerThread.getAllSystemVariables: Exception: %s" %
                    str(err))
        else:
            try:
                variables = self.proxies["%s-%s" %
                                         (self._interface_id,
                                          remote)].getAllSystemVariables()
            except Exception as err:
                LOG.debug("ServerThread.getAllSystemVariables: Exception: %s" %
                          str(err))
        return variables

    def getSystemVariable(self, remote, name):
        """Get single system variable from CCU / Homegear"""
        var = None
        if self.remotes[remote]['username'] and self.remotes[remote][
                'password']:
            LOG.debug(
                "ServerThread.getSystemVariable: Getting System variable via JSON-RPC"
            )
            session = self.jsonRpcLogin(remote)
            if not session:
                return
            try:
                params = {"_session_id_": session, "name": name}
                response = self._rpcfunctions.jsonRpcPost(
                    self.remotes[remote]['ip'], "SysVar.getValueByName",
                    params)
                if response['error'] is None and response['result']:
                    try:
                        var = float(response['result'])
                    except Exception as err:
                        var = response['result'] == 'true'

                self.jsonRpcLogout(remote, session)
            except Exception as err:
                self.jsonRpcLogout(remote, session)
                LOG.warning("ServerThread.getSystemVariable: Exception: %s" %
                            str(err))
        else:
            try:
                var = self.proxies["%s-%s" % (self._interface_id,
                                              remote)].getSystemVariable(name)
            except Exception as err:
                LOG.debug("ServerThread.getSystemVariable: Exception: %s" %
                          str(err))
        return var

    def deleteSystemVariable(self, remote, name):
        """Delete a system variable from CCU / Homegear"""
        if self.remotes[remote]['username'] and self.remotes[remote][
                'password']:
            LOG.debug(
                "ServerThread.deleteSystemVariable: Getting System variable via JSON-RPC"
            )
            session = self.jsonRpcLogin(remote)
            if not session:
                return
            try:
                params = {"_session_id_": session, "name": name}
                response = self._rpcfunctions.jsonRpcPost(
                    self.remotes[remote]['ip'], "SysVar.deleteSysVarByName",
                    params)
                if response['error'] is None and response['result']:
                    deleted = response['result']
                    LOG.warning(
                        "ServerThread.deleteSystemVariable: Deleted: %s" %
                        str(deleted))

                self.jsonRpcLogout(remote, session)
            except Exception as err:
                self.jsonRpcLogout(remote, session)
                LOG.warning(
                    "ServerThread.deleteSystemVariable: Exception: %s" %
                    str(err))
        else:
            try:
                return self.proxies["%s-%s" %
                                    (self._interface_id,
                                     remote)].deleteSystemVariable(name)
            except Exception as err:
                LOG.debug("ServerThread.deleteSystemVariable: Exception: %s" %
                          str(err))

    def setSystemVariable(self, remote, name, value):
        """Set a system variable on CCU / Homegear"""
        if self.remotes[remote]['username'] and self.remotes[remote][
                'password']:
            LOG.debug(
                "ServerThread.setSystemVariable: Setting System variable via JSON-RPC"
            )
            session = self.jsonRpcLogin(remote)
            if not session:
                return
            try:
                params = {
                    "_session_id_": session,
                    "name": name,
                    "value": value
                }
                if value is True or value is False:
                    params['value'] = int(value)
                    response = self._rpcfunctions.jsonRpcPost(
                        self.remotes[remote]['ip'], "SysVar.setBool", params)
                else:
                    response = self._rpcfunctions.jsonRpcPost(
                        self.remotes[remote]['ip'], "SysVar.setFloat", params)
                if response['error'] is None and response['result']:
                    res = response['result']
                    LOG.debug(
                        "ServerThread.setSystemVariable: Result while setting variable: %s"
                        % str(res))
                else:
                    if response['error']:
                        LOG.debug(
                            "ServerThread.setSystemVariable: Error while setting variable: %s"
                            % str(response['error']))

                self.jsonRpcLogout(remote, session)
            except Exception as err:
                self.jsonRpcLogout(remote, session)
                LOG.warning("ServerThread.setSystemVariable: Exception: %s" %
                            str(err))
        else:
            try:
                return self.proxies["%s-%s" % (self._interface_id,
                                               remote)].setSystemVariable(
                                                   name, value)
            except Exception as err:
                LOG.debug("ServerThread.setSystemVariable: Exception: %s" %
                          str(err))

    def getServiceMessages(self, remote):
        """Get service messages from CCU / Homegear"""
        try:
            return self.proxies["%s-%s" % (self._interface_id,
                                           remote)].getServiceMessages()
        except Exception as err:
            LOG.debug("ServerThread.getServiceMessages: Exception: %s" %
                      str(err))

    def rssiInfo(self, remote):
        """Get RSSI information for all devices from CCU / Homegear"""
        try:
            return self.proxies["%s-%s" %
                                (self._interface_id, remote)].rssiInfo()
        except Exception as err:
            LOG.debug("ServerThread.rssiInfo: Exception: %s" % str(err))

    def setInstallMode(self, remote, on=True, t=60, mode=1, address=None):
        """Activate or deactivate installmode on CCU / Homegear"""
        try:
            args = [on]
            if on and t:
                args.append(t)
                if address:
                    args.append(address)
                else:
                    args.append(mode)

            return self.proxies["%s-%s" %
                                (self._interface_id, remote)].setInstallMode(
                                    *args)
        except Exception as err:
            LOG.debug("ServerThread.setInstallMode: Exception: %s" % str(err))

    def getInstallMode(self, remote):
        """Get remaining time in seconds install mode is active from CCU / Homegear"""
        try:
            return self.proxies["%s-%s" %
                                (self._interface_id, remote)].getInstallMode()
        except Exception as err:
            LOG.debug("ServerThread.getInstallMode: Exception: %s" % str(err))

    def getAllMetadata(self, remote, address):
        """Get all metadata of device"""
        try:
            return self.proxies["%s-%s" % (self._interface_id,
                                           remote)].getAllMetadata(address)
        except Exception as err:
            LOG.debug("ServerThread.getAllMetadata: Exception: %s" % str(err))

    def getMetadata(self, remote, address, key):
        """Get metadata of device"""
        try:
            return self.proxies["%s-%s" %
                                (self._interface_id, remote)].getMetadata(
                                    address, key)
        except Exception as err:
            LOG.debug("ServerThread.getMetadata: Exception: %s" % str(err))

    def setMetadata(self, remote, address, key, value):
        """Set metadata of device"""
        try:
            return self.proxies["%s-%s" %
                                (self._interface_id, remote)].setMetadata(
                                    address, key, value)
        except Exception as err:
            LOG.debug("ServerThread.setMetadata: Exception: %s" % str(err))

    def deleteMetadata(self, remote, address, key):
        """Delete metadata of device"""
        try:
            return self.proxies["%s-%s" %
                                (self._interface_id, remote)].deleteMetadata(
                                    address, key)
        except Exception as err:
            LOG.debug("ServerThread.deleteMetadata: Exception: %s" % str(err))

    def listBidcosInterfaces(self, remote):
        """Return all available BidCos Interfaces"""
        try:
            return self.proxies["%s-%s" % (self._interface_id,
                                           remote)].listBidcosInterfaces()
        except Exception as err:
            LOG.debug("ServerThread.listBidcosInterfaces: Exception: %s" %
                      str(err))

    def homegearCheckInit(self, remote):
        """Check if proxy is still initialized"""
        rdict = self.remotes.get(remote)
        if not rdict:
            return False
        if rdict.get('type') != BACKEND_HOMEGEAR:
            return False
        try:
            interface_id = "%s-%s" % (self._interface_id, remote)
            return self.proxies[interface_id].clientServerInitialized(
                interface_id)
        except Exception as err:
            LOG.debug("ServerThread.homegearCheckInit: Exception: %s" %
                      str(err))
            return False
Ejemplo n.º 38
0
class PaperDaemon(Thread):
    """This system checks, converts and stores
    """

    running = False

    def __init__(self, indir, pdir, outdir, logger):
        Thread.__init__(self)
        self.fsw = FilesystemWatcher(logger)
        self.fsw.watch_directory(indir)
        self.watchdir = indir
        self.pdir = pdir
        self.outdir = outdir
        self.logger = logger
        self.paper_classifier = PaperClassifier()
        self.processq = Queue()
        self.workercount = 0

# ---------------------------------------------------------------------

    def register_worker_pool(self, size):
        self.workercount += size

    def unregister_worker_pool(self, size):
        self.workercount -= size

    def setup_server(self):

        self.logger.info("Establishing queue management server")

        self.qm = SimpleXMLRPCServer((config['PP_LISTEN_ADDRESS'],
                                      config['PP_LISTEN_PORT']),
                                     logRequests=False,
                                     allow_none=True)

        # load preprocessing stats from disk
        self.stats = load_pp_stats(self.outdir)

        self.qm.register_function(lambda: self.processq.qsize(), "qsize")

        self.qm.register_function(lambda x: _get_uptox_items(
            x, self.processq),             "get_work")

        self.qm.register_function(self.register_worker_pool, "register_pool")

        self.qm.register_function(lambda: self.workercount, "poolsize")

        self.qm.register_function(self.unregister_worker_pool,
                                  "unregister_pool")

        self.qm.register_function(lambda: self.stats[0], "average")

        self.qm.register_function(
            lambda x: store_result(x, self.fsw.paper_queue, self.outdir,
                                   self.logger), "return_result")

        self.logger.info("Listening for paper workers on %s:%d auth=%s",
                         config['PP_LISTEN_ADDRESS'], config['PP_LISTEN_PORT'],
                         config['PP_AUTH_KEY'])

        t = Thread(target=lambda: self.qm.serve_forever())
        t.start()

# ---------------------------------------------------------------------

    def run(self):
        self.running = True

        self.setup_server()
        self.fsw.start()

        # enqueue any xml papers in the working directory
        for root, _, files in os.walk(self.pdir):

            for file in files:
                if(file.endswith(".xml")):
                    self.processq.put(os.path.join(root, file))

        self.logger.info(
            "Found %d files in the 'working' dir queued for annotation", self.processq.qsize())

        while self.running:

            self.task_files = []

            try:
                task = self.fsw.paper_queue.get()
            except:
                continue

            try:
                if task[0] == 'STOP':
                    print("Stopping...")
                    break
                elif task[0] == "QUEUE":
                    # Add paper to queue and convert from PDF if required
                    self.logger.info("Checking if %s exists", task[1])
                    try:
                        self.enqueue(task[1])

                    except PaperExistsException:
                        print("Paper Already exists, cleaning up")
                        self.cleanup(task[1])

                elif task[0] == "STORE":
                    try:
                        self.store(task[1])
                    except PaperExistsException:
                        print("Paper Already exists, cleaning up")
                        self.cleanup(task[1].paper)

            except Exception as e:
                # get exception information and dump to user
                exc_type, exc_obj, exc_tb = sys.exc_info()
                self.logger.error("Error processing paper: %s", e)

                for line in traceback.format_tb(exc_tb):
                    self.logger.error(line)

        self.logger.info("Exited main loop. Shutting down...")
        # when we come out of the loop kill the filesystem watcher
        self.logger.info("Shutting down filesystem watcher...")
        self.fsw.stop()
        self.logger.info("Shutting down XML-RPC server...")
        self.qm.shutdown()


# ---------------------------------------------------------------------

    def store(self, result):
        """Once a document has been handled, store it in file"""

        if(isinstance(result, PreprocessingException)):
            self.handleProcessingException(result)
            print(result.files)
        else:
            filename, outfile, timetaken = result

            if(self.paperExists(outfile)):
                inform_watcher(self.logger, filename,
                               exception=PaperExistsException("Paper Already Exists"))

                self.cleanup(filename)
                return None

            # store the paper object in database
            paperObj = self.storePaperData(outfile)

            # add paper classification to database
            paperObj = self.classifyPaper(paperObj)

            filenames = [filename, outfile]

            basename = os.path.basename(filename)
            name, ext = os.path.splitext(basename)
            pdf = os.path.join(self.watchdir, name + ".pdf")

            if os.path.exists(pdf):
                filenames.append(pdf)

            # add the related files to the db
            self.savePaperFiles(filenames, paperObj)

            self.logger.info("Paper has been added successfully")

            try:
                inform_watcher(self.logger, filename, paperObj=paperObj)
            except Exception as e:
                self.logger.warn("Failed to inform watcher about paper"
                                 + " success: %s", e)

            # if config.has_key('TWITTER_ENABLED') and config['TWITTER_ENABLED']:
            #     try:
            #         tweet_paper(paperObj)
            #     except Exception as e:
            #         self.logger.warn("Could not tweet about paper %s", e)

            # finally update stats
            average = self.stats[0]

            total = self.stats[1] + 1

            if(average == 0.0):
                self.stats = (timetaken, total)
            else:
                self.stats = (average + ((timetaken - average)
                                         / self.stats[1]), total)

            # save the preprocessing stats to disk
            save_pp_stats(self.stats, self.outdir)

            return paperObj

# ---------------------------------------------------------------------
    def savePaperFiles(self, filenames, paper):

        for file in filenames:

            dirname = os.path.dirname(file)
            basename = os.path.basename(file)

            final_path = os.path.join(self.outdir, basename)

            self.logger.info("Saving file %s", final_path)

            if(file != final_path):
                os.rename(file, final_path)

            fileObj = PaperFile(path=final_path)
            db.session.add(fileObj)
            paper.files.append(fileObj)

        db.session.commit()

# ---------------------------------------------------------------------

    def handleProcessingException(self, result):
        """Method for handling processing exceptions"""
        # get exception information and dump to user
        self.logger.error("Error processing paper %s: %s",
                          result.paper, result)

        inform_watcher(self.logger, result.paper, exception=result)

        try:
            print(result.files)
            # send the error report
            send_error_report(result, result.traceback,
                              [result.paper])

        except Exception as e:
            self.logger.error("ERROR SENDING EMAIL: %s", e)

        self.cleanup(result.paper)

# ---------------------------------------------------------------------

    def storeFile(self, filename, data):
        """Store file data retrieved from a remote worker"""

        self.logger.info("Results are in for %s, storing to disk...",
                         filename)

        basename = os.path.basename(filename)
        name, ext = os.path.splitext(basename)

        outfile = os.path.join(self.outdir, name+"_final.xml")

        with open(outfile, 'wb') as f:
            f.write(data)

        return outfile


# ---------------------------------------------------------------------

    def cleanup(self, filename):
        """If something went wrong, clean up mess"""

        basename = os.path.basename(filename)
        dirname = os.path.dirname(filename)
        name, ext = os.path.splitext(basename)

        # delete the file
        self.logger.info("Removing file %s", filename)
        os.unlink(filename)

        pdf = os.path.join(self.watchdir, basename) + ".pdf"

        if(os.path.exists(pdf)):
            self.logger.info("Removing PDF file %s", pdf)

            os.unlink(pdf)

# ---------------------------------------------------------------------

    def stop(self):
        self.logger.info("Sending stop command to task queue")
        self.running = False
        self.fsw.paper_queue.put(("STOP",))
        self.join()

# ---------------------------------------------------------------------

    def enqueue(self, file):
        """Check if a file is a PDF and if it is, convert"""

        self.logger.info("Checking format of file %s", file)

        basename = os.path.basename(file)
        name, ext = os.path.splitext(basename)

        # pdf = False

        # if(basename.endswith(".pdf")):

        #     self.logger.info("%s has been converted and re-queued for paper check",
        #                      self.convertPDF(file))

        # else:
        #     if(self.paperExists(file)):
        #         raise PaperExistsException("Paper Already Exists")

        basename = os.path.basename(file)
        newname = os.path.join(self.pdir, basename)
        os.rename(file, newname)

        self.processq.put(newname)

        # enqueue the file to be annotated
        self.logger.info("%s has been enqueued for annotation", basename)


# ---------------------------------------------------------------------

    def paperExists(self, infile):
        """Return true if paper with same authors and title already in db"""
        parser = PaperParser()

        paper = parser.paperExists(infile)

        if paper != None:
            inform_watcher(self.logger, infile, exists=True, paperObj=paper)

        return paper != None

# ---------------------------------------------------------------------

    def classifyPaper(self, paper):
        """Decide what 'type' the paper is - case study, research or review"""

        type = str(self.paper_classifier.classify_paper(paper))
        self.logger.info("Determined paper %s is of type %s", paper.title,
                         type)

        paper.type = type

        return db.session.merge(paper)


# ---------------------------------------------------------------------

    def storePaperData(self, infile):
        """Call the metadata parser and return DB id for this paper"""
        parser = PaperParser()
        paper = parser.storePaper(infile)
        self.logger.info("Added paper '%s' to database", paper.title)
        return paper
Ejemplo n.º 39
0
class serverRPC:
    def __init__(self,
                 ip="localhost",
                 puerto=5500,
                 tipo="main",
                 procesadorGhz=1.0,
                 ramMB=8192):
        #Server Control
        self.server = SimpleXMLRPCServer((ip, puerto),
                                         requestHandler=RequestHandler,
                                         allow_none=True)
        self.server.register_introspection_functions()
        self.ip = ip
        self.puerto = puerto
        self.tipo = tipo
        self.conecction = None
        self.busyWith = None
        self.servers = []
        self.procesadorGhz = procesadorGhz
        self.ramMB = ramMB

        #Control Tkinter
        self.executing = False
        self.executingHelp = False
        self.startExecuteTime = 0.0
        self.numberOfExecutes = 0

        #Tkinter
        self.root = tkinter.Tk()
        self.root.wm_title("machine-" + tipo)  #da el titulo a la ventana

        scrollbar = tkinter.Scrollbar(self.root, orient=tkinter.VERTICAL)
        scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)

        self.TextoBox = tkinter.Text(self.root,
                                     height=8,
                                     width=80,
                                     yscrollcommand=scrollbar.set)
        self.TextoBox.pack(side=tkinter.LEFT, fill=tkinter.Y)

        frame = tkinter.Frame(self.root)
        frame.pack()

        tkinter.Label(frame, text='Estado').grid(row=1, column=0)
        self.answer = tkinter.StringVar()
        tkinter.Label(frame, textvariable=self.answer).grid(row=1, column=1)
        self.buttonSend = None
        if (tipo[:6] != "server" and tipo != "main"):
            tkinter.Label(frame, text='Procesador (Ghz)').grid(row=0, column=0)
            self.BoxProcesador = tkinter.DoubleVar()
            tkinter.Entry(frame,
                          textvariable=self.BoxProcesador).grid(row=0,
                                                                column=1)
            tkinter.Label(frame, text='Ram (MB)').grid(row=0, column=2)
            self.BoxRAM = tkinter.DoubleVar()
            tkinter.Entry(frame, textvariable=self.BoxRAM).grid(row=0,
                                                                column=3)
            self.buttonSend = tkinter.Button(frame,
                                             text='Send',
                                             command=self.sendCom)
            self.buttonSend.grid(row=2, columnspan=2)

    def printBox(self, value):
        self.TextoBox.insert(tkinter.END, "\n" + str(value))
        self.TextoBox.see(tkinter.END)

    def printBoxRemote(self, value):
        self.TextoBox.after(250, self.printBox, str(value))

    def sendCom(self):
        self.buttonSend.config(state="disable")
        valueProcesador = self.BoxProcesador.get()
        valueRAM = self.BoxRAM.get()
        if (valueProcesador is not None and valueRAM is not None):
            serverToGet = self.conecction.getExecuter(
                valueProcesador, valueRAM,
                "http://" + self.ip + ":" + str(self.puerto))
            self.BoxProcesador.set("")
            self.BoxRAM.set("")
            if (serverToGet is not None):

                self.busyWith = self.getServer(serverToGet)

                self.startExecute()

        self.answer.set("processing...")

    def getServer(self, serverToGet):
        self.buttonSend.after(10000, self.cleanProcess)
        return xmlrpc.client.ServerProxy(serverToGet)

    def runGraph(self):
        self.root.mainloop()

    def getStatus(self):
        return self.procesadorGhz, self.ramMB

    def startExecute(self):
        self.answer.set("Ejecutando...")
        self.executing = True
        self.numberOfExecutes = 0
        self.executeLocal()

    def executeLocal(self):

        if (self.busyWith is not None and self.executing):
            self.numberOfExecutes += 1
            self.busyWith.printBoxRemote(
                "Ejecutando vez {}, proveniente de {}%".format(
                    self.numberOfExecutes,
                    "http://" + self.ip + ":" + str(self.puerto)))

        if (self.executing):
            self.numberOfExecutes += 1
            self.printBox("Ejecutando vez {}".format(self.numberOfExecutes))
            self.buttonSend.after(250, self.executeLocal)

    def getExecuter(self, procesadorGhz=1.0, ramMB=8192, machine=""):
        self.printBox(
            "Buscando equipo con procesador {} Ghz y ram {} MB".format(
                procesadorGhz, ramMB))
        servers = []

        for i in self.servers:
            servers.append([xmlrpc.client.ServerProxy(i), i])

        #machineConecction=xmlrpc.client.ServerProxy(machine)
        serverToAsign = None
        trys = 0
        while serverToAsign is None:
            for i in servers:
                procesador, ram = i[0].getStatus()
                if (procesadorGhz < procesador and ramMB < ram):
                    if (i[0].setProcess(machine)):
                        serverToAsign = i[1]
                        self.printBox("Se ha encontrado un servidor")
                        break
            if (trys >= 10):
                self.printBox("No hay servidores disponibles para lo recibido")
                break
            else:
                trys += 1
        if (serverToAsign is not None):
            self.printBox("Entregado valor compartido {}".format(machine))
            return serverToAsign
            #self.buttonExec.after(5000, self.execute)
        else:
            self.printBox("No hay disponible")

        #else:
        #	self.printBox("Terminado valor compartido con {}".format(self.busyWith))
        #	self.busyWith.cleanProcess()
        #	self.busyWith=None

    def setProcess(self, client):
        if (self.busyWith is None):
            self.busyWith = client
            self.printBox("Se ha dado compartido a {}".format(self.busyWith))
            return True
        self.printBox("Estoy ocupado, No se le puede compartir a {}".format(
            self.busyWith))
        return False

    def cleanProcess(self):
        self.printBox("Se ha terminado el compartido a {}".format(
            self.busyWith))
        if (self.tipo[:6] != "server" and self.tipo != "main"):
            self.busyWith.cleanProcess()
            self.buttonSend.config(state="normal")

        self.busyWith = None
        self.executing = False
        self.answer.set("Ready")

    # Funciones del servidor para el cliente
    def register(self, ipServer, puertoServer):
        #value=xmlrpc.client.ServerProxy("http://"+ipServer+":"+puertoServer)
        self.servers.append("http://" + ipServer + ":" + puertoServer)
        self.printBox(
            "Se ha registrado el servidor {}".format("http://" + ipServer +
                                                     ":" + puertoServer))

    def runServer(self):
        print("corriendo server de tipo {}".format(self.tipo))
        if self.tipo == "main":
            self.server.register_function(self.register, 'register')
            self.server.register_function(self.getExecuter, 'getExecuter')

        elif self.tipo[:6] == "server":
            self.server.register_function(self.setProcess, 'setProcess')
            self.server.register_function(self.getStatus, 'getStatus')
            self.server.register_function(self.cleanProcess, 'cleanProcess')
            self.server.register_function(self.printBoxRemote,
                                          'printBoxRemote')
            ipServer = str(input("Ingrese la ip del server principal\n"))
            puertoServer = str(
                input("ingrese el puerto del server principal\n"))

            self.conecction = xmlrpc.client.ServerProxy("http://" + ipServer +
                                                        ":" + puertoServer,
                                                        allow_none=True)
            self.conecction.register(self.ip, str(self.puerto))
        elif (self.tipo[:7] == "cliente"):
            #self.server.register_function(self.setTime, 'setTime')
            #self.server.register_function(self.getTime, 'getTime')
            ipServer = str(input("Ingrese la ip del server principal\n"))
            puertoServer = str(
                input("ingrese el puerto del server principal\n"))
            self.conecction = xmlrpc.client.ServerProxy("http://" + ipServer +
                                                        ":" + puertoServer,
                                                        allow_none=True)
        else:
            print("Hay errores")
            raise SystemExit(1)
        self.server.serve_forever()
Ejemplo n.º 40
0
 def _start(self):
     """    供内部用来启动XML-RPC服务器    """
     s = SimpleXMLRPCServer(("", get_port(self.url)), logRequests=False)
     s.register_instance(self)
     s.serve_forever()
Ejemplo n.º 41
0
class serverRPC: 
	def __init__(self, ip="localhost", puerto=5500, tipo="main"):
		#Server Control
		self.server=SimpleXMLRPCServer((ip, puerto), requestHandler=RequestHandler, allow_none=True)
		self.server.register_introspection_functions()
		self.ip=ip
		self.puerto=puerto
		self.ownAddres="http://"+ip+":"+str(puerto)
		# used how name of server/client
		self.tipo=tipo
		# data about coneection to server
		self.conecction=None
		# list with name files from server
		self.filesList=[]
		# States on program
		self.reading=False
		self.writing=False


		#Tkinter
		self.root = tkinter.Tk()
		# Window name
		self.root.wm_title("archivos - "+tipo)
		# Scrollbar object
		scrollbar = tkinter.Scrollbar(self.root, orient=tkinter.VERTICAL)
		# Text Box - Log server
		self.TextoBox = tkinter.Text(self.root, height=8, width=80, yscrollcommand=scrollbar.set)
		# Text Box - Text to edit
		self.TextoBox2 = tkinter.Text(self.root, height=8, width=80, yscrollcommand=scrollbar.set)
		# set scrollbar movement
		scrollbar.config(command=self.yview)
		# set scrollbar direction
		scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
		# set scrollbar on Text Box
		self.TextoBox.pack(side=tkinter.LEFT, fill=tkinter.Y)
		
		self.TextoBox2.pack(side=tkinter.LEFT, fill=tkinter.Y)
		# Set text box mode only read
		self.TextoBox2.config(state=tkinter.DISABLED)
		self.TextoBox.config(state=tkinter.DISABLED)
		# Set frame from Tkinter Object
		frame = tkinter.Frame(self.root)
		frame.pack()
		
		self.list = tkinter.Listbox(self.root, selectmode=tkinter.SINGLE, yscrollcommand=scrollbar.set)
		self.list.pack(fill=tkinter.BOTH, expand=1)
		
		
		
		self.buttonShowFile = tkinter.Button(frame, text='Show File', command=self.readFile)
		self.buttonShowFile.grid(row=2, columnspan=1)

		self.buttonWriteFile = tkinter.Button(frame, text='Write File', command=self.writeFile)
		self.buttonWriteFile.grid(row=3, columnspan=1)

		self.buttonSave = tkinter.Button(frame, text='Save', command=self.saveFile)
		self.buttonSave.grid(row=4, columnspan=1)
		
		self.buttonUpdate = tkinter.Button(frame, text='Update Files List', command=self.updateListBox)
		self.buttonUpdate.grid(row=5, columnspan=1)
		
		self.buttonDelete = tkinter.Button(frame, text='DELETE', command=self.deleteFileServer)
		self.buttonDelete.grid(row=6, columnspan=1)

		self.buttonCancel = tkinter.Button(frame, text='Cancel', command=self.cancelAction)
		self.buttonCancel.grid(row=7, columnspan=1)
		
		


		tkinter.Label(frame, text='Estado').grid(row=1, column=0)
		self.answer = tkinter.StringVar()
		tkinter.Label(frame, textvariable=self.answer).grid(row=1, column=1)
		
		#Temp value
		self.buttonCancel.config(state=tkinter.DISABLED)
		self.answer.set("Ready")
	
	def yview(self, *args):
		self.TextoBox.yview(*args)
		self.TextoBox2.yview(*args)
		self.list.yview(*args)

	def printBox1(self, value):
		self.TextoBox.config(state=tkinter.NORMAL)
		self.TextoBox.insert(tkinter.END, "\n"+"[ "+time.asctime(time.localtime(time.time()))+"] "+str(value))
		self.TextoBox.see(tkinter.END)
		self.TextoBox.config(state=tkinter.DISABLED)

	def printBox2(self, value):
		self.TextoBox2.config(state=tkinter.NORMAL)
		self.TextoBox2.delete('1.0', tkinter.END)
		self.TextoBox2.insert(tkinter.END, str(value))
		self.TextoBox2.see(tkinter.END)
		self.TextoBox2.config(state=tkinter.DISABLED)
	
	def setList(self, item):
		self.list.delete(0, tkinter.END)#Borra TODO
		if(isinstance(item, list)):
			for i in item:
				self.list.insert(tkinter.END, i)


	def readFile(self):
		fileSelected=self.list.curselection()
		if(len(fileSelected)==1):
			fileName=self.list.get(fileSelected[0])
			permission=self.conecction.getPermission(self.ownAddres, fileName)
			if(permission=="lectura" or permission=="escritura"):
				self.printBox1("Solicitado archivo {}".format(fileName))
				fileData=self.conecction.getFile(fileName)
				self.printBox1("Obtenido archivo {}".format(fileName))
				self.printBox2(fileData)
				self.reading=True
				self.list.activate(fileSelected[0])
				self.list.configure(state=tkinter.DISABLED)
				self.buttonShowFile.config(state=tkinter.DISABLED)
				self.buttonWriteFile.config(state=tkinter.DISABLED)
				self.buttonSave.config(state=tkinter.DISABLED)
				self.buttonDelete.config(state=tkinter.DISABLED)
				self.buttonUpdate.config(state=tkinter.DISABLED)
				self.buttonCancel.config(state=tkinter.NORMAL)
				
				self.answer.set("Leyendo archivo")
			else:
				self.printBox1("ERROR, Permisos insuficientes")
		elif(len(fileSelected)==0):
			self.printBox1("No hay archivo seleccionado")
		else:
			self.printBox1("Hay mas de uno seleccionado")
	
	def writeFile(self):
		fileSelected=self.list.curselection()
		
		if(len(fileSelected)==1):
			fileName=self.list.get(fileSelected[0])
			self.printBox1("Solicitado archivo {} para escritura".format(fileName))
			permission=self.conecction.getPermission(self.ownAddres, fileName)
			if(permission=="escritura"):
				if(self.conecction.setBusy(self.ownAddres, fileName)):#Set busy file
					
					fileData=self.conecction.getFile(fileName)
					self.printBox1("Obtenido archivo {}".format(fileName))
					self.printBox2(fileData)
					self.TextoBox2.config(state=tkinter.NORMAL)
					self.writing=True
					self.list.activate(fileSelected[0])
					self.list.configure(state=tkinter.DISABLED)
					self.buttonShowFile.config(state=tkinter.DISABLED)
					self.buttonWriteFile.config(state=tkinter.DISABLED)
					self.buttonDelete.config(state=tkinter.DISABLED)
					self.buttonUpdate.config(state=tkinter.DISABLED)
					self.buttonSave.config(state=tkinter.NORMAL)
					self.buttonCancel.config(state=tkinter.NORMAL)
					self.list.activate(fileSelected[0])
					self.answer.set("Editando archivo")

				else:
					self.printBox1("ERROR, El archivo no se puede editar, está ocupado")
			else:
				self.printBox1("ERROR, Permisos insuficientes para escribir el archivo {}".format(fileName))
		elif(len(fileSelected)==0):
			self.printBox1("No hay archivo seleccionado")
		else:
			self.printBox1("Hay mas de uno seleccionado")

	def deleteFileServer(self):
		fileSelected=self.list.curselection()
		
		if(len(fileSelected)==1):
			fileName=self.list.get(fileSelected[0])
			self.printBox1("Solicitado borrado {}".format(fileName))
			permission=self.conecction.getPermission(self.ownAddres, fileName)
			if(permission=="escritura"):
				if(self.conecction.setBusy(self.ownAddres, fileName)):#Set busy file
					
					self.conecction.deleteFile(fileName)
					self.answer.set("Borrando Archivo")
				else:
					self.printBox1("ERROR, El archivo no se puede borrar está ocupado")
			else:
				self.printBox1("ERROR, Permisos insuficientes para borrar el archivo {}".format(fileName))
		elif(len(fileSelected)==0):
			self.printBox1("No hay archivo seleccionado")
		else:
			self.printBox1("Hay mas de uno seleccionado")

	def modifyFile(self, fileName, fileData):
		fileEdit=open(fileName, "w")
		fileEdit.write(fileData)
		fileEdit.close()

	def deleteFile(self, fileName):
		os.remove(fileName)

	def saveFile(self):
		self.answer.set("Guardando...")
		fileName=self.list.get(tkinter.ACTIVE)
		fileData=self.TextoBox2.get(1.0, tkinter.END)
		self.conecction.modifyFile(fileName, fileData)
		self.writing=False
		self.list.configure(state=tkinter.NORMAL)
		self.buttonShowFile.config(state=tkinter.NORMAL)
		self.buttonWriteFile.config(state=tkinter.NORMAL)
		self.buttonUpdate.config(state=tkinter.NORMAL)
		self.buttonDelete.config(state=tkinter.NORMAL)
		self.buttonSave.config(state=tkinter.DISABLED)
		self.buttonCancel.config(state=tkinter.DISABLED)
		self.printBox2("")
		self.printBox1("Guardado archivo {}".format(fileName))
		self.answer.set("Ready")

	def cancelAction(self):
		if(self.writing):
			fileName=self.list.get(tkinter.ACTIVE)
			self.conecction.removeBusy(fileName)
		self.writing=False
		self.reading=False
		self.printBox2("")
		self.printBox1("acción cancelada")
		self.buttonShowFile.config(state=tkinter.NORMAL)
		self.buttonWriteFile.config(state=tkinter.NORMAL)
		self.buttonUpdate.config(state=tkinter.NORMAL)
		self.buttonDelete.config(state=tkinter.NORMAL)
		self.buttonCancel.config(state=tkinter.DISABLED)
		self.list.config(state=tkinter.NORMAL)


	def runGraph(self):
		self.root.mainloop()

	def updateListBox(self):
		listaTemp=self.conecction.getFilesList()
		self.list.delete(0, tkinter.END)#Borra TODO
		for i in listaTemp:
			self.list.insert(tkinter.END, i)
		self.printBox1("Se ha actualizado la lista")

	def getFiles(self):
		files = [f for f in os.listdir('.') if os.path.isfile(f)]
		files.remove(__file__)
		return files

	def getFile(self, fileName):
		fileText=open(fileName, "r").read()
		return fileText

	def registrarse(self):
		self.conecction.register(self.ip, str(self.puerto))
	
	def runServer(self):
		print("corriendo server de tipo {}".format(self.tipo))
		self.server.register_function(self.getFiles, 'getFiles')
		self.server.register_function(self.getFile, 'getFile')
		self.server.register_function(self.modifyFile, 'modifyFile')
		self.server.register_function(self.deleteFile, 'deleteFile')
		self.server.register_function(self.printBox1, 'printBox1')
		ipServer = str(input("Ingrese la ip del server principal\n"))
		puertoServer = str(input("ingrese el puerto del server principal\n"))

		self.conecction=xmlrpc.client.ServerProxy("http://"+ipServer+":"+puertoServer, allow_none=True)
		self.buttonCancel.after(1000, self.registrarse)
		self.server.serve_forever()
Ejemplo n.º 42
0
    try:
        with open('keys/client_keys.txt', 'r') as client_keys:
            for i, line in enumerate(client_keys):
                CLIENT_KEYS.append(line.rstrip().encode())
    except OSError:
        print('Coordinator unable to read client_keys')
        raise


if __name__ == '__main__':
    SERVER.register_function(add_fs)
    SERVER.register_function(remove_fs)
    SERVER.register_function(get_fs)
    SERVER.register_function(get_enc_session_key)
    SERVER.register_function(register_client)

    try:
        read_keys()
    except OSError:
        print(
            'Coordinator unable to read one or more of fs_keys or client_keys. Exiting...'
        )
        exit()

    try:
        print(f'Central coordinator started at port {COORDINATOR_PORT}')
        SERVER.serve_forever()
    except KeyboardInterrupt:
        print('Coordinator terminated')
Ejemplo n.º 43
0
                    1] + ' postou: ' + row[2] + '\n'
    except:
        posts = 'Erro: NOME inexistente ou DATA digitada de maneira errada'

    return posts  # retorna posts encontrados


server.register_function(RetrieveTopic, 'retrieveTopic')


# Recupera a quantidade de posts em um intervalo entre duas datas
def Poll(topico, data1, data2):

    try:
        cursor.execute(
            "SELECT COUNT(*) FROM post WHERE time>=(?) AND time<=(?)", (
                data1,
                data2,
            ))
        results = cursor.fetchall()
    except:
        results = 0

    return results


server.register_function(Poll, 'poll')

server.serve_forever(
)  # faz a parte Servidor rodar em loop e funcionar ate o fim da execucao do programa
Ejemplo n.º 44
0
class HumanoidAgent():
    def __init__(self, config_json):
        self.domain = config_json["domain"]
        if "port" in config_json:
            self.rpc_port = config_json["port"]
        else:
            self.rpc_port = self.get_random_port()
        print("Serving at %s:%d" % (self.domain, self.rpc_port))
        self.server = SimpleXMLRPCServer((self.domain, self.rpc_port),
                                         RPCHandler)
        self.server.register_function(self.predict, "predict")
        self.server.register_function(self.render_view_tree,
                                      "render_view_tree")
        self.server.register_function(self.render_content_free_view_tree,
                                      "render_content_free_view_tree")

        train_config_path = os.path.join(
            os.path.dirname(os.path.realpath(__file__)), "train",
            "config.json")
        with open(train_config_path, "r") as train_config_file:
            self.train_config_json = json.load(train_config_file)

        self.model = MultipleScreenModel(self.train_config_json,
                                         training=False)
        self.saver = tf.train.Saver()
        self.sess = tf.Session()
        self.saver.restore(self.sess, config_json["model_path"])
        self.data_processor = DroidBotDataProcessor(config_json)
        self.text_generator = TextGenerator(config_json)
        print("=== Humanoid XMLRPC service ready at %s:%d ===" %
              (self.domain, self.rpc_port))

    def get_random_port(self):
        temp_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        temp_sock.bind(("", 0))
        port = temp_sock.getsockname()[1]
        temp_sock.close()
        return port

    def predict(self, query_json_str):
        query_json = json.loads(query_json_str)
        try:
            self.data_processor.update_origin_dim(query_json["screen_res"])
            possible_events = query_json["possible_events"]
            image, heat, interact = self.data_processor.process(query_json)
            heatmap, interact, pool5_heat_out = self.sess.run(
                [
                    self.model.predict_heatmaps, self.model.predict_interacts,
                    self.model.pool5_heat_out
                ],
                feed_dict=self.model.get_feed_dict(image, heat, interact))
            """
            visualize_data(image[0] + 0.5)
            visualize_data(image[1] + 0.5)
            visualize_data(image[2] + 0.5)
            visualize_data(image[3] + 0.5)
            visualize_data(heatmap[0])
            print(interact[0])
            """
            # print(event_probs)
            # print(prob_idx)
            event_probs = self.data_processor.events_to_probs(
                possible_events, heatmap[0, :, :, 0], interact[0])
            prob_idx = sorted(range(len(event_probs)),
                              key=lambda k: event_probs[k],
                              reverse=True)
            text = self.text_generator.get_text(pool5_heat_out.reshape([1,
                                                                        -1]))
            # print(prob_idx, text)
            return json.dumps({"indices": prob_idx, "text": text})
        except Exception as e:
            traceback.print_exc()
            event_indices = list(range(len(query_json["possible_events"])))
            random.shuffle(event_indices)
            return json.dumps({"indices": event_indices, "text": "Humanoid"})

    def render_view_tree(self, query_json_str):
        try:
            query_json = json.loads(query_json_str)
            self.data_processor.update_origin_dim(query_json["screen_res"])
            view_tree = query_json["view_tree"]
            image = self.data_processor.view_tree_to_image(view_tree)
            texts = self.data_processor.view_tree_texts(view_tree)
            return json.dumps({
                "image": image.astype(int).flatten().tolist(),
                "texts": texts
            })
        except Exception as e:
            traceback.print_exc()
            return ""

    def render_content_free_view_tree(self, query_json_str):
        try:
            query_json = json.loads(query_json_str)
            self.data_processor.update_origin_dim(query_json["screen_res"])
            view_tree = query_json["view_tree"]
            image = self.data_processor.view_tree_to_image(view_tree)
            return json.dumps({"image": image.astype(int).flatten().tolist()})
        except Exception as e:
            traceback.print_exc()
            return ""

    def run(self):
        self.server.serve_forever()
Ejemplo n.º 45
0
def setup_server():
    args = get_args().parse_args()
    server = SimpleXMLRPCServer(("localhost", args.port))
    print(f"Listening on port {args.port}...")
    server.register_function(main_train, "main_train")
    server.serve_forever()
Ejemplo n.º 46
0
def run_xmlrpc():
    song_handler = SongHandler()
    server = SimpleXMLRPCServer(('localhost', 7777), allow_none=True)
    server.register_instance(song_handler)
    server.serve_forever()
Ejemplo n.º 47
0
class RPC:
    _metodo_rpc = [
        'signin', 'login', 'post', 'edit', 'delete', 'listTile', 'searchUser'
    ]

    def __init__(self, direccion):

        self._servidor = SimpleXMLRPCServer(direccion, allow_none=True)

        for metodo in self._metodo_rpc:
            self._servidor.register_function(getattr(self, metodo))

    def signin(self, data):
        a = self.searchUser(data)
        if (a == None):
            self.insertUser(data)
            return True
        else:
            return False

    def login(self, data):
        datos = data.split(',')
        rta = self.searchUser(data).split(',')
        if datos[0] == rta[0] and datos[1] == rta[1]:  # usuario  y clave
            return True
        else:
            return False

    def post(self, data, usuario):
        with open("post.txt", "r") as file:
            for last_line in file:
                pass
        index = int(last_line.split(',')[0]) + 1
        data['ID'] = index
        file.close()
        f = open('post.txt', 'a')
        f.write('\n' + str(data['ID']) + ',' + data['Titular'] + ',' +
                data['fechacreacion'] + ',' + data['fechaactualizacion'] +
                ',' + data['Autor'] + ',' + data['Contenido'])
        f.close()

    def edit(self, datas, user):
        index = datas['ID']
        with open('post.txt', 'r') as f:
            data = f.readlines()
            if len(data) == 0:
                return False
            elif len(data) == 1:
                dataIndex = data[0].split(',')[0]
                dataUser = data[0].split(',')[1]
                if (int(dataIndex) == index
                        and dataUser == user) or (int(dataIndex) == index and
                                                  user == 'admin'):  # corregir
                    data[0] = data[0].replace(
                        data[0].split(',')[1],
                        datas['Titular'])  # remplazo titulo
                    data[0] = data[0].replace(
                        data[0].split(',')[5],
                        str(datas['Contenido']))  # remplazo contenido
                    with open('post.txt', 'w') as file:
                        file.writelines(data)
                        return True
            elif len(data) > 1:
                for i in range(len(data)):  # recorro todas las lineas
                    dataIndex = data[i].split(',')[0]
                    dataUser = data[i].split(',')[4]
                    if (int(dataIndex) == index
                            and dataUser == user) or (int(dataIndex) == index
                                                      and user == 'admin'):
                        data[i] = data[i].replace(
                            data[i].split(',')[1],
                            datas['Titular'])  # remplazo titulo
                        data[i] = data[i].replace(
                            data[i].split(',')[5],
                            str(datas['Contenido']))  # remplazo contenido
                        with open('post.txt', 'w') as file:
                            file.writelines(data)
                            return True
        return False

    def delete(self, index, user, op):
        with open("post.txt", "r+") as f:
            lines = f.readlines()
            if lines[index - 1].split(',')[4] == user or op == 'True':
                del lines[index - 1]
                f.seek(0)
                f.truncate()
                f.writelines(lines)
                return True
            else:
                return False

    def listTile(self):
        with open('post.txt', 'r') as f:
            ans = f.readlines()
            return ans

    def searchUser(self, data):
        with open('usuarios.txt', 'r') as f:
            for line in f:
                if data in line:
                    return line

    def insertUser(self, data):
        f = open('usuarios.txt', 'a')
        f.write('\n' + data + ',False')
        return True

    def iniciar_servidor(self):
        self._servidor.serve_forever()
Ejemplo n.º 48
0
def serverHandler(myHost, myPort, usrHost, usrPort):
    """! Configura e inicia o servidor RPC do usuário.
    
    @param newHost Host que o usuário utilizará.
    """

    global host, port, server, allUsers

    host = myHost
    port = myPort

    # Tenta criar o socket do server
    try:
        server = SimpleXMLRPCServer((host, port),
                                    allow_none=True,
                                    logRequests=False)
    except OSError:
        print(f"Outro usuario ja existe no endereco {host}:{port}")
        exit(0)

    # Tenta fazer contato com o outro usuario passado, se ele foi passado
    if (usrHost != None) and (usrPort != None):
        try:
            with xmlrpc.client.ServerProxy(f"http://{usrHost}:{usrPort}/",
                                           allow_none=True) as proxy:
                allUsers = proxy.rpc_connToUser(host, port)
        except:
            print(
                f"Nao foi encontrado o usuario p/ se conctar no endereco {usrHost}:{usrPort}"
            )
            exit(0)

        # Verifica se usuario foi conectado
        if allUsers == None:
            print(
                f"Numero maximo de usuarios alcancado na rede do usuario {usrHost}:{usrPort}"
            )
            exit(0)

    # Caso n for passado outro usuario, ele se adiciona nem allUsers
    else:
        rpc_newUser(host, port)

    # Verifica se ha outros usuarios online no mesmo host
    # while(port < 8004):
    #     try:
    #         if server == None:
    #             server = SimpleXMLRPCServer((host, port), allow_none=True, logRequests=False)
    #             openPort = port
    #     except OSError:
    #         allUsers.append( {"host": host, "port": port} )
    #     finally:
    #         port += 1

    # port = openPort

    # avisando outros usuarios que um novo host conectou
    # if len(allUsers) > 0:
    #     if server != None:
    #         for user in allUsers:
    #             with xmlrpc.client.ServerProxy(f"http://{user['host']}:{user['port']}/", allow_none=True) as proxy:
    #                 proxy.rpc_newUser(host, port)

    #     else:
    #         print("Numero maximo de usuarios ja alcançado!")
    #         exit(0)

    # usuario se adiciona tmb
    # allUsers.append( {"host": host, "port": port} )

    server.register_function(rpc_newUser, "rpc_newUser")
    server.register_function(rpc_getFilesList, "rpc_getFilesList")
    server.register_function(rpc_getFilesTotalSize, "rpc_getFilesTotalSize")
    server.register_function(rpc_receiveNewFile, "rpc_receiveNewFile")
    server.register_function(rpc_removeUser, "rpc_removeUser")
    server.register_function(rpc_getCopiesList, "rpc_getCopiesList")
    server.register_function(rpc_fromCopyToFile, "rpc_fromCopyToFile")
    server.register_function(rpc_renameCopy, "rpc_renameCopy")
    server.register_function(rpc_renameFile, "rpc_renameFile")
    server.register_function(rpc_removeCopy, "rpc_removeCopy")
    server.register_function(rpc_removeFile, "rpc_removeFile")
    server.register_function(rpc_connToUser, "rpc_connToUser")

    server.serve_forever()
Ejemplo n.º 49
0
                        str(idOrga)[1:len(idOrga) - 3],
                        str(idUsager)[1:len(idUsager) - 3]
                    ]

                else:
                    print("Echec de l'authentification")
                    return 0

            else:
                print("Echec de l'authentification")
                return 0

        else:
            print("Echec de l'authentification")
            return 0

    def rechercheProjetsDispo(self, id):
        t = ('' + str(id) + '', )
        tabProjet = []
        for projet in self.curseur.execute(
                'SELECT nom FROM Projets WHERE id_Organisation =?', t):
            tabProjet.append(str(projet)[2:len(projet) - 4])
        return tabProjet


daemon = SimpleXMLRPCServer((socket.gethostbyname(socket.gethostname()), 9998),
                            allow_none=1)
objetControleurServeurBD = ControleurServeurBD()
daemon.register_instance(objetControleurServeurBD)
daemon.serve_forever()
Ejemplo n.º 50
0
class DroidMaster(object):
    """
    The main class of droidmaster
    DroidMaster currently supports QEMU instance pool only
    """
    # this is a single instance class
    instance = None
    POLL_INTERVAL = 1

    def __init__(self,
                 app_path=None,
                 is_emulator=False,
                 output_dir=None,
                 env_policy=None,
                 policy_name=None,
                 random_input=False,
                 script_path=None,
                 event_count=None,
                 event_interval=None,
                 timeout=None,
                 keep_app=None,
                 keep_env=False,
                 cv_mode=False,
                 debug_mode=False,
                 profiling_method=None,
                 grant_perm=False,
                 enable_accessibility_hard=False,
                 qemu_hda=None,
                 qemu_no_graphic=False,
                 humanoid=None,
                 ignore_ad=False,
                 replay_output=None):
        """
        initiate droidmaster, and
        initiate droidbot's with configurations
        :return:
        """
        logging.basicConfig(
            level=logging.DEBUG if debug_mode else logging.INFO,
            format=GV.LOG_FORMAT)
        self.logger = logging.getLogger('DroidMaster')
        DroidMaster.instance = self

        # 1. Save DroidBot Parameters
        self.app_path = app_path
        self.is_emulator = is_emulator

        self.output_dir = output_dir
        if not os.path.isdir(output_dir):
            os.makedirs(output_dir)

        self.env_policy = env_policy
        self.policy_name = policy_name
        self.random_input = random_input
        self.script_path = script_path
        self.event_count = event_count
        self.event_interval = event_interval
        self.timeout = timeout
        self.keep_app = keep_app
        self.keep_env = keep_env
        self.cv_mode = cv_mode
        self.debug_mode = debug_mode
        self.profiling_method = profiling_method
        self.grant_perm = grant_perm
        self.enable_accessibility_hard = enable_accessibility_hard
        self.humanoid = humanoid
        self.ignore_ad = ignore_ad
        self.replay_output = replay_output

        # 2. Initiate Device Pool
        self.domain = "localhost"
        self.rpc_port = Device(device_serial="").get_random_port()

        self.qemu_hda = qemu_hda
        self.qemu_no_graphic = qemu_no_graphic

        self.device_pool_capacity = 6
        self.device_pool = {}
        self.device_unique_id = 0

        self.app = App(app_path, output_dir=self.output_dir)
        self.qemu_app_hda = "%s_%s" % (self.qemu_hda,
                                       self.app.get_package_name())

        for i in range(self.device_pool_capacity):
            adb_port = Device(device_serial="").get_random_port()
            device_serial = "%s:%s" % (self.domain, adb_port)
            qemu_port = Device(device_serial="").get_random_port()
            device = Device(
                device_serial=device_serial,
                is_emulator=self.is_emulator,
                output_dir=self.output_dir,
                cv_mode=self.cv_mode,
                grant_perm=self.grant_perm,
                enable_accessibility_hard=self.enable_accessibility_hard)

            self.device_pool[device_serial] = {
                "domain": self.domain,
                "adb_port": adb_port,
                "qemu_port": qemu_port,
                # droidbot is indexed by device_serial
                # qemu is indexed by droidbot
                "droidbot": None,
                "qemu": None,
                "id": None,
                "device": device
            }
        self.logger.info(self.device_pool)

        # 2. This Server's Parameter
        self.timer = None
        self.enabled = True
        self.successful_spawn_events = set()

    @staticmethod
    def get_instance():
        if DroidMaster.instance is None:
            print("Error: DroidMaster is not initiated!")
            sys.exit(-1)
        return DroidMaster.instance

    def get_available_devices(self):
        return sorted([self.device_pool[x]
                       for x in self.device_pool
                       if self.device_pool[x]["droidbot"] is None and \
                          self.device_pool[x]["qemu"] is None], key=lambda x: x["adb_port"])

    def get_running_devices(self):
        return sorted([self.device_pool[x]
                       for x in self.device_pool
                       if self.device_pool[x]["droidbot"] is not None and \
                          self.device_pool[x]["qemu"] is not None], key=lambda x: x["adb_port"])

    def start_device(self,
                     device,
                     hda,
                     from_snapshot=False,
                     init_script_path=None):
        # 1. get device ID
        device["id"] = self.device_unique_id
        # 2. new QEMU adapter
        device["qemu"] = QEMUConn(hda, device["qemu_port"], device["adb_port"],
                                  self.qemu_no_graphic)
        device["qemu"].set_up()
        device["qemu"].connect(from_snapshot)
        # 3. new DroidWorker adapter
        script_path = init_script_path if init_script_path else self.script_path
        device["droidbot"] = DroidBotConn(
            device["id"],
            app_path=self.app_path,
            device_serial=device["device"].serial,
            is_emulator=self.is_emulator,
            output_dir=self.output_dir,
            env_policy=self.env_policy,
            policy_name=self.policy_name,
            random_input=self.random_input,
            script_path=script_path,
            event_count=self.event_count,
            event_interval=self.event_interval,
            timeout=self.timeout,
            keep_app=self.keep_app,
            keep_env=self.keep_env,
            cv_mode=self.cv_mode,
            debug_mode=self.debug_mode,
            profiling_method=self.profiling_method,
            grant_perm=self.grant_perm,
            enable_accessibility_hard=self.enable_accessibility_hard,
            master="http://%s:%d/" % (self.domain, self.rpc_port),
            humanoid=self.humanoid,
            ignore_ad=self.ignore_ad,
            replay_output=self.replay_output)
        device["droidbot"].set_up()
        self.logger.info("Worker: DOMAIN[%s], ADB[%s], QEMU[%d], ID[%d]" %
                         (device["domain"], device["adb_port"],
                          device["qemu_port"], device["id"]))
        self.device_unique_id += 1

    def stop_device(self, device):
        device["droidbot"].tear_down()
        device["droidbot"].disconnect()
        device["droidbot"] = None
        device["qemu"].disconnect()
        device["qemu"].tear_down()
        device["qemu"] = None

    def qemu_create_img(self, new_hda, back_hda):
        self.logger.info("%s -> %s" % (back_hda, new_hda))
        p = subprocess.Popen([
            "qemu-img", "create", "-f", "qcow2", new_hda, "-o",
            "backing_file=%s" % back_hda, "8G"
        ])
        p.wait()

    def spawn(self, device_serial, init_script_json):
        """
          A worker requests to spawn a new worker
          based on its current state
        """
        if init_script_json in self.successful_spawn_events:
            self.logger.warning("Event spawned already")
            return False

        available_devices = self.get_available_devices()
        if not len(available_devices):
            self.logger.warning("No available device slot")
            return False

        calling_device = self.device_pool[device_serial]
        calling_device["qemu"].send_command("stop")
        calling_device["qemu"].send_command("savevm spawn")

        # copy qemu image file (almost RAM image size only)
        new_hda = "%s.%d" % (self.qemu_app_hda, self.device_unique_id)
        shutil.copyfile(calling_device["qemu"].hda, new_hda)

        # prepare init script file
        init_script_path = os.path.join(self.output_dir,
                                        "%d.json" % self.device_unique_id)
        with open(init_script_path, "w") as init_script_file:
            init_script_file.write(init_script_json)

        self.start_device(available_devices[0],
                          new_hda,
                          from_snapshot=True,
                          init_script_path=init_script_path)

        calling_device["qemu"].send_command("delvm spawn")
        calling_device["qemu"].send_command("cont")

        self.successful_spawn_events.add(init_script_json)
        self.logger.info("Spawning worker")
        return True

    def start_worker(self):
        """
          Start the first worker (with device 0), used by DroidMaster itself
        """
        available_devices = self.get_available_devices()
        if not len(available_devices):
            self.logger.warning("No available device slot")
            return False

        device = available_devices[0]
        # if app image doesn't exist, create it first
        if not os.path.exists(self.qemu_app_hda):
            self.qemu_create_img(self.qemu_app_hda, self.qemu_hda)
            app_install_qemu = QEMUConn(self.qemu_app_hda, device["qemu_port"],
                                        device["adb_port"],
                                        self.qemu_no_graphic)
            app_install_qemu.set_up()
            app_install_qemu.connect()
            device["device"].wait_for_device()
            device["device"].install_app(self.app)
            app_install_qemu.disconnect()
            device["device"].shutdown()
            app_install_qemu.tear_down()

        new_hda = "%s.%d" % (self.qemu_app_hda, self.device_unique_id)
        self.qemu_create_img(new_hda, self.qemu_app_hda)

        self.start_device(available_devices[0], new_hda)
        return True

    def stop_worker(self, device_serial):
        self.stop_device(self.device_pool[device_serial])

    def start_daemon(self):
        self.server = SimpleXMLRPCServer((self.domain, self.rpc_port),
                                         RPCHandler)
        print("Listening on port %s..." % self.rpc_port)
        self.server.register_function(self.spawn, "spawn")
        self.server.register_function(self.start_worker, "start_worker")
        self.server.register_function(self.stop_worker, "stop_worker")
        self.server.serve_forever()

    def stop_daemon(self):
        print("Shutting down DroidMaster server...")
        self.server.shutdown()
        self.server_thread.join(0)

    def start(self):
        """
        start interacting
        :return:
        """
        if not self.enabled:
            return
        self.logger.info("Starting DroidMaster")
        try:
            if self.timeout > 0:
                self.timer = threading.Timer(self.timeout, self.stop)
                self.timer.start()

            if not self.enabled:
                return

            # enable server listening workers
            self.server_thread = threading.Thread(target=self.start_daemon)
            self.server_thread.daemon = True
            self.server_thread.start()
            time.sleep(1)  # wait server to start

            # create first droidbot instance
            proxy = ServerProxy("http://%s:%d/" % (self.domain, self.rpc_port))
            proxy.start_worker()

            while len(self.get_running_devices()):
                time.sleep(self.POLL_INTERVAL)

        except KeyboardInterrupt:
            self.logger.info("Keyboard interrupt.")
            pass
        except Exception:
            import traceback
            traceback.print_exc()
            self.stop()
            sys.exit(-1)

        self.stop()
        self.logger.info("DroidMaster Stopped")

    def stop(self):
        self.enabled = False
        if self.timer and self.timer.isAlive():
            self.timer.cancel()
        # stop listening server
        self.stop_daemon()
        # stop all workers
        running_devices = self.get_running_devices()
        for device in running_devices:
            self.stop_device(device)
Ejemplo n.º 51
0
class serverRPC:
    def __init__(self, ip="localhost", puerto=5500, tipo="main"):
        #Server Control
        self.server = SimpleXMLRPCServer((ip, puerto),
                                         requestHandler=RequestHandler,
                                         allow_none=True)
        self.server.register_introspection_functions()
        self.ip = ip
        self.puerto = str(puerto)
        self.tipo = tipo
        self.conecction = None
        self.servers = []
        self.dataClock = []
        #Tkinter
        self.root = tkinter.Tk()
        self.root.wm_title("timer-" + tipo)  #da el titulo a la ventana

        scrollbar = tkinter.Scrollbar(self.root, orient=tkinter.VERTICAL)
        scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)

        self.TextoBox = tkinter.Text(self.root,
                                     height=8,
                                     width=80,
                                     yscrollcommand=scrollbar.set)
        self.TextoBox.pack(side=tkinter.LEFT, fill=tkinter.Y)

        frame = tkinter.Frame(self.root)
        frame.pack()

        self.clock = tkinter.Label(self.root,
                                   font=('times', 20, 'bold'),
                                   bg='white')
        self.clock.pack(fill=tkinter.BOTH, expand=1)
        self.clockTime = Clock(self)
        self.clock.config(text=self.clockTime.getStrTime())

        tkinter.Label(frame, text='Send to Server').grid(row=0, column=0)
        self.command = tkinter.StringVar()
        tkinter.Entry(frame, textvariable=self.command).grid(row=0, column=1)
        tkinter.Label(frame, text='Estado').grid(row=1, column=0)
        self.answer = tkinter.StringVar()
        tkinter.Label(frame, textvariable=self.answer).grid(row=1, column=1)

        button = tkinter.Button(frame, text='Send', command=self.sendCom)
        button.grid(row=2, columnspan=2)

        button = tkinter.Button(frame, text='Sinc Web', command=self.sincClock)
        button.grid(row=3, columnspan=1)

        button = tkinter.Button(frame, text='Stop', command=self.stopClock)
        button.grid(row=3, columnspan=2)

        button = tkinter.Button(frame,
                                text='Sinc AllClock',
                                command=self.sincAll)
        button.grid(row=2, columnspan=1)

        self.buttonReg = None
        self.updateClock()

    def printBox(self, value):
        self.TextoBox.insert(tkinter.END, "\n" + str(value))
        self.TextoBox.see(tkinter.END)

    def sendCom(self):
        date, tiempo = self.command.get().split(" ")
        self.clockTime.setTime(date, tiempo)
        self.answer.set("processing...")
        #self.serverDato=value
        self.command.set("")
        #self.answer1.set("Ready")
        #self.opReq=True

    def updateClock(self):
        newTime = self.clockTime.getStrTime()
        if (newTime is not None):
            self.clock.config(text=newTime)
            # calls itself every 200 milliseconds
            # to update the time display as needed
        # could use >200 ms, but display gets jerky
        #if(self.clockTime.freeze):
        #	self.printBox("esta freeze")
        self.clock.after(200, self.updateClock)

    def runGraph(self):
        self.root.mainloop()

    def stopClock(self):
        self.clockTime.freezeTime()

    def getAddres(self):
        return (self.ip, self.puerto)

    # Server functions
    def getTime(self):
        return self.clockTime.getTime()

    def getAllAddres(self):
        return self.servers

    def sincClock(self):
        if (self.conecction is not None):
            if (not self.clockTime.sincTime):
                self.conecction.sinc((self.ip, self.puerto))
                value = self.conecction.getTime()
                self.clockTime.setTime(valueFloat=value)
                self.printBox("Sincronizando del servidor")
            else:
                self.printBox("ya hay un evento de sinc activo")
        else:
            self.printBox("Sincronizando en la web")
            c = ntplib.NTPClient()
            response = c.request('time4.google.com', version=3)
            value = response.tx_time
            self.clockTime.setTime(valueFloat=value)

    def setTime(self, value):
        self.clockTime.setTime(valueFloat=value)

    # Funciones del servidor para el cliente
    def register(self, ipServer, puertoServer):
        value = xmlrpc.client.ServerProxy("http://" + ipServer + ":" +
                                          puertoServer)
        self.servers.append(value)
        print("servidores", self.servers)
        self.printBox(
            "Se ha registrado el servidor {}".format("http://" + ipServer +
                                                     ":" + puertoServer))

    def sincAll(self):
        if (self.tipo != "main"):
            self.dataClock.append(self.conecction.getTime())
        else:
            self.dataClock.append(self.getTime())
        if (self.tipo != "main"):
            addres = self.conecction.getAllAddres()  #
            self.servers = []
            for i in addres:
                value = xmlrpc.client.ServerProxy("http://" +
                                                  i["_ServerProxy__host"],
                                                  allow_none=True)

                self.servers.append(value)
        else:
            self.getAddres()

        for i in self.servers:
            self.dataClock.append(i.getTime())
        lastSize = len(self.dataClock)
        lastnewSize = 0
        while lastSize != lastnewSize:
            lastSize = len(self.dataClock)
            if (len(self.dataClock) == 0):
                break
            timeMedia = self.desvEst()
            lastnewSize = len(self.dataClock)
        self.setTime(timeMedia + time.clock())
        timeValue = timeMedia + time.clock()
        tempTime = time.asctime(time.localtime(timeValue))

        self.printBox(
            "Cambiando a tiempo -> {}".format(tempTime[11:19] + "\n" +
                                              tempTime[0:4] + tempTime[8:10] +
                                              "/" + tempTime[4:6] + "/" +
                                              tempTime[20:24]))
        self.dataClock = []
        self.clock.after(5000, self.sincAll)

    def desvEst(self):
        self.printBox("Calcuando desviación")
        totalTime = 0
        if len(self.dataClock) == 1:
            return self.dataClock[0]
        for i in self.dataClock:
            totalTime += i
        media = totalTime / (len(self.dataClock))
        tempSum = 0
        for i in self.dataClock:
            tempSum += (i - media)**2
        desvEstandar = (1 / ((len(self.dataClock)) * tempSum))**(1 / 2)
        tempvalues = []
        for enum, i in enumerate(self.dataClock):
            if (i < media - desvEstandar * 2 or i > media + desvEstandar * 2):
                tempvalues.append(enum)
        for enum, i in enumerate(tempvalues):
            self.dataClock.pop(i - enum)
        return media

    def runServer(self):
        print("corriendo server de tipo {}".format(self.tipo))
        if self.tipo == "main":
            self.server.register_function(self.register, 'register')
            self.server.register_function(self.getTime, 'getTime')
            self.server.register_function(self.getAllAddres, 'getAllAddres')
        else:
            self.server.register_function(self.getTime, 'getTime')
            self.server.register_function(self.getAddres, 'getAddres')
            ipServer = str(input("Ingrese la ip del server principal"))
            puertoServer = str(input("ingrese el puerto del server principal"))
            self.conecction = xmlrpc.client.ServerProxy("http://" + ipServer +
                                                        ":" + puertoServer,
                                                        allow_none=True)
            self.conecction.register(self.ip, self.puerto)
        self.server.serve_forever()
Ejemplo n.º 52
0
class serverRPC:
    def __init__(self, ip="localhost", puerto=5500, tipo="main"):
        #Server Control
        self.server = SimpleXMLRPCServer((ip, puerto),
                                         requestHandler=RequestHandler,
                                         allow_none=True)
        self.server.register_introspection_functions()
        self.ip = ip
        self.puerto = puerto
        self.tipo = tipo
        self.servers = {}
        self.filesBusy = {}
        self.filesList = []

        #Tkinter
        self.root = tkinter.Tk()
        self.root.wm_title("archivos - " + tipo)
        scrollbar = tkinter.Scrollbar(self.root, orient=tkinter.VERTICAL)
        self.TextoBox = tkinter.Text(self.root,
                                     height=8,
                                     width=80,
                                     yscrollcommand=scrollbar.set)
        self.TextoBox2 = tkinter.Text(self.root,
                                      height=8,
                                      width=80,
                                      yscrollcommand=scrollbar.set)
        scrollbar.config(command=self.yview)
        scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)

        self.TextoBox.pack(side=tkinter.LEFT, fill=tkinter.Y)
        self.TextoBox.config(state=tkinter.DISABLED)
        self.TextoBox2.pack(side=tkinter.LEFT, fill=tkinter.Y)
        self.TextoBox2.config(state=tkinter.DISABLED)
        frame = tkinter.Frame(self.root)
        frame.pack()

        self.list = tkinter.Listbox(self.root,
                                    selectmode=tkinter.SINGLE,
                                    yscrollcommand=scrollbar.set)
        self.list.pack(fill=tkinter.BOTH, expand=1)

        self.buttonShowFile = tkinter.Button(frame,
                                             text='Show File',
                                             command=self.showInfo)
        self.buttonShowFile.grid(row=2, columnspan=1)

        self.buttonShowBusy = tkinter.Button(frame,
                                             text='Select Busy Files',
                                             command=self.showBusy)
        self.buttonShowBusy.grid(row=3, columnspan=1)

        self.buttonUpdate = tkinter.Button(frame,
                                           text='Update Files',
                                           command=self.updateFilesAll)
        self.buttonUpdate.grid(row=4, columnspan=1)

        self.buttonCancel = tkinter.Button(frame,
                                           text='Cancel',
                                           command=self.cancelAction)
        self.buttonCancel.grid(row=5, columnspan=1)

        self.buttonShowBusy.config(state=tkinter.DISABLED)

        tkinter.Label(frame, text='Estado').grid(row=1, column=0)
        self.answer = tkinter.StringVar()
        self.answer.set("Ready")
        tkinter.Label(frame, textvariable=self.answer).grid(row=1, column=1)
        #Temp value

    def yview(self, *args):
        self.TextoBox.yview(*args)
        self.TextoBox2.yview(*args)
        self.list.yview(*args)

    def printBox1(self, value):
        self.TextoBox.config(state=tkinter.NORMAL)
        self.TextoBox.insert(
            tkinter.END,
            "\n" + time.asctime(time.localtime(time.time())) + str(value))
        self.TextoBox.see(tkinter.END)
        self.TextoBox.config(state=tkinter.DISABLED)

    def printBox2(self, value):
        self.TextoBox2.config(state=tkinter.NORMAL)
        self.TextoBox2.delete('1.0', tkinter.END)
        self.TextoBox2.insert(tkinter.END, str(value))
        self.TextoBox2.see(tkinter.END)
        self.TextoBox2.config(state=tkinter.DISABLED)

    def showInfo(self):
        fileSelected = self.list.curselection()
        if (len(fileSelected) == 1):
            fileName = self.list.get(fileSelected[0])
            fileNumber = 0
            if (":" in fileName):
                fileName, fileNumber = fileName.split(":")
                fileNumber = int(fileNumber)
            serverContentFile = None
            fileState = None
            for enum, i in enumerate(self.filesBusy.get(fileName)):
                if (enum == fileNumber):
                    fileContent = self.servers[i].getFile(fileName)
                    serverContentFile = i
                    if (self.filesBusy.get(fileName).get(i) is None):
                        fileState = "No ocupado"
                    else:
                        fileState = "Ocupado"
            self.printBox1(
                "Archivo: {}\nGuardado en: {}\nEstado: {}\n#----------#\n".
                format(fileName, serverContentFile, fileState))
            self.printBox2(fileContent)

        elif (len(fileSelected) == 0):
            self.printBox1("No hay archivo seleccionado")
        else:
            self.printBox1("Hay mas de uno seleccionado")
        self.answer.set("Leyendo archivo")

    def showBusy(self):
        self.answer.set("Obteniendo Busy")
        tempIndex = 0
        self.list.selection_clear(0, tkinter.END)
        for i in self.filesBusy:
            for j in self.filesBusy.get(i):
                if (self.filesBusy.get(i).get(j) is not None):
                    self.list.selection_set(tempIndex, tempIndex)
                tempIndex += 1
        self.printBox1("Mostrando ocupados")
        self.answer.set("Ready")

    def runGraph(self):
        self.root.mainloop()

    def register(self, ipServer, puertoServer):
        #value=xmlrpc.client.ServerProxy("http://"+ipServer+":"+puertoServer)
        self.answer.set("Registrando cliente")
        self.servers["http://" + ipServer + ":" +
                     puertoServer] = xmlrpc.client.ServerProxy(
                         "http://" + ipServer + ":" + puertoServer,
                         allow_none=True)
        self.updateFilesAll()
        self.printBox1(
            "Se ha registrado el servidor {}".format("http://" + ipServer +
                                                     ":" + puertoServer))
        self.answer.set("Ready")

    def updateBusy(self):
        self.answer.set("actualizando Busy")
        for i in self.servers:  #lista de direcciones de servidores
            for j in self.servers.get(
                    i).getFiles():  #Obtiene los archivos por cada servidor
                if not j in self.filesBusy:  #verificando si el servidor NO posee dicho archivo
                    self.filesBusy[j] = {}
                    self.filesBusy[j][i] = None
                elif not i in self.filesBusy.get(j):
                    self.filesBusy[j][i] = None

    def updateFilesList(self):
        self.answer.set("actualizando lista")
        listTemp = []
        for i in self.filesBusy:
            for enum, j in enumerate(self.filesBusy.get(i)):
                if (enum == 0):
                    listTemp.append(i)
                else:
                    listTemp.append(i + ":" + str(enum))
        self.filesList = listTemp

    def updateFilesAll(self):
        self.answer.set("Actualizando archivos")

        self.updateBusy()
        self.updateFilesList()
        self.updateListBox()
        self.answer.set("Ready")
        self.buttonUpdate.after(1000, self.updateFilesListData)

    def updateFilesListData(self):
        for i in self.servers:
            self.servers.get(i).printBox1(
                "Hay actualización disponible de archivos")
        self.printBox1("Actualizado archivos")

    def updateListBox(self):
        listaTemp = self.getFilesList()
        self.list.delete(0, tkinter.END)  #Borra TODO
        for i in listaTemp:
            self.list.insert(tkinter.END, i)

    def getFilesList(self):
        return self.filesList

    def setBusy(self, machine, fileName):
        fileNumber = 0
        if (":" in fileName):
            fileName, fileNumber = fileName.split(":")
            fileNumber = int(fileNumber)
        for enum, i in enumerate(self.filesBusy.get(fileName)):
            if (enum == fileNumber):
                if self.filesBusy.get(fileName).get(i) is None:
                    self.filesBusy[fileName][i] = machine
                    self.printBox1(
                        "Ahora está ocupado el archivo {} guardado en {} por {}"
                        .format(fileName, i, machine))
                    return True
                else:
                    break

        return False

    def removeBusy(self, fileName):
        fileNumber = 0
        if (":" in fileName):
            fileName, fileNumber = fileName.split(":")
            fileNumber = int(fileNumber)
        for enum, i in enumerate(self.filesBusy.get(fileName)):
            if (enum == fileNumber):
                if self.filesBusy.get(fileName).get(i) is not None:
                    self.filesBusy[fileName][i] = None
                    self.printBox1(
                        "Ya no está ocupado el archivo {} guardado en {}".
                        format(fileName, i))
                    return True
                else:
                    break

        return False

    def modifyFile(self, fileName, fileData):
        fileNumber = 0
        if (":" in fileName):
            fileName, fileNumber = fileName.split(":")
            fileNumber = int(fileNumber)
        for enum, i in enumerate(self.filesBusy.get(fileName)):
            if (enum == fileNumber):
                serverFile = i
                self.filesBusy[fileName][i] = None

        valueVersion = 0
        extension = False
        temp = ""
        versiones = []
        for enum, i in enumerate(reversed(fileName)):
            if i.isdigit():
                temp = i + temp
            elif (not extension):
                if (i == "."):
                    extension = True
                elif (i == "v" and len(temp) > 0):
                    pos = enum
                    break
                elif (len(temp) > 0):
                    temp = ""
                    pos = enum
                    break
            else:
                if (i == "v"):
                    pos = enum
                    break
                else:
                    temp = ""
                    pos = enum
                    break
        print("temp ->", temp)
        if (len(temp) > 0):
            if (fileName[-(pos + 2)] == " "):
                fileName = fileName[:-(pos + 2)]
                print("fileName ->", fileName)
                print("pos ->", pos)
                valueVersion = int(temp)
        else:
            fileName = fileName[:-(pos)]

        for i in self.filesBusy:
            if (serverFile in self.filesBusy.get(i) and fileName in i):
                if (fileName in i):
                    temp = ""
                    pos = 0
                    extension = False
                    #print("esta en value")
                    for enum, j in enumerate(reversed(i)):
                        if j.isdigit():
                            temp = j + temp
                        elif (not extension):
                            if (j == "."):
                                extension = True
                            elif (j == "v" and len(temp) > 0):
                                pos = enum
                                break
                            elif (len(temp) > 0):
                                temp = ""
                                pos = enum
                                break
                        else:
                            if (j == "v"):
                                pos = enum
                                break
                            else:
                                temp = ""
                                pos = enum
                                break

                if (len(temp) > 0):
                    if (i[-(pos + 2)] == " "):
                        if (fileName == i[:-(pos + 2)]):
                            versiones.append(int(temp))
        while (True):
            valueVersion += 1
            if not valueVersion in versiones:
                fileName += " v" + str(valueVersion) + ".txt"
                if (not fileName in self.filesBusy):
                    self.filesBusy[fileName] = {}
                    self.filesBusy[fileName][serverFile] = None
                else:
                    self.filesBusy[fileName][serverFile] = None
                break

        self.servers.get(serverFile).modifyFile(fileName, fileData)
        self.printBox1("modificado el archivo {} guardado en {}".format(
            fileName, serverFile))
        self.updateFilesAll()

    def getFile(self, fileName):
        fileNumber = 0
        serverFile = None
        if (":" in fileName):
            fileName, fileNumber = fileName.split(":")
            fileNumber = int(fileNumber)
        for enum, i in enumerate(self.filesBusy.get(fileName)):
            if (enum == fileNumber):
                serverFile = i
        self.printBox1("Solicitado el archivo {} guardado en {}".format(
            fileName, serverFile))
        return self.servers.get(serverFile).getFile(fileName)

    def cancelAction(self):
        # self.reading=False
        self.printBox2("")
        self.printBox1("acción cancelada")
        self.buttonShowFile.config(state=tkinter.NORMAL)
        self.buttonShowBusy.config(state=tkinter.NORMAL)
        self.buttonCancel.config(state=tkinter.DISABLED)
        self.answer.set("Ready")

    def runServer(self):
        print("corriendo server de tipo {}".format(self.tipo))
        if self.tipo == "main":
            self.server.register_function(self.register, 'register')
            self.server.register_function(self.getFilesList, 'getFilesList')
            self.server.register_function(self.getFile, 'getFile')
            self.server.register_function(self.setBusy, 'setBusy')
            self.server.register_function(self.modifyFile, 'modifyFile')

        else:
            print("Hay errores")
            raise SystemExit(1)
        self.server.serve_forever()
Ejemplo n.º 53
0
from xmlrpc.server import SimpleXMLRPCServer


def func(s):
    '''
    服务器端的函数
    '''
    return s


if __name__ == '__main__':
    # 初始化一个服务器对象
    server = SimpleXMLRPCServer(('localhost', 4001))
    # 注册函数,字串参数指明远程调用的方法
    server.register_function(func, "echo")  # 远程调用使用echo方法
    print("Listening for Client")
    server.serve_forever()  # 保持服务
Ejemplo n.º 54
0
 def start_rcp_server(self, port):
     server = SimpleXMLRPCServer(('localhost', int(port)),
                                 logRequests=False,
                                 allow_none=True)
     server.register_instance(self)
     server.serve_forever()
Ejemplo n.º 55
0
                # 用远程调用递归查询
                res = c.help_find(targetid)
                if (dis > res[0]):
                    dis = res[0]
                    ip = res[1]
        # 若没有更近的最近的就是自己
        return ip

    def get_blocknamedir(self):
        l, r = self.get_mylrID()
        blocknamedir = {}
        id = l
        s = xmlrpc.client.ServerProxy("http://" + id + ":" + port + "/")
        blocknamedir['leftID'] = s.get_blocklist()
        id = r
        s = xmlrpc.client.ServerProxy("http://" + id + ":" + port + "/")
        blocknamedir['rightID'] = s.get_blocklist()
        id = self.id
        s = xmlrpc.client.ServerProxy("http://" + id + ":" + port + "/")
        blocknamedir['myID'] = s.get_blocklist()


if __name__ == "__main__":
    port = "1234"
    c = Client(DNSip)
    server = Server(id)
    s = SimpleXMLRPCServer(("0.0.0.0", int(port)))
    print("start service...")
    s.register_instance(server)
    s.serve_forever()
Ejemplo n.º 56
0
Archivo: ccu.py Proyecto: p0l0/pydevccu
class ServerThread(threading.Thread):
    """XML-RPC server thread to handle messages from CCU / Homegear"""
    def __init__(self,
                 addr=(const.IP_LOCALHOST_V4, const.PORT_RF),
                 devices=None,
                 persistance=False,
                 logic=False):
        LOG.debug("ServerThread.__init__")
        threading.Thread.__init__(self)
        self.addr = addr
        LOG.debug("__init__: Registering RPC methods")
        self._rpcfunctions = RPCFunctions(devices, persistance, logic)
        LOG.debug("ServerThread.__init__: Setting up server")
        self.server = SimpleXMLRPCServer(addr,
                                         requestHandler=RequestHandler,
                                         logRequests=False,
                                         allow_none=True)
        self.server.register_introspection_functions()
        self.server.register_multicall_functions()
        LOG.debug("ServerThread.__init__: Registering RPC functions")
        self.server.register_instance(self._rpcfunctions,
                                      allow_dotted_names=True)

    def run(self):
        LOG.info("Starting server at http://%s:%i", self.addr[0], self.addr[1])
        self._rpcfunctions.active = True
        self.server.serve_forever()

    def stop(self):
        """Shut down our XML-RPC server."""
        self._rpcfunctions.active = False
        for logic_device in self._rpcfunctions.logic_devices:
            logic_device.active = False
        self._rpcfunctions._saveParamsets()
        LOG.info("Shutting down server")
        self.server.shutdown()
        LOG.debug("ServerThread.stop: Stopping ServerThread")
        self.server.server_close()
        LOG.info("Server stopped")

    # Convenience methods at server scope
    def setValue(self, address, value_key, value, force=False):
        return self._rpcfunctions.setValue(address, value_key, value, force)

    def getValue(self, address, value_key):
        return self._rpcfunctions.getValue(address, value_key)

    def getDeviceDescription(self, address):
        return self._rpcfunctions.getDeviceDescription(address)

    def getParamsetDescription(self, address, paramset):
        return self._rpcfunctions.getParamsetDescription(address, paramset)

    def getParamset(self, address, paramset):
        return self._rpcfunctions.getParamset(address, paramset)

    def putParamset(self, address, paramset_key, paramset, force=False):
        return self._rpcfunctions.putParamset(address, paramset_key, paramset,
                                              force)

    def listDevices(self):
        return self._rpcfunctions.listDevices()

    def getServiceMessages(self):
        return self._rpcfunctions.getServiceMessages()

    def supportedDevices(self):
        return self._rpcfunctions.supported_devices

    def addDevices(self, devices=None):
        devices = self._rpcfunctions._loadDevices(devices=devices)
        for interface_id, proxy in self._rpcfunctions.remotes.items():
            LOG.debug("addDevices: Pushing new devices to %s", interface_id)
            proxy.newDevices(interface_id, devices)

    def removeDevices(self, devices=None):
        self._rpcfunctions._removeDevices(devices)
Ejemplo n.º 57
0
class KeyValueServer(object):
    _rpc_methods = ['get', 'set', 'delete', 'exists', 'keys', 'post_img']

    def __init__(self, address, user='******', db_dir=None):
        self._data = {}
        self._servre = SimpleXMLRPCServer(address, allow_none=True)
        for name in self._rpc_methods:
            self._servre.register_function(getattr(self, name))
        if db_dir is None:
            self.db_dir = './'
        else:
            self.db_dir = db_dir
        # --------
        self.user = user

    def get(self, name):
        return self._data[name]

    def set(self, name, value):
        self._data[name] = value

    def delete(self, name):
        del self._data[name]

    def exists(self, name):
        return name in self._data

    def keys(self):
        return list(self._data)

    def servre_forever(self):
        self._servre.serve_forever()

    def help(self):
        # todo 返回一个说明字典,这样能比较好地去使用其中的函数
        info_dict = {
            "get": None,
            "set": None,
            "delete": None,
            "exists": None,
            "keys": None,
            # --------------------
            "post_img": None,
            # --------------------
        }
        return info_dict

    # ------------------------------------------------------------------------------------------------------------------

    def post_img(self, img_data, img_title='untreated'):
        try:
            frame = pickle.loads(img_data.data)
            save_dir = os.path.join(self.db_dir, img_title)
            os.makedirs(save_dir, exist_ok=True)
            img_name = get_str_md5(img_data.data)
            save_path = os.path.join(save_dir, img_name + '.jpg')
            cv2.imencode('.jpg', frame)[1].tofile(save_path)
            return 200
        except Exception as e:
            print(e)
            print(e.__traceback__.tb_frame.f_globals["__file__"])
            print(e.__traceback__.tb_lineno)
            return 500

    # ------------------------------------------------------------------------------------------------------------------

    def get_todays_presentation(self):
        # 返回今天提交的所有内容
        pass

    def tack_board(self, message):
        # 布告板
        # 记录发布的人,时间
        # 能不能获取连接机器的 IP 地址
        pass