Skip to content

Commit

Permalink
Peer messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Hoffman committed May 15, 2014
1 parent 1983b9d commit 7b682f9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
3 changes: 1 addition & 2 deletions html/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ angular.module('app')
var socket = new Connection(function(msg) {
switch(msg.type) {
case 'peer':
$scope.add_peer(msg)
console.log('add peer',msg)
$scope.add_peer(msg)
break;
case 'peer_remove':
$scope.remove_peer(msg)
Expand Down
24 changes: 16 additions & 8 deletions node/crypto2crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,22 @@ def __init__(self, my_ip, my_port, market_id):
_dbclient = MongoClient()
self._db = _dbclient.openbazaar

settings = self._db.settings.find_one({'id':market_id})
self.settings = self._db.settings.find_one({'id':"%s"%market_id})
self.market_id = market_id

if settings:
self.nickname = settings['nickname'] if settings.has_key("nickname") else ""
self.secret = settings['secret']
self.pubkey = settings['pubkey']
if self.settings:
self.nickname = self.settings['nickname'] if self.settings.has_key("nickname") else ""
self.secret = self.settings['secret']
self.pubkey = self.settings['pubkey']
else:
self.nickname = 'Default'
key = bitcoin.EllipticCurveKey()
key.new_key_pair()
hexkey = key.secret.encode('hex')
self._db.settings.insert({"id":market_id, "secret":hexkey, "pubkey":bitcoin.GetPubKey(key._public_key.pubkey, False).encode('hex')})
self.settings = self._db.settings.find_one({'id':"%s"%market_id})



# self.nickname, self.secret, self.pubkey = \
# self.load_crypto_details(store_file)
Expand All @@ -80,10 +84,13 @@ def load_crypto_details(self, store_file):

def get_profile(self):
peers = {}

self.settings = self._db.settings.find_one({'id':"%s"%self.market_id})
print 'SETTINGS',self.settings
for uri, peer in self._peers.iteritems():
if peer._pub:
peers[uri] = peer._pub.encode('hex')
return {'uri': self._uri, 'pub': self._myself.get_pubkey().encode('hex'),
return {'uri': self._uri, 'pub': self._myself.get_pubkey().encode('hex'),'nickname': self.nickname,
'peers': peers}

def respond_pubkey_if_mine(self, nickname, ident_pubkey):
Expand Down Expand Up @@ -130,7 +137,7 @@ def create_peer(self, uri, pub):
def send_enc(self, uri, msg):
peer = self._peers[uri]
pub = peer._pub


# Now send a hello message to the peer
if pub:
Expand All @@ -148,6 +155,7 @@ def init_peer(self, msg):

uri = msg['uri']
pub = msg.get('pub')
nickname = msg.get('nickname')
msg_type = msg.get('type')

if not self.valid_peer_uri(uri):
Expand All @@ -170,12 +178,12 @@ def init_peer(self, msg):
# test if we have to update the pubkey
if not self._peers[uri]._pub:
self._log.info("Setting public key for seed node")
print pub
self._peers[uri]._pub = pub.decode('hex')
self.trigger_callbacks('peer', self._peers[uri])

if (self._peers[uri]._pub != pub.decode('hex')):
self._log.info("Updating public key for node")
self._peers[uri]._nickname = nickname
self._peers[uri]._pub = pub.decode('hex')

self.trigger_callbacks('peer', self._peers[uri])
Expand Down
7 changes: 4 additions & 3 deletions node/market.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,12 @@ def load_page(self, welcome):

def save_settings(self, msg):
print "Settings to save",msg
self._db.settings.update({}, msg, True)
print self._transport
self._db.settings.update({'id':'%s'%self._transport.market_id}, {'$set':msg}, True)

def get_settings(self):

settings = self._db.settings.find_one()
print self._transport.market_id
settings = self._db.settings.find_one({'id':'%s'%self._transport.market_id})

if settings:
return { "bitmessage": settings['bitmessage'] if settings.has_key("bitmessage") else "",
Expand Down
2 changes: 2 additions & 0 deletions node/ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,12 @@ def client_shout(self, socket_handler, msg):
# messages coming from "the market"
def on_node_peer(self, peer):
self._log.info("Add peer")

response = {'type': 'peer',
'pubkey': peer._pub.encode('hex')
if peer._pub
else 'unknown',
#'nickname': peer.
'uri': peer._address}
self.send_to_client(None, response)

Expand Down

0 comments on commit 7b682f9

Please sign in to comment.