1 # -*- coding: utf-8 -*- |
|
2 #Copyright (C) 2009 Sandro Knauß <bugs@sandroknauss.de> |
|
3 |
|
4 #This program is free software; you can redistribute it and/or modify it under the terms |
|
5 #of the GNU General Public License as published by the Free Software Foundation; |
|
6 #either version 3 of the License, or any later version. |
|
7 #This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
|
8 #without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
9 #See the GNU General Public License for more details. |
|
10 |
|
11 #You should have received a copy of the GNU General Public License |
|
12 #along with this program; if not, see <http://www.gnu.org/licenses/>. |
|
13 |
|
14 import multiprocessing, logging |
|
15 #logging anfangen |
|
16 logger=logging.getLogger("iro") |
|
17 logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)s(%(processName)s)-%(levelname)s: %(message)s') |
|
18 |
|
19 |
|
20 # Server code |
|
21 from xmlrpc import SecureUserDBXMLRPCServer,UserDB |
|
22 |
|
23 from user import User, Admin |
|
24 import anbieter |
|
25 import ConfigParser |
|
26 |
|
27 from job import SMSJob, FAXJob, MailJob |
|
28 from joblist import Joblist |
|
29 from providerlist import Providerlist |
|
30 from acounting import Acounting |
|
31 |
|
32 class MyUserDB(UserDB): |
|
33 def __init__(self, userlist,jobqueue): |
|
34 UserDB.__init__(self, None,userlist,jobqueue) |
|
35 |
|
36 def createUser(self, user): |
|
37 self.userlist[self.createHash(user)]=user["class"](user["name"],self.jobqueue) |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 class MySipgate(anbieter.sipgate): |
|
43 |
|
44 def __init__(self,user="",password="" ): |
|
45 anbieter.sipgate.__init__(self, user, password) |
|
46 |
|
47 def setJob(self, job): |
|
48 self.job=job |
|
49 |
|
50 def updateStatus(self, arranged=None, failed=None): |
|
51 if arranged: |
|
52 self.job.addGood(arranged) |
|
53 |
|
54 if failed: |
|
55 self.job.addFailed(failed) |
|
56 |
|
57 class MySMTP(anbieter.SMTP): |
|
58 |
|
59 def __init__(self,config_filename=None,section="smtp"): |
|
60 anbieter.SMTP.__init__(self,config_filename,section) |
|
61 |
|
62 def setJob(self, job): |
|
63 self.job=job |
|
64 |
|
65 def updateStatus(self, arranged=None, failed=None): |
|
66 if arranged: |
|
67 self.job.addGood(arranged) |
|
68 |
|
69 if failed: |
|
70 self.job.addFailed(failed) |
|
71 |
|
72 class MySmstrade(anbieter.smstrade): |
|
73 |
|
74 def __init__(self ): |
|
75 anbieter.smstrade.__init__(self ) |
|
76 |
|
77 def setJob(self, job): |
|
78 self.job=job |
|
79 |
|
80 def updateStatus(self, arranged=None, failed=None): |
|
81 if arranged: |
|
82 self.job.addGood(arranged) |
|
83 |
|
84 if failed: |
|
85 self.job.addFailed(failed) |
|
86 |
|
87 |
|
88 |
|
89 def start(userlist): |
|
90 from multiprocessing import Queue |
|
91 from multiprocessing.managers import BaseManager |
|
92 |
|
93 |
|
94 class MyManager(BaseManager): |
|
95 pass |
|
96 |
|
97 MyManager.register('SMSJob', SMSJob) |
|
98 MyManager.register('FaxJob', FAXJob) |
|
99 MyManager.register('MailJob',MailJob) |
|
100 MyManager.register('Providerlist',Providerlist) |
|
101 MyManager.register('Acounting',Acounting) |
|
102 manager = MyManager() |
|
103 manager.start() |
|
104 |
|
105 |
|
106 conf=["iro.conf", "~/iro.conf","/etc/iro/iro.conf"] |
|
107 |
|
108 #anbieter erzeugen und konfigurieren |
|
109 |
|
110 sip=MySipgate() |
|
111 sip.read_basic_config(conf) |
|
112 |
|
113 localhost=MySMTP() |
|
114 localhost.read_basic_config(conf) |
|
115 |
|
116 smstrade=MySmstrade() |
|
117 smstrade.read_basic_config(conf) |
|
118 |
|
119 cp = ConfigParser.ConfigParser() |
|
120 cp.read(conf) |
|
121 dbconn={'type':cp.get('db', 'type'), |
|
122 'host':cp.get('db', 'host'), |
|
123 'db':cp.get('db', 'db'), |
|
124 'user':cp.get('db', 'user'), |
|
125 'passwd':cp.get('db', 'passwd'), |
|
126 'table':cp.get('db', 'table'), |
|
127 } |
|
128 |
|
129 |
|
130 #Benutzerdatenbank erstellen |
|
131 queue = Queue() |
|
132 provider=manager.Providerlist() |
|
133 provider.add("sipgate", sip, ["sms", "fax", ]) |
|
134 provider.add("smstrade", smstrade, ["sms", ]) |
|
135 #provider.add("geonet", None, ["sms", "fax", ]) |
|
136 #provider.add("fax.de", None, ["sms", "fax", ]) |
|
137 provider.add("localhost", localhost, ["mail", ]) |
|
138 provider.setDefault("sms","smstrade") |
|
139 provider.setDefault("fax","sipgate") |
|
140 provider.setDefault("mail","localhost") |
|
141 jobqueue=Joblist(manager, queue, provider,dbconn) |
|
142 |
|
143 userdb=MyUserDB(userlist,jobqueue) |
|
144 |
|
145 #working thread erstellen |
|
146 from worker import Worker |
|
147 worker=Worker(queue) |
|
148 worker.start() |
|
149 |
|
150 #Server starten |
|
151 cp = ConfigParser.ConfigParser() |
|
152 cp.read(conf) |
|
153 cert=cp.get('server', 'cert') |
|
154 key=cp.get('server', 'key') |
|
155 server = SecureUserDBXMLRPCServer(addr=("localhost", 8000), |
|
156 userdb=userdb, |
|
157 certificate=cert,privatekey=key) |
|
158 server.relam="xmlrpc" |
|
159 |
|
160 logger.info('Server gestartet...') |
|
161 try: |
|
162 server.serve_forever() |
|
163 except KeyboardInterrupt: |
|
164 pass |
|
165 except: |
|
166 logger.exception('Äh, ein Fehler ist aufgetreten') |
|
167 finally: |
|
168 logger.info('Server wird beendet...') |
|
169 queue.close() |
|
170 worker.terminate() |
|
171 |
|
172 if __name__ == '__main__': |
|
173 userlist=[{"name":"test","password":"test", "class":User}, |
|
174 {"name":"test2","password":"test2", "class": Admin}] |
|
175 start(userlist) |
|
176 |
|
177 |
|