def __init__(self, host, port): srv = SimpleJSONRPCServer((host, port)) srv.register_introspection_functions() srv.register_function(self.ping) mc = MissionCache() srv.register_instance(mc) print "[GameServerService] Up and running!" srv.serve_forever()
def __init__(self, host, port, testing=False, injector=None): srv = SimpleJSONRPCServer((host, port)) srv.register_introspection_functions() srv.register_function(self.ping) mc = EventServiceMethods() srv.register_instance(mc) if testing and injector is not None: injector.inject(mc) print "[GameServerService] Up and running!" srv.serve_forever()
class WindowService(object): def __init__(self, hostname='localhost', port=8080): global methods self._methods = methods self.server = SimpleJSONRPCServer((hostname, port), logRequests=False) self.server.register_introspection_functions() for method in self._methods: self.server.register_function(eval('self.{}'.format(method))) self.processes = {} def run(self): try: self.server.serve_forever() except KeyboardInterrupt: pass finally: for process in self.processes.values(): process.join() @register def get_available_methods(self): ''' Return a dictionary mapping each available method name to the doc-string for method. ''' return dict([(method, eval('self.{}.__doc__'.format(method))) for method in self._methods]) @register def dump_args(self, args): logging.info('args={}'.format(args)) @register def get_available_video_modes(self, window_xid): ''' Return a list of dictionaries describing the available video modes. For example: {'device': path('/dev/v4l/by-id/usb-Vimicro_Vimicro_USB_Camera__Altair_-video-index0'), 'dimensions': (160, 120), 'fourcc': 'YV12', 'framerate': Fps(num=30, denom=1), 'height': 120, 'interlaced': False, 'name': 'video/x-raw-yuv', 'width': 160} ''' process = self.processes[window_xid] return pickle.dumps(process(command='get_available_video_modes', ack=True)['response']) @register def select_video_mode(self, window_xid): ''' Launch a GTK prompt to select a video mode. ''' process = self.processes[window_xid] return pickle.dumps(process(command='select_video_mode', ack=True)['response']) @register def select_video_caps(self, window_xid): ''' Launch a GTK prompt to select a video device and caps string. ''' process = self.processes[window_xid] return process(command='select_video_caps', ack=True)['response'] @register def terminate_process(self, window_xid): ''' Terminate the GStreamer process. ''' process = self.processes[window_xid] result = process(command='join', ack=True) process.terminate() del self.processes[window_xid] return True @register def stop_pipeline(self, window_xid): ''' Stop the GStreamer pipeline. ''' process = self.processes[window_xid] process(command='stop') @register def start_pipeline(self, window_xid): ''' Start the GStreamer pipeline. ''' process = self.processes[window_xid] process(command='start') @register def get_video_mode_map(self, window_xid): ''' Return a dictionary mapping video mode strings to video settings dictionaries. ''' process = self.processes[window_xid] result = process(command='get_video_mode_map', ack=True) return pickle.dumps(result['response']) @register def get_video_mode_enum(self, window_xid): process = self.processes[window_xid] result = process(command='get_video_mode_enum', ack=True) return pickle.dumps(result['response']) @register def get_video_source_configs(self, window_xid): process = self.processes[window_xid] result = process(command='get_video_source_configs', ack=True) return pickle.dumps(result['response']) @register def get_process_pid(self, window_xid): ''' Return the ID of the GStreamer process. ''' process = self.processes[window_xid] result = process(command='get_pid', ack=True) return result['response'] @register def get_pid(self): ''' Return the ID of this server process. ''' return os.getpid() @register def create_process(self, window_xid, force_aspect_ratio=True): ''' Create a new GStreamer process for the specified window xid. ''' process = WindowProcess(window_xid, force_aspect_ratio=force_aspect_ratio) process.start() self.processes[window_xid] = process return None @register def create_pipeline(self, window_xid, video_settings, output_path=None, bitrate=None, draw_queue_pickle=None, with_scale=True, with_warp=True, with_frame_grab=True): ''' Create a new GStreamer pipeline with the specified video settings. ''' process = self.processes[window_xid] device, caps_str = video_settings if draw_queue_pickle: try: draw_queue = pickle.loads(str(draw_queue_pickle)) except (Exception, ), why: logging.warning(str(why)) draw_queue = None else:
return logger def switch(parameters): command = ["pihat"] for key in ("brand", "channel", "id", "state", "frequency"): command.append("--" + key + "=" + parameters[key]) process = subprocess.Popen( command, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT ) messages = [] while True: next_line = process.stdout.readline() if next_line == "" and process.poll() != None: break if not next_line.strip(): continue else: messages.append(next_line) return (process.returncode, messages) if __name__ == "__main__": global logger logger = set_logger() logger.debug("Running JSON-RPC server on port 8888") server = SimpleJSONRPCServer(("0.0.0.0", int(8888)), logger) server.register_introspection_functions() server.register_function(switch) server.serve_forever()
logger.addHandler(console) return logger def switch(parameters): command=["pihat"] for key in ("brand","channel","id","state","frequency"): command.append("--"+key+"="+parameters[key]) process=subprocess.Popen(command, shell = False ,stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.STDOUT) messages=[] while True: next_line = process.stdout.readline() if next_line == '' and process.poll() != None: break if not next_line.strip(): continue else: messages.append(next_line) return (process.returncode,messages) if __name__ == '__main__': global logger logger = set_logger() logger.debug('Running JSON-RPC server on port 8888') server = SimpleJSONRPCServer(('0.0.0.0', int(8888)),logger) server.register_introspection_functions() server.register_function(switch) server.serve_forever()