Skip to content

Commit

Permalink
Fix loading resources from local dir (+3 squashed commits)
Browse files Browse the repository at this point in the history
Make a DHCP request for only the Wii U interface

Add CLI

- run_server wii_u_interface normal_interface
- get_ket wii_u_interface wps_pin (first for numbers)

Update dependencies
  • Loading branch information
rolandoislas committed Mar 21, 2017
1 parent ce24aa0 commit d682a20
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 48 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ dist/
*.egg-info/
setup.cfg
temp/
.drc-sim/
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ RUN apt-get update \
python-pip \
libffi-dev \
zlib1g-dev \
libjpeg62-turbo-dev \
libjpeg-dev \
net-tools \
wireless-tools \
sysvinit-utils \
psmisc \
libavcodec-dev \
libswscale-dev \
rfkill \
isc-dhcp-client
isc-dhcp-client \
ifmetric \
python-tk

ADD drc*.py /root/
ADD setup.py /root/
Expand All @@ -29,4 +31,4 @@ ADD MANIFEST.in /root/
RUN cd /root/ && python setup.py install && rm -rf /root/*

ENV TERM xterm
ENTRYPOINT ["drc-sim-helper.py"]
ENTRYPOINT ["drc-sim-backend.py"]
5 changes: 0 additions & 5 deletions debian/changelog

This file was deleted.

1 change: 0 additions & 1 deletion debian/compat

This file was deleted.

14 changes: 0 additions & 14 deletions debian/control

This file was deleted.

7 changes: 0 additions & 7 deletions debian/drcsim.triggers

This file was deleted.

10 changes: 0 additions & 10 deletions debian/rules

This file was deleted.

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from setuptools import find_packages

setup(name='drcsim',
version='1.1',
version='1.3',
description='Wii U gamepad simulator.',
install_requires=['construct<2.8', 'Pillow==3.4.2', 'cffi==1.9.1', 'netifaces==0.10.5', 'pexpect==4.2.1'],
packages=find_packages(),
Expand Down
26 changes: 26 additions & 0 deletions src/server/data/args.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import argparse

import sys


class Args:
args = None
Expand All @@ -10,6 +12,7 @@ def __init__(self):
@staticmethod
def parse_args():
arg_parser = argparse.ArgumentParser(description="Drc-sim backend decodes packets and serves clients")
# Logging
arg_parser.add_argument("-d", "--debug", action="store_const", const=True, default=False,
help="debug output")
arg_parser.add_argument("-e", "--extra", action="store_const", const=True, default=False,
Expand All @@ -20,4 +23,27 @@ def parse_args():
help="verbose debug output")
arg_parser.add_argument("-c", "--cli", action="store_const", const=True, default=False,
help="disable gui")
# CLI
args = ["-c", "--cli", "-h", "--help"]
found = False
for arg in args:
if arg in sys.argv:
found = True
if found:
subparsers = arg_parser.add_subparsers()
# Run Server
run_server = subparsers.add_parser("run_server")
run_server.add_argument("wii_u_interface", type=str)
run_server.add_argument("normal_interface", type=str)
# Get Key
get_key = subparsers.add_parser("get_key")
get_key.add_argument("wii_u_interface", type=str)
get_key.add_argument("wps_pin", type=str)
Args.args = arg_parser.parse_args()
# Add sub arguments
Args.args.run_server = False
Args.args.get_key = False
if "run_server" in sys.argv:
Args.args.run_server = True
elif "get_key" in sys.argv:
Args.args.get_key = True
13 changes: 13 additions & 0 deletions src/server/data/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,17 @@ class Resource:
def __init__(self, in_path):
pre = "resources/"
Logger.debug("Loading resource \"%s\"", join(pre, in_path))
current_dir = os.path.dirname(__file__).split(os.sep)
# Check local files first
file_path = "/"
if current_dir >= 3:
for path in range(0, len(current_dir) - 3):
file_path = join(file_path, current_dir[path])
file_path = join(file_path, pre, in_path)
if os.path.exists(file_path):
self.resource = open(file_path).read()
Logger.extra("Found resource in local resource directory.")
return
# Attempt to get from package
self.resource = pkg_resources.resource_string(pkg_resources.Requirement.parse("drcsim"), join(pre, in_path))
Logger.extra("Found resource in package.")
98 changes: 96 additions & 2 deletions src/server/ui/cli/cli_main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,103 @@
import time

from src.server.control.gamepad import Gamepad
from src.server.data import constants
from src.server.data.args import Args
from src.server.util.interface_util import InterfaceUtil
from src.server.util.logging.logger_cli import LoggerCli
from src.server.util.wpa_supplicant import WpaSupplicant


class CliMain:
def __init__(self):
LoggerCli.throw(NotImplementedError("CLI not implemented"))
self.getting_key = False
self.gamepad = None
self.wpa_supplicant = None

def start(self):
LoggerCli.throw(NotImplementedError("CLI not implemented"))
LoggerCli.warn("The CLI not user friendly. It is here to provide a way to automate"
" the server via a shell. The GUI is a better alternative for normal use.")
if Args.args.run_server:
self.run_server()
elif Args.args.get_key:
self.get_key()
else:
self.stop()

def stop(self):
LoggerCli.info("Stopping")
self.getting_key = False
if self.gamepad and self.gamepad.running:
self.gamepad.close()
if self.wpa_supplicant:
self.wpa_supplicant.stop()

def run_server(self):
LoggerCli.info("Starting server")
normal_interface = Args.args.normal_interface
wii_u_interface = Args.args.wii_u_interface
self.check_interfaces(normal_interface, wii_u_interface)
self.prompt_unmanaged(wii_u_interface)
self.wpa_supplicant = WpaSupplicant()
self.wpa_supplicant.connect(constants.PATH_CONF_CONNECT, wii_u_interface)
self.wpa_supplicant.add_status_change_listener(self.status_changed)
InterfaceUtil.dhclient(wii_u_interface)
InterfaceUtil.set_metric(normal_interface, 0)
InterfaceUtil.set_metric(wii_u_interface, 1)
self.gamepad = Gamepad()
self.gamepad.start()
while self.gamepad.running:
time.sleep(1)

def check_interfaces(self, normal_interface, wii_u_interface):
if normal_interface == wii_u_interface:
LoggerCli.throw(Exception("The Wii U and normal interfaces cannot be the same."))
try:
InterfaceUtil.get_mac(normal_interface)
InterfaceUtil.get_mac(wii_u_interface)
except ValueError:
LoggerCli.throw(Exception("Invalid interface selected."))

def status_changed(self, status):
LoggerCli.info("Connection status changed to %s.", status)
if status in (WpaSupplicant.TERMINATED, WpaSupplicant.NOT_FOUND, WpaSupplicant.DISCONNECTED,
WpaSupplicant.FAILED_START):
self.stop()

def status_changed_key(self, status):
LoggerCli.info("Connection status changed to %s.", status)
if status == WpaSupplicant.DISCONNECTED:
LoggerCli.info("Successfully received PSK from the Wii U.")
self.stop()
elif status in (WpaSupplicant.TERMINATED, WpaSupplicant.NOT_FOUND, WpaSupplicant.FAILED_START):
self.stop()

def get_key(self):
LoggerCli.info("Getting key")
wii_u_interface = Args.args.wii_u_interface
try:
InterfaceUtil.get_mac(wii_u_interface)
except ValueError:
LoggerCli.throw(Exception("Invalid interface selected."))
if len(Args.args.wps_pin) != 4:
LoggerCli.throw(Exception("WPS PIN should be 4 digits"))
self.prompt_unmanaged(wii_u_interface)
self.wpa_supplicant = WpaSupplicant()
self.wpa_supplicant.get_psk(constants.PATH_CONF_CONNECT_TMP, wii_u_interface, Args.args.wps_pin)
self.wpa_supplicant.add_status_change_listener(self.status_changed_key)
self.getting_key = True
while self.getting_key:
time.sleep(1)

def prompt_unmanaged(self, interface):
if not InterfaceUtil.is_managed_by_network_manager(interface):
return
LoggerCli.info("The interface \"%s\" is managed by Network Manager. It must be set to "
"unmanaged to function with DRC Sim. Network manager will not be able to "
"use this interface after it is set to unmanaged.", interface)
response = raw_input("Set %s as unmanaged? (y/n)" % interface)
LoggerCli.debug(response)
if response in ("y", "yes", "Y", "Yes", "YES"):
InterfaceUtil.set_unmanaged_by_network_manager(interface)
else:
LoggerCli.throw(Exception("Interface is managed by Network Manager."))
2 changes: 1 addition & 1 deletion src/server/ui/gui/frame/frame_run_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def wpa_status_changed(self, status):
self.label_wpa_status.config(text=status)
if status == WpaSupplicant.CONNECTED:
LoggerGui.debug("Routing")
InterfaceUtil.dhclient()
InterfaceUtil.dhclient(self.wii_u_interface)
InterfaceUtil.set_metric(self.normal_interface, 0)
InterfaceUtil.set_metric(self.wii_u_interface, 1)
LoggerGui.debug("Starting backend")
Expand Down
4 changes: 2 additions & 2 deletions src/server/util/interface_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ def set_metric(cls, interface, metric):
ProcessUtil.call(["ifmetric", interface, str(metric)])

@classmethod
def dhclient(cls):
def dhclient(cls, interface):
ProcessUtil.call(["killall", "dhclient"])
ProcessUtil.call(["dhclient"])
ProcessUtil.call(["dhclient", interface])

@classmethod
def is_managed_by_network_manager(cls, interface):
Expand Down
2 changes: 0 additions & 2 deletions src/server/util/wpa_supplicant.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,6 @@ def get_psk_thread(self, code):
wii_u_bssids = []
while self.running and scan_tries > 0:
self.psk_thread_cli.sendline("scan")
LoggerWpa.debug("CLI expect waiting for scan start")
self.psk_thread_cli.expect("OK")
LoggerWpa.debug("CLI expect waiting for scan results available event")
self.psk_thread_cli.expect("<3>CTRL-EVENT-SCAN-RESULTS", timeout=60)
self.psk_thread_cli.sendline("scan_results")
Expand Down

0 comments on commit d682a20

Please sign in to comment.