diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | cfg/ogagent.cfg | 7 | ||||
-rw-r--r-- | main.py | 39 | ||||
-rw-r--r-- | src/__init__.py | 0 | ||||
-rw-r--r-- | src/ogClient.py | 95 | ||||
-rw-r--r-- | src/ogConfig.py | 24 |
6 files changed, 168 insertions, 0 deletions
@@ -127,3 +127,6 @@ dmypy.json # Pyre type checker .pyre/ + +# ignore swp +*.swp diff --git a/cfg/ogagent.cfg b/cfg/ogagent.cfg new file mode 100644 index 0000000..2e1f9ce --- /dev/null +++ b/cfg/ogagent.cfg @@ -0,0 +1,7 @@ +[opengnsys] +ip=192.168.2.175 +port=1234 +# Remote OpenGnsys Service +remote=https://192.168.2.10/opengnsys/rest +# Log Level, if ommited, will be set to INFO +log=DEBUG @@ -0,0 +1,39 @@ +from src.ogClient import *
+from src.ogConfig import *
+
+CONNECTING = 0
+RECEIVING = 1
+
+def main():
+ ogconfig = ogConfig()
+ if (not ogconfig.parserFile('cfg/ogagent.cfg')):
+ print 'Error: Parsing configuration file'
+ return 0
+
+ ip = ogconfig.getValueSection('opengnsys', 'ip')
+ port = ogconfig.getValueSection('opengnsys', 'port')
+
+ client = ogClient(ip, int(port))
+ client.connect()
+
+ while 1:
+ sock = client.get_socket()
+ state = client.get_state()
+
+ if state == CONNECTING:
+ readset = [ sock ]
+ writeset = [ sock ]
+ else:
+ readset = [ sock ]
+ writeset = [ ]
+
+ readable, writable, exception = select.select(readset, writeset, [ ])
+ if state == CONNECTING and sock in writable:
+ client.connect2()
+ elif state == RECEIVING and sock in readable:
+ client.receive()
+ else:
+ print "bad state" + str(state)
+
+if __name__ == "__main__":
+ main()
diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/__init__.py diff --git a/src/ogClient.py b/src/ogClient.py new file mode 100644 index 0000000..8c965fa --- /dev/null +++ b/src/ogClient.py @@ -0,0 +1,95 @@ +import errno
+import select
+import socket
+import time
+import httplib
+from mimetools import Message
+from StringIO import StringIO
+
+CONNECTING = 0
+RECEIVING = 1
+
+class ogClient:
+ def __init__(self, ip, port):
+ self.ip = ip
+ self.port = port
+
+ def get_socket(self):
+ return self.sock
+
+ def get_state(self):
+ return self.state
+
+ def connect(self):
+ print "connecting"
+ self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ self.sock.setblocking(0)
+ self.state = CONNECTING
+ self.data = ""
+ self.trailer = False
+ self.content_len = 0
+
+ try:
+ self.sock.connect((self.ip, self.port))
+ except socket.error, err:
+ if err.errno == errno.EINPROGRESS:
+ return
+ elif err.errno == errno.ECONNREFUSED:
+ return
+
+ print "Error connect " + str(err)
+
+ def connect2(self):
+ try:
+ self.sock.connect((self.ip, self.port))
+ except socket.error, err:
+ if err.errno == errno.EISCONN:
+ print "connected"
+ self.state = RECEIVING
+ else:
+ print "connection refused, retrying..."
+ self.state = CONNECTING
+ self.sock.close()
+ self.connect()
+
+ def receive(self):
+ print "receiving"
+ try:
+ data = self.sock.recv(1024)
+ except socket.err, err:
+ print "Error3 " + str(err)
+
+ if len(data) == 0:
+ self.state = CONNECTING
+ self.sock.close()
+ self.connect()
+
+ print "received " + data
+ self.data = self.data + data
+
+ if not self.trailer:
+ if self.data.find("\r\n") > 0:
+ # https://stackoverflow.com/questions/4685217/parse-raw-http-headers
+ request_line, headers_alone = self.data.split('\n', 1)
+ headers = Message(StringIO(headers_alone))
+
+ print "DEBUG: \r\n trailer received"
+ print "DEBUG: HTTP keys are " + str(headers.keys())
+
+ if 'content-length' in headers.keys():
+ self.content_len = int(headers['content-length'])
+
+ self.trailer = True
+
+ if self.trailer and len(self.data) >= self.content_len:
+ #
+ # TODO: handle request here
+ #
+
+ self.sock.send("HTTP/1.0 200 OK\r\n\r\n")
+
+ # Cleanup state information from request
+ self.data = ""
+ self.content_len = 0
+ self.trailer = False
+ print "DEBUG: request has been processed!"
diff --git a/src/ogConfig.py b/src/ogConfig.py new file mode 100644 index 0000000..84d3346 --- /dev/null +++ b/src/ogConfig.py @@ -0,0 +1,24 @@ +import configparser + +class ogConfig: + def __init__(self): + self.parser = configparser.ConfigParser() + + def parserFile(self, path): + self.parser.read(path) + if len(self.parser.sections()) == 0: + return False + + return True + + def getSections(self): + return self.parser.sections() + + def getContainsSection(self, section): + return section in self.parser + + def getValueSection(self, section, key): + if (not self.getContainsSection(section)): + return '' + + return self.parser[section][key] |