2012-08-14 03:51:55 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2014-08-30 17:15:14 +00:00
|
|
|
# Nemubot is a smart and modulable IM bot.
|
|
|
|
# Copyright (C) 2012-2014 nemunaire
|
2012-08-14 03:51:55 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2014-08-30 17:15:14 +00:00
|
|
|
import io
|
2014-08-14 10:49:38 +00:00
|
|
|
import logging
|
2012-04-09 02:19:39 +00:00
|
|
|
import socket
|
2014-08-30 17:15:14 +00:00
|
|
|
import queue
|
2012-04-09 02:19:39 +00:00
|
|
|
|
2014-08-30 17:15:14 +00:00
|
|
|
# Lists for select
|
|
|
|
_rlist = []
|
|
|
|
_wlist = []
|
|
|
|
_xlist = []
|
2012-04-09 02:19:39 +00:00
|
|
|
|
2014-08-30 17:15:14 +00:00
|
|
|
# Extends from IOBase in order to be compatible with select function
|
|
|
|
class AbstractServer(io.IOBase):
|
2012-08-30 13:29:11 +00:00
|
|
|
|
2014-08-30 17:15:14 +00:00
|
|
|
"""An abstract server: handle communication with an IM server"""
|
2014-08-14 10:49:38 +00:00
|
|
|
|
2014-08-30 17:15:14 +00:00
|
|
|
def __init__(self, send_callback=None):
|
|
|
|
"""Initialize an abstract server
|
2012-05-21 10:21:14 +00:00
|
|
|
|
2014-08-30 17:15:14 +00:00
|
|
|
Keyword argument:
|
|
|
|
send_callback -- Callback when developper want to send a message
|
|
|
|
"""
|
2012-07-03 11:00:16 +00:00
|
|
|
|
2014-09-21 17:46:18 +00:00
|
|
|
self.logger = logging.getLogger("nemubot.server." + self.id)
|
2014-08-30 17:15:14 +00:00
|
|
|
self._sending_queue = queue.Queue()
|
|
|
|
if send_callback is not None:
|
|
|
|
self._send_callback = send_callback
|
2012-07-22 23:23:04 +00:00
|
|
|
else:
|
2014-08-30 17:15:14 +00:00
|
|
|
self._send_callback = self._write_select
|
|
|
|
|
|
|
|
|
|
|
|
def open(self):
|
|
|
|
"""Generic open function that register the server un _rlist in case of successful _open"""
|
|
|
|
if self._open():
|
|
|
|
_rlist.append(self)
|
2014-09-08 00:26:50 +00:00
|
|
|
_xlist.append(self)
|
2014-08-30 17:15:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
"""Generic close function that register the server un _{r,w,x}list in case of successful _close"""
|
|
|
|
if self._close():
|
|
|
|
if self in _rlist:
|
|
|
|
_rlist.remove(self)
|
|
|
|
if self in _wlist:
|
|
|
|
_wlist.remove(self)
|
|
|
|
if self in _xlist:
|
|
|
|
_xlist.remove(self)
|
|
|
|
|
|
|
|
|
|
|
|
def write(self, message):
|
|
|
|
"""Send a message to the server using send_callback"""
|
|
|
|
self._send_callback(message)
|
|
|
|
|
|
|
|
def write_select(self):
|
|
|
|
"""Internal function used by the select function"""
|
|
|
|
try:
|
2014-08-31 08:51:44 +00:00
|
|
|
_wlist.remove(self)
|
2014-08-30 17:15:14 +00:00
|
|
|
while not self._sending_queue.empty():
|
|
|
|
self._write(self._sending_queue.get_nowait())
|
2014-08-31 08:51:44 +00:00
|
|
|
self._sending_queue.task_done()
|
2014-08-30 17:15:14 +00:00
|
|
|
|
|
|
|
except queue.Empty:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _write_select(self, message):
|
|
|
|
"""Send a message to the server safely through select"""
|
|
|
|
self._sending_queue.put(self.format(message))
|
|
|
|
self.logger.debug("Message '%s' appended to Queue", message)
|
|
|
|
if self not in _wlist:
|
|
|
|
_wlist.append(self)
|
2014-09-08 00:26:50 +00:00
|
|
|
|
|
|
|
def exception(self):
|
|
|
|
"""Exception occurs in fd"""
|
|
|
|
print("Unhandle file descriptor exception on server " + self.id)
|
2014-10-05 16:19:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
def send_response(self, response):
|
|
|
|
"""Send a formated Message class"""
|
|
|
|
if response is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
elif isinstance(response, list):
|
|
|
|
for r in response:
|
|
|
|
self.send_response(r)
|
|
|
|
|
|
|
|
else:
|
|
|
|
vprnt = self.printer()
|
|
|
|
response.accept(vprnt)
|
|
|
|
self.write(vprnt.pp)
|