def __init__(self, instance=None): """Instantiate an mgen.Controller The 'instance' name is optional. If it is _not_ given, an mgen instance named "mgen-<processID>" where the <processID> is the process id of the Python script. If an mgen instance with the given name already exists, any commands this controller issues will be to that mgen instance. Otherwise a new mgen instance is created. Note that the mgen output can be monitored only for the mgen instance the controller creates. :type self: object """ args = ['mgen', 'flush', 'instance'] if instance is None: self.instance_name = 'mgen-' + str(os.getpid()) else: self.instance_name = instance args.append(self.instance_name) fp = open(os.devnull, 'w') # redirect stderr to /dev/null to hide self.proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=fp) fp.close() # Read the mgen stdout and find the "START event to confirm it # has launched successfully before trying to connect to it # (TBD - parse and save START event info for first readline() output) for line in self: field = line.split() if (len(field) > 1) and (field[1] == "START"): break # Connect a ProtoPipe to the mgen instance to be able to control # the mgen instance as needed in the future self.mgen_pipe = protokit.Pipe("MESSAGE") self.mgen_pipe.Connect(self.instance_name)
def __init__(self, instance=None, gpsKey=None): """Instantiate an mgen.Controller The 'instance' name is optional. If it is _not_ given, an mgen instance named "mgen-<processID>-<objectID>" where the <processID> is the process id of the Python script in hexadecimal notation and the <objectId> is the Python object id() of this Controller. If an mgen instance with the given name already exists, any commands this Controller issues will be to that mgen instance. Otherwise a new mgen instance is created. Note that the mgen output can be monitored only for the mgen instance the controller creates, if applicable. """ self.flow_dict = {} self.sink_proc = None self.gpsKey = gpsKey args = ['mgen', 'flush'] if instance is None: pidHex = "%x" % os.getpid() idHex = "%x" % id(self) self.instance_name = 'mgen-' + pidHex + '-' + idHex else: self.instance_name = instance if self.gpsKey is not None: args.append('gpskey') args.append(self.gpsKey) args.append('instance') args.append(self.instance_name) # TBD - should we try to connect to the instance_name to # see if it already exists instead of always creating a # child process??? self.mgen_pipe = protokit.Pipe("MESSAGE") try: self.mgen_pipe.Connect(self.instance_name) self.proc = None except: fp = open(os.devnull, 'w') # redirect stderr to /dev/null to hide self.proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=fp) fp.close() # Read the mgen stdout and find the "START event to confirm it # has launched successfully before trying to connect to it # (TBD - parse and save START event info for first readline() output) for line in self: field = line.split() if (len(field) > 1) and (field[1] == "START"): break # Connect a ProtoPipe to the mgen instance to be able to control # the mgen instance as needed in the future self.mgen_pipe.Connect(self.instance_name)
#!/usr/bin/env python import sys import protokit if len(sys.argv) <= 2: print "Usage: %s <consoleRPipeName> (n | r | g | i)" % sys.argv[0] sys.exit(1) send_pipe = protokit.Pipe("MESSAGE") send_pipe.Connect(sys.argv[1]) recv_pipe = protokit.Pipe("MESSAGE") if sys.argv[2] is 'n': message = "-sendConsoleNeighbors" elif sys.argv[2] is 'r': message = "-sendConsoleRoutes" elif sys.argv[2] is 'i': message = "-sendConsoleRouterID" elif sys.argv[2] is 'g': message = "-sendConsoleGraphML" else: print "Second arg '%s' isn't 'n','r','g' or 'i'" % sys.argv[2] sys.exit(1) recv_pipe.Listen(sys.argv[1] + "console") send_pipe.Send("-nrlConsole " + sys.argv[1] + "console") recv_pipe.Recv(100000) send_pipe.Send(message)
#!/usr/bin/env python import sys import protokit if len(sys.argv) <= 2: print "Usage: %s <pipeName> <message>" % sys.argv[0] sys.exit(1) pipe = protokit.Pipe("MESSAGE") pipe.Connect(sys.argv[1]) message = ' '.join(sys.argv[2:]) pipe.Send(message) print "Sent message '%s'" % message
import protokit import sys import time # Usage: python pipeExample {send | recv} <pipeName> # Take your pick of which pipe type you want to try # (Note that "STREAM" is currently limited to a single # accepted connection) #pipeType = "MESSAGE" pipeType = "STREAM" mode = sys.argv[1] pipeName = sys.argv[2] protoPipe = protokit.Pipe(pipeType) if "recv" == mode: protoPipe.Listen(pipeName) if "STREAM" == pipeType: protoPipe.Accept() while True: buf = protoPipe.Recv(8192) print buf else: protoPipe.Connect(pipeName) count = 0 while True: count += 1 protoPipe.Send("Hello, ProtoPipe %s (%d) ..." % (pipeName, count)) time.sleep(1.0)