root/trunk/jjigw/spidentd.py

Revision 166, 3.1 kB (checked in by jajcus, 1 month ago)

- missing import
- use logging module instead of stderr

Line 
1 #!/usr/bin/python -u
2 #
3 #  Jajcus' Jabber to IRC Gateway
4 #  Copyright (C) 2004  Jacek Konieczny <jajcus@jajcus.net>
5 #
6 #  This program is free software; you can redistribute it and/or modify
7 #  it under the terms of the GNU General Public License as published by
8 #  the Free Software Foundation; either version 2 of the License, or
9 #  (at your option) any later version.
10 #
11 #  This program is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #  GNU General Public License for more details.
15 #
16 #  You should have received a copy of the GNU General Public License along
17 #  with this program; if not, write to the Free Software Foundation, Inc.,
18 #  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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 # vi: sts=4 et sw=4
88
Note: See TracBrowser for help on using the browser.