Browse Source

protocol -> listener, create a pcomm object to wrap process communication event sends.

Chris McDonough 17 years ago
parent
commit
044ebeaf41

+ 16 - 13
src/supervisor/childutils.py

@@ -41,6 +41,11 @@ def write_stdout(msg):
 def get_headers(line):
     return dict([ x.split(':') for x in line.split() ])
 
+def eventdata(payload):
+    headerinfo, data = payload.split('\n')
+    headers = get_headers(headerinfo)
+    return headers, data
+
 def get_asctime():
     now = time.time()
     msecs = (now - long(now)) * 1000
@@ -48,21 +53,19 @@ def get_asctime():
     asctime = '%s,%03d' % (part1, msecs)
     return asctime
 
-def send_proc_comm(msg, write=write_stdout):
-    write(ProcessCommunicationEvent.BEGIN_TOKEN)
-    write(msg)
-    write(ProcessCommunicationEvent.END_TOKEN)
+class ProcessCommunicationsProtocol:
+    def send(self, msg, write=write_stdout):
+        write(ProcessCommunicationEvent.BEGIN_TOKEN)
+        write(msg)
+        write(ProcessCommunicationEvent.END_TOKEN)
 
-def send_proc_comm_stdout(msg):
-    return send_proc_comm(msg, write_stdout)
+    def stdout(self, msg):
+        return self.send(msg, write_stdout)
 
-def send_proc_comm_stderr(msg):
-    return send_proc_comm(msg, write_stderr)
+    def stderr(self, msg):
+        return self.send(msg, write_stderr)
 
-def eventdata(payload):
-    headerinfo, data = payload.split('\n')
-    headers = get_headers(headerinfo)
-    return headers, data
+pcomm = ProcessCommunicationsProtocol()
 
 class EventListenerProtocol:
     def wait(self):
@@ -78,4 +81,4 @@ class EventListenerProtocol:
     def fail(self, *ignored):
         write_stdout(PEventListenerDispatcher.EVENT_REJECTED_TOKEN)
 
-protocol = EventListenerProtocol()
+listener = EventListenerProtocol()

+ 1 - 1
src/supervisor/scripts/loop_eventgen.py

@@ -27,7 +27,7 @@ def main(max):
     report = open('/tmp/report', 'w')
     i = 0
     while 1:
-        childutils.send_proc_comm_stdout('the_data')
+        childutils.pcomm.stdout('the_data')
         data = sys.stdin.readline()
         report.write(str(i) + ' @ %s\n' % childutils.get_asctime())
         report.flush()

+ 2 - 2
src/supervisor/scripts/loop_listener.py

@@ -24,12 +24,12 @@ from supervisor import childutils
 def main():
     rpcinterface = childutils.getRPCInterface(os.environ)
     while 1:
-        headers, payload = childutils.protocol.wait()
+        headers, payload = childutils.listener.wait()
         if headers['eventname'].startswith('PROCESS_COMMUNICATION'):
             pheaders, pdata = childutils.eventdata(payload)
             pname = '%s:%s' % (pheaders['processname'], pheaders['groupname'])
             rpcinterface.supervisor.sendProcessStdin(pname, 'Got it yo\n')
-        childutils.protocol.ok()
+        childutils.listener.ok()
 
 if __name__ == '__main__':
     main()

+ 1 - 1
src/supervisor/scripts/osx_memmon_eventgen.py

@@ -223,7 +223,7 @@ def main(every):
     while 1:
         infos = rpc.supervisor.getAllProcessInfo()
         result = do(infos)
-        childutils.send_proc_comm_stdout(result)
+        childutils.pcomm.stdout(result)
         time.sleep(every)
 
 def do(infos):

+ 2 - 2
src/supervisor/scripts/osx_memmon_listener.py

@@ -43,7 +43,7 @@ except ImportError:
 def main(maxkb):
     rpc = childutils.getRPCInterface(os.environ)
     while 1:
-        headers, payload = childutils.protocol.wait()
+        headers, payload = childutils.listener.wait()
         if headers['eventname'].startswith('PROCESS_COMMUNICATION'):
             pheaders, pdata = childutils.eventdata(payload)
             procname, groupname = pheaders['processname'], pheaders['groupname']
@@ -55,7 +55,7 @@ def main(maxkb):
                         if  rss > maxkb:
                             rpc.supervisor.stopProcess(name)
                             rpc.supervisor.startProcess(name)
-        childutils.protocol.ok()
+        childutils.listener.ok()
 
 if __name__ == '__main__':
     maxbytes = datatypes.byte_size(sys.argv[1])