|
Revision 166, 3.1 kB
(checked in by jajcus, 1 month ago)
|
- missing import
- use logging module instead of stderr
|
| Line | |
|---|
| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
import Queue |
|---|
| 21 |
import threading |
|---|
| 22 |
import socket |
|---|
| 23 |
import logging |
|---|
| 24 |
import time |
|---|
| 25 |
|
|---|
| 26 |
class SPIdentD: |
|---|
| 27 |
def __init__(self,component,config): |
|---|
| 28 |
self.__logger=logging.getLogger("jjigw.SPIdentD") |
|---|
| 29 |
self.socket_path=config.socket |
|---|
| 30 |
self.component=component |
|---|
| 31 |
self.socket=None |
|---|
| 32 |
self.queue=Queue.Queue(100) |
|---|
| 33 |
self.thread=threading.Thread(target=self.run_thread) |
|---|
| 34 |
self.thread.setDaemon(1) |
|---|
| 35 |
self.thread.start() |
|---|
| 36 |
|
|---|
| 37 |
def run_thread(self): |
|---|
| 38 |
while not self.component.shutdown: |
|---|
| 39 |
self.socket=socket.socket(socket.AF_UNIX) |
|---|
| 40 |
try: |
|---|
| 41 |
try: |
|---|
| 42 |
self.socket.connect(self.socket_path) |
|---|
| 43 |
self.loop() |
|---|
| 44 |
except socket.error: |
|---|
| 45 |
self.__logger.exception("Exception cought for path \"%s\":" % (self.socket_path)) |
|---|
| 46 |
pass |
|---|
| 47 |
finally: |
|---|
| 48 |
try: |
|---|
| 49 |
self.socket.close() |
|---|
| 50 |
except: |
|---|
| 51 |
pass |
|---|
| 52 |
self.socket=None |
|---|
| 53 |
if not self.component.shutdown: |
|---|
| 54 |
self.__logger.info("Waiting before spidentd connection restart...") |
|---|
| 55 |
time.sleep(10) |
|---|
| 56 |
|
|---|
| 57 |
def loop(self): |
|---|
| 58 |
while not self.component.shutdown: |
|---|
| 59 |
try: |
|---|
| 60 |
item=self.queue.get(1,1) |
|---|
| 61 |
except Queue.Empty: |
|---|
| 62 |
continue |
|---|
| 63 |
while item: |
|---|
| 64 |
try: |
|---|
| 65 |
if item[0]=="add": |
|---|
| 66 |
ci=item[1] |
|---|
| 67 |
self.socket.send("add %s:%i %s:%i %s\n" % ( |
|---|
| 68 |
ci.localip,ci.localport,ci.remoteip,ci.remoteport,ci.user)) |
|---|
| 69 |
elif item[0]=="remove": |
|---|
| 70 |
ci=item[1] |
|---|
| 71 |
self.socket.send("remove %s:%i %s:%i\n" % ( |
|---|
| 72 |
ci.localip,ci.localport,ci.remoteip,ci.remoteport)) |
|---|
| 73 |
except socket.error: |
|---|
| 74 |
self.queue.put(item) |
|---|
| 75 |
raise |
|---|
| 76 |
try: |
|---|
| 77 |
item=self.queue.get(0) |
|---|
| 78 |
except Queue.Empty: |
|---|
| 79 |
break |
|---|
| 80 |
|
|---|
| 81 |
def register_connection(self,conninfo): |
|---|
| 82 |
self.queue.put(("add",conninfo)) |
|---|
| 83 |
|
|---|
| 84 |
def unregister_connection(self,conninfo): |
|---|
| 85 |
self.queue.put(("remove",conninfo)) |
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|