diff -uNr a/blatta/lib/client.py b/blatta/lib/client.py --- a/blatta/lib/client.py eac411466552678c425d22b90318ad903bcad61213f50ddc38a35dd9db76c771414f6e63e4df35282fef40c3c0b12177b8856dc41be4887307ab871080fc9e3b +++ b/blatta/lib/client.py 2e8badabcb9694cac1263be7cc37e5ddee7e2719520e6a8ca50bee27f1ac28508c38fcf217056e37fc552e74987b4134b3c43d9b96a7d53e0ad0aa214827d6e1 @@ -437,7 +437,7 @@ if peer: self.pest_reply("keys:") for key in peer.keys: - self.pest_reply("%s" % key) + self.pest_reply("'%s'" % key) else: self.pest_reply("unknown peer: %s" % handle) @@ -450,8 +450,11 @@ self.state.add_peer(arguments[0]) self.pest_reply("added new peer %s" % arguments[0]) self.message(":%s JOIN %s" % (arguments[0], self.server.channel_name)) - except: + except Exception, ex: self.pest_reply("error attempting to add peer %s" % arguments[0]) + stack = traceback.format_exc() + logging.debug(ex) + logging.debug(stack) else: self.pest_reply("Usage: PEER ") @@ -461,9 +464,10 @@ self.state.remove_peer(arguments[0]) self.pest_reply("removed peer %s" % arguments[0]) self.message(":%s PART %s" % (arguments[0], self.server.channel_name)) - except Exception, e: - logging.debug(e) - self.pest_reply("Error attempting to remove peer") + except Exception, ex: + self.pest_reply("Error attempting to remove peer: %s" % ex) + stack = traceback.format_exc() + logging.debug(stack) else: self.pest_reply("Usage: UNPEER ") @@ -503,13 +507,14 @@ try: handle, address = arguments address_ip, port = string.split(address, ":") - self.state.update_at({"handle": handle, - "address": address_ip, - "port": port}, - False) + self.state.update_at( + {"handle": handle, + "address": address_ip, + "port": int(port)}, + False) self.pest_reply("updated address table: %s %s" % (handle, address)) except Exception as ex: - self.pest_reply("Error attempting to update address table") + self.pest_reply("Error attempting to update address table: %s" % ex) stack = traceback.format_exc() logging.debug(stack) return diff -uNr a/blatta/lib/infosec.py b/blatta/lib/infosec.py --- a/blatta/lib/infosec.py f6b26aa5d1367c471975ff5e943315e7136ab40d21c7a329e030a2cfffc06b4e8c3c143844560db05b9233087617d571b0d460f0c4ff63e61afc58d79fd4a986 +++ b/blatta/lib/infosec.py 842d56751fd2e61caa10c290815b740c3d4a0170d705c8f47ff4ce5604b73fb82494b3b4cbe656f25df2adc8d348e001c4fb6568b3149bc2f977417d29df4668 @@ -91,7 +91,7 @@ logging.error("aborting send: message modified by station!") return - for peer in self.state.get_keyed_peers(): + for peer in self.state.get_keyed_peers(exclude_addressless=True): # we don't want to send a broadcast back to the originator if message.peer and (peer.peer_id == message.peer.peer_id): @@ -132,7 +132,7 @@ if message.command != IGNORE: logging.debug("packing message bytes: %s" % message.body) else: - logging.debug("packing rubbish message bytes: %s" % binascii.hexlify(message.body)) + logging.debug("packing rubbish message bytes: %s" % binascii.hexlify(message.body)[0:8]) message_bytes = struct.pack(MESSAGE_PACKET_FORMAT, message.timestamp, self_chain, net_chain, speaker, message.body) return message_bytes diff -uNr a/blatta/lib/server.py b/blatta/lib/server.py --- a/blatta/lib/server.py 81527bdb79d3f7c5f42234e6355a530d86fcdc3974a0461782dcd4fe5fc245f8cdca423f4f033cf207224544525ec222a647f8bf56e70bf5a0f04fbb73120d89 +++ b/blatta/lib/server.py 110bba88ae7779dc404b10c2b80f93c6bbbcdf72f05511702cc4afacc22640bdc3c0326fd8d7aaafa92f22e35c39aa808a7ba0cfd892259aa292946fbaec8f17 @@ -1,4 +1,4 @@ -VERSION = "9985" +VERSION = "9984" import os import select diff -uNr a/blatta/lib/state.py b/blatta/lib/state.py --- a/blatta/lib/state.py 034e0af7be67265b08fa39744e1ae5637f0b6bf28021748b964cbc1dcb5147f95b6b94ee94160946c54198669f4dad4f1c98552ade5b16bb78f327c20958777f +++ b/blatta/lib/state.py 19d07b2cc67c9bfd80a99ea29c8b9064d30253381e6afe2a4cb6bb30d329d6a92258d4d9671588896d71b0619973604c07312fe005f1e926a955efbe40784a52 @@ -3,6 +3,7 @@ import imp import hashlib import logging +import datetime from itertools import chain class State(object): @@ -23,8 +24,7 @@ cursor.execute("create table if not exists at(handle_id integer,\ address text not null,\ port integer not null,\ - active_at datetime default null,\ - updated_at datetime default current_timestamp,\ + updated_at datetime default null,\ unique(handle_id, address, port))") cursor.execute("create table if not exists wot(peer_id integer primary key)") @@ -57,7 +57,7 @@ cursor = self.cursor() at = [] if handle == None: - results = cursor.execute("select handle_id,address,port,active_at from at\ + results = cursor.execute("select handle_id, address, port, updated_at from at\ order by updated_at desc").fetchall() else: result = cursor.execute("select handle_id from handles where handle=?", @@ -66,7 +66,7 @@ handle_id = result[0] else: return [] - results = cursor.execute("select handle_id,address,port,active_at from at \ + results = cursor.execute("select handle_id, address, port, updated_at from at \ where handle_id=? order by updated_at desc", (handle_id,)).fetchall() for result in results: @@ -158,25 +158,52 @@ if row != None: handle_id = row[0] else: - return + raise Exception("handle not found") - try: - cursor.execute("insert into at(handle_id, address, port) values(?, ?, ?)", - (handle_id, peer["address"], peer["port"])) - except sqlite3.IntegrityError as ex: - cursor.execute("update at set updated_at = current_timestamp\ - where handle_id=? and address=? and port=?", - (handle_id, peer["address"], peer["port"])) - if set_active_at: - cursor.execute("update at set active_at = current_timestamp\ - where handle_id=? and address=? and port=?", - (handle_id, peer["address"], peer["port"])) + at_entry = cursor.execute("select handle_id, address, port from at where handle_id=?", + (handle_id,)).fetchone() + + # if there are no AT entries for this handle, insert one + timestamp = datetime.datetime.now() if set_active_at else None + if at_entry == None: + cursor.execute("insert into at(handle_id, address, port, updated_at) values(?, ?, ?, ?)", + (handle_id, + peer["address"], + peer["port"], + timestamp)) + logging.debug("inserted new at entry for %s: %s:%d" % ( + peer['handle'], + peer['address'], + peer['port'])) + + # otherwise update the existing entry if it differs + else: + try: + if (at_entry[1] != peer['address'] or + at_entry[2] != peer['port']): + cursor.execute("update at set updated_at = ?,\ + address = ?,\ + port = ?\ + where handle_id=?", + (timestamp, + peer["address"], + peer["port"], + handle_id)) + + logging.debug("updated at entry for %s: %s:%d" % ( + peer['handle'], + peer['address'], + peer['port'])) + except sqlite3.IntegrityError: + cursor.execute("delete from at where handle_id=?", (handle_id,)) + + self.conn.commit() def add_peer(self, handle): cursor = self.cursor() cursor.execute("insert into wot(peer_id) values(null)") - peer_id = self.cursor.lastrowid + peer_id = cursor.lastrowid cursor.execute("insert into handles(peer_id, handle) values(?, ?)", (peer_id, handle)) self.conn.commit() @@ -189,7 +216,7 @@ result = cursor.execute("select peer_id from handles where handle=?", (handle,)).fetchone() if result == None: - return + raise Exception("handle not found") else: peer_id = result[0] # get all aliases @@ -248,7 +275,7 @@ def listify(self, results): return list(chain.from_iterable(results)) - def get_keyed_peers(self): + def get_keyed_peers(self, exclude_addressless=False): cursor = self.cursor() peer_ids = self.listify(cursor.execute("select peer_id from keys").fetchall()) peers = [] @@ -257,7 +284,7 @@ peer = self.get_peer_by_handle(handle) if self.is_duplicate(peers, peer): continue - if peer.address == None or peer.port == None: + if exclude_addressless and (peer.address == None or peer.port == None): continue peers.append(peer) return peers diff -uNr a/blatta/start_test_net.sh b/blatta/start_test_net.sh --- a/blatta/start_test_net.sh 2d26b4e88714111204fdffb51aa603bc94f517781589411bd72be924abe9fc4ddf6f344ebf3dec070580c4211b0fd533b3c8a45617f8c07a685166a33ae3118d +++ b/blatta/start_test_net.sh bb6a2ca2267f79b30c0f393a552ec5cf79bd1b248a71093e72631955fa1f8d637ae3e0b2d5bc6fca90a9615347df64d79bbb390173a4f5b494c192545421e7a9 @@ -1,6 +1,6 @@ #!/bin/bash # start 3 servers on different ports -./blatta --log-level info --channel-name \#aleth --irc-port 9968 --udp-port 7778 --db-path a.db --address-table-path test_net_configs/a.py > logs/a & -./blatta --log-level info --channel-name \#aleth --irc-port 6669 --udp-port 7779 --db-path b.db --address-table-path test_net_configs/b.py > logs/b & +./blatta --log-level debug --channel-name \#aleth --irc-port 9968 --udp-port 7778 --db-path a.db --address-table-path test_net_configs/a.py > logs/a & +# ./blatta --log-level info --channel-name \#aleth --irc-port 6669 --udp-port 7779 --db-path b.db --address-table-path test_net_configs/b.py > logs/b & ./blatta --log-level debug --channel-name \#aleth --irc-port 6670 --udp-port 7780 --db-path c.db --address-table-path test_net_configs/c.py > logs/c &