- 5E6216FB339F461E197C6C2F878148CBF2032CE6C433F83DF2C29E88B31C467F9C8B689E14D5B0471A6275E7A40E286AC9DBEA69C60D3A16171E4BAE6B091339
+ 7621F8A10842A06CBD590E6DC7460985C762458875771369D1707F24D6449021762A24277E9B1BBB017F5DF601219C8B80A38E6142861503EC7DC265F013A92A
blatta/lib/state.py
(13 . 6)(13 . 7)
367 self.cursor.execute("create table if not exists at(handle_id integer,\
368 address text not null,\
369 port integer not null,\
370 active_at datetime default null,\
371 updated_at datetime default current_timestamp,\
372 unique(handle_id, address, port))")
373
(37 . 7)(38 . 7)
375 def get_at(self, handle=None):
376 at = []
377 if handle == None:
378 results = self.cursor.execute("select handle_id,address,port,updated_at from at\
379 results = self.cursor.execute("select handle_id,address,port,active_at from at\
380 order by updated_at desc").fetchall()
381 else:
382 result = self.cursor.execute("select handle_id from handles where handle=?",
(46 . 7)(47 . 7)
384 handle_id = result[0]
385 else:
386 return []
387 results = self.cursor.execute("select handle_id,address,port,updated_at from at \
388 results = self.cursor.execute("select handle_id,address,port,active_at from at \
389 where handle_id=? order by updated_at desc",
390 (handle_id,)).fetchall()
391 for result in results:
(55 . 7)(56 . 7)
393 (handle_id,)).fetchone()[0]
394 at.append({"handle": h,
395 "address": "%s:%s" % (address, port),
396 "updated_at": updated_at})
397 "active_at": updated_at})
398 return at
399
400
(96 . 30)(97 . 33)
402 self.cursor.execute("insert into handles(peer_id, handle) values(?, ?)",
403 (peer_id, peer["name"]))
404 handle_id = self.cursor.lastrowid
405 self.cursor.execute("insert into at(handle_id, address, port) values(?, ?, ?)",
406 (handle_id, peer["address"], peer["port"]))
407 self.cursor.execute("insert into at(handle_id, address, port, updated_at) values(?, ?, ?, ?)",
408 (handle_id, peer["address"], peer["port"], None))
409 self.cursor.execute("insert into keys(peer_id, key) values(?, ?)",
410 (peer_id, key))
411
412 self.conn.commit()
413
414 def update_address_table(self, peer):
415 def update_address_table(self, peer, set_active_at=True):
416 row = self.cursor.execute("select handle_id from handles where handle=?",
417 (peer["handle"],)).fetchone()
418 if row != None:
419 handle_id = row[0]
420 else:
421 return
422
423
424 try:
425 self.cursor.execute("insert into at(handle_id, address, port) values(?, ?, ?)",
426 (handle_id, peer["address"], peer["port"]))
427 self.conn.commit()
428 except sqlite3.IntegrityError as ex:
429 self.cursor.execute("update at set updated_at = current_timestamp\
430 where handle_id=? and address=? and port=?",
431 (handle_id, peer["address"], peer["port"]))
432 self.conn.commit()
433 if set_active_at:
434 self.cursor.execute("update at set active_at = current_timestamp\
435 where handle_id=? and address=? and port=?",
436 (handle_id, peer["address"], peer["port"]))
437 self.conn.commit()
438
439 def add_peer(self, handle):
440 self.cursor.execute("insert into wot(peer_id) values(null)")
(127 . 6)(131 . 7)
442 self.cursor.execute("insert into handles(peer_id, handle) values(?, ?)",
443 (peer_id, handle))
444 self.conn.commit()
445
446
447 def remove_peer(self, handle):
448 # get peer id