Permalink
Browse files

Add interfaces for new transports

  • Loading branch information...
cpacia committed Mar 9, 2016
1 parent a49f3e8 commit f5393e6f6d29dd5e9840a3c0e95ea498266f9b5b
Showing with 88 additions and 4 deletions.
  1. +1 −1 daemon.py
  2. +83 −0 interfaces.py
  3. +4 −2 net/wireprotocol.py
  4. +0 −1 openbazaard.py
View
@@ -1,5 +1,5 @@
__author__ = 'chris'
import sys, os, time, atexit
import sys, os, atexit
from signal import SIGTERM
View
@@ -3,6 +3,89 @@
from zope.interface import Interface, Attribute
class Multiplexer(Interface):
"""
This interface defines the structure of the protocol class that handles creating new network connections
and sending and receiving messages. At present this is only used by the OpenBazaarProtocol class which
is the protocol for our UDP server. In the future if we want to add additional transports, like I2P, we
they will need to implement this interface so as to not break the rest of the code.
"""
processors = Attribute("""A list of `MessageProcessors`""")
testnet = Attribute("""`bool` are we using testnet""")
vendors = Attribute("""A list `dht.node.Node` vendors""")
ws = Attribute("""The websocket API server""")
blockchain = Attribute("""The `LibbitcoinClient` instance""")
def register_processor(processor):
"""
A method add a `MessageProcessor` to the processors attribute.
"""
def unregister_processor(processor):
"""
Remove a `MessageProcessor` from the processors list.
"""
def set_servers(ws, blockchain):
"""
Set the ws and blockchain attributes.
"""
def send_message(datagram, address, relay_addr):
"""
Send a message over the wire to the given address
Args:
datagram: the serialized message to send
address: the recipients address `tuple`
relay_addr: a replay address `tuple` if used, otherwise None
"""
def __getitem__(addr):
"""
Return the `Connection` of the given address.
Args:
addr: Tuple of destination address (ip, port).
Raises:
KeyError: No connection is handling the given address.
"""
class ConnectionHandler(Interface):
"""
A handler class for each connection.
"""
connection = Attribute("""a `Connection` object for this handler""")
node = Attribute("""a `dht.node.Node` object for the peer. This may be set after receiving the first message""")
processors = Attribute("""A list of `MessageProcessors`""")
def receive_message(datagram):
"""
Receive a datagram over the wire.
"""
def check_new_connection():
"""
Return True if this is the first time this is called else False
"""
class Connection(Interface):
"""
A class representing a connection to a remote peer
"""
handler = Attribute("""a `ConnectionHandler` object for this connection""")
state = Attribute("""a `txrudp.connection.State` enum showing this connection's state""")
def send_message(message):
"""
Send the serialized message to the remote peer.
"""
class MessageProcessor(Interface):
"""
This is an interface for processing messages coming off the wire. Classes that implement this interface should be
View
@@ -7,7 +7,7 @@
from config import SEEDS
from dht.node import Node
from dht.utils import digest
from interfaces import MessageProcessor
from interfaces import MessageProcessor, Multiplexer, ConnectionHandler
from log import Logger
from protos.message import Message, PING, NOT_FOUND
from protos.objects import RESTRICTED, FULL_CONE
@@ -18,6 +18,7 @@
from txrudp.crypto_connection import CryptoConnectionFactory
from txrudp.rudp import ConnectionMultiplexer
from zope.interface.verify import verifyObject
from zope.interface import implements
class OpenBazaarProtocol(ConnectionMultiplexer):
@@ -27,6 +28,7 @@ class OpenBazaarProtocol(ConnectionMultiplexer):
of all connections, parses messages coming off the wire and passes them off to
the appropriate classes for processing.
"""
implements(Multiplexer)
def __init__(self, db, ip_address, nat_type, testnet=False, relaying=False):
"""
@@ -50,6 +52,7 @@ def __init__(self, db, ip_address, nat_type, testnet=False, relaying=False):
ConnectionMultiplexer.__init__(self, CryptoConnectionFactory(self.factory), self.ip_address[0], relaying)
class ConnHandler(Handler):
implements(ConnectionHandler)
def __init__(self, processors, nat_type, relay_node, *args, **kwargs):
super(OpenBazaarProtocol.ConnHandler, self).__init__(*args, **kwargs)
@@ -59,7 +62,6 @@ def __init__(self, processors, nat_type, relay_node, *args, **kwargs):
self.node = None
self.relay_node = relay_node
self.addr = None
self.ban_score = None
self.is_new_node = True
self.on_connection_made()
self.time_last_message = 0
View
@@ -7,7 +7,6 @@
import stun
import sys
import time
import urllib2
from api.ws import WSFactory, AuthenticatedWebSocketProtocol, AuthenticatedWebSocketFactory
from api.restapi import RestAPI
from config import DATA_FOLDER, KSIZE, ALPHA, LIBBITCOIN_SERVER,\

0 comments on commit f5393e6

Please sign in to comment.