From d7face3089f56cf80886246bbe3b7122bc55b4bb Mon Sep 17 00:00:00 2001 From: Rossen Georgiev Date: Mon, 15 Feb 2016 01:10:45 +0000 Subject: [PATCH] reworked job handling --- steam/client/__init__.py | 40 +++++++++++++--- steam/client/features/__init__.py | 6 +++ steam/client/jobs.py | 76 ------------------------------- 3 files changed, 40 insertions(+), 82 deletions(-) create mode 100644 steam/client/features/__init__.py delete mode 100644 steam/client/jobs.py diff --git a/steam/client/__init__.py b/steam/client/__init__.py index 324ef44..04ad340 100644 --- a/steam/client/__init__.py +++ b/steam/client/__init__.py @@ -6,25 +6,26 @@ from steam.enums import EResult from steam.core.msg import MsgProto from steam.core.cm import CMClient from steam import SteamID -from steam.client.jobs import JobManager +from steam.client.features import FeatureBase logger = logging.getLogger("SteamClient") -class SteamClient(EventEmitter): +class SteamClient(EventEmitter, FeatureBase): + current_jobid = 0 + def __init__(self): self.cm = CMClient() - self.job = JobManager(self) - - # re-emit all events from CMClient - self.cm.on(None, self.emit) # register listners + self.cm.on(None, self._handle_cm_events) self.on(EMsg.ClientLogOnResponse, self._handle_logon) self.on("disconnected", self._handle_disconnect) self.logged_on = False + super(SteamClient, self).__init__() + def __repr__(self): return "<%s() %s>" % (self.__class__.__name__, 'online' if self.connected else 'offline', @@ -49,8 +50,23 @@ class SteamClient(EventEmitter): def disconnect(self): self.cm.disconnect() + def _handle_cm_events(self, event, *args): + self.emit(event, *args) + + if isinstance(event, EMsg): + message = args[0] + + if message.proto: + jobid = message.header.jobid_target + else: + jobid = message.header.targetJobID + + if jobid not in (-1, 18446744073709551615): + self.emit("job_%d" % jobid, *args) + def _handle_disconnect(self): self.logged_on = False + self.current_jobid = 0 def _handle_logon(self, msg): result = EResult(msg.body.eresult) @@ -77,6 +93,18 @@ class SteamClient(EventEmitter): self.cm.send_message(message) + def send_job(self, message): + jobid = self.current_jobid = (self.current_jobid + 1) % 4294967295 + + if message.proto: + message.header.jobid_source = jobid + else: + message.header.sourceJobID = jobid + + self.send(message) + + return "job_%d" % jobid + def _pre_login(self): if self.logged_on: logger.debug("Trying to login while logged on???") diff --git a/steam/client/features/__init__.py b/steam/client/features/__init__.py new file mode 100644 index 0000000..82215f1 --- /dev/null +++ b/steam/client/features/__init__.py @@ -0,0 +1,6 @@ +class FeatureBase(object): + """ + This object is used as base to implement all high level functionality. + The features are seperated into submodules. + """ + pass diff --git a/steam/client/jobs.py b/steam/client/jobs.py deleted file mode 100644 index e91511a..0000000 --- a/steam/client/jobs.py +++ /dev/null @@ -1,76 +0,0 @@ -import logging -from gevent import event -from eventemitter import EventEmitter -from steam.enums.emsg import EMsg - -logger = logging.getLogger("JobManager") - - -class JobManager(EventEmitter): - """ - Takes care of managing job messages - """ - - _max_ulong = (2**64)-2 - - def __init__(self, client): - """ - Takes an instance of SteamClient - """ - - self._client = client - self._client.on(None, self._handle_event) - self._jobid = 0 - self._jobs = {} - - def _handle_event(self, event, *args): - if len(args) != 1: - return - - message = args[0] - - if not isinstance(event, EMsg) and not self._jobs: - return - - if message.proto: - jobid = message.header.jobid_target - else: - jobid = message.header.targetJobID - - if jobid in self._jobs: - logger.debug("Response for job: %d, %s" % (jobid, repr(message))) - self._jobs.pop(jobid).set(message) - - def get_jobid(self): - """ - Returns the next job id - """ - self._jobid = (self._jobid + 1) % self._max_ulong - return self._jobid - - def send(self, message): - """ - Sends a message as job, and returns the job id - """ - jobid = self.get_jobid() - - logger.debug("Sending job: %d, %s" % (jobid, repr(message))) - - if message.proto: - message.header.jobid_source = jobid - else: - message.header.sourceJobID = jobid - - self._jobs[jobid] = event.AsyncResult() - self._client.send(message) - return jobid - - def wait_for(self, jobid, timeout=None): - """ - Blocks waiting for specified job id - """ - - if jobid not in self._jobs: - raise ValueError("Specified jobid doen't exist. Did you call send() to get one?") - - return self._jobs[jobid].get(True, timeout)