def get_state(proc_name): exec_cmd_msg = exec_cmd_pb2.Exec_Cmd() exec_cmd_msg.command = exec_cmd_pb2.Exec_Cmd.STATE exec_cmd_msg.name = proc_name msg = exec_cmd_msg.SerializeToString() unixIPC.send("exec_cmd_pb2",msg) flush() msgs = unixIPC.waitForRecv(); if len(msgs) == 0: print "ERROR: IPC timeout to mod_exec during get_state()"; else: # Bad idea ahead. Works for mod_exec b/c it only sends solicited exec_resp messages if len(msgs) > 1: print "Note: Uh-oh, I ate extra messages: ",msgs[1:len(msgs)] (type, msg, client) = msgs[0]; exec_resp_msg = exec_resp_pb2.Exec_Resp() exec_resp_msg.ParseFromString(msg); if exec_resp_msg.type == exec_resp_pb2.Exec_Resp.STATE: if exec_resp_msg.processes[0].name == proc_name: if exec_resp_msg.processes[0].state == exec_resp_pb2.Exec_Resp.RUNNING: return RUNNING; elif exec_resp_msg.processes[0].state == exec_resp_pb2.Exec_Resp.STOPPED: return STOPPED; elif exec_resp_msg.processes[0].state == exec_resp_pb2.Exec_Resp.RESTARTING: return RESTARTING; else: print "ERROR: mod_exec response to a read did not return the same process name!" else: print "ERROR: Expected STATE response but got",exec_resp_msg.type
def kill(): exec_cmd_msg = exec_cmd_pb2.Exec_Cmd() exec_cmd_msg.command = exec_cmd_pb2.Exec_Cmd.KILL msg = exec_cmd_msg.SerializeToString() unixIPC.send("exec_cmd_pb2",msg) if blocking_mode: flush()
def publish(source, data): cmd_msg = xbee_relay_cmd_pb2.XBee_Relay_Cmd() cmd_msg.command = xbee_relay_cmd_pb2.XBee_Relay_Cmd.PUBLISH_DATA cmd_msg.source = source; cmd_msg.data = str(data); unixIPC.send("xbee_relay_cmd_pb2",cmd_msg.SerializeToString()) return proc_end();
def restart(proc_name): exec_cmd_msg = exec_cmd_pb2.Exec_Cmd() exec_cmd_msg.command = exec_cmd_pb2.Exec_Cmd.RESTART exec_cmd_msg.name = proc_name msg = exec_cmd_msg.SerializeToString() unixIPC.send("exec_cmd_pb2",msg) if blocking_mode: flush()
def list(): exec_cmd_msg = exec_cmd_pb2.Exec_Cmd() exec_cmd_msg.command = exec_cmd_pb2.Exec_Cmd.LIST msg = exec_cmd_msg.SerializeToString() unixIPC.send("exec_cmd_pb2",msg) flush() msgs = unixIPC.waitForRecv(); if len(msgs) == 0: print "ERROR: IPC timeout to mod_exec during list()"; else: # Bad idea ahead. Works for mod_exec b/c it only sends solicited exec_resp messages if len(msgs) > 1: print "Note: Uh-oh, I ate extra messages: ",msgs[1:len(msgs)] (type, msg, client) = msgs[0]; exec_resp_msg = exec_resp_pb2.Exec_Resp() try: exec_resp_msg.ParseFromString(msg); except google.protobuf.message.DecodeError: print "Error parsing exec_resp_msg" if exec_resp_msg.type == exec_resp_pb2.Exec_Resp.STATE: retval = []; for proc in exec_resp_msg.processes: p = Process() p.name = proc.name; if proc.state == exec_resp_pb2.Exec_Resp.RUNNING: p.state = RUNNING; elif proc.state == exec_resp_pb2.Exec_Resp.STOPPED: p.state = STOPPED; elif proc.state == exec_resp_pb2.Exec_Resp.RESTARTING: p.state = RESTARTING; retval.append(p); return retval; else: print "ERROR: Expected STATE response but got",exec_resp_msg.type
def broadcast(data): cmd_msg = xbee_replay_cmd_pb2.XBee_Relay_Cmd() cmd_msg.command = xbee_relay_cmd_pb2.XBee_Relay_Cmd.BROADCAST_TO_XBEE cmd_msg.data = str(data) unixIPC.send("xbee_relay_cmd_pb2",cmd_msg.SerializeToString()) return proc_end();
def forward(to, data): cmd_msg = xbee_replay_cmd_pb2.XBee_Relay_Cmd() cmd_msg.command = xbee_relay_cmd_pb2.XBee_Relay_Cmd.FORWARD_TO_XBEE cmd_msg.data = str(data) unixIPC.send("xbee_relay_cmd_pb2",cmd_msg.SerializeToString()) return proc_end();
def register_as_relay(): cmd_msg = xbee_relay_cmd_pb2.XBee_Relay_Cmd() cmd_msg.command = xbee_relay_cmd_pb2.XBee_Relay_Cmd.REGISTER_AS_RELAY unixIPC.send("xbee_relay_cmd_pb2",cmd_msg.SerializeToString()) flush()