- 034E0AF7BE67265B08FA39744E1AE5637F0B6BF28021748B964CBC1DCB5147F95B6B94EE94160946C54198669F4DAD4F1C98552ADE5B16BB78F327C20958777F+ 19D07B2CC67C9BFD80A99EA29C8B9064D30253381E6AFE2A4CB6BB30D329D6A92258D4D9671588896D71B0619973604C07312FE005F1E926A955EFBE40784A52blatta/lib/state.py(3 . 6)(3 . 7)
94 import imp
95 import hashlib
96 import logging
97 import datetime
98 from itertools import chain
99
100 class State(object):
(23 . 8)(24 . 7)
102 cursor.execute("create table if not exists at(handle_id integer,\
103 address text not null,\
104 port integer not null,\
105 active_at datetime default null,\
106 updated_at datetime default current_timestamp,\
107 updated_at datetime default null,\
108 unique(handle_id, address, port))")
109
110 cursor.execute("create table if not exists wot(peer_id integer primary key)")
(57 . 7)(57 . 7)
112 cursor = self.cursor()
113 at = []
114 if handle == None:
115 results = cursor.execute("select handle_id,address,port,active_at from at\
116 results = cursor.execute("select handle_id, address, port, updated_at from at\
117 order by updated_at desc").fetchall()
118 else:
119 result = cursor.execute("select handle_id from handles where handle=?",
(66 . 7)(66 . 7)
121 handle_id = result[0]
122 else:
123 return []
124 results = cursor.execute("select handle_id,address,port,active_at from at \
125 results = cursor.execute("select handle_id, address, port, updated_at from at \
126 where handle_id=? order by updated_at desc",
127 (handle_id,)).fetchall()
128 for result in results:
(158 . 25)(158 . 52)
130 if row != None:
131 handle_id = row[0]
132 else:
133 return
134 raise Exception("handle not found")
135
136 try:
137 cursor.execute("insert into at(handle_id, address, port) values(?, ?, ?)",
138 (handle_id, peer["address"], peer["port"]))
139 except sqlite3.IntegrityError as ex:
140 cursor.execute("update at set updated_at = current_timestamp\
141 where handle_id=? and address=? and port=?",
142 (handle_id, peer["address"], peer["port"]))
143 if set_active_at:
144 cursor.execute("update at set active_at = current_timestamp\
145 where handle_id=? and address=? and port=?",
146 (handle_id, peer["address"], peer["port"]))
147 at_entry = cursor.execute("select handle_id, address, port from at where handle_id=?",
148 (handle_id,)).fetchone()
149
150 # if there are no AT entries for this handle, insert one
151 timestamp = datetime.datetime.now() if set_active_at else None
152 if at_entry == None:
153 cursor.execute("insert into at(handle_id, address, port, updated_at) values(?, ?, ?, ?)",
154 (handle_id,
155 peer["address"],
156 peer["port"],
157 timestamp))
158 logging.debug("inserted new at entry for %s: %s:%d" % (
159 peer['handle'],
160 peer['address'],
161 peer['port']))
162
163 # otherwise update the existing entry if it differs
164 else:
165 try:
166 if (at_entry[1] != peer['address'] or
167 at_entry[2] != peer['port']):
168 cursor.execute("update at set updated_at = ?,\
169 address = ?,\
170 port = ?\
171 where handle_id=?",
172 (timestamp,
173 peer["address"],
174 peer["port"],
175 handle_id))
176
177 logging.debug("updated at entry for %s: %s:%d" % (
178 peer['handle'],
179 peer['address'],
180 peer['port']))
181 except sqlite3.IntegrityError:
182 cursor.execute("delete from at where handle_id=?", (handle_id,))
183
184
185 self.conn.commit()
186
187 def add_peer(self, handle):
188 cursor = self.cursor()
189 cursor.execute("insert into wot(peer_id) values(null)")
190 peer_id = self.cursor.lastrowid
191 peer_id = cursor.lastrowid
192 cursor.execute("insert into handles(peer_id, handle) values(?, ?)",
193 (peer_id, handle))
194 self.conn.commit()
(189 . 7)(216 . 7)
196 result = cursor.execute("select peer_id from handles where handle=?",
197 (handle,)).fetchone()
198 if result == None:
199 return
200 raise Exception("handle not found")
201 else:
202 peer_id = result[0]
203 # get all aliases
(248 . 7)(275 . 7)
205 def listify(self, results):
206 return list(chain.from_iterable(results))
207
208 def get_keyed_peers(self):
209 def get_keyed_peers(self, exclude_addressless=False):
210 cursor = self.cursor()
211 peer_ids = self.listify(cursor.execute("select peer_id from keys").fetchall())
212 peers = []
(257 . 7)(284 . 7)
214 peer = self.get_peer_by_handle(handle)
215 if self.is_duplicate(peers, peer):
216 continue
217 if peer.address == None or peer.port == None:
218 if exclude_addressless and (peer.address == None or peer.port == None):
219 continue
220 peers.append(peer)
221 return peers