Skip to content

Commit

Permalink
comments, and import fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Sep 13, 2016
1 parent 6573acd commit af1bbf3
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
7 changes: 6 additions & 1 deletion dataplicity/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def disk_poll(self):
)

def poll(self):
"""Called at regulat intervals."""
"""Called at regular intervals."""
t = time.time()
log.debug('poll t=%.02fs', t)
try:
Expand All @@ -122,13 +122,17 @@ def make_sync_id(cls):
return sync_id

def sync(self):
"""Sync with server."""
try:
with self._sync_lock:
self._sync()
except Exception as e:
log.error("sync failed %s", e)

def _sync(self):
"""Perform sync."""
# Syncing is a much simpler process in Dataplicity agent,
# than previous versions.
start = time.time()
sync_id = self.make_sync_id()
try:
Expand Down Expand Up @@ -203,6 +207,7 @@ def set_m2m_identity(self, identity):
"""
Tell the server of our m2m identity, return the identity if it was set,
or None if it could not be set.
"""
if self.auth_token is None:
if not self.disable_sync:
Expand Down
2 changes: 1 addition & 1 deletion dataplicity/jsonrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import json
import logging

from dataplicity.compat import urlopen, text_type
from .compat import urlopen, text_type


log = logging.getLogger('dataplicity')
Expand Down
10 changes: 9 additions & 1 deletion dataplicity/m2mmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from . import constants
from .m2m import WSClient, EchoService
from dataplicity.m2m.remoteprocess import RemoteProcess
from .m2m.remoteprocess import RemoteProcess


log = logging.getLogger('m2m')
Expand Down Expand Up @@ -167,12 +167,14 @@ def m2m_client(self):

@classmethod
def init(cls, client, m2m_url=None):
"""Set up the m2m manager for Dataplicity."""
url = m2m_url or constants.M2M_URL
manager = cls(client, url)
manager.add_terminal('shell', 'bash -i')
return manager

def on_client_close(self):
"""Client is closing, shutdown all terminals."""
for terminal in self.terminals.values():
terminal.close()

Expand Down Expand Up @@ -203,9 +205,11 @@ def add_terminal(self, name, remote_process, user=None, group=None):
self.terminals[name] = Terminal(name, remote_process, user=user, group=group)

def get_terminal(self, name):
"""Get a named terminal."""
return self.terminals.get(name, None)

def on_instruction(self, sender, data):
"""Instructions sent by privileged client."""
log.debug("instruction %r from %s", data, sender)
action = data['action']
if action == 'sync':
Expand All @@ -225,19 +229,23 @@ def on_instruction(self, sender, data):
elif action == 'reboot-device':
log.debug('reboot requested')
self.reboot()
# Unrecognized instructions are ignored

def open_terminal(self, name, port, size=None):
"""Open a new terminal."""
terminal = self.get_terminal(name)
if terminal is None:
log.warning("no terminal called '%s'", name)
return
terminal.launch(self.m2m_client.get_channel(port), size=size)

def open_echo_service(self, port):
"""Open an echo service (ping)."""
log.debug('opening echo service on m2m port %s', port)
EchoService(self.m2m_client.get_channel(port))

def open_portforward(self, service, route):
"""Open a port forward service."""
self.client.port_forward.open_service(service, route)

def reboot(self):
Expand Down
2 changes: 1 addition & 1 deletion dataplicity/subcommand.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import unicode_literals
from __future__ import print_function

from dataplicity.compat import with_metaclass
from .compat import with_metaclass


registry = {}
Expand Down
3 changes: 0 additions & 3 deletions dataplicity/subcommands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ class Run(SubCommand):
"""Run the dataplicity service in the foreground"""
help = """Run dataplicity agent"""

def add_arguments(self, parser):
return parser

def run(self):
client = self.app.make_client()
client.run_forever()

0 comments on commit af1bbf3

Please sign in to comment.