Skip to content

Commit

Permalink
#540 query and expose the interface speed using comtypes
Browse files Browse the repository at this point in the history
git-svn-id: https://xpra.org/svn/Xpra/trunk@18790 3bb7dfac-3a0b-4e04-842a-767bc560f471
  • Loading branch information
totaam committed Mar 21, 2018
1 parent 02d3a2d commit a5a4ee2
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 28 deletions.
13 changes: 9 additions & 4 deletions src/xpra/net/bytestreams.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file is part of Xpra.
# Copyright (C) 2011-2017 Antoine Martin <[email protected]>
# Copyright (C) 2011-2018 Antoine Martin <[email protected]>
# Copyright (C) 2008, 2009, 2010 Nathaniel Smith <[email protected]>
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.
Expand All @@ -13,7 +13,7 @@
log = Logger("network", "protocol")
from xpra.net.common import ConnectionClosedException
from xpra.util import envint, envbool, csv
from xpra.os_util import WIN32, PYTHON2
from xpra.os_util import WIN32, PYTHON2, POSIX
from xpra.platform.features import TCP_OPTIONS, IP_OPTIONS, SOCKET_OPTIONS


Expand Down Expand Up @@ -354,8 +354,12 @@ def do_get_socket_info(self):
except:
pass
try:
fd = s.fileno()
info["fileno"] = fd
if POSIX:
fd = s.fileno()
else:
fd = 0
if fd:
info["fileno"] = fd
from xpra.platform.netdev_query import get_interface_speed
#ie: self.local = ("192.168.1.7", "14500")
if self.local and len(self.local)==2:
Expand All @@ -364,6 +368,7 @@ def do_get_socket_info(self):
#ie: iface = "eth0"
if iface and iface!="lo":
s = get_interface_speed(fd, iface)
log("get_interface_speed(%i, %s)=%s", fd, iface, s)
if s>0:
info["speed"] = s
except:
Expand Down
12 changes: 9 additions & 3 deletions src/xpra/net/net_util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# This file is part of Xpra.
# Copyright (C) 2013-2017 Antoine Martin <[email protected]>
# Copyright (C) 2013-2018 Antoine Martin <[email protected]>
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.

Expand Down Expand Up @@ -395,6 +395,7 @@ def get_info():
def main():
from xpra.util import print_nested_dict
from xpra.platform import program_context
from xpra.platform.netdev_query import get_interface_speed
from xpra.log import enable_color, add_debug_category, enable_debug_for
with program_context("Network-Info", "Network Info"):
enable_color()
Expand All @@ -407,9 +408,14 @@ def main():
print("Network interfaces found:")
for iface in get_interfaces():
if if_nametoindex:
print("* %s (index=%s)" % (iface.ljust(20), if_nametoindex(iface)))
s = "* %s (index=%s)" % (iface.ljust(20), if_nametoindex(iface))
else:
print("* %s" % iface)
s = "* %s" % iface
speed = get_interface_speed(0, iface)
if speed>0:
from xpra.simple_stats import std_unit
s += " (speed=%sbps)" % std_unit(speed)
print(s)

def pver(v):
if type(v) in (tuple, list):
Expand Down
31 changes: 31 additions & 0 deletions src/xpra/platform/win32/comtypes_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This file is part of Xpra.
# Copyright (C) 2016-2018 Antoine Martin <[email protected]>
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.


import logging
logging.getLogger("comtypes").setLevel(logging.INFO)

import comtypes #@UnresolvedImport
assert comtypes
from comtypes import client #@UnresolvedImport


class QuietenLogging(object):

def __init__(self, *_args):
self.loggers = [logging.getLogger(x) for x in ("comtypes.client._code_cache", "comtypes.client._generate")]
self.saved_levels = [x.getEffectiveLevel() for x in self.loggers]
self.verbose = getattr(client._generate, "__verbose__", None)

def __enter__(self):
client._generate.__verbose__ = False
for logger in self.loggers:
logger.setLevel(logging.WARNING)

def __exit__(self, *_args):
if self.verbose is not None:
client._generate.__verbose__ = self.verbose
for i, logger in enumerate(self.loggers):
logger.setLevel(self.saved_levels[i])
25 changes: 5 additions & 20 deletions src/xpra/platform/win32/win32_webcam.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,13 @@ def log(*args):
logging.getLogger("comtypes").setLevel(logging.INFO)

import comtypes #@UnresolvedImport
from comtypes import client #@UnresolvedImport
from comtypes.automation import VARIANT #@UnresolvedImport
from comtypes.persist import IPropertyBag, IErrorLog #@UnresolvedImport
from ctypes import POINTER
from xpra.platform.win32.comtypes_util import QuietenLogging


def quiet_load_tlb(tlb_filename):
log("quiet_load_tlb(%s)", tlb_filename)
#suspend logging during comtypes.client._code_cache and loading of tlb files:
#also set minimum to INFO for all of comtypes
loggers = [logging.getLogger(x) for x in ("comtypes.client._code_cache", "comtypes.client._generate")]
saved_levels = [x.getEffectiveLevel() for x in loggers]
try:
for logger in loggers:
logger.setLevel(logging.WARNING)
log("loading module from %s", tlb_filename)
from comtypes import client #@UnresolvedImport
client._generate.__verbose__ = False
module = client.GetModule(tlb_filename)
log("%s=%s", tlb_filename, module)
return module
finally:
for i, logger in enumerate(loggers):
logger.setLevel(saved_levels[i])

#load directshow:
win32_tlb_dir = os.path.join(tlb_dir, "win32")
if os.path.exists(win32_tlb_dir):
Expand All @@ -54,7 +37,9 @@ def quiet_load_tlb(tlb_filename):
log("directshow_tlb=%s", directshow_tlb)
if not os.path.exists(directshow_tlb):
raise ImportError("DirectShow.tlb is missing")
directshow = quiet_load_tlb(directshow_tlb)
with QuietenLogging():
directshow = client.GetModule(directshow_tlb)
log("directshow: %s", directshow)
log("directshow: %s", dir(directshow))

CLSID_VideoInputDeviceCategory = comtypes.GUID("{860BB310-5D01-11d0-BD3B-00A0C911CE86}")
Expand Down
4 changes: 3 additions & 1 deletion src/xpra/platform/xposix/netdev_query.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file is part of Xpra.
# Copyright (C) 2017 Antoine Martin <[email protected]>
# Copyright (C) 2017-2018 Antoine Martin <[email protected]>
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.

Expand Down Expand Up @@ -59,6 +59,8 @@ cdef extern from "sys/ioctl.h":

def get_interface_speed(int sockfd, char *ifname):
""" returns the ethtool speed in bps, or 0 """
if sockfd==0:
return 0
cdef ifreq ifr
cdef ethtool_cmd edata
ifr.ifr_ifrn.ifrn_name = ifname
Expand Down

0 comments on commit a5a4ee2

Please sign in to comment.