1 # -*- coding: utf-8 -*- |
1 # Copyright (c) 2012 netzguerilla.net <iro@netzguerilla.net> |
2 #Copyright (C) 2009 Sandro Knauß <bugs@sandroknauss.de> |
2 # |
|
3 # This file is part of Iro. |
|
4 # |
|
5 # Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
6 # this software and associated documentation files (the "Software"), to deal in |
|
7 # the Software without restriction, including without limitation the rights to use, |
|
8 # copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
|
9 # #Software, and to permit persons to whom the Software is furnished to do so, |
|
10 # subject to the following conditions: |
|
11 # |
|
12 # The above copyright notice and this permission notice shall be included in |
|
13 # all copies or substantial portions of the Software. |
|
14 # |
|
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
|
16 # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
|
17 # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
18 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
|
19 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
|
20 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
3 |
21 |
4 #This program is free software; you can redistribute it and/or modify it under the terms |
22 from twisted.application.service import Service, MultiService |
5 #of the GNU General Public License as published by the Free Software Foundation; |
23 from twisted.application import internet |
6 #either version 3 of the License, or any later version. |
24 from twisted.web import resource, server |
7 #This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
25 from twisted.internet import reactor |
8 #without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
26 from twisted.python import log |
9 #See the GNU General Public License for more details. |
|
10 |
27 |
11 #You should have received a copy of the GNU General Public License |
28 from sqlalchemy import create_engine, pool |
12 #along with this program; if not, see <http://www.gnu.org/licenses/>. |
|
13 |
29 |
14 import multiprocessing, logging |
30 import config, install |
15 #logging anfangen |
31 from .view import xmlrpc, jsonrpc, jsonresource |
16 logger=logging.getLogger("iro") |
32 from .model import setEngine, setPool |
17 logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)s(%(processName)s)-%(levelname)s: %(message)s') |
33 from .controller.pool import startPool, dbPool |
|
34 |
|
35 class IroService(Service): |
|
36 def startService(self): |
|
37 log.msg("Starting service...") |
|
38 engine = create_engine(config.main.dburl, |
|
39 poolclass = pool.SingletonThreadPool, pool_size=dbPool.maxthreads, pool_recycle=3600) |
|
40 |
|
41 setEngine(engine) |
|
42 startPool(reactor) |
|
43 setPool(dbPool) |
|
44 reactor.callWhenRunning(config.readConfig) |
|
45 Service.startService(self) |
18 |
46 |
19 |
47 |
20 # Server code |
48 def makeService(cfg): |
21 from xmlrpc import SecureUserDBXMLRPCServer,UserDB |
49 top_service = MultiService() |
|
50 config.confFiles.insert(0, cfg["config"]) |
|
51 if not install.checkConfig(): |
|
52 log.err("You can create a sample configuration file by running iro-install") |
|
53 raise Exception("Please update or create your configuration file %s." % cfg["config"]) |
|
54 config.init() |
22 |
55 |
23 from user import User, Admin |
56 if not install.checkDatabaseConnection(): |
24 import anbieter |
57 raise Exception("Can't connect to database") |
25 import ConfigParser |
58 |
|
59 if not install.checkDatabase(): |
|
60 raise Exception("Database not in right format. Please run iro-install --install") |
26 |
61 |
27 from job import SMSJob, FAXJob, MailJob |
62 routes = [ s for s in config.configParser.sections() if not s in ["main",]] |
28 from joblist import Joblist |
63 ao = install.getAllRoutes(routes, False) |
29 from providerlist import Providerlist |
64 for o in ao["orphand"]: |
30 from acounting import Acounting |
65 log.msg("Offer(%s) is orphand (no route using this offer)."%o) |
|
66 if ao["added"]: |
|
67 raise Exception("offerlist is not up-to-date.\nPlease run iro-install --update") |
31 |
68 |
32 class MyUserDB(UserDB): |
69 root = resource.Resource() |
33 def __init__(self, userlist,jobqueue): |
70 xmlrpc.appendResource(root) |
34 UserDB.__init__(self, None,userlist,jobqueue) |
71 jsonrpc.appendResource(root) |
35 |
72 jsonresource.appendResource(root) |
36 def createUser(self, user): |
73 |
37 self.userlist[self.createHash(user)]=user["class"](user["name"],self.jobqueue) |
74 v2 = resource.Resource() |
|
75 xmlrpc.appendResource(v2) |
|
76 jsonrpc.appendResource(v2) |
|
77 jsonresource.appendResource(v2) |
|
78 root.putChild('1.0a', v2) |
38 |
79 |
39 |
80 internet.TCPServer(config.main.port, server.Site(root)).setServiceParent(top_service) |
40 |
81 IroService().setServiceParent(top_service) |
41 |
82 return top_service |
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 |
|