2019-11-20 14:41:14 +00:00
|
|
|
VERSION: str = ""
|
|
|
|
with open("VERSION", "r") as version_file:
|
|
|
|
VERSION = "v%s" % version_file.read().strip()
|
|
|
|
SOURCE: str = "https://git.io/bitbot"
|
|
|
|
URL: str = "https://bitbot.dev"
|
|
|
|
|
2019-06-17 10:06:03 +00:00
|
|
|
import enum, queue, os, queue, select, socket, sys, threading, time, traceback
|
2019-06-06 16:05:44 +00:00
|
|
|
import typing, uuid
|
2020-02-04 15:31:17 +00:00
|
|
|
from src import Config, EventManager, Exports, IRCServer, Logging
|
|
|
|
from src import ModuleManager, PollHook, PollSource, Socket, Timers, utils
|
2018-09-28 15:51:36 +00:00
|
|
|
|
2019-05-01 16:13:41 +00:00
|
|
|
class TriggerResult(enum.Enum):
|
|
|
|
Return = 1
|
|
|
|
Exception = 2
|
2018-11-27 15:06:10 +00:00
|
|
|
|
2019-06-09 13:59:28 +00:00
|
|
|
class TriggerEventType(enum.Enum):
|
|
|
|
Action = 1
|
|
|
|
Kill = 2
|
|
|
|
class TriggerEvent(object):
|
|
|
|
def __init__(self, type: TriggerEventType,
|
|
|
|
callback: typing.Callable[[], None]=None):
|
|
|
|
self.type = type
|
|
|
|
self.callback = callback
|
|
|
|
|
2019-10-08 11:20:08 +00:00
|
|
|
class ListLambdaPollHook(PollHook.PollHook):
|
|
|
|
def __init__(self,
|
|
|
|
collection: typing.Callable[[], typing.Iterable[typing.Any]],
|
|
|
|
func: typing.Callable[[typing.Any], None]):
|
|
|
|
self._collection = collection
|
|
|
|
self._func = func
|
|
|
|
def next(self):
|
|
|
|
timeouts = [self._func(i) for i in self._collection()]
|
|
|
|
timeouts = [t for t in timeouts if t is not None]
|
|
|
|
return min(timeouts or [None])
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
class Bot(object):
|
2020-01-27 23:22:23 +00:00
|
|
|
def __init__(self, directory, data_directory, args, cache, config, database,
|
|
|
|
events, exports, log, modules, timers):
|
2018-09-29 08:24:26 +00:00
|
|
|
self.directory = directory
|
2020-01-27 23:22:23 +00:00
|
|
|
self.data_directory = data_directory
|
2018-09-28 15:51:36 +00:00
|
|
|
self.args = args
|
2018-09-29 11:53:39 +00:00
|
|
|
self.cache = cache
|
2018-09-28 15:51:36 +00:00
|
|
|
self.config = config
|
|
|
|
self.database = database
|
|
|
|
self._events = events
|
|
|
|
self._exports = exports
|
|
|
|
self.log = log
|
|
|
|
self.modules = modules
|
2018-10-12 17:07:23 +00:00
|
|
|
self._timers = timers
|
2018-09-28 15:51:36 +00:00
|
|
|
|
2018-09-11 17:24:34 +00:00
|
|
|
self.start_time = time.time()
|
2019-10-17 15:53:48 +00:00
|
|
|
self._writing = False
|
|
|
|
self._reading = False
|
2018-10-06 14:37:05 +00:00
|
|
|
self.servers = {}
|
2019-09-01 07:38:22 +00:00
|
|
|
self.reconnections = {}
|
2019-06-06 16:05:44 +00:00
|
|
|
|
2019-06-09 13:59:28 +00:00
|
|
|
self._event_queue = queue.Queue() # type: typing.Queue[TriggerEvent]
|
2019-06-06 16:05:44 +00:00
|
|
|
|
2019-06-07 14:07:31 +00:00
|
|
|
self._read_poll = select.poll()
|
|
|
|
self._write_poll = select.poll()
|
2019-06-06 16:05:44 +00:00
|
|
|
|
|
|
|
self._rtrigger_server, self._rtrigger_client = socket.socketpair()
|
2019-06-07 14:07:31 +00:00
|
|
|
self._read_poll.register(self._rtrigger_server.fileno(), select.POLLIN)
|
2019-06-06 16:05:44 +00:00
|
|
|
|
2019-06-06 18:21:18 +00:00
|
|
|
self._rtrigger_lock = threading.Lock()
|
|
|
|
self._rtriggered = False
|
2019-06-07 09:39:19 +00:00
|
|
|
self._write_condition = threading.Condition()
|
2019-06-06 18:21:18 +00:00
|
|
|
|
2019-06-06 16:05:44 +00:00
|
|
|
self._read_thread = None
|
|
|
|
self._write_thread = None
|
2018-10-06 14:45:56 +00:00
|
|
|
|
2019-10-10 12:58:45 +00:00
|
|
|
self._poll_timeouts = [] # typing.List[PollHook.PollHook]
|
2019-10-08 11:20:08 +00:00
|
|
|
self._poll_timeouts.append(ListLambdaPollHook(
|
|
|
|
lambda: self.servers.values(),
|
|
|
|
lambda server: server.until_read_timeout()))
|
|
|
|
|
|
|
|
self._poll_timeouts.append(ListLambdaPollHook(
|
|
|
|
lambda: self.servers.values(),
|
|
|
|
lambda server: server.until_next_ping()))
|
|
|
|
|
|
|
|
self._poll_timeouts.append(ListLambdaPollHook(
|
|
|
|
lambda: self.servers.values(), self._throttle_timeout))
|
|
|
|
|
2019-10-11 12:59:28 +00:00
|
|
|
self._poll_sources = [] # typing.List[PollSource.PollSource]
|
|
|
|
|
2019-10-10 12:58:45 +00:00
|
|
|
def add_poll_hook(self, hook: PollHook.PollHook):
|
|
|
|
self._poll_timeouts.append(hook)
|
2019-10-11 12:59:28 +00:00
|
|
|
def add_poll_source(self, source: PollSource.PollSource):
|
|
|
|
self._poll_sources.append(source)
|
2019-10-10 12:58:45 +00:00
|
|
|
|
2019-10-08 11:20:08 +00:00
|
|
|
def _throttle_timeout(self, server: IRCServer.Server):
|
|
|
|
if server.socket.waiting_throttled_send():
|
|
|
|
return server.socket.send_throttle_timeout()
|
|
|
|
return None
|
|
|
|
|
2019-06-06 16:58:35 +00:00
|
|
|
def _trigger_both(self):
|
|
|
|
self.trigger_read()
|
|
|
|
self.trigger_write()
|
|
|
|
def trigger_read(self):
|
2019-06-06 18:21:18 +00:00
|
|
|
with self._rtrigger_lock:
|
|
|
|
if not self._rtriggered:
|
|
|
|
self._rtriggered = True
|
|
|
|
self._rtrigger_client.send(b"TRIGGER")
|
2019-06-06 16:58:35 +00:00
|
|
|
def trigger_write(self):
|
2019-06-07 09:39:19 +00:00
|
|
|
with self._write_condition:
|
|
|
|
self._write_condition.notify()
|
2019-06-06 16:05:44 +00:00
|
|
|
|
2018-11-27 14:25:12 +00:00
|
|
|
def trigger(self,
|
2019-06-06 22:23:46 +00:00
|
|
|
func: typing.Optional[typing.Callable[[], typing.Any]]=None,
|
|
|
|
trigger_threads=True) -> typing.Any:
|
2018-11-27 14:25:12 +00:00
|
|
|
func = func or (lambda: None)
|
2019-02-25 10:36:17 +00:00
|
|
|
|
|
|
|
if utils.is_main_thread():
|
2018-11-27 14:25:12 +00:00
|
|
|
returned = func()
|
2019-06-06 22:23:46 +00:00
|
|
|
if trigger_threads:
|
|
|
|
self._trigger_both()
|
2018-11-27 14:25:12 +00:00
|
|
|
return returned
|
|
|
|
|
2020-08-26 10:32:31 +00:00
|
|
|
func_queue: queue.Queue[typing.Tuple[TriggerResult, typing.Any]
|
2020-08-26 10:30:50 +00:00
|
|
|
] = queue.Queue(1)
|
2018-11-27 14:25:12 +00:00
|
|
|
|
2019-06-06 16:05:44 +00:00
|
|
|
def _action():
|
|
|
|
try:
|
|
|
|
returned = func()
|
|
|
|
type = TriggerResult.Return
|
|
|
|
except Exception as e:
|
|
|
|
returned = e
|
|
|
|
type = TriggerResult.Exception
|
|
|
|
func_queue.put([type, returned])
|
2019-06-09 13:59:28 +00:00
|
|
|
event_item = TriggerEvent(TriggerEventType.Action, _action)
|
|
|
|
self._event_queue.put(event_item)
|
2018-11-27 14:25:12 +00:00
|
|
|
|
2018-12-10 13:34:53 +00:00
|
|
|
type, returned = func_queue.get(block=True)
|
2019-06-06 16:05:44 +00:00
|
|
|
|
2019-06-06 22:23:46 +00:00
|
|
|
if trigger_threads:
|
|
|
|
self._trigger_both()
|
2019-06-06 16:05:44 +00:00
|
|
|
|
2020-08-26 10:32:31 +00:00
|
|
|
if (type == TriggerResult.Exception and
|
|
|
|
isinstance(returned, Exception)):
|
2018-11-27 15:06:10 +00:00
|
|
|
raise returned
|
2019-05-01 16:13:41 +00:00
|
|
|
elif type == TriggerResult.Return:
|
2018-11-27 15:06:10 +00:00
|
|
|
return returned
|
2018-10-06 14:45:56 +00:00
|
|
|
|
2019-10-25 13:31:09 +00:00
|
|
|
def panic(self, reason):
|
|
|
|
exc_info = False
|
|
|
|
if any(sys.exc_info()):
|
|
|
|
exc_info = True
|
2019-06-17 10:06:03 +00:00
|
|
|
|
2019-10-25 13:31:09 +00:00
|
|
|
self.log.critical("panic() called: %s", [reason], exc_info=exc_info)
|
2019-12-10 05:26:16 +00:00
|
|
|
sys.exit(utils.consts.Exit.PANIC)
|
2019-06-17 10:06:03 +00:00
|
|
|
|
2020-02-04 15:31:17 +00:00
|
|
|
def get_config(self, name: str) -> Config.Config:
|
|
|
|
path = os.path.join(self.data_directory, "%s.conf" % name)
|
|
|
|
config = Config.Config(name, path)
|
|
|
|
config.load()
|
|
|
|
return config
|
|
|
|
|
2019-10-14 11:55:32 +00:00
|
|
|
def _module_lists(self):
|
2020-02-04 15:31:17 +00:00
|
|
|
module_lists = self.get_config("modules")
|
|
|
|
whitelist = module_lists.get_list("whitelist")
|
|
|
|
blacklist = module_lists.get_list("blacklist")
|
2019-06-01 14:06:57 +00:00
|
|
|
|
2019-12-07 11:07:43 +00:00
|
|
|
return whitelist, blacklist
|
2019-06-01 14:06:57 +00:00
|
|
|
|
2019-10-14 12:02:16 +00:00
|
|
|
def load_modules(self
|
2019-10-14 11:55:32 +00:00
|
|
|
) -> typing.Tuple[typing.List[str], typing.List[str]]:
|
|
|
|
whitelist, blacklist = self._module_lists()
|
2019-05-12 13:10:59 +00:00
|
|
|
return self.modules.load_modules(self, whitelist=whitelist,
|
2019-10-14 12:02:16 +00:00
|
|
|
blacklist=blacklist)
|
2019-10-14 11:55:32 +00:00
|
|
|
def try_reload_modules(self) -> ModuleManager.TryReloadResult:
|
|
|
|
whitelist, blacklist = self._module_lists()
|
|
|
|
return self.modules.try_reload_modules(self, whitelist=whitelist,
|
|
|
|
blacklist=blacklist)
|
2019-05-12 13:10:59 +00:00
|
|
|
|
2018-11-05 18:23:02 +00:00
|
|
|
def add_server(self, server_id: int, connect: bool = True,
|
2019-04-30 11:05:36 +00:00
|
|
|
connection_param_args: typing.Dict[str, str]={}
|
|
|
|
) -> IRCServer.Server:
|
|
|
|
connection_params = utils.irc.IRCConnectionParameters(
|
|
|
|
*self.database.servers.get(server_id))
|
|
|
|
connection_params.args = connection_param_args
|
2018-11-05 18:23:02 +00:00
|
|
|
|
|
|
|
new_server = IRCServer.Server(self, self._events,
|
2018-11-05 18:30:14 +00:00
|
|
|
connection_params.id, connection_params.alias, connection_params)
|
2018-09-19 12:25:12 +00:00
|
|
|
self._events.on("new.server").call(server=new_server)
|
2018-11-05 11:53:33 +00:00
|
|
|
|
2018-11-24 12:14:36 +00:00
|
|
|
if not connect:
|
2018-11-05 11:53:33 +00:00
|
|
|
return new_server
|
|
|
|
|
|
|
|
self.connect(new_server)
|
|
|
|
|
2016-07-05 11:18:13 +00:00
|
|
|
return new_server
|
2018-09-30 12:28:26 +00:00
|
|
|
|
2019-04-24 11:07:30 +00:00
|
|
|
def get_server_by_id(self, id: int) -> typing.Optional[IRCServer.Server]:
|
2018-09-30 12:28:26 +00:00
|
|
|
for server in self.servers.values():
|
|
|
|
if server.id == id:
|
|
|
|
return server
|
2018-10-31 15:12:46 +00:00
|
|
|
return None
|
2019-04-24 11:07:30 +00:00
|
|
|
def get_server_by_alias(self, alias: str) -> typing.Optional[IRCServer.Server]:
|
|
|
|
alias_lower = alias.lower()
|
|
|
|
for server in self.servers.values():
|
|
|
|
if server.alias.lower() == alias_lower:
|
|
|
|
return server
|
|
|
|
return None
|
2018-09-30 12:28:26 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def connect(self, server: IRCServer.Server) -> bool:
|
2016-03-29 11:56:58 +00:00
|
|
|
try:
|
|
|
|
server.connect()
|
2019-03-08 10:12:01 +00:00
|
|
|
except Exception as e:
|
2020-07-03 09:31:23 +00:00
|
|
|
ip = ""
|
|
|
|
if server.socket.connected_ip is not None:
|
|
|
|
ip = f" ({server.socket.connected_ip})"
|
|
|
|
|
|
|
|
self.log.warn("Failed to connect to %s%s: %s",
|
|
|
|
[str(server), ip, str(e)])
|
2019-10-25 13:18:22 +00:00
|
|
|
self.log.debug("Connection failure reason:", exc_info=True)
|
2016-03-29 11:56:58 +00:00
|
|
|
return False
|
2018-07-02 11:08:26 +00:00
|
|
|
self.servers[server.fileno()] = server
|
2019-06-07 14:07:31 +00:00
|
|
|
self._read_poll.register(server.fileno(), select.POLLIN)
|
2016-03-29 11:56:58 +00:00
|
|
|
return True
|
2017-01-27 21:41:47 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def get_poll_timeout(self) -> float:
|
2018-08-29 13:33:27 +00:00
|
|
|
timeouts = []
|
2019-10-08 11:20:08 +00:00
|
|
|
for poll_timeout in self._poll_timeouts:
|
|
|
|
timeouts.append(poll_timeout.next())
|
|
|
|
|
2019-06-06 22:45:33 +00:00
|
|
|
min_secs = min([timeout for timeout in timeouts if not timeout == None])
|
2019-06-24 16:22:08 +00:00
|
|
|
return max([min_secs, 0])
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def disconnect(self, server: IRCServer.Server):
|
2016-03-30 18:32:14 +00:00
|
|
|
del self.servers[server.fileno()]
|
2019-06-06 21:36:56 +00:00
|
|
|
self._trigger_both()
|
2016-03-30 18:32:14 +00:00
|
|
|
|
2019-10-08 12:49:43 +00:00
|
|
|
def _timed_reconnect(self, timer: Timers.Timer):
|
|
|
|
server_id = timer.kwargs["server_id"]
|
|
|
|
params = timer.kwargs.get("connection_params", None)
|
|
|
|
if not self.reconnect(server_id, params):
|
|
|
|
timer.redo()
|
2019-09-01 07:38:22 +00:00
|
|
|
else:
|
2019-10-08 12:49:43 +00:00
|
|
|
del self.reconnections[server_id]
|
2018-11-05 18:23:02 +00:00
|
|
|
def reconnect(self, server_id: int, connection_params: typing.Optional[
|
|
|
|
utils.irc.IRCConnectionParameters]=None) -> bool:
|
2019-05-01 16:13:55 +00:00
|
|
|
args = {} # type: typing.Dict[str, str]
|
|
|
|
if not connection_params == None:
|
|
|
|
args = typing.cast(utils.irc.IRCConnectionParameters,
|
|
|
|
connection_params).args
|
|
|
|
|
|
|
|
server = self.add_server(server_id, False, args)
|
2019-06-17 12:53:17 +00:00
|
|
|
server.reconnected = True
|
2016-03-30 18:32:14 +00:00
|
|
|
if self.connect(server):
|
|
|
|
self.servers[server.fileno()] = server
|
2018-11-05 12:27:11 +00:00
|
|
|
return True
|
|
|
|
return False
|
2016-04-14 15:48:17 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def set_setting(self, setting: str, value: typing.Any):
|
2018-08-05 21:41:38 +00:00
|
|
|
self.database.bot_settings.set(setting, value)
|
2018-10-30 14:58:48 +00:00
|
|
|
def get_setting(self, setting: str, default: typing.Any=None) -> typing.Any:
|
2018-08-05 21:41:38 +00:00
|
|
|
return self.database.bot_settings.get(setting, default)
|
2019-10-07 11:46:52 +00:00
|
|
|
def find_settings(self, pattern: str=None, prefix: str=None,
|
|
|
|
default: typing.Any=[]) -> typing.List[typing.Any]:
|
|
|
|
if not pattern == None:
|
|
|
|
return self.database.bot_settings.find(pattern, default)
|
|
|
|
elif not prefix == None:
|
|
|
|
return self.database.bot_settings.find_prefix(prefix, default)
|
|
|
|
else:
|
|
|
|
raise ValueError("Please provide 'pattern' or 'prefix'")
|
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def del_setting(self, setting: str):
|
2018-08-06 13:10:14 +00:00
|
|
|
self.database.bot_settings.delete(setting)
|
2016-04-14 15:48:17 +00:00
|
|
|
|
2019-06-06 16:05:44 +00:00
|
|
|
def _daemon_thread(self, target: typing.Callable[[], None]):
|
|
|
|
thread = threading.Thread(target=target)
|
|
|
|
thread.daemon = True
|
|
|
|
thread.start()
|
|
|
|
return thread
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
def run(self):
|
2019-10-17 15:53:48 +00:00
|
|
|
self._writing = True
|
|
|
|
self._reading = True
|
|
|
|
|
2019-06-13 16:11:46 +00:00
|
|
|
self._read_thread = self._daemon_thread(
|
2019-06-14 23:39:52 +00:00
|
|
|
lambda: self._loop_catch("read", self._read_loop))
|
2019-06-13 16:11:46 +00:00
|
|
|
self._write_thread = self._daemon_thread(
|
2019-06-14 23:39:52 +00:00
|
|
|
lambda: self._loop_catch("write", self._write_loop))
|
2019-06-06 16:05:44 +00:00
|
|
|
self._event_loop()
|
|
|
|
|
2019-10-17 15:53:48 +00:00
|
|
|
def stop(self, reason: str="Stopping"):
|
|
|
|
self._reading = False # disable read thread
|
|
|
|
self.trigger_read()
|
|
|
|
for server in self.servers.values():
|
|
|
|
line = server.send_quit(reason)
|
|
|
|
line.events.on("send").hook(self._shutdown_hook(server))
|
|
|
|
def _shutdown_hook(self, server):
|
|
|
|
def shutdown(e):
|
|
|
|
server.disconnect()
|
|
|
|
self.disconnect(server)
|
|
|
|
if not self.servers:
|
|
|
|
self._writing = False
|
|
|
|
return shutdown
|
|
|
|
|
2019-06-09 13:59:28 +00:00
|
|
|
def _kill(self):
|
2019-10-17 15:53:48 +00:00
|
|
|
self._writing = False
|
|
|
|
self._reading = False
|
2019-06-09 13:59:28 +00:00
|
|
|
self._trigger_both()
|
|
|
|
|
2019-06-06 16:05:44 +00:00
|
|
|
def _event_loop(self):
|
2019-10-17 15:53:48 +00:00
|
|
|
while ((self._writing or self._reading) or
|
|
|
|
not self._event_queue.empty()):
|
2019-06-23 18:00:37 +00:00
|
|
|
try:
|
|
|
|
item = self._event_queue.get(block=True,
|
|
|
|
timeout=self.get_poll_timeout())
|
|
|
|
except queue.Empty:
|
2019-06-26 12:43:00 +00:00
|
|
|
# caused by timeout being hit.
|
2019-06-23 18:00:37 +00:00
|
|
|
continue
|
2019-06-23 19:50:40 +00:00
|
|
|
finally:
|
|
|
|
self._check()
|
2019-06-09 13:59:28 +00:00
|
|
|
|
|
|
|
if item.type == TriggerEventType.Action:
|
|
|
|
try:
|
|
|
|
item.callback()
|
|
|
|
except:
|
|
|
|
self._kill()
|
|
|
|
raise
|
|
|
|
elif item.type == TriggerEventType.Kill:
|
|
|
|
self._kill()
|
2019-06-17 10:06:03 +00:00
|
|
|
if not item.callback == None:
|
|
|
|
item.callback()
|
2019-10-22 13:26:40 +00:00
|
|
|
break
|
2019-06-06 16:05:44 +00:00
|
|
|
|
2019-06-06 20:03:25 +00:00
|
|
|
def _post_send_factory(self, server, lines):
|
|
|
|
return lambda: server._post_send(lines)
|
|
|
|
def _post_read_factory(self, server, lines):
|
|
|
|
return lambda: server._post_read(lines)
|
|
|
|
|
2019-06-14 23:39:52 +00:00
|
|
|
def _loop_catch(self, name: str, loop: typing.Callable[[], None]):
|
2019-06-13 16:11:46 +00:00
|
|
|
try:
|
|
|
|
loop()
|
2019-06-17 10:06:03 +00:00
|
|
|
except Exception as e:
|
2019-10-25 13:27:12 +00:00
|
|
|
self.panic("Exception on '%s' thread" % name)
|
2019-06-13 16:11:46 +00:00
|
|
|
|
2019-06-06 16:05:44 +00:00
|
|
|
def _write_loop(self):
|
2019-10-17 15:53:48 +00:00
|
|
|
while self._writing:
|
2019-10-11 12:59:28 +00:00
|
|
|
poll_sources = {}
|
2019-06-07 09:39:19 +00:00
|
|
|
with self._write_condition:
|
2019-10-11 12:59:28 +00:00
|
|
|
fds = []
|
2019-06-07 09:39:19 +00:00
|
|
|
for fd, server in self.servers.items():
|
|
|
|
if server.socket.waiting_immediate_send():
|
2019-10-11 12:59:28 +00:00
|
|
|
fds.append(fd)
|
|
|
|
|
|
|
|
for poll_source in self._poll_sources:
|
|
|
|
for fileno in poll_source.get_writables():
|
|
|
|
poll_sources[fileno] = poll_source
|
|
|
|
fds.append(fileno)
|
2019-06-07 09:39:19 +00:00
|
|
|
|
2019-10-11 12:59:28 +00:00
|
|
|
if not fds:
|
2019-06-07 09:39:19 +00:00
|
|
|
self._write_condition.wait()
|
|
|
|
continue
|
2019-10-11 12:59:28 +00:00
|
|
|
else:
|
|
|
|
for fd in fds:
|
|
|
|
self._write_poll.register(fd, select.POLLOUT)
|
2019-06-06 16:05:44 +00:00
|
|
|
|
|
|
|
events = self._write_poll.poll()
|
2019-06-07 09:39:19 +00:00
|
|
|
|
2019-06-06 16:05:44 +00:00
|
|
|
for fd, event in events:
|
2019-06-07 14:07:31 +00:00
|
|
|
if event & select.POLLOUT:
|
2019-06-06 16:05:44 +00:00
|
|
|
self._write_poll.unregister(fd)
|
2019-06-06 21:36:56 +00:00
|
|
|
if fd in self.servers:
|
|
|
|
server = self.servers[fd]
|
|
|
|
|
|
|
|
try:
|
|
|
|
lines = server._send()
|
|
|
|
except:
|
|
|
|
self.log.error("Failed to write to %s",
|
|
|
|
[str(server)])
|
|
|
|
raise
|
2019-06-09 13:59:28 +00:00
|
|
|
event_item = TriggerEvent(TriggerEventType.Action,
|
|
|
|
self._post_send_factory(server, lines))
|
|
|
|
self._event_queue.put(event_item)
|
2019-10-11 12:59:28 +00:00
|
|
|
elif fd in poll_sources:
|
2019-10-14 13:51:53 +00:00
|
|
|
def _trigger(source, fd):
|
|
|
|
return lambda: source.is_writeable(fd)
|
|
|
|
self.trigger(_trigger(poll_sources[fd], fd))
|
2019-06-06 16:05:44 +00:00
|
|
|
|
|
|
|
def _read_loop(self):
|
2019-10-11 12:59:28 +00:00
|
|
|
poll_sources = {}
|
2019-10-17 15:53:48 +00:00
|
|
|
while self._reading:
|
2019-10-11 12:59:28 +00:00
|
|
|
new_poll_sources = {}
|
|
|
|
for poll_source in self._poll_sources:
|
|
|
|
for fileno in poll_source.get_readables():
|
|
|
|
new_poll_sources[fileno] = poll_source
|
|
|
|
for fileno in new_poll_sources:
|
|
|
|
if not fileno in poll_sources:
|
|
|
|
poll_sources[fileno] = new_poll_sources[fileno]
|
|
|
|
self._read_poll.register(fileno, select.POLLIN)
|
|
|
|
for fileno in list(poll_sources.keys()):
|
|
|
|
if not fileno in new_poll_sources:
|
|
|
|
del poll_sources[fileno]
|
|
|
|
self._read_poll.unregister(fileno)
|
|
|
|
|
2019-06-23 18:00:37 +00:00
|
|
|
events = self._read_poll.poll()
|
2018-09-28 15:51:36 +00:00
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
for fd, event in events:
|
2019-06-06 16:05:44 +00:00
|
|
|
if fd == self._rtrigger_server.fileno():
|
|
|
|
# throw away data from trigger socket
|
2019-06-06 18:21:18 +00:00
|
|
|
with self._rtrigger_lock:
|
2019-06-09 13:59:28 +00:00
|
|
|
self._rtrigger_server.recv(1024)
|
2019-06-06 18:21:18 +00:00
|
|
|
self._rtriggered = False
|
2019-10-11 12:59:28 +00:00
|
|
|
elif fd in poll_sources:
|
2019-10-14 13:51:53 +00:00
|
|
|
def _trigger(source, fd):
|
|
|
|
return lambda: source.is_readable(fd)
|
|
|
|
self.trigger(_trigger(poll_sources[fd], fd))
|
2019-06-06 16:05:44 +00:00
|
|
|
else:
|
2019-06-06 21:36:56 +00:00
|
|
|
if not fd in self.servers:
|
|
|
|
self._read_poll.unregister(fd)
|
|
|
|
continue
|
|
|
|
|
2019-06-06 16:05:44 +00:00
|
|
|
server = self.servers[fd]
|
2019-06-07 14:07:31 +00:00
|
|
|
if event & select.POLLIN:
|
2019-06-06 16:05:44 +00:00
|
|
|
lines = server.read()
|
|
|
|
if lines == None:
|
|
|
|
server.disconnect()
|
2018-10-08 22:03:49 +00:00
|
|
|
continue
|
|
|
|
|
2019-06-23 17:53:25 +00:00
|
|
|
event_item = TriggerEvent(TriggerEventType.Action,
|
|
|
|
self._post_read_factory(server, lines))
|
|
|
|
self._event_queue.put(event_item)
|
2019-06-06 19:26:05 +00:00
|
|
|
elif event & select.POLLHUP:
|
2019-06-07 14:07:31 +00:00
|
|
|
self.log.warn("Recieved POLLHUP for %s", [str(server)])
|
2019-06-06 16:05:44 +00:00
|
|
|
server.disconnect()
|
|
|
|
|
2019-06-06 19:22:56 +00:00
|
|
|
def _check(self):
|
2019-10-08 11:20:08 +00:00
|
|
|
for poll_timeout in self._poll_timeouts:
|
2019-10-10 13:12:58 +00:00
|
|
|
if poll_timeout.next() == 0:
|
|
|
|
poll_timeout.call()
|
2019-06-06 16:05:44 +00:00
|
|
|
|
|
|
|
throttle_filled = False
|
|
|
|
for server in list(self.servers.values()):
|
|
|
|
if server.read_timed_out():
|
|
|
|
self.log.warn("Pinged out from %s", [str(server)])
|
|
|
|
server.disconnect()
|
|
|
|
elif server.ping_due() and not server.ping_sent:
|
|
|
|
server.send_ping()
|
|
|
|
server.ping_sent = True
|
2019-06-06 22:19:57 +00:00
|
|
|
|
2019-06-06 16:05:44 +00:00
|
|
|
if not server.socket.connected:
|
|
|
|
self._events.on("server.disconnect").call(server=server)
|
|
|
|
self.disconnect(server)
|
|
|
|
|
|
|
|
if not self.get_server_by_id(server.id):
|
|
|
|
reconnect_delay = self.config.get("reconnect-delay", 10)
|
2019-09-01 07:38:22 +00:00
|
|
|
|
2019-10-09 11:47:03 +00:00
|
|
|
timer = self._timers.add("timed-reconnect",
|
|
|
|
self._timed_reconnect, reconnect_delay,
|
|
|
|
server_id=server.id)
|
2019-09-01 07:38:22 +00:00
|
|
|
self.reconnections[server.id] = timer
|
|
|
|
|
2019-06-06 16:05:44 +00:00
|
|
|
self.log.warn(
|
|
|
|
"Disconnected from %s, reconnecting in %d seconds",
|
|
|
|
[str(server), reconnect_delay])
|
|
|
|
elif (server.socket.waiting_throttled_send() and
|
|
|
|
server.socket.throttle_done()):
|
|
|
|
server.socket._fill_throttle()
|
|
|
|
throttle_filled = True
|
|
|
|
|
|
|
|
if throttle_filled:
|
2019-06-06 22:19:57 +00:00
|
|
|
self.trigger_write()
|