Skip to content

Commit

Permalink
Add register command to server.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomjnixon committed Apr 10, 2012
1 parent 40b6035 commit 7138768
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/commands.py
Expand Up @@ -19,3 +19,4 @@
call = "call"
docall = "docall"
ping = "ping"
register = "register"
21 changes: 20 additions & 1 deletion src/server/meta.py
Expand Up @@ -41,4 +41,23 @@ def ls(self, dir='/'):
@shet_action
def type(self, loc='/'):
return self.server.fs.get_node(loc).type


@shet_action("connections/list")
def list_connections(self):
"""Get a dictionary mapping from connection id to a list of paths owned by
that connection.
"""
return dict((conn.connection_id,
[node.path for node in conn.fs_nodes])
for conn
in self.server.connections)

@shet_action("connections/disconnect")
def disconnect_connection(self, connection_id):
"""Disconnect a connection given it's id."""
self.server.disconnect_id(connection_id)

@shet_action("connections/owner")
def find_owner(self, path):
"""Get the id of the connection that registered the given path."""
return self.server.fs.get_node(path).owner.connection_id
16 changes: 16 additions & 0 deletions src/server/server.py
Expand Up @@ -8,13 +8,15 @@

from shet.server import file_system

import uuid

class ShetServerProtocol(ShetProtocol):

def connectionMade(self):
self.factory.connections.append(self)
self.connected = True
self.on_connection_lost = Deferred()
self.connection_id = str(uuid.uuid1())

self.fs_nodes = []
self.watches = set()
Expand All @@ -37,6 +39,14 @@ def connectionLost(self, reason):
def cmd_ping(self, *args):
return args

@command(commands.register)
def cmd_register(self, connection_id):
"""Set the connection id of this connection. This closes all other
connections with the same id, so it must be unique.
"""
self.factory.disconnect_id(connection_id)
self.connection_id = connection_id

@command(commands.mkprop)
def cmd_mkprop(self, path):
self.fs_nodes.append(file_system.Property(self.factory.fs,
Expand Down Expand Up @@ -156,3 +166,9 @@ class ShetServerFactory(Factory):
def __init__(self):
self.connections = []
self.fs = file_system.FileSystem()

def disconnect_id(self, connection_id):
"""Close all connections with this connection id."""
for connection in self.connections:
if connection.connection_id == connection_id:
connection.transport.loseConnection()

0 comments on commit 7138768

Please sign in to comment.