def run(self): while True: # this blocks until message received, or timeout occurs timeout = 10.0 # value in seconds hdr = self.connection.get_message(timeout) if hdr: msg = Messaging.MsgFactory(hdr) self.db.handle_message(msg)
def logMessage(self, hdr): #write to log, if log is open if self.logFile != None: if self.logFileType and self.logFileType == "JSON": msgObj = Messaging.MsgFactory(hdr) self.logFile.write(msgjson.toJson(msgObj).encode('utf-8')) else: self.logFile.write(hdr.rawBuffer().raw)
def logMsg(self, hdr): #write to log, if log is open if self.logFile != None: log = '' if self.logFileType == "csv": msg = Messaging.MsgFactory(hdr) if not msg.MsgName() in self.loggedMsgHeaderRow: self.loggedMsgHeaderRow[msg.MsgName()] = True log = msg.csvHeader(timeColumn=True) + '\n' log += msg.toCsv(timeColumn=True) + '\n' log = log.encode('utf-8') elif self.logFileType == "json": msg = Messaging.MsgFactory(hdr) log = msg.toJson(includeHeader=True) + '\n' log = log.encode('utf-8') elif self.logFileType == "bin": log = hdr.rawBuffer().raw self.logFile.write(log) self.logFile.flush()
def main(args=None): msgLib = Messaging(None, 0, "NetworkHeader") if len(sys.argv) > 1 and sys.argv[1] == "server": connection = SynchronousMsgServer(Messaging.hdr) else: connection = SynchronousMsgClient(Messaging.hdr) # say my name connectMsg = msgLib.Messages.Network.Connect() connectMsg.SetName("CLI") connection.send_message(connectMsg) # do default subscription to get *everything* subscribeMsg = msgLib.Messages.Network.MaskedSubscription() connection.send_message(subscribeMsg) _cmd = "" try: while True: cmd = input("") #print("got input cmd [" + cmd + "]") if cmd: if "getmsg" in cmd: msgIDs = [] msgIDNames = cmd.split(",")[1:] for msgname in msgIDNames: try: if int(msgname, 0): msgIDs.append(int(msgname,0)) except ValueError: if msgname in Messaging.MsgIDFromName: msgIDs.append(int(Messaging.MsgIDFromName[msgname], 0)) else: print("invalid msg " + msgname) # this blocks until message received, or timeout occurs timeout = 10.0 # value in seconds hdr = connection.get_message(timeout, msgIDs) if hdr: msg = msgLib.MsgFactory(hdr) # print as JSON for debug purposes json = Messaging.toJson(msg) print(json) else: print("{}") else: # this translates the input command from CSV to a message, and sends it. msg = Messaging.csvToMsg(cmd) if msg: connection.send_message(msg) # I can't get exit on Ctrl-C to work! except KeyboardInterrupt: print('You pressed Ctrl+C!') connection.stop()
def recv(self, msgIds=[], timeout=None): # if user didn't pass a list, put the single param into a list if not isinstance(msgIds, list): msgIds = [msgIds] # if they passed classes, get the ID of each for i in range(0,len(msgIds)): if hasattr(msgIds[i], 'ID'): msgIds[i] = msgIds[i].ID if timeout != None and self._timeout != timeout: self._timeout = timeout if self._timeout == 0.0: self._sock.setblocking(0) else: self._sock.setblocking(1) self._sock.settimeout(self._timeout) while True: try: # see if there's enough for header data = self._sock.recv(Messaging.hdr.SIZE, socket.MSG_PEEK) if len(data) == Messaging.hdr.SIZE: # create header based on peek'd data hdr = Messaging.hdr(data) # see if there's enough for the body, too data += self._sock.recv(hdr.GetDataLength(), socket.MSG_PEEK) if len(data) != Messaging.hdr.SIZE + hdr.GetDataLength(): print("didn't get whole body, error!") continue # read out what we peek'd. data = self._sock.recv(Messaging.hdr.SIZE + hdr.GetDataLength()) # reset the header based on appended data hdr = Messaging.hdr(data) id = hdr.GetMessageID() if id in self._extra_msgs_to_record: self.received[id] = msg if len(msgIds) == 0 or id in msgIds: msg = Messaging.MsgFactory(hdr) self.received[id] = msg return msg except socket.timeout: return None except BlockingIOError: return None
def recv(self, msgIds=[], timeout=None): # if user didn't pass a list, put the single param into a list if not isinstance(msgIds, list): msgIds = [msgIds] # if they passed classes, get the ID of each for i in range(0,len(msgIds)): if hasattr(msgIds[i], 'ID'): msgIds[i] = msgIds[i].ID if timeout != None and self.timeout != timeout: self.timeout = timeout while True: try: data = self.synchronous_rx_queue.get(True, self.timeout) hdr = Messaging.hdr(data) id = hdr.GetMessageID() if len(msgIds) == 0 or id in msgIds: msg = Messaging.MsgFactory(hdr) return msg except queue.Empty: return None
class InfluxDBMsgClient: def __init__(self): self.msgLib = Messaging(None, 0, "NetworkHeader") self.connection = SynchronousMsgClient(Messaging.hdr) # say my name connectMsg = self.msgLib.Messages.Network.Connect() connectMsg.SetName("InfluxDB") self.connection.send_message(connectMsg) # do default subscription to get *everything* subscribeMsg = self.msgLib.Messages.Network.MaskedSubscription() self.connection.send_message(subscribeMsg) self.db = InfluxDBConnection() def run(self): while True: # this blocks until message received, or timeout occurs timeout = 10.0 # value in seconds hdr = self.connection.get_message(timeout) if hdr: msg = self.msgLib.MsgFactory(hdr) self.db.send_message(msg)
def sendMsg(self, hdr): msg = Messaging.MsgFactory(hdr) self.db.handle_message(msg)
def onMessageReceived(self, hdr): c = self.sender() # check for name, subscription, etc. if hasattr(self.networkMsgs, 'Connect') and hdr.GetMessageID( ) == self.networkMsgs.Connect.ID: connectMsg = self.networkMsgs.Connect(hdr.rawBuffer()) c.name = connectMsg.GetName() c.statusLabel.setText(c.name) elif hasattr(self.networkMsgs, 'SubscriptionList') and hdr.GetMessageID( ) == self.networkMsgs.SubscriptionList.ID: c.subscriptions = {} subListMsg = self.networkMsgs.SubscriptionList(hdr.rawBuffer()) for idx in range(0, self.networkMsgs.SubscriptionList.GetIDs.count): id = subListMsg.GetIDs(idx) if id != 0: c.subscriptions[id] = id self.onStatusUpdate("updating subscription for " + c.name + " to " + ', '.join( hex(x) for x in c.subscriptions.keys())) elif hasattr(self.networkMsgs, 'MaskedSubscription') and hdr.GetMessageID( ) == self.networkMsgs.MaskedSubscription.ID: subMsg = self.networkMsgs.MaskedSubscription(hdr.rawBuffer()) c.subMask = subMsg.GetMask() c.subValue = subMsg.GetValue() self.onStatusUpdate("updating subscription for " + c.name + " to id & " + hex(c.subMask) + " == " + hex(c.subValue)) elif hasattr(self.networkMsgs, 'StartLog') and hdr.GetMessageID( ) == self.networkMsgs.StartLog.ID: startLog = self.networkMsgs.StartLog(hdr.rawBuffer()) self.logFileType = startLog.GetLogFileType() logFileName = startLog.GetLogFileName() if not logFileName: logFileName = QtCore.QDateTime.currentDateTime().toString( "yyyyMMdd-hhmmss") + ".log" self.startLog(logFileName) elif hasattr(self.networkMsgs, 'StopLog') and hdr.GetMessageID( ) == self.networkMsgs.StopLog.ID: self.stopLog() elif hasattr(self.networkMsgs, 'QueryLog') and hdr.GetMessageID( ) == self.networkMsgs.QueryLog.ID: self.queryLog() elif hasattr(self.networkMsgs, 'PrivateSubscriptionList') and hdr.GetMessageID( ) == self.networkMsgs.PrivateSubscriptionList.ID: subListMsg = self.networkMsgs.PrivateSubscriptionList( hdr.rawBuffer()) privateSubs = [] for idx in range( 0, self.networkMsgs.PrivateSubscriptionList.GetIDs.count): id = subListMsg.GetIDs(idx) if id == 0: break privateSubs.append(id) if id in self.privateSubscriptions: self.privateSubscriptions[id].append(c) else: self.privateSubscriptions[id] = [c] self.onStatusUpdate("adding Private subscription for " + c.name + ": " + ', '.join(hex(x) for x in privateSubs)) else: #write to log, if log is open if self.logFile != None: if self.logFileType and self.logFileType == "JSON": msgObj = Messaging.MsgFactory(hdr) self.logFile.write( Messaging.toJson(msgObj).encode('utf-8')) else: self.logFile.write(hdr.rawBuffer().raw) for client in self.clients.values(): if client != c: id = hdr.GetMessageID() if id in client.subscriptions or (id & client.subMask == client.subValue): try: if id in self.privateSubscriptions: # if it's a "private" message, only give it to clients that specifically said they want it # or to clients that are a hardware link. if client in self.privateSubscriptions[ id] or client.isHardwareLink: client.sendMsg(hdr) else: client.sendMsg(hdr) except Exception as ex: self.onStatusUpdate( "Exception in server.py while sending to client " + client.name + ": [" + str(ex) + "]")