Skip to content

Commit

Permalink
Double Entry: added comments. Closes Tribler#11.
Browse files Browse the repository at this point in the history
  • Loading branch information
snorberhuis committed Jan 28, 2015
1 parent d047288 commit c655935
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Tribler/community/doubleentry/community.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ def validate_signature(public_key_binary, payload, signature):


class DoubleEntrySettings(object):
"""
Class that contains settings for the double entry community.
"""

def __init__(self):
self.socks_listen_ports = range(9000, 9000)
Expand Down
10 changes: 8 additions & 2 deletions Tribler/community/doubleentry/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@


class DoubleEntryConversion(BinaryConversion):
"""
Class that handles all encoding and decoding of DoubleEntry messages.
"""

def __init__(self, community):
super(DoubleEntryConversion, self).__init__(community, "\x01")
Expand All @@ -27,8 +30,11 @@ def _encode_decode(self, encode, decode, message):
return result

def _encode_signature_request(self, message):
# Encode a tuple containing the timestamp, the signature of the requester and the responder.
# Return (encoding,)
"""
Encode a signature_request message.
:param message: The message to be encoded.
:return: encoded signature request message.
"""
return encode((message.payload.timestamp, message.payload.public_key_requester,
message.payload.signature_requester)),

Expand Down
33 changes: 29 additions & 4 deletions Tribler/community/doubleentry/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
"""
Python file to bootstrap a single DOubleEntry Community.
"""
import os
import sys
import random
Expand All @@ -21,8 +24,15 @@

# Class that handles boot strapping the DoubleEntry Community.
class DoubleEntry(object):
"""
Class that handles boot strapping the DoubleEntry Community when running a single community instance without Tribler.
"""

def __init__(self):
"""
Setup this class to be able to run the community with the run method.
:return:
"""
self.community = None
self._member = None
self.settings = DoubleEntrySettings()
Expand All @@ -35,6 +45,10 @@ def __init__(self):
self.dispersy = self.session.lm.dispersy

def create_tribler_session(self):
"""
Create a tribler session usable to create a DoubleEntry Community.
:return: Tribler.Core.Session
"""
# Setup the tribler session
config = SessionStartupConfig()
config.set_state_dir(os.path.join(BASE_DIR, ".Tribler-%d") % self.settings.socks_listen_ports[0])
Expand All @@ -53,7 +67,10 @@ def create_tribler_session(self):
return Session(config)

def run(self):
# Start the community.
"""
Starts the Double Entry community with a new EC key as ID.
:return:
"""
def start_community():
community = DoubleEntryCommunity
self._member = self.dispersy.get_new_member(u"NID_secp160k1")
Expand All @@ -64,11 +81,16 @@ def start_community():
blockingCallFromThread(reactor, start_community)

def signature_request(self):
"""
Instruct the community to send out a signature request.
"""
self.community.publish_signature_request_message()


# Reads cmdline input to operate the Double Entry community.
class CommandHandler(LineReceiver):
"""
Reads cmdline input to operate the Double Entry community.
"""
from os import linesep
delimiter = linesep

Expand All @@ -78,7 +100,7 @@ def __init__(self, doubleentry):
def connectionMade(self):
self.transport.write('>>> ')

# Process a line received.

def lineReceived(self, line):
if line == 'r':
self.doubleentry.signature_request()
Expand All @@ -87,6 +109,10 @@ def lineReceived(self, line):


def main(argv):
"""
Main method to start a Double Entry community that listens to commandline input.
:param argv:
"""
logging.config.fileConfig("logger.conf")

doubleentry = DoubleEntry()
Expand All @@ -98,6 +124,5 @@ def main(argv):
time.sleep(1)



if __name__ == "__main__":
main(sys.argv[1:])

0 comments on commit c655935

Please sign in to comment.