Skip to content
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

added "update_client_protocol" method #24

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/Rammbock/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ def _start_client(self, client_class, ip=None, port=None, name=None, timeout=Non
client.set_own_ip_and_port(ip=ip, port=port)
return self._clients.add(client, name)

def _is_client(self, client_name):
client_exist = True
try:
self._clients.get_with_name(client_name)
except KeyError:
client_exist = False
return client_exist

def _get_protocol(self, protocol):
try:
protocol = self._protocols[protocol] if protocol else None
Expand All @@ -206,6 +214,19 @@ def get_client_protocol(self, name=None):
"""
return self._clients.get(name).protocol_name or ''

def update_client_protocol(self, protocol, client=None):
"""Updates client protocol. If client is not
given then update the latest client.

Examples:
| Update client protocol | protocol_name |
| Update client protocol | protocol_name | client_name |
"""
protocol = self._get_protocol(protocol)
client = self._clients.get(client)
client.set_protocol(protocol)
client.update_message_stream()

def accept_connection(self, name=None, alias=None):
"""Accepts a connection to server identified by `name` or the latest
server if `name` is empty.
Expand Down
6 changes: 6 additions & 0 deletions src/Rammbock/networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,12 @@ def connect_to(self, server_ip, server_port):
self._is_connected = True
return self

def set_protocol(self, protocol):
self._protocol = protocol

def update_message_stream(self):
self._message_stream = self._get_message_stream()


class UDPClient(_Client, _UDPNode):
pass
Expand Down
6 changes: 3 additions & 3 deletions src/Rammbock/templates/primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def __init__(self, length, name, default_value=None, align=None):
self.length = Length(length, align)

def _encode_value(self, value, message, little_endian=False):
self._raise_error_if_no_value(value, message)
# self._raise_error_if_no_value(value, message)
length, aligned_length = self.length.decode_lengths(message)
binary = to_bin_of_length(length, value)
binary = binary[::-1] if little_endian else binary
Expand All @@ -162,7 +162,7 @@ def _get_int_value(self, message, value):
return to_twos_comp(value, bin_len)

def _encode_value(self, value, message, little_endian=False):
self._raise_error_if_no_value(value, message)
# self._raise_error_if_no_value(value, message)
value = self._get_int_value(message, value)
return UInt._encode_value(self, value, message, little_endian)

Expand Down Expand Up @@ -202,7 +202,7 @@ def __init__(self, length, name, default_value=None):
raise AssertionError('Binary field length must be static. Length: %s' % length)

def _encode_value(self, value, message, little_endian=False):
self._raise_error_if_no_value(value, message)
# self._raise_error_if_no_value(value, message)
minimum_binary = to_bin(value)
length, aligned = self.length.decode_lengths(message, len(minimum_binary))
binary = to_bin_of_length(self._byte_length(length), value)
Expand Down