Permalink
Browse files

Working example client

  • Loading branch information...
1 parent 74dfb57 commit 227a90714eaee53c8952a53eb2a2b8beac1a4264 udoprog committed Apr 6, 2010
Showing with 55 additions and 52 deletions.
  1. +2 −2 adc/logger.py
  2. +1 −12 adc/parser.py
  3. +35 −23 adc/types.py
  4. +17 −15 example-client.py
View
@@ -1,5 +1,3 @@
-__all__ = ['DEBUG', 'INFO', 'WARN', 'ERROR', 'NONE'];
-
import datetime;
import collections;
@@ -64,3 +62,5 @@ def error(self, *m):
def setcb(self, sev, cb):
self.callbacks[sev] = cb;
+
+__all__ = ['DEBUG', 'INFO', 'WARN', 'ERROR', 'NONE', 'Logger'];
View
@@ -2,7 +2,7 @@
from pyparsing import *
-from .types import Message
+from .types import *
"""
The following module is a parser based on the ADC specification version 1.0:
@@ -16,12 +16,6 @@ class ADCParser:
ParserElement.setDefaultWhitespaceChars("")
ParserElement.enablePackrat();
- FEATURE_ADD="+"
- FEATURE_REM="-"
- SEPARATOR=" "
- TYPE_SEP=":"
- EOL="\n"
-
"""
separator ::= ' '
"""
@@ -132,31 +126,26 @@ class ADCParser:
"""
b_message_header ::= 'B' command_name separator my_sid
"""
- B_HEADER = ["B"];
b_message_header = Word(B_HEADER, exact=1).setResultsName('type') + command_name + separator + my_sid;
"""
cih_message_header ::= ('C' | 'I' | 'H') command_name
"""
- CIH_HEADER = ["C", "I", "H"];
cih_message_header = (Word(CIH_HEADER, exact=1)).setResultsName('type') + command_name
"""
de_message_header ::= ('D' | 'E') command_name separator my_sid separator target_sid
"""
- DE_HEADER = ["D", "E"];
de_message_header = Word(DE_HEADER, exact = 1).setResultsName('type') + command_name + separator + my_sid + separator + target_sid
"""
f_message_header ::= 'F' command_name separator my_sid separator (('+'|'-') feature_name)+
"""
- F_HEADER = ["F"];
f_message_header = Word(F_HEADER, exact=1).setResultsName('type') + command_name + separator + my_sid + feature_list
"""
u_message_header ::= 'U' command_name separator my_cid
"""
- U_HEADER = ["U"];
u_message_header = Word(U_HEADER, exact=1).setResultsName('type') + command_name + separator + my_cid
"""
View
@@ -8,6 +8,18 @@
B32 = "B32";
STR = "STR";
+FEATURE_ADD="+"
+FEATURE_REM="-"
+SEPARATOR=" "
+TYPE_SEP=":"
+EOL="\n"
+
+B_HEADER = ["B"];
+CIH_HEADER = ["C", "I", "H"];
+DE_HEADER = ["D", "E"];
+F_HEADER = ["F"];
+U_HEADER = ["U"];
+
class Base32:
"""
A read only type to indicate that the containing message should be encoded using Base32
@@ -102,7 +114,7 @@ def __str__(self):
if self.header is None:
return "";
- return ADCParser.SEPARATOR.join([self.header.__str__()] + self.params)
+ return SEPARATOR.join([self.header.__str__()] + self.params)
def get(self, a_key):
"""
@@ -160,34 +172,34 @@ def create(klass, tree_root):
header_type = header_type[0];
- if header_type in ADCParser.B_HEADER:
+ if header_type in B_HEADER:
return BHeader(tree_root);
- if header_type in ADCParser.CIH_HEADER:
+ if header_type in CIH_HEADER:
if header_type == 'C':
return CHeader(tree_root);
if header_type == 'I':
return IHeader(tree_root);
if header_type == 'H':
return HHeader(tree_root);
- if header_type in ADCParser.DE_HEADER:
+ if header_type in DE_HEADER:
if header_type == 'D':
return DHeader(tree_root);
if header_type == 'E':
return EHeader(tree_root);
- if header_type in ADCParser.F_HEADER:
+ if header_type in F_HEADER:
return FHeader(tree_root);
- if header_type in ADCParser.U_HEADER:
+ if header_type in U_HEADER:
return UHeader(tree_root);
return None;
class BHeader(Header):
validates = ['cmd', 'my_sid'];
- types = ADCParser.B_HEADER;
+ types = B_HEADER;
def __init__(self, tree_root=None, **kw):
kw['type'] = 'B';
@@ -203,11 +215,11 @@ def __repr__(self):
return "<BHeader type=" + repr(self.type) + " cmd=" + repr(self.cmd) + " my_sid=" + repr(self.my_sid) + ">"
def __str__(self):
- return ADCParser.SEPARATOR.join([self.type + self.cmd, self.my_sid]);
+ return SEPARATOR.join([self.type + self.cmd, self.my_sid]);
class CIHHeader(Header):
validates = ['cmd'];
- types = ADCParser.CIH_HEADER;
+ types = CIH_HEADER;
def __repr__(self):
return "<CIHHeader type=" + repr(self.type) + " cmd=" + repr(self.cmd) + ">"
@@ -216,29 +228,29 @@ def __str__(self):
return self.type + self.cmd;
class CHeader(CIHHeader):
- types = ADCParser.CIH_HEADER;
+ types = CIH_HEADER;
def __init__(self, *args, **kw):
kw['type'] = 'C';
CIHHeader.__init__(self, *args, **kw);
class IHeader(CIHHeader):
- types = ADCParser.CIH_HEADER;
+ types = CIH_HEADER;
def __init__(self, *args, **kw):
kw['type'] = 'I';
CIHHeader.__init__(self, *args, **kw);
class HHeader(CIHHeader):
- types = ADCParser.CIH_HEADER;
+ types = CIH_HEADER;
def __init__(self, *args, **kw):
kw['type'] = 'H';
CIHHeader.__init__(self, *args, **kw);
class DEHeader(Header):
validates = ['cmd', 'my_sid', 'target_sid']
- types = ADCParser.DE_HEADER;
+ types = DE_HEADER;
def __init__(self, tree_root=None, **kw):
if tree_root:
@@ -254,25 +266,25 @@ def __repr__(self):
return "<DEHeader type=" + repr(self.type) + " cmd=" + repr(self.cmd) + " my_sid=" + repr(self.my_sid) + " target_sid=" + repr(self.target_sid) + ">"
def __str__(self):
- return ADCParser.SEPARATOR.join([self.type + self.cmd, self.my_sid, self.target_sid]);
+ return SEPARATOR.join([self.type + self.cmd, self.my_sid, self.target_sid]);
class DHeader(DEHeader):
- types = ADCParser.DE_HEADER;
+ types = DE_HEADER;
def __init__(self, *args, **kw):
kw['type'] = 'D';
DEHeader.__init__(self, *args, **kw);
class EHeader(DEHeader):
- types = ADCParser.DE_HEADER;
+ types = DE_HEADER;
def __init__(self, *args, **kw):
kw['type'] = 'E';
DEHeader.__init__(self, *args, **kw);
class FHeader(Header):
validates = ['cmd', 'my_sid']
- types = ADCParser.F_HEADER;
+ types = F_HEADER;
def __init__(self, tree_root=None, **kw):
if tree_root:
@@ -281,9 +293,9 @@ def __init__(self, tree_root=None, **kw):
self.rem = list();
for t, f in tree_root.get("feature_list"):
- if t == ADCParser.FEATURE_ADD:
+ if t == FEATURE_ADD:
self.add.append(f);
- elif t == ADCParser.FEATURE_REM:
+ elif t == FEATURE_REM:
self.rem.append(f);
else:
kw['type'] = 'F';
@@ -303,12 +315,12 @@ def __repr__(self):
return "<FHeader type=" + repr(self.type) + " cmd=" + repr(self.cmd) + " my_sid=" + repr(self.my_sid) + " features=" + repr(self.features) + ">"
def __str__(self):
- features = [ADCParser.FEATURE_ADD + feat for feat in self.add] + [ADCParser.FEATURE_REM + feat for feat in self.rem];
- return ADCParser.SEPARATOR.join([self.type + self.cmd, self.my_sid] + features);
+ features = [FEATURE_ADD + feat for feat in self.add] + [FEATURE_REM + feat for feat in self.rem];
+ return SEPARATOR.join([self.type + self.cmd, self.my_sid] + features);
class UHeader(Header):
validates = ['my_cid', 'type'];
- types = ADCParser.U_HEADER;
+ types = U_HEADER;
def __init__(self, tree_root=None, **kw):
if tree_root:
@@ -322,5 +334,5 @@ def __repr__(self):
return "<FHeader type=" + repr(self.type) + " cmd=" + repr(self.cmd) + " my_cid=" + repr(self.my_cid) + ">"
def __str__(self):
- return ADCParser.SEPARATOR.join([self.type + self.cmd, self.my_cid]);
+ return SEPARATOR.join([self.type + self.cmd, self.my_cid]);
View
@@ -1,14 +1,15 @@
from twisted.internet.protocol import Factory, ClientFactory
from twisted.internet import reactor
-from adc.protocol import ADCProtocol
-
import uuid;
-from adc.types import *
+from adc.protocol import ADCProtocol
+# Context
from adc.protocol.helpers import *
-
+# contants, encode/decode functions
+from adc.types import *
+# Logger
from adc.logger import *
class ADCClientToHubProtocol(ADCProtocol):
@@ -28,13 +29,8 @@ def __init__(self):
self.users_by_sid = dict();
def sendInfo(self, **kw):
- base = dict(
- NI=encode(self.factory.nickname),
- SS=encode(self.factory.sharesize)
- );
-
- base.update(kw);
-
+ arguments = dict(NI=encode(self.factory.nickname), SS=encode(self.factory.sharesize));
+ arguments.update(kw);
self.sendFrame(Message(BHeader(cmd='INF', my_sid=encode(self.sid)), **base));
@context(Context.INITIAL)
@@ -61,10 +57,13 @@ def do_isup(self, frame, AD, RM):
self.log.error("No hash method specified, closing connection");
self.transport.loseConnection();
- if not self.cid:
- self.pid = self.hash_method(uuid.uuid1().hex);
- self.cid = self.hash_method(self.pid);
- self.log.debug("Private id:", repr(self.pid));
+ if self.cid:
+ self.log.error("Cid already set, context is invalid");
+ self.transport.loseConnection();
+
+ self.pid = self.hash_method(uuid.uuid1().hex);
+ self.cid = self.hash_method(self.pid);
+ self.log.debug("Private id:", repr(self.pid));
@context(Context.PROTOCOL, IHeader, 'SID')
@parameters(STR)
@@ -189,3 +188,6 @@ def clientConnectionFailed(self, connector, reason):
def entry():
reactor.connectTCP("localhost", 1511, ADCClientToHub(nickname="udoprog", sharesize=1024**5));
reactor.run();
+
+if __name__ == "__main__":
+ entry();

0 comments on commit 227a907

Please sign in to comment.