-
-
Notifications
You must be signed in to change notification settings - Fork 143
Add ThreadSafeClient implementation #203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PaulWasTaken
wants to merge
3
commits into
python-smpplib:master
Choose a base branch
from
PaulWasTaken:feature/thread-safe-client
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,12 +19,14 @@ | |
| """SMPP client module""" | ||
|
|
||
| import binascii | ||
| import collections | ||
| import logging | ||
| import select | ||
| import socket | ||
| import struct | ||
| import warnings | ||
|
|
||
| from monotonic import monotonic | ||
| from smpplib import consts, exceptions, smpp | ||
|
|
||
|
|
||
|
|
@@ -91,7 +93,6 @@ def __init__( | |
| else: | ||
| self.allow_unknown_opt_params = allow_unknown_opt_params | ||
|
|
||
|
|
||
| self._socket = self._create_socket() | ||
|
|
||
| def __enter__(self): | ||
|
|
@@ -301,7 +302,7 @@ def set_message_received_handler(self, func): | |
| def set_message_sent_handler(self, func): | ||
| """Set new function to handle message sent event""" | ||
| self.message_sent_handler = func | ||
|
|
||
| def set_query_resp_handler(self, func): | ||
| """Set new function to handle query resp event""" | ||
| self.query_resp_handler = func | ||
|
|
@@ -421,3 +422,73 @@ def query_message(self, **kwargs): | |
| qsm = smpp.make_pdu('query_sm', client=self, **kwargs) | ||
| self.send_pdu(qsm) | ||
| return qsm | ||
|
|
||
|
|
||
| class ThreadSafeClient(Client): | ||
| should_stop = False | ||
|
|
||
| def __init__(self, *args, **kwargs): | ||
| # Socket polling period | ||
| select_timeout = kwargs.get('select_timeout', 1.0) | ||
|
|
||
| super(ThreadSafeClient, self).__init__(*args, **kwargs) | ||
|
|
||
| self._select_timeout = select_timeout | ||
|
|
||
| self._send_queue = collections.deque() | ||
| self._read_sock, self._send_sock = socket.socketpair() | ||
|
|
||
| # It will help not to spam the server | ||
| self._last_active_time = 0.0 | ||
|
|
||
| def accept(self, obj): | ||
| """Accept an object""" | ||
| raise NotImplementedError('not implemented') | ||
|
|
||
| def send_pdu(self, pdu, send_later=False): | ||
| if send_later: | ||
| self._send_queue.append(pdu) | ||
| self._send_sock.send(b'\x00') | ||
| return True | ||
| else: | ||
| pdu_sent = super(ThreadSafeClient, self).send_pdu(pdu) | ||
| self._last_active_time = monotonic() | ||
| return pdu_sent | ||
|
|
||
| def send_message(self, send_later=True, **kwargs): | ||
| submit_sm_pdu = smpp.make_pdu('submit_sm', client=self, **kwargs) | ||
| self.send_pdu(submit_sm_pdu, send_later=send_later) | ||
| return submit_sm_pdu | ||
|
|
||
| def _should_prolong_session(self): | ||
| # We need some time to send enquire_link before the next `select` call comes | ||
| passed_from_last_message = monotonic() - self._last_active_time | ||
|
|
||
| return self.timeout - self._select_timeout <= passed_from_last_message | ||
|
|
||
| def observe(self, ignore_error_codes=None, auto_send_enquire_link=True): | ||
| while not self.should_stop: | ||
| rlist, _, _ = select.select( | ||
| [self._socket, self._read_sock], [], [], self._select_timeout, | ||
| ) | ||
|
|
||
| if self.should_stop: | ||
| break | ||
|
|
||
| if not rlist: | ||
| if self._should_prolong_session(): | ||
| if not auto_send_enquire_link: | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: check |
||
| raise exceptions.SessionProlongationDisabled() | ||
|
|
||
| self.logger.debug('Sending enquire_link') | ||
| pdu = smpp.make_pdu('enquire_link', client=self) | ||
| self.send_pdu(pdu) | ||
| else: | ||
| for ready_socket in rlist: | ||
| if ready_socket is self._socket: | ||
| self.read_once(ignore_error_codes, auto_send_enquire_link) | ||
| else: | ||
| self._read_sock.recv(1) | ||
| self.send_pdu(self._send_queue.pop()) | ||
|
|
||
| self.logger.info('Finished observing...') | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we sending a
00? Is it ano-op? If so, why are we sending it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is some sort of notification frame. We put it into the send socket to wake up our read socket