[IMP] Added YAML for demo data.
[odoo/odoo.git] / bin / service / netrpc_server.py
1 # -*- coding: utf-8 -*-
2
3 #
4 # Copyright P. Christeas <p_christ@hol.gr> 2008,2009
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
19 #
20 ##############################################################################
21
22 """ This file contains instance of the net-rpc server
23
24     
25 """
26 import netsvc
27 import threading
28 import tools
29 import os
30 import socket
31
32 import tiny_socket
33 class TinySocketClientThread(threading.Thread, netsvc.OpenERPDispatcher):
34     def __init__(self, sock, threads):
35         threading.Thread.__init__(self)
36         self.sock = sock
37         # Only at the server side, use a big timeout: close the
38         # clients connection when they're idle for 20min.
39         self.sock.settimeout(1200)
40         self.threads = threads
41
42     def __del__(self):
43         if self.sock:
44             try:
45                 self.socket.shutdown(
46                     getattr(socket, 'SHUT_RDWR', 2))
47             except: pass
48             # That should garbage-collect and close it, too
49             self.sock = None
50
51     def run(self):
52         # import select
53         self.running = True
54         try:
55             ts = tiny_socket.mysocket(self.sock)
56         except:
57             self.threads.remove(self)
58             self.running = False
59             return False
60         while self.running:
61             try:
62                 msg = ts.myreceive()
63             except:
64                 self.threads.remove(self)
65                 self.running = False
66                 return False
67             try:
68                 result = self.dispatch(msg[0], msg[1], msg[2:])
69                 ts.mysend(result)
70             except netsvc.OpenERPDispatcherException, e:
71                 try:
72                     new_e = Exception(tools.exception_to_unicode(e.exception)) # avoid problems of pickeling
73                     ts.mysend(new_e, exception=True, traceback=e.traceback)
74                 except:
75                     self.running = False
76                     break
77             except Exception, e:
78                 # this code should not be reachable, therefore we warn
79                 netsvc.Logger().notifyChannel("net-rpc", netsvc.LOG_WARNING, "exception: %s" % str(e))
80                 break
81
82         self.threads.remove(self)
83         self.running = False
84         return True
85
86     def stop(self):
87         self.running = False
88
89
90 class TinySocketServerThread(threading.Thread,netsvc.Server):
91     def __init__(self, interface, port, secure=False):
92         threading.Thread.__init__(self, name="Net-RPC socket")
93         netsvc.Server.__init__(self)
94         self.__port = port
95         self.__interface = interface
96         self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
97         self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
98         self.socket.bind((self.__interface, self.__port))
99         self.socket.listen(5)
100         self.threads = []
101         netsvc.Logger().notifyChannel("web-services", netsvc.LOG_INFO, 
102                          "starting NET-RPC service at %s port %d" % (interface or '0.0.0.0', port,))
103
104     def run(self):
105         # import select
106         try:
107             self.running = True
108             while self.running:
109                 (clientsocket, address) = self.socket.accept()
110                 ct = TinySocketClientThread(clientsocket, self.threads)
111                 clientsocket = None
112                 self.threads.append(ct)
113                 ct.start()
114                 lt = len(self.threads)
115                 if (lt > 10) and (lt % 10 == 0):
116                      # Not many threads should be serving at the same time, so log
117                      # their abuse.
118                      netsvc.Logger().notifyChannel("web-services", netsvc.LOG_DEBUG,
119                         "Netrpc: %d threads" % len(self.threads))
120             self.socket.close()
121         except Exception, e:
122             netsvc.Logger().notifyChannel("web-services", netsvc.LOG_WARNING,
123                         "Netrpc: closing because of exception %s" % str(e))
124             self.socket.close()
125             return False
126
127     def stop(self):
128         self.running = False
129         for t in self.threads:
130             t.stop()
131         try:
132             self.socket.shutdown(
133                 getattr(socket, 'SHUT_RDWR', 2))
134             self.socket.close()
135         except:
136             return False
137
138     def stats(self):
139         res = "Net-RPC: " + ( (self.running and "running") or  "stopped")
140         i = 0
141         for t in self.threads:
142             i += 1
143             res += "\nNet-RPC #%d: %s " % (i, t.name)
144             if t.isAlive():
145                 res += "running"
146             else:
147                 res += "finished"
148             if t.sock:
149                 res += ", socket"
150         return res
151
152 netrpcd = None
153
154 def init_servers():
155     global netrpcd
156     if tools.config.get_misc('netrpcd','enable', True):
157         netrpcd = TinySocketServerThread(tools.config.get_misc('netrpcd','interface', ''), \
158             int(tools.config.get_misc('netrpcd','port', tools.config.get('netport', 8070))))