def __init__(self, name='Client', timeout=10): # keep a reference to Messages, for convenience of cmdline scripts self.Messages = Messaging.Messages # load all messages if not already loaded if Messaging.hdr == None: Messaging.LoadAllMessages() self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self._sock.connect(("127.0.0.1", 5678)) self._timeout = timeout if self._timeout == 0.0: self._sock.setblocking(0) else: self._sock.setblocking(1) self._sock.settimeout(self._timeout) # say my name connectMsg = Messaging.Messages.Network.Connect() connectMsg.SetName(name) self.send(connectMsg) # do default subscription to get *everything* subscribeMsg = Messaging.Messages.Network.MaskedSubscription() self.send(subscribeMsg) # keep a dictionary of the latest value of all received messages self.received = {} # also record particular messages the user wants to record, even if # they never called recv on them self._extra_msgs_to_record = {}
def main(args=None): Messaging.LoadAllMessages() timeout = 10.0 if len(sys.argv) > 1 and sys.argv[1] == "server": connection = Server(timeout) else: connection = Client("CLI", timeout) msg_cmd = MsgCmd(connection, timeout) try: msg_cmd.cmdloop() except KeyboardInterrupt: print("^C")
def __init__(self): Messaging.LoadAllMessages() self.connection = SynchronousMsgClient(Messaging.hdr) # say my name connectMsg = Messaging.Messages.Network.Connect() connectMsg.SetName("InfluxDB") self.connection.send_message(connectMsg) # do default subscription to get *everything* subscribeMsg = Messaging.Messages.Network.MaskedSubscription() self.connection.send_message(subscribeMsg) self.db = InfluxDBConnection(self)
from msgtools.lib.messaging import Messaging as M from msgtools.lib.message import Message as Msg from msgtools.console.client import Client M.LoadAllMessages() cxn = Client('ExampleClient') while True: # blocking read for some number of seconds msg = cxn.recv(timeout=10.0) # check if we timeout if msg == None: print("Didn't get a message, timeout occured!") else: # print any message as JSON print(msg.toJson()) # check type of specific message if type(msg) == M.Messages.TestCase1: # get a field using accessor function, or dict lookup which is equivalent print("tc1.FieldE: %f" % msg.GetFieldE()) print("tc1.FieldE: %f" % msg.FieldE) elif type(msg) == M.Messages.TestCase2: print("tc2.F1: " + str(msg.F1)) # create a message to send tc2 = M.Messages.TestCase2() # set a field tc2.F1 = "Val0" tc2.Field6 = 1 tc2.SetF2(1.5, 0) tc2.SetF2(2.5, 1)
def __init__(self): QtWidgets.QMainWindow.__init__(self) self.settings = QtCore.QSettings("MsgTools", "MessageServer") self.logFile = None self.logFileType = None parser = argparse.ArgumentParser( description=DESCRIPTION, epilog=EPILOG, formatter_class=argparse.RawDescriptionHelpFormatter) # iterate over plugins, and setup argparse for each for entry_point in pkg_resources.iter_entry_points( "msgtools.server.plugin"): parser.add_argument('--%s' % entry_point.name, dest='last%s' % entry_point.name, action='store_true', help='''Load the %s plugin via %s:%s''' % (entry_point.name, entry_point.module_name, entry_point.attrs[0])) parser.add_argument( '--%s=' % entry_point.name, dest=entry_point.name, help="Same as above, passing param %s to the plugin" % (entry_point.name.upper())) parser.add_argument( '--plugin', help='Specify a filesystem path to a plugin module to load.') parser.add_argument( '--msgdir', help= '''Specify the directory for generated python code to use for headers, and other message definitions''') parser.add_argument( '--port', type=int, help='The TCP port to use. Websockets are always TCP port + 1.') parser.add_argument( '--debug', action='store_true', help='Set if you want extra error info printed to stdout.') # if we had plugins before, but no cmdline args now, add simulated # cmdline args for our old plugins if len(sys.argv) == 1: last_plugins = self.settings.value("pluginsLoaded", "", str).split("|") for plugin in last_plugins: if plugin: sys.argv.append("--" + plugin) args = parser.parse_args() Messaging.debug = False if hasattr(args, 'debug') == False else args.debug try: Messaging.LoadAllMessages(searchdir=args.msgdir) except ImportError: print("\nERROR! Auto-generated python code not found!") print( "cd to a directory downstream from a parent of obj/CodeGenerator/Python" ) print("or specify that directory with --msgdir=PATH\n") quit() self.networkMsgs = Messaging.Messages.Network self.clients = {} self.privateSubscriptions = {} self.initializeGui() self.pluginPorts = [] tcpport = 5678 wsport = 5679 if args.port is not None: tcpport = args.port wsport = tcpport + 1 for entry_point in pkg_resources.iter_entry_points( "msgtools.server.plugin"): # check for argparse data for the plugin plugin_option = getattr(args, entry_point.name) plugin_last_option = getattr(args, 'last' + entry_point.name) if plugin_last_option is not False or plugin_option is not None: param = plugin_option if plugin_option is not None else None self.load_plugin(entry_point.name, entry_point, param) # load plugin via path to .py file if args.plugin is not None: filename = args.plugin import os moduleName = os.path.splitext(os.path.basename(filename))[0] if Messaging.debug: print("loading module %s as %s" % (filename, moduleName)) name = filename.replace("/", "_") import importlib self.plugin = importlib.machinery.SourceFileLoader( name, filename).load_module(name) pluginPort = self.plugin.PluginConnection(None) pluginPort.statusUpdate.connect(self.onStatusUpdate) # try to allow plugin to emit new connections try: pluginPort.newConnection.connect(self.onNewConnection) except AttributeError: # if that fails, just use the plugin as one new connection. self.onNewConnection(pluginPort) pluginPort.start() self.pluginPorts.append(pluginPort) self.tcpServer = TcpServer(tcpport) self.tcpServer.statusUpdate.connect(self.onStatusUpdate) self.tcpServer.newConnection.connect(self.onNewConnection) self.wsServer = WebSocketServer(wsport) self.wsServer.statusUpdate.connect(self.onStatusUpdate) self.wsServer.newConnection.connect(self.onNewConnection) self.tcpServer.start() self.wsServer.start() name = self.tcpServer.serverInfo() + "(TCP) and " + str( self.wsServer.portNumber) + "(WebSocket)" self.statusBar().addPermanentWidget(QtWidgets.QLabel(name)) self.readSettings()
def setUpClass(cls): print("----------- Running setup") Messaging.LoadAllMessages()