def start(self, mode): """ In CLIENT_MODE: Establish a connection with the server before sending a new message. In SERVER_MODE: Start listening to the incoming messages from the clients. :param mode: (int) Either CLIENT_MODE or SERVER_MODE. :return: None """ address = 'tcp://{}:{}'.format(self.ip, self.port) if mode == CLIENT_MODE: self.mode = mode self.logger.info( "Starting Scoreboard Client listening on {}:{} ...".format( self.ip, self.port)) self.instance = Proxy(address) elif mode == SERVER_MODE: self.mode = mode self.scoreboard = Scoreboard() server = Server(self, address) self.logger.info( "Starting Scoreboard Server listening on {}:{} ...".format( self.ip, self.port)) server.serve_forever()
def test_server_resync(self): from pizco import Server app = QApplication([]) srv = RemotePizcoEmitterResync() pizco_srv = Server(srv,rep_endpoint="tcp://127.0.0.1:1211") from threading import Thread import numpy as np from guiqwt.plot import ImageWidget from guiqwt.builder import make import time im = ImageWidget() im.register_all_curve_tools() ts = TestSync() def test_thread(): ts.initialize_subobject(im) srv.std_signal.connect(ts.receiver_int) v_int = 0 while True: v_int += 1 srv.emission() time.sleep(0.5) if v_int==5: break im.close() t = Thread(None,test_thread) t.start() im.show() app.exec_() pizco_srv.stop() assert(ts._internal_value != 0)
def test_proxy_resync(self): from pizco import Server pxy = Server.serve_in_process(RemotePizcoEmitter,(),{},rep_endpoint="tcp://127.0.0.1:1111") app = QApplication([]) resync_proxy(pxy) from threading import Thread import numpy as np from guiqwt.plot import ImageWidget from guiqwt.builder import make import time im = ImageWidget() im.register_all_curve_tools() ts = TestSync() def test_thread(): ts.initialize_subobject(im) pxy.std_signal.connect(ts.receiver_int) #std_signal.connect(pg.setValue) #test_built_in.connect(pg.setRange) v_int = 0 while True: v_int += 1 #ts.update_curve() goes through a non generic signal pxy.emission() time.sleep(0.5) if v_int==5: break im.close() t = Thread(None,test_thread) t.start() im.show() app.exec_() pxy._proxy_stop_server() pxy._proxy_stop_me() assert(ts._internal_value != 0)
def test_server(self): s = Server(Example()) proxy = Proxy(s.rep_endpoint) self.assertEqual(s.served_object.simple_attribute, 12) self.assertEqual(proxy.simple_attribute, 12) proxy.simple_attribute = 24 self.assertEqual(s.served_object.simple_attribute, 24) self.assertEqual(proxy.simple_attribute, 24) self.assertRaises(AttributeError, getattr, proxy, 'not_an_attribute') self.assertEqual(s.served_object.dict_attribute[1], 2) self.assertEqual(proxy.dict_attribute[1], 2) self.assertRaises(KeyError, operator.getitem, proxy.dict_attribute, 2) proxy.dict_attribute[2] = 4 self.assertEqual(s.served_object.dict_attribute[2], 4) self.assertEqual(proxy.dict_attribute[2], 4) self.assertEqual(s.served_object.rw_prop, 42) self.assertEqual(proxy.rw_prop, 42) proxy.rw_prop = 21 self.assertEqual(s.served_object.rw_prop, 21) self.assertEqual(proxy.rw_prop, 21) self.assertEqual(proxy.fun_simple(), 46) self.assertEqual(proxy.fun_arg1(2), 4) self.assertEqual(proxy.fun_arg2(2, 3), 8) self.assertEqual(proxy.fun_arg2(y=2), 4) self.assertRaises(ValueError, proxy.fun_raise) proxy._proxy_stop_server() proxy._proxy_stop_me()
def test_future(self): s = Server(Example()) proxy = Proxy(s.rep_endpoint) fut = proxy.fut() self.assertEqual(fut.result(), 10) fut = proxy.fut_raise() self.assertIsInstance(fut.exception(), ValueError) fut = proxy.fut_raise() self.assertRaises(ValueError, fut.result) proxy._proxy_stop_me() s.stop()
def test_agent_serve_in_process(self): address = 'tcp://127.0.0.1:9874' proxy = Server.serve_in_process(Example, (), {}, address) time.sleep(SLEEP_SECS * 10) self.assertEqual(proxy.simple_attribute, 12) proxy._proxy_stop_server() proxy._proxy_stop_me()
import time import argparse from pizco import Server from common import House parser = argparse.ArgumentParser('Start house server') parser.add_argument('-v', '--verbose', action='store_true', help='Print logging output') parser.add_argument('-g', '--gui', action='store_true', help='Show graphical user interface') args = parser.parse_args() proxy = Server.serve_in_process(House, (), {}, 'tcp://127.0.0.1:8000', verbose=args.verbose, gui=args.gui) time.sleep(1) proxy.door_open = True proxy.lights_on = True time.sleep(.1) proxy.paint('green') proxy.lights_on = False proxy.door_open = False for step in range(3, 0, -1): time.sleep(1) print('Stopping in {}'.format(step)) proxy._proxy_stop_server()
# -*- coding: utf-8 -*- from pizco import Server from common import House s = Server.serve_in_process( House, args=(), kwargs={}, rep_endpoint='tcp://127.0.0.1:8000', gui=True, verbose=True)
# -*- coding: utf-8 -*- from pizco import Server from common import House s = Server(House(), 'tcp://127.0.0.1:8000') s.serve_forever()