3 from concurrent.futures
import Future
6 from socket
import error
as SocketError
12 import trollius
as asyncio
20 super(ServiceError, self).
__init__(
'UA Service Error')
28 class SocketClosedException(UaError):
35 alternative to io.BytesIO making debug easier 36 and added a few conveniance methods 39 def __init__(self, data, start_pos=0, size=-1):
44 size = len(data) - start_pos
48 return "Buffer(size:{0}, data:{1})".format(
58 read and pop number of bytes for buffer 61 raise NotEnoughData(
"Not enough data left in buffer, request for {0}, we have {1}".format(size, self))
72 return a shadow copy, optionnaly only copy 'size' bytes 74 if size == -1
or size > self.
_size:
80 skip size bytes in buffer 83 raise NotEnoughData(
"Not enough data left in buffer, request for {0}, we have {1}".format(size, self))
90 wrapper to make it possible to have same api for 91 normal sockets, socket from asyncio, StringIO, etc.... 99 Receive up to size bytes from socket 104 chunk = self.socket.recv(size)
105 except (OSError, SocketError)
as ex:
114 self.socket.sendall(data)
118 return os.urandom(size)
123 run an asyncio loop in a thread 127 threading.Thread.__init__(self)
128 self.
logger = logging.getLogger(__name__)
134 threading.Thread.start(self)
138 self.logger.debug(
"Starting subscription thread")
139 self.
loop = asyncio.new_event_loop()
140 asyncio.set_event_loop(self.
loop)
142 self._cond.notify_all()
143 self.loop.run_forever()
144 self.logger.debug(
"subscription thread ended")
147 return self.loop.create_server(proto, hostname, port)
151 stop subscription loop, thus the subscription thread 153 self.loop.call_soon_threadsafe(self.loop.stop)
156 self.loop.call_soon_threadsafe(callback)
160 threadsafe call_later from asyncio 162 p = functools.partial(self.loop.call_later, delay, callback)
163 self.loop.call_soon_threadsafe(p)
167 task = asyncio.async(coro, loop=self.
loop)
169 task.add_done_callback(cb)
170 future.set_result(task)
174 threadsafe create_task from asyncio 177 p = functools.partial(self.
_create_task, future, coro, cb)
178 self.loop.call_soon_threadsafe(p)
179 return future.result()
182 cond = threading.Condition()
192 task = self.loop.run_until_complete(coro)
193 future.set_result(task)
197 threadsafe run_until_completed from asyncio 201 self.loop.call_soon_threadsafe(p)
202 return future.result()
def call_soon(self, callback)
def create_server(self, proto, hostname, port)
def create_nonce(size=32)
def __init__(self, data, start_pos=0, size=-1)
def run_coro_and_wait(self, coro)
def _create_task(self, future, coro, cb=None)
def call_later(self, delay, callback)
def _run_until_complete(self, future, coro)
def create_task(self, coro, cb=None)
def run_until_complete(self, coro)