diff options
author | Alvaro Neira Ayuso <aneira@soleta.eu> | 2019-12-12 12:17:18 +0100 |
---|---|---|
committer | Alvaro Neira Ayuso <alvaroneay@gmail.com> | 2020-01-19 19:50:44 +0100 |
commit | 076e15bb299dbcc1f8cc0d6f260c26f39d81f983 (patch) | |
tree | 368a8b2a91442cd63771937f79632b8f6ff459b1 | |
parent | 29fe301ec82a657ee889411ad032af9909ceb6a4 (diff) |
Modify Client state to use enum
During our connections, we are using states to control the Client Socket. We
defined using global variables. In case that we modify this global variable,
we need to change it in serveral parts of the code.
Using enums and declaring a new class, we can redefine the values or create new
states without changing the same code in differents python files.
-rw-r--r-- | main.py | 9 | ||||
-rw-r--r-- | src/ogClient.py | 14 |
2 files changed, 11 insertions, 12 deletions
@@ -1,9 +1,6 @@ from src.ogClient import *
from src.ogConfig import *
-CONNECTING = 0
-RECEIVING = 1
-
def main():
ogconfig = ogConfig()
if (not ogconfig.parserFile('cfg/ogagent.cfg')):
@@ -20,7 +17,7 @@ def main(): sock = client.get_socket()
state = client.get_state()
- if state == CONNECTING:
+ if state == State.CONNECTING:
readset = [ sock ]
writeset = [ sock ]
else:
@@ -28,9 +25,9 @@ def main(): writeset = [ ]
readable, writable, exception = select.select(readset, writeset, [ ])
- if state == CONNECTING and sock in writable:
+ if state == State.CONNECTING and sock in writable:
client.connect2()
- elif state == RECEIVING and sock in readable:
+ elif state == State.RECEIVING and sock in readable:
client.receive()
else:
print "bad state" + str(state)
diff --git a/src/ogClient.py b/src/ogClient.py index 8c965fa..296c03c 100644 --- a/src/ogClient.py +++ b/src/ogClient.py @@ -5,9 +5,11 @@ import time import httplib
from mimetools import Message
from StringIO import StringIO
+from enum import Enum
-CONNECTING = 0
-RECEIVING = 1
+class State(Enum):
+ CONNECTING = 0
+ RECEIVING = 1
class ogClient:
def __init__(self, ip, port):
@@ -24,7 +26,7 @@ class ogClient: print "connecting"
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setblocking(0)
- self.state = CONNECTING
+ self.state = State.CONNECTING
self.data = ""
self.trailer = False
self.content_len = 0
@@ -45,10 +47,10 @@ class ogClient: except socket.error, err:
if err.errno == errno.EISCONN:
print "connected"
- self.state = RECEIVING
+ self.state = State.RECEIVING
else:
print "connection refused, retrying..."
- self.state = CONNECTING
+ self.state = State.CONNECTING
self.sock.close()
self.connect()
@@ -60,7 +62,7 @@ class ogClient: print "Error3 " + str(err)
if len(data) == 0:
- self.state = CONNECTING
+ self.state = State.CONNECTING
self.sock.close()
self.connect()
|