2009-11-27 07:23:48 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2009-09-03 13:00:38 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Copyright P. Christeas <p_christ@hol.gr> 2008,2009
|
2009-11-27 07:23:48 +00:00
|
|
|
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
|
2009-09-03 13:00:38 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2009-11-27 07:23:48 +00:00
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
2009-09-03 13:00:38 +00:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2009-11-27 07:23:48 +00:00
|
|
|
# GNU Affero General Public License for more details.
|
2009-09-03 13:00:38 +00:00
|
|
|
#
|
2009-11-27 07:23:48 +00:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2013-01-18 15:23:46 +00:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-09-03 13:00:38 +00:00
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
2011-06-23 09:04:57 +00:00
|
|
|
#.apidoc title: NET-RPC Server
|
|
|
|
|
2009-09-03 13:00:38 +00:00
|
|
|
""" This file contains instance of the net-rpc server
|
|
|
|
"""
|
2010-06-16 10:15:57 +00:00
|
|
|
import logging
|
2010-05-18 15:41:23 +00:00
|
|
|
import select
|
2009-09-03 13:00:38 +00:00
|
|
|
import socket
|
2010-06-16 10:15:57 +00:00
|
|
|
import sys
|
|
|
|
import threading
|
|
|
|
import traceback
|
2011-12-15 06:46:02 +00:00
|
|
|
import openerp
|
2012-09-22 10:51:07 +00:00
|
|
|
import openerp.service.netrpc_socket
|
2011-02-07 12:57:23 +00:00
|
|
|
import openerp.netsvc as netsvc
|
|
|
|
import openerp.tools as tools
|
2010-03-18 17:55:06 +00:00
|
|
|
|
2012-01-24 15:07:50 +00:00
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
2012-12-09 18:15:36 +00:00
|
|
|
class Server:
|
|
|
|
""" Generic interface for all servers with an event loop etc.
|
|
|
|
Override this to impement http, net-rpc etc. servers.
|
|
|
|
|
|
|
|
Servers here must have threaded behaviour. start() must not block,
|
|
|
|
there is no run().
|
|
|
|
"""
|
|
|
|
__is_started = False
|
|
|
|
__servers = []
|
|
|
|
__starter_threads = []
|
|
|
|
|
|
|
|
# we don't want blocking server calls (think select()) to
|
|
|
|
# wait forever and possibly prevent exiting the process,
|
|
|
|
# but instead we want a form of polling/busy_wait pattern, where
|
|
|
|
# _server_timeout should be used as the default timeout for
|
|
|
|
# all I/O blocking operations
|
|
|
|
_busywait_timeout = 0.5
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
Server.__servers.append(self)
|
|
|
|
if Server.__is_started:
|
|
|
|
# raise Exception('All instances of servers must be inited before the startAll()')
|
|
|
|
# Since the startAll() won't be called again, allow this server to
|
|
|
|
# init and then start it after 1sec (hopefully). Register that
|
|
|
|
# timer thread in a list, so that we can abort the start if quitAll
|
|
|
|
# is called in the meantime
|
|
|
|
t = threading.Timer(1.0, self._late_start)
|
|
|
|
t.name = 'Late start timer for %s' % str(self.__class__)
|
|
|
|
Server.__starter_threads.append(t)
|
|
|
|
t.start()
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
_logger.debug("called stub Server.start")
|
|
|
|
|
|
|
|
def _late_start(self):
|
|
|
|
self.start()
|
|
|
|
for thr in Server.__starter_threads:
|
|
|
|
if thr.finished.is_set():
|
|
|
|
Server.__starter_threads.remove(thr)
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
_logger.debug("called stub Server.stop")
|
|
|
|
|
|
|
|
def stats(self):
|
|
|
|
""" This function should return statistics about the server """
|
|
|
|
return "%s: No statistics" % str(self.__class__)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def startAll(cls):
|
|
|
|
if cls.__is_started:
|
|
|
|
return
|
|
|
|
_logger.info("Starting %d services" % len(cls.__servers))
|
|
|
|
for srv in cls.__servers:
|
|
|
|
srv.start()
|
|
|
|
cls.__is_started = True
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def quitAll(cls):
|
|
|
|
if not cls.__is_started:
|
|
|
|
return
|
|
|
|
_logger.info("Stopping %d services" % len(cls.__servers))
|
|
|
|
for thr in cls.__starter_threads:
|
|
|
|
if not thr.finished.is_set():
|
|
|
|
thr.cancel()
|
|
|
|
cls.__starter_threads.remove(thr)
|
|
|
|
|
|
|
|
for srv in cls.__servers:
|
|
|
|
srv.stop()
|
|
|
|
cls.__is_started = False
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def allStats(cls):
|
|
|
|
res = ["Servers %s" % ('stopped', 'started')[cls.__is_started]]
|
|
|
|
res.extend(srv.stats() for srv in cls.__servers)
|
|
|
|
return '\n'.join(res)
|
|
|
|
|
|
|
|
def _close_socket(self):
|
2012-12-10 13:21:33 +00:00
|
|
|
netsvc.close_socket(self.socket)
|
2012-12-09 18:15:36 +00:00
|
|
|
|
2011-09-02 14:12:32 +00:00
|
|
|
class TinySocketClientThread(threading.Thread):
|
2009-09-03 13:00:38 +00:00
|
|
|
def __init__(self, sock, threads):
|
2010-07-26 09:33:36 +00:00
|
|
|
spn = sock and sock.getpeername()
|
|
|
|
spn = 'netrpc-client-%s:%s' % spn[0:2]
|
|
|
|
threading.Thread.__init__(self, name=spn)
|
2009-09-03 13:00:38 +00:00
|
|
|
self.sock = sock
|
2009-10-28 00:40:02 +00:00
|
|
|
# Only at the server side, use a big timeout: close the
|
|
|
|
# clients connection when they're idle for 20min.
|
|
|
|
self.sock.settimeout(1200)
|
2009-09-03 13:00:38 +00:00
|
|
|
self.threads = threads
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
self.running = True
|
|
|
|
try:
|
2012-09-22 10:51:07 +00:00
|
|
|
ts = openerp.server.netrpc_socket.mysocket(self.sock)
|
2010-06-24 13:43:32 +00:00
|
|
|
except Exception:
|
2009-09-03 13:00:38 +00:00
|
|
|
self.threads.remove(self)
|
2009-10-28 00:40:02 +00:00
|
|
|
self.running = False
|
2009-09-03 13:00:38 +00:00
|
|
|
return False
|
2010-06-16 10:15:57 +00:00
|
|
|
|
2009-09-03 13:00:38 +00:00
|
|
|
while self.running:
|
|
|
|
try:
|
|
|
|
msg = ts.myreceive()
|
2011-09-29 07:17:13 +00:00
|
|
|
result = netsvc.dispatch_rpc(msg[0], msg[1], msg[2:])
|
2009-09-03 13:00:38 +00:00
|
|
|
ts.mysend(result)
|
2010-06-16 10:15:57 +00:00
|
|
|
except socket.timeout:
|
|
|
|
#terminate this channel because other endpoint is gone
|
|
|
|
break
|
2009-10-27 20:12:44 +00:00
|
|
|
except Exception, e:
|
2010-06-16 10:15:57 +00:00
|
|
|
try:
|
2013-01-18 15:23:46 +00:00
|
|
|
valid_exception = Exception(netrpc_handle_exception_legacy(e))
|
2011-12-15 06:46:02 +00:00
|
|
|
valid_traceback = getattr(e, 'traceback', sys.exc_info())
|
|
|
|
formatted_traceback = "".join(traceback.format_exception(*valid_traceback))
|
2012-01-24 15:07:50 +00:00
|
|
|
_logger.debug("netrpc: communication-level exception", exc_info=True)
|
2011-12-15 06:46:02 +00:00
|
|
|
ts.mysend(valid_exception, exception=True, traceback=formatted_traceback)
|
2011-06-06 07:48:56 +00:00
|
|
|
break
|
2010-06-16 10:15:57 +00:00
|
|
|
except Exception, ex:
|
|
|
|
#terminate this channel if we can't properly send back the error
|
2012-01-24 15:07:50 +00:00
|
|
|
_logger.exception("netrpc: cannot deliver exception message to client")
|
2010-06-16 10:15:57 +00:00
|
|
|
break
|
2009-09-03 13:00:38 +00:00
|
|
|
|
2011-06-16 10:26:24 +00:00
|
|
|
netsvc.close_socket(self.sock)
|
2011-06-06 07:48:56 +00:00
|
|
|
self.sock = None
|
2009-10-27 20:12:44 +00:00
|
|
|
self.threads.remove(self)
|
2009-10-28 00:40:02 +00:00
|
|
|
self.running = False
|
2009-10-27 20:12:44 +00:00
|
|
|
return True
|
2009-09-03 13:00:38 +00:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.running = False
|
2013-01-18 15:23:46 +00:00
|
|
|
|
2011-12-15 06:46:02 +00:00
|
|
|
def netrpc_handle_exception_legacy(e):
|
|
|
|
if isinstance(e, openerp.osv.osv.except_osv):
|
|
|
|
return 'warning -- ' + e.name + '\n\n' + e.value
|
|
|
|
if isinstance(e, openerp.exceptions.Warning):
|
|
|
|
return 'warning -- Warning\n\n' + str(e)
|
2013-01-18 15:23:46 +00:00
|
|
|
if isinstance(e, openerp.exceptions.WarningConfig):
|
|
|
|
return 'warning -- Warning\n\n' + str(e)
|
2011-12-15 06:46:02 +00:00
|
|
|
if isinstance(e, openerp.exceptions.AccessError):
|
|
|
|
return 'warning -- AccessError\n\n' + str(e)
|
|
|
|
if isinstance(e, openerp.exceptions.AccessDenied):
|
|
|
|
return 'AccessDenied ' + str(e)
|
|
|
|
return openerp.tools.exception_to_unicode(e)
|
2009-09-03 13:00:38 +00:00
|
|
|
|
2012-12-09 18:15:36 +00:00
|
|
|
class TinySocketServerThread(threading.Thread,Server):
|
2009-09-03 13:00:38 +00:00
|
|
|
def __init__(self, interface, port, secure=False):
|
2010-09-22 11:46:31 +00:00
|
|
|
threading.Thread.__init__(self, name="NetRPCDaemon-%d"%port)
|
2012-12-09 18:15:36 +00:00
|
|
|
Server.__init__(self)
|
2009-09-03 13:00:38 +00:00
|
|
|
self.__port = port
|
|
|
|
self.__interface = interface
|
|
|
|
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
|
|
self.socket.bind((self.__interface, self.__port))
|
|
|
|
self.socket.listen(5)
|
|
|
|
self.threads = []
|
2012-01-24 17:30:17 +00:00
|
|
|
_logger.info("starting NET-RPC service on %s:%s", interface or '0.0.0.0', port)
|
2009-09-03 13:00:38 +00:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
try:
|
|
|
|
self.running = True
|
|
|
|
while self.running:
|
2010-07-13 15:22:44 +00:00
|
|
|
fd_sets = select.select([self.socket], [], [], self._busywait_timeout)
|
2010-05-18 15:41:23 +00:00
|
|
|
if not fd_sets[0]:
|
|
|
|
continue
|
2009-09-03 13:00:38 +00:00
|
|
|
(clientsocket, address) = self.socket.accept()
|
|
|
|
ct = TinySocketClientThread(clientsocket, self.threads)
|
2009-10-28 00:40:02 +00:00
|
|
|
clientsocket = None
|
2009-09-03 13:00:38 +00:00
|
|
|
self.threads.append(ct)
|
|
|
|
ct.start()
|
2009-10-27 20:12:44 +00:00
|
|
|
lt = len(self.threads)
|
|
|
|
if (lt > 10) and (lt % 10 == 0):
|
2012-01-19 19:17:56 +00:00
|
|
|
# Not many threads should be serving at the same time, so log
|
|
|
|
# their abuse.
|
2012-02-08 14:13:58 +00:00
|
|
|
_logger.debug("Netrpc: %d threads", len(self.threads))
|
2009-09-03 13:00:38 +00:00
|
|
|
self.socket.close()
|
|
|
|
except Exception, e:
|
2012-02-08 14:22:48 +00:00
|
|
|
_logger.warning("Netrpc: closing because of exception %s", e)
|
2009-09-03 13:00:38 +00:00
|
|
|
self.socket.close()
|
|
|
|
return False
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.running = False
|
|
|
|
for t in self.threads:
|
|
|
|
t.stop()
|
2010-05-18 15:41:23 +00:00
|
|
|
self._close_socket()
|
2009-09-03 13:00:38 +00:00
|
|
|
|
2009-10-28 00:40:02 +00:00
|
|
|
def stats(self):
|
2009-11-24 14:44:05 +00:00
|
|
|
res = "Net-RPC: " + ( (self.running and "running") or "stopped")
|
|
|
|
i = 0
|
|
|
|
for t in self.threads:
|
|
|
|
i += 1
|
|
|
|
res += "\nNet-RPC #%d: %s " % (i, t.name)
|
|
|
|
if t.isAlive():
|
|
|
|
res += "running"
|
|
|
|
else:
|
|
|
|
res += "finished"
|
|
|
|
if t.sock:
|
|
|
|
res += ", socket"
|
|
|
|
return res
|
2009-09-03 13:00:38 +00:00
|
|
|
|
|
|
|
netrpcd = None
|
|
|
|
|
2012-12-09 18:46:32 +00:00
|
|
|
def start_service():
|
2009-11-24 14:44:05 +00:00
|
|
|
global netrpcd
|
2010-05-31 14:17:39 +00:00
|
|
|
if tools.config.get('netrpc', False):
|
2012-12-09 18:15:36 +00:00
|
|
|
netrpcd = TinySocketServerThread(tools.config.get('netrpc_interface', ''), int(tools.config.get('netrpc_port', 8070)))
|
2011-11-22 08:58:48 +00:00
|
|
|
|
2012-12-09 18:46:32 +00:00
|
|
|
def stop_service():
|
|
|
|
Server.quitAll()
|
|
|
|
|
2011-11-22 08:58:48 +00:00
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|