Skip to content

Commit

Permalink
#1773: test all network state packets
Browse files Browse the repository at this point in the history
git-svn-id: https://xpra.org/svn/Xpra/trunk@23151 3bb7dfac-3a0b-4e04-842a-767bc560f471
  • Loading branch information
totaam committed Jul 16, 2019
1 parent f02ea66 commit e038c45
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
23 changes: 22 additions & 1 deletion src/unittests/unit/server/mixins/networkstate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,31 @@ class NetworkStateMixinTest(ServerMixinTest):

def test_networkstate(self):
from xpra.server.mixins.networkstate_server import NetworkStateServer
from xpra.server.source.networkstate_mixin import NetworkStateMixin
opts = AdHocStruct()
opts.pings = 1
opts.bandwidth_limit = "10Mbps"
self._test_mixin_class(NetworkStateServer, opts)
self._test_mixin_class(NetworkStateServer, opts, {}, NetworkStateMixin)
self.handle_packet(("ping", 10))
self.handle_packet(("ping", -1000))
self.handle_packet(("ping_echo", 10, 500, 500, 600, 10))
for v in (None, "foo", 1, 2.0, [], (), set()):
try:
self.handle_packet(("connection-data", v))
except:
pass
else:
raise Exception("should not allow %s (%s) as connection-data" % (v, type(v)))
self.handle_packet(("connection-data", {}))
for v in (None, "foo", 2.0, [], (), set()):
try:
self.handle_packet(("bandwidth-limit", v))
except:
pass
else:
raise Exception("should not allow %s (%s) as connection-data" % (v, type(v)))
self.handle_packet(("bandwidth-limit", 10))


def main():
unittest.main()
Expand Down
9 changes: 6 additions & 3 deletions src/xpra/server/mixins/networkstate_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,19 @@ def _process_bandwidth_limit(self, proto, packet):
if not ss:
return
bandwidth_limit = packet[1]
assert isinstance(bandwidth_limit, int)
if self.bandwidth_limit and bandwidth_limit>self.bandwidth_limit or bandwidth_limit<=0:
bandwidth_limit = self.bandwidth_limit or 0
if ss.bandwidth_limit==bandwidth_limit:
#unchanged
return
ss.bandwidth_limit = bandwidth_limit
#we can't assume to have a full ClientConnection object:
client_id = getattr(ss, "counter", None)
if bandwidth_limit==0:
bandwidthlog.info("bandwidth-limit restrictions removed for client %i", ss.counter)
bandwidthlog.info("bandwidth-limit restrictions removed for client %s", client_id)
else:
bandwidthlog.info("bandwidth-limit changed to %sbps for client %i", std_unit(bandwidth_limit), ss.counter)
bandwidthlog.info("bandwidth-limit changed to %sbps for client %s", std_unit(bandwidth_limit), client_id)

def send_ping(self):
for ss in self._server_sources.values():
Expand All @@ -169,7 +172,7 @@ def _process_ping(self, proto, packet):


def init_packet_handlers(self):
self._authenticated_packet_handlers.update({
self.add_packet_handlers({
"ping": self._process_ping,
"ping_echo": self._process_ping_echo,
"connection-data": self._process_connection_data,
Expand Down
8 changes: 1 addition & 7 deletions src/xpra/server/source/client_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ def parse_hello(self, c):
msg = version_compat_check(self.proxy_version)
if msg:
proxylog.warn("Warning: proxy version may not be compatible: %s", msg)
self.update_connection_data(c.dictget("connection-data"))
self.client_connection_data = c.dictget("connection-data")
if getattr(self, "mmap_size", 0)>0:
log("mmap enabled, ignoring bandwidth-limit")
self.bandwidth_limit = 0
Expand Down Expand Up @@ -435,12 +435,6 @@ def send_async(self, *parts, **kwargs):
self.send(*parts, **kwargs)


#client tells us about network connection status:
def update_connection_data(self, data):
netlog("update_connection_data(%s)", data)
self.client_connection_data = data


def send_hello(self, server_capabilities):
capabilities = server_capabilities.copy()
for bc in CC_BASES:
Expand Down
20 changes: 15 additions & 5 deletions src/xpra/server/source/networkstate_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def init_state(self):
self.last_ping_echoed_time = 0
self.check_ping_echo_timers = {}
self.ping_timer = None
self.bandwidth_limit = 0

def cleanup(self):
self.cancel_ping_echo_timers()
Expand Down Expand Up @@ -71,8 +72,9 @@ def process_ping(self, time_to_echo):
fl1, fl2, fl3 = os.getloadavg()
l1,l2,l3 = int(fl1*1000), int(fl2*1000), int(fl3*1000)
#and the last client ping latency we measured (if any):
if self.statistics.client_ping_latency:
_, cl = self.statistics.client_ping_latency[-1]
stats = getattr(self, "statistics", None)
if stats and stats.client_ping_latency:
_, cl = stats.client_ping_latency[-1]
cl = int(1000.0*cl)
self.send_async("ping_echo", time_to_echo, l1, l2, l3, cl, will_have_more=False)
#if the client is pinging us, ping it too:
Expand All @@ -96,8 +98,16 @@ def process_ping_echo(self, packet):
pass
self.last_ping_echoed_time = echoedtime
client_ping_latency = monotonic_time()-echoedtime/1000.0
self.statistics.client_ping_latency.append((monotonic_time(), client_ping_latency))
stats = getattr(self, "statistics", None)
if stats:
stats.client_ping_latency.append((monotonic_time(), client_ping_latency))
self.client_load = l1, l2, l3
if server_ping_latency>=0:
self.statistics.server_ping_latency.append((monotonic_time(), server_ping_latency/1000.0))
if server_ping_latency>=0 and stats:
stats.server_ping_latency.append((monotonic_time(), server_ping_latency/1000.0))
log("ping echo client load=%s, measured server latency=%s", self.client_load, server_ping_latency)


def update_connection_data(self, data):
log("update_connection_data(%s)", data)
assert isinstance(data, dict)
self.client_connection_data = data

0 comments on commit e038c45

Please sign in to comment.