Skip to content

Commit

Permalink
DoubleEntry: added numerical example experiment for Tribler#10.
Browse files Browse the repository at this point in the history
  • Loading branch information
snorberhuis committed Feb 2, 2015
1 parent 39be24a commit 334e324
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Tribler/community/doubleentry/community.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ def validate_signature(public_key_binary, payload, signature):
# Invalid public key.
return False

def get_key(self):
return self._ec

class DoubleEntrySettings(object):
"""
Expand Down
39 changes: 39 additions & 0 deletions Tribler/community/doubleentry/experiments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from Tribler.dispersy.crypto import ECCrypto

__author__ = 'norberhuis'
"""
File containing experiments
"""


class NumericalExample:

def __init__(self, double_entry_community):
self._community = double_entry_community

def perform_experiment(self):
self.save_key_to_file(self._community.get_key())

message = self._community.create_signature_request_message()

self.save_payload_to_file(message.payload.timestamp)
self.save_signature_to_file(message.payload.signature_requester)

@staticmethod
def save_key_to_file(ec):
pem = ECCrypto().key_to_pem(ec)
f = open("key.pem", 'w')
f.write(pem)
f.close()

@staticmethod
def save_payload_to_file(payload):
f = open("payload", 'w')
f.write(payload)
f.close()

@staticmethod
def save_signature_to_file(signature):
f = open("signature", 'w')
f.write(signature)
f.close()
19 changes: 13 additions & 6 deletions Tribler/community/doubleentry/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from Tribler.community.doubleentry.community import DoubleEntryCommunity
from Tribler.community.doubleentry.community import DoubleEntrySettings

from Tribler.community.doubleentry.experiments import NumericalExample

from twisted.internet.threads import blockingCallFromThread
from twisted.internet.stdio import StandardIO
from twisted.protocols.basic import LineReceiver
Expand All @@ -25,13 +27,13 @@
# 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.
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
Expand Down Expand Up @@ -80,6 +82,9 @@ def start_community():

blockingCallFromThread(reactor, start_community)

def get_community(self):
return self.community

def signature_request(self):
"""
Instruct the community to send out a signature request.
Expand All @@ -94,16 +99,18 @@ class CommandHandler(LineReceiver):
from os import linesep
delimiter = linesep

def __init__(self, doubleentry):
self.doubleentry = doubleentry
def __init__(self, double_entry):
self.double_entry = double_entry

def connectionMade(self):
self.transport.write('>>> ')


def lineReceived(self, line):
if line == 'r':
self.doubleentry.signature_request()
self.double_entry.signature_request()
elif line == 'n':
experiment = NumericalExample(self.double_entry.get_community())
experiment.perform_experiment()

self.transport.write('>>> ')

Expand Down

0 comments on commit 334e324

Please sign in to comment.