示例#1
0
 def run(self):
     try:
         for i in range (0,3):
             self.output("waiting for msg1")
             msg = self.listener.WaitForMsg('Msg1Name')
             self.output(Messaging.toJson(msg))
             self.output("waiting for msg2")
             msg = self.listener.WaitForMsg('Msg2Name')
             self.output(Messaging.toJson(msg))
     except MsgTimeoutError as e:
         self.output(">>>> " + str(e))
示例#2
0
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()
示例#3
0
 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) + "]")