Permalink
Browse files

refactored classes to ease reuse

  • Loading branch information...
1 parent 9953e48 commit 1fd665568b24789063d73eb3c221bf7b282c1395 @videlalvaro committed Jan 31, 2010
Showing with 83 additions and 57 deletions.
  1. +58 −0 receiver.py
  2. +25 −57 session_inspector.py
View
@@ -0,0 +1,58 @@
+from twisted.internet import reactor
+from twisted.internet.protocol import Factory
+from twisted.internet.error import ConnectionDone
+from twisted.protocols.basic import LineOnlyReceiver
+import sys
+from conf import *
+from session_inspector import SessionInspectorMemcache
+
+class InspectorReceiver(LineOnlyReceiver):
+ delimiter = '\r\n'
+
+ def __init__(self):
+ self.inspector = SessionInspectorMemcache()
+
+ def connectionMade(self):
+ self.peer = self.transport.getPeer()
+ self.peerAddr = "%s:%d" % (self.peer.host, self.peer.port)
+ print("%s connection with %s established" % (self.__class__.__name__, self.peerAddr))
+
+ def connectionLost(self, reason):
+ if reason.check(ConnectionDone):
+ print("%s connection with %s closed cleanly" % (self.__class__.__name__, self.peerAddr))
+ else:
+ print("%s connection with %s lost: %s" % (self.__class__.__name__, self.peerAddr, reason.value))
+
+ def isAuth(self, is_auth):
+ self.transport.write("%d\r\n" % (is_auth, ))
+
+ def lineReceived(self, line):
+ try:
+ values = line.strip().split()
+
+ if len(values) < 1:
+ pass
+
+ if values[0] == 'isauth' and len(values) > 1:
+ for key in values[1:]:
+ self.isAuth(self.inspector.isauth(key))
+
+ elif values[0] == 'quit':
+ self.transport.loseConnection()
+ else:
+ raise Exception('Invalid command')
+ except Exception as inst:
+ print type(inst)
+ print inst.args
+ print inst
+ print('invalid line received from client %s' % self.peerAddr)
+ return
+
+def startListener(interface, port, protocol):
+ factory = Factory()
+ factory.protocol = protocol
+ return reactor.listenTCP( int(port), factory, interface=interface )
+
+startListener(INSPECTOR_INTERFACE, INSPECTOR_PORT, InspectorReceiver)
+
+reactor.run()
View
@@ -1,65 +1,33 @@
-from twisted.internet import reactor
-from twisted.internet.protocol import Factory
-from twisted.internet.error import ConnectionDone
-from twisted.protocols.basic import LineOnlyReceiver
import memcache
-import sys
from PHPUnserialize import *
from conf import *
-mc = memcache.Client(sessServers, debug=0)
-
-class InspectorReceiver(LineOnlyReceiver):
- delimiter = '\r\n'
-
- def connectionMade(self):
- self.peer = self.transport.getPeer()
- self.peerAddr = "%s:%d" % (self.peer.host, self.peer.port)
- print("%s connection with %s established" % (self.__class__.__name__, self.peerAddr))
-
- def connectionLost(self, reason):
- if reason.check(ConnectionDone):
- print("%s connection with %s closed cleanly" % (self.__class__.__name__, self.peerAddr))
- else:
- print("%s connection with %s lost: %s" % (self.__class__.__name__, self.peerAddr, reason.value))
+class SessionInspector():
- def isAuth(self, is_auth):
- self.transport.write("%d\r\n" % (is_auth, ))
+ def __init__(self):
+ global prefix, auth_key
+ self.prefix = prefix
+ self.auth_key = auth_key
- def lineReceived(self, line):
- global mc, prefix, auth_key
-
- try:
- values = line.strip().split()
-
- if len(values) < 1:
- pass
-
- if values[0] == 'isauth' and len(values) > 1:
- for key in values[1:]:
- data = mc.get(prefix + key)
- if not data:
- self.isAuth(False)
- else:
- try:
- session = PHPUnserialize().session_decode(data)
- self.isAuth(session[auth_key])
- except:
- self.isAuth(False)
-
- elif values[0] == 'quit':
- self.transport.loseConnection()
- else:
- raise Exception('Invalid command')
- except:
- print('invalid line received from client %s' % self.peerAddr)
- return
+ def getData(self, key):
+ raise NotImplementedError("getData is an abstract method")
-def startListener(interface, port, protocol):
- factory = Factory()
- factory.protocol = protocol
- return reactor.listenTCP( int(port), factory, interface=interface )
-
-startListener(INSPECTOR_INTERFACE, INSPECTOR_PORT, InspectorReceiver)
+ def isauth(self, key):
+ data = self.getData(key)
+ if not data:
+ return False
+ else:
+ try:
+ session = PHPUnserialize().session_decode(data)
+ return session[self.auth_key]
+ except:
+ return False
+
+class SessionInspectorMemcache(SessionInspector):
+ def __init__(self):
+ SessionInspector.__init__(self)
+ global sessServers
+ self.mc = memcache.Client(sessServers, debug=0)
-reactor.run()
+ def getData(self, key):
+ return self.mc.get(self.prefix + key)

0 comments on commit 1fd6655

Please sign in to comment.