From 18e0376ffd9dee51f554f422b8e83bcfaa0ab218 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 25 Sep 2011 02:53:05 +0200 Subject: Reaktor/UDP: working release Reaktor/UDP is customizable via a single json config parsed at the very beginning, starting a number of asynchronous UDP listeners. See Readme for more info --- UDP/README | 7 ++++--- UDP/config | 15 +++++++++++++++ UDP/index | 33 ++++++++++++++++++++++++++++----- commands/licht_resolver | 37 +++++++++++++++++++++++++++++++++++++ udp_commands/licht_resolver | 1 + 5 files changed, 85 insertions(+), 8 deletions(-) create mode 100644 UDP/config create mode 100755 commands/licht_resolver create mode 120000 udp_commands/licht_resolver diff --git a/UDP/README b/UDP/README index ad7117f..587c804 100644 --- a/UDP/README +++ b/UDP/README @@ -5,6 +5,7 @@ From 2011-09-16: { "port": 1337, "pattern": "XXYY", + "comment" : "does the right thing", "action" : { "POST": { "url" : "xyz", @@ -15,10 +16,9 @@ From 2011-09-16: ] ## Head definition { - "bind" : "127.0.0.1", + "bind_addr" : "127.0.0.1", "port" : 1234, "pattern" : "XXZZ", - "terminator" : "\r\n" "action" : { }, } bind is an optional entry which lets the user define a bind address for the server. @@ -35,6 +35,7 @@ terminator is optional which lets the user define the EOM terminator. Process is taken from //hyper/process/test/bc.json - rev:a7fd3f stdout/stderr are optional and if left away data will be written to real stdout/stderr +in the process in envp => "payload" will be allocated for the given payload from the network "PROCESS" : { { @@ -43,7 +44,7 @@ stdout/stderr are optional and if left away data will be written to real stdout/ "bc" ], "envp": { - "was": "geht" + "was": "geht", }, "stdout": "uri:///path/to/somewhere", "stderr": "uri:///path/to/somewhere" diff --git a/UDP/config b/UDP/config new file mode 100644 index 0000000..34f0d0b --- /dev/null +++ b/UDP/config @@ -0,0 +1,15 @@ +[ + { + "comment" : "listener for licht.shack", + "bind_addr" : "0.0.0.0", + "port" : 2342, + "pattern" : "", + "action" : { + "PROCESS" : { + "path" : "/krebs/Reaktor/UDP/licht_resolver", + "argv" : [ ], + "envp" : { } + } + } + } +] diff --git a/UDP/index b/UDP/index index b7a2150..3e0bbd1 100755 --- a/UDP/index +++ b/UDP/index @@ -1,9 +1,10 @@ #!/usr/bin/python import asyncore, socket -import logging +import logging,subprocess import re import sys +import json log = logging.getLogger('ubot') def enable_syslog(logger): @@ -19,7 +20,7 @@ def enable_syslog(logger): class ubot(asyncore.dispatcher): """ UDP Bot """ - def __init__(self, port,pattern,action,bind_addr="",): + def __init__(self, port,pattern,action,comment="",bind_addr="",): asyncore.dispatcher.__init__(self) self.bind_addr = bind_addr self.port = port @@ -64,13 +65,35 @@ class ubot(asyncore.dispatcher): def start_action(self): """ runs all the defined actions""" log.debug("Actions: %s" % str(self.action)) - log.error("not yet implemented") - sys.exit(23) + self.start_process() + self.start_post() + + def start_process(self): + try: + act = self.action["PROCESS"] + proc = [] + proc.append(act["path"]) + proc.extend(act["argv"]) + + env = act["envp"] + env["payload"] = json.dumps(self.data) + log.info("Starting Process: %s (env: %s)" % (proc,env)) + subprocess.Popen(proc,env=env) + except Exception as e: + log.error(e) + def start_post(self): + pass + +def load_conf(conf_file): + return json.load(open(conf_file)) + if __name__ == "__main__": import os #enable_syslog(log) + HERE = os.path.dirname(os.path.realpath(__file__)) lol = logging.DEBUG if os.environ.get('debug',False) else logging.INFO logging.basicConfig(level=lol) - ubot(1337,r'abc',{}) + for i in load_conf("%s/config" %HERE): + ubot(**i) asyncore.loop() diff --git a/commands/licht_resolver b/commands/licht_resolver new file mode 100755 index 0000000..5bdb651 --- /dev/null +++ b/commands/licht_resolver @@ -0,0 +1,37 @@ +#! /usr/bin/python +# -*- coding: utf-8 -*- + +map = { + 'shackspace': { + 'device': { + 0: 'Licht0, Zickenzone; Fenster', + 1: 'Licht1, Sofaecke; Fenster', + 2: 'Licht2, Zickenzone; Ghetto', + 3: 'Licht3, Sofaecke; Ghetto', + 4: 'Licht4, Richtung Getränkelager', + 5: 'Licht5, Porschekonsole', + 6: 'Licht6, Tomatenecke', + 7: 'Licht7, Ghetto', + 10: 'Hauptschalter' + }, + 'state': { + 0: 'aus', + 1: 'an', + 2: 'aus in T-10s' + }, + '->': 'ist' + } +} + +from struct import unpack +import json +from os import environ as env + +location = "shackspace" + +map = map[location] +print (env["payload"]+"") +did, sid= unpack('BB', json.loads(env['payload'])) +device,state = map['device'][did], map['state'][sid] +arrow = map['->'] +print ' '.join([device, arrow, state]) diff --git a/udp_commands/licht_resolver b/udp_commands/licht_resolver new file mode 120000 index 0000000..0d8b095 --- /dev/null +++ b/udp_commands/licht_resolver @@ -0,0 +1 @@ +../commands/licht_resolver \ No newline at end of file -- cgit v1.2.3