def testClientServer(self): """ Do some client server tests. """ import time def callback(text): callback.count += 1 callback.last_result = text def testfunc(): pass callback.count = 0 cl = ClientConnector("localhost", test_port) cl.start() self.assertTrue(cl.is_connected(), "Connection failed.") testService = cl.get_service_interface("test") result = testService.test(text="Spam") self.assertEqual(result, "Spam", "Expecting same message back.") result = testService.test("Spam") self.assertEqual(result, "Spam", "Expecting same message back.") with self.assertRaises(TypeError): result = testService.test(testfunc) testService.on_trigger += callback testService.trigger(text="Eggs") self.assertEqual(callback.count, 1, "No callback received") self.assertEqual(callback.last_result, "Callback: Eggs", "Received incorrect result text.") testService.trigger2("Ham") self.assertEqual(callback.count, 2, "No callback received") self.assertEqual(callback.last_result, "Callback2: Ham", "Received incorrect result text.") from quartjes.connector.protocol import default_timeout with self.assertRaises(TimeoutError): testService.test_timeout(default_timeout + 10) time.sleep(10) cl.stop() time.sleep(10) cl.start() result = testService.test(text="Spam") self.assertEqual(result, "Spam", "Expecting same message back.") cl.stop()
class CocosGui(object): """ Main object containing the Cocos2D GUI. Initialize and start this object to start a show. """ def __init__(self, hostname=None, port=1234, width=1024, height=768, fullscreen=True): """ Set up all parameters and objects for the GUI. """ self._width = width self._height = height self._fullscreen = fullscreen self._hostname = hostname self._port = port self._refresh_ticker_on_update = True self._ticker_layer = None self._center_display_controller = None self._title_layer = None self._mix_layer = None self._connector = None self._drinks = None def start(self): """ Start running the Cocos GUI. This method will block until the GUI has been shut down. After returning there might still be some processes cleaning up. """ print("Starting Cocos GUI") print("\nUsing following settings:") print("\tConnection: %s:%d" % (self._hostname, self._port)) print("\tGraphics: (%d, %d) fullscreen=%s" %(self._width, self._height, self._fullscreen)) print() print("Loaded libraries:") print("\tPyglet:\t%s" % pyglet.version) print("\tCocos:\t%s" % cocos.version) import twisted print("\tTwisted:%s" % twisted.version.short()) print() print("Opening connection to server...") self._connector = ClientConnector(self._hostname, self._port) self._connector.start() self._drinks = self._connector.database.get_drinks() random.shuffle(self._drinks) self._connector.database.on_drinks_updated += self._update_drinks self._connector.stock_exchange.on_next_round += self._next_round print() print("Initializing graphics...") cocos.director.director.init(width=self._width, height=self._height, fullscreen=self._fullscreen) cocos.director.director.set_show_FPS(debug_mode) self.show_gl_info() print() print("Starting the show!") self.show_ticker_scene() print() print("Shut down in progress...") print() self._center_display_controller.stop() self._connector.stop() @staticmethod def show_gl_info(): """ Display some information about the GL renderer. """ from pyglet.gl.gl_info import GLInfo info = GLInfo() info.set_active_context() print("GL Info:") print("\tVersion:\t%s" % info.get_version()) print("\tRenderer:\t%s" % info.get_renderer()) print("\tVendor:\t\t%s" % info.get_vendor()) print("\tExtensions:\t%s" % info.get_extensions()) from pyglet.gl.glu_info import GLUInfo info = GLUInfo() info.set_active_context() print() print("GLU Info:") print("\tVersion:\t%s" % info.get_version()) print("\tExtensions:\t%s" % info.get_extensions()) def show_ticker_scene(self, new_ticker=False): """ Replace everything on screen with the scene displaying the drink ticker. If the engine is not yet running, this method will start a new engine and will block until the engine has stopped. """ # Instantiate objects if not self._ticker_layer or new_ticker: self._ticker_layer = BottomTicker(screen_width=self._width) if not self._center_display_controller: self._center_display_controller = CenterDisplayController() if not self._title_layer: self._title_layer = TitleLayer() # Glue everything together self._ticker_layer.update_drinks(self._drinks) self._ticker_layer.on_focused_drink_changed += self._center_display_controller.drink_focused_changed # Show the scene scene = cocos.scene.Scene(self._ticker_layer, self._center_display_controller.get_layer(), self._title_layer) self._display_scene(scene) @staticmethod def _display_scene(scene): """ Change the display to show the given scene. If no engine is running yet, an engine will be started. In that case this method will not return until the engine has stopped again. """ if cocos.director.director.scene is None: cocos.director.director.run(scene) else: cocos.director.director.replace(cocos.scenes.transitions.FadeTransition(scene)) def _update_drinks(self, drinks): random.shuffle(drinks) self._drinks = drinks if self._ticker_layer: self._ticker_layer.update_drinks(drinks) if self._center_display_controller: self._center_display_controller.update_drinks(drinks) def _next_round(self): pass
class Sales_client(Frame): f1 = ("Arial", 12, "bold") def __init__(self, root): root.title('Sales client') Frame.__init__(self, root) self.pack(fill=BOTH, expand=1) self.createWidgets() self.conn = ClientConnector() def connect_to_server(self): if self.conn.is_connected(): self.conn.stop() self.b_connect_to_server.config(text="Not connected to server", bg="#ff0000", activebackground="#ff5555") self.ed.destroy() self.sd.destroy() else: try: self.conn.host = str(self.e_server_hostname.get()) self.conn.port = int(self.e_server_port.get()) except: import traceback traceback.print_exc() pass self.conn.start() self.b_connect_to_server.config(text="Connected", bg="#00ff00", activebackground="#55ff55") self.ed = edit_db_dialog(self) self.sd = sell_dialog(self) self.sd.pack(side=BOTTOM, fill=BOTH, expand=1) def edit_db(self): if self.conn.is_connected(): self.sd.forget() self.ed.pack(side=BOTTOM, fill=BOTH, expand=1) else: tkMessageBox.showwarning("Not connected to server", "Please connected to a server first.") def sell(self): if self.conn.is_connected(): self.ed.forget() self.sd.pack(side=BOTTOM, fill=BOTH, expand=1) else: tkMessageBox.showwarning("Not connected to server", "Please connected to a server first.") def createWidgets(self): # frame 1 self.frame1 = Frame(self) Label(self.frame1, text="Server hostname/IP:", font=self.f1).pack(side=LEFT, fill=X, expand=1) self.e_server_hostname = Entry(self.frame1, font=self.f1) self.e_server_hostname.pack(side=LEFT, fill=X, expand=1) Label(self.frame1, text="Port:", font=self.f1).pack(side=LEFT, fill=X, expand=1) self.e_server_port = Entry(self.frame1, font=self.f1) self.e_server_port.pack(side=LEFT, fill=X, expand=1) self.b_connect_to_server = Button(self.frame1, text="Connect to server", bg="#ff0000", activebackground="#ff5555", command=self.connect_to_server, font=self.f1) self.b_connect_to_server.pack(side=LEFT, fill=X, expand=1, padx=10) self.b_edit_dialog = Button(self.frame1, text="Edit database", command=self.edit_db, font=self.f1) self.b_sell_dialog = Button(self.frame1, text="Sell dialog", command=self.sell, font=self.f1) self.frame1.pack(fill=X) self.b_edit_dialog.pack(side=LEFT, fill=X, expand=1, padx=10) self.b_sell_dialog.pack(side=LEFT, fill=X, expand=1, padx=10)
class CocosGui(object): """ Main object containing the Cocos2D GUI. Initialize and start this object to start a show. """ def __init__(self, hostname=None, port=1234, width=1024, height=768, fullscreen=True): """ Set up all parameters and objects for the GUI. """ self._width = width self._height = height self._fullscreen = fullscreen self._hostname = hostname self._port = port self._refresh_ticker_on_update = True self._ticker_layer = None self._center_display_controller = None self._title_layer = None self._mix_layer = None self._connector = None self._drinks = None def start(self): """ Start running the Cocos GUI. This method will block until the GUI has been shut down. After returning there might still be some processes cleaning up. """ print("Starting Cocos GUI") print("\nUsing following settings:") print("\tConnection: %s:%d" % (self._hostname, self._port)) print("\tGraphics: (%d, %d) fullscreen=%s" % (self._width, self._height, self._fullscreen)) print() print("Loaded libraries:") print("\tPyglet:\t%s" % pyglet.version) print("\tCocos:\t%s" % cocos.version) import twisted print("\tTwisted:%s" % twisted.version.short()) print() print("Opening connection to server...") self._connector = ClientConnector(self._hostname, self._port) self._connector.start() self._drinks = self._connector.database.get_drinks() random.shuffle(self._drinks) self._connector.database.on_drinks_updated += self._update_drinks self._connector.stock_exchange.on_next_round += self._next_round print() print("Initializing graphics...") cocos.director.director.init(width=self._width, height=self._height, fullscreen=self._fullscreen) cocos.director.director.set_show_FPS(debug_mode) self.show_gl_info() print() print("Starting the show!") self.show_ticker_scene() print() print("Shut down in progress...") print() self._center_display_controller.stop() self._connector.stop() @staticmethod def show_gl_info(): """ Display some information about the GL renderer. """ from pyglet.gl.gl_info import GLInfo info = GLInfo() info.set_active_context() print("GL Info:") print("\tVersion:\t%s" % info.get_version()) print("\tRenderer:\t%s" % info.get_renderer()) print("\tVendor:\t\t%s" % info.get_vendor()) print("\tExtensions:\t%s" % info.get_extensions()) from pyglet.gl.glu_info import GLUInfo info = GLUInfo() info.set_active_context() print() print("GLU Info:") print("\tVersion:\t%s" % info.get_version()) print("\tExtensions:\t%s" % info.get_extensions()) def show_ticker_scene(self, new_ticker=False): """ Replace everything on screen with the scene displaying the drink ticker. If the engine is not yet running, this method will start a new engine and will block until the engine has stopped. """ # Instantiate objects if not self._ticker_layer or new_ticker: self._ticker_layer = BottomTicker(screen_width=self._width) if not self._center_display_controller: self._center_display_controller = CenterDisplayController() if not self._title_layer: self._title_layer = TitleLayer() # Glue everything together self._ticker_layer.update_drinks(self._drinks) self._ticker_layer.on_focused_drink_changed += self._center_display_controller.drink_focused_changed # Show the scene scene = cocos.scene.Scene(self._ticker_layer, self._center_display_controller.get_layer(), self._title_layer) self._display_scene(scene) @staticmethod def _display_scene(scene): """ Change the display to show the given scene. If no engine is running yet, an engine will be started. In that case this method will not return until the engine has stopped again. """ if cocos.director.director.scene is None: cocos.director.director.run(scene) else: cocos.director.director.replace( cocos.scenes.transitions.FadeTransition(scene)) def _update_drinks(self, drinks): random.shuffle(drinks) self._drinks = drinks if self._ticker_layer: self._ticker_layer.update_drinks(drinks) if self._center_display_controller: self._center_display_controller.update_drinks(drinks) def _next_round(self): pass
class Sales_client(Frame): f1 = ("Arial", 12, "bold") def __init__(self, root): root.title('Sales client') Frame.__init__(self, root) self.pack(fill=BOTH,expand=1) self.createWidgets() self.conn = ClientConnector() def connect_to_server(self): if self.conn.is_connected(): self.conn.stop() self.b_connect_to_server.config(text = "Not connected to server", bg="#ff0000",activebackground="#ff5555") self.ed.destroy() self.sd.destroy() else: try: self.conn.host = str(self.e_server_hostname.get()) self.conn.port = int(self.e_server_port.get()) except: import traceback traceback.print_exc() pass self.conn.start() self.b_connect_to_server.config(text = "Connected", bg="#00ff00",activebackground="#55ff55") self.ed = edit_db_dialog(self) self.sd = sell_dialog(self) self.sd.pack(side=BOTTOM,fill=BOTH,expand=1) def edit_db(self): if self.conn.is_connected(): self.sd.forget() self.ed.pack(side=BOTTOM,fill=BOTH,expand=1) else: tkMessageBox.showwarning("Not connected to server","Please connected to a server first.") def sell(self): if self.conn.is_connected(): self.ed.forget() self.sd.pack(side=BOTTOM,fill=BOTH,expand=1) else: tkMessageBox.showwarning("Not connected to server","Please connected to a server first.") def createWidgets(self): # frame 1 self.frame1 = Frame(self) Label(self.frame1, text="Server hostname/IP:",font = self.f1).pack(side=LEFT,fill=X,expand=1) self.e_server_hostname = Entry(self.frame1,font = self.f1) self.e_server_hostname.pack(side=LEFT,fill=X,expand=1) Label(self.frame1, text="Port:",font = self.f1).pack(side=LEFT,fill=X,expand=1) self.e_server_port = Entry(self.frame1, font = self.f1) self.e_server_port.pack(side=LEFT,fill=X,expand=1) self.b_connect_to_server = Button(self.frame1, text = "Connect to server", bg="#ff0000", activebackground="#ff5555", command = self.connect_to_server, font = self.f1) self.b_connect_to_server.pack(side=LEFT,fill=X,expand=1,padx=10) self.b_edit_dialog = Button(self.frame1, text = "Edit database",command = self.edit_db, font = self.f1) self.b_sell_dialog = Button(self.frame1, text = "Sell dialog", command = self.sell, font = self.f1) self.frame1.pack(fill=X) self.b_edit_dialog.pack(side=LEFT,fill=X,expand=1,padx=10) self.b_sell_dialog.pack(side=LEFT,fill=X,expand=1,padx=10)