Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def status(message, gateway):
# we use the channelid to send back information
# but don't instantiate a channel object
d = {
"numchannels": len(gateway._channelfactory._channels),
"numexecuting": gateway._execpool.active_count(),
"execmodel": gateway.execmodel.backend,
}
gateway._send(Message.CHANNEL_DATA, message.channelid, dumps_internal(d))
gateway._send(Message.CHANNEL_CLOSE, message.channelid)
def from_io(io):
try:
header = io.read(9) # type 1, channel 4, payload 4
if not header:
raise EOFError("empty read")
except EOFError:
e = sys.exc_info()[1]
raise EOFError("couldnt load message header, " + e.args[0])
msgtype, channel, payload = struct.unpack("!bii", header)
return Message(msgtype, channel, io.read(payload))
target = gateway._channelfactory.new(message.channelid)
target._strconfig = loads_internal(message.data, gateway)
types = [
status,
reconfigure,
gateway_terminate,
channel_exec,
channel_data,
channel_close,
channel_close_error,
channel_last_message,
]
for i, handler in enumerate(types):
Message._types.append(handler)
setattr(Message, handler.__name__.upper(), i)
def _send(self, msgcode, channelid=0, data=bytes()):
message = Message(msgcode, channelid, data)
try:
message.to_io(self._io)
self._trace("sent", message)
except (IOError, ValueError):
e = sys.exc_info()[1]
self._trace("failed to send", message, e)
# ValueError might be because the IO is already closed
raise IOError("cannot send (already closed?)")
def test_wire_protocol(self):
for i, handler in enumerate(Message._types):
one = py.io.BytesIO()
data = "23".encode("ascii")
Message(i, 42, data).to_io(one)
two = py.io.BytesIO(one.getvalue())
msg = Message.from_io(two)
assert msg.msgcode == i
assert isinstance(msg, Message)
assert msg.channelid == 42
assert msg.data == data
assert isinstance(repr(msg), str)
else:
target = gateway._channelfactory.new(message.channelid)
target._strconfig = loads_internal(message.data, gateway)
types = [
status,
reconfigure,
gateway_terminate,
channel_exec,
channel_data,
channel_close,
channel_close_error,
channel_last_message,
]
for i, handler in enumerate(types):
Message._types.append(handler)
setattr(Message, handler.__name__.upper(), i)