- 770B11A71459E33F5F4D2C72CF55DCB79B18C711F3041D8D23375A1419224D309A50C4F10C466527DC7EE910DBDA120ABE877D68E8981C7F51CA4A401BF54775
+ CA409E06CB9491B00378C24BB0A189ED752C01A46B3D8406F403F7E7D378D4A62F91A1ADA8EAB3FFDF374E4A62EA3A5EDF3EDB48CD5500BBDE30F3E8153C209F
blatta/lib/state.py
(1 . 22)(1 . 33)
73 import calendar
74
75 from peer import Peer
76 from message import EMPTY_CHAIN
77 from message import Message
78 import sqlite3
79 import imp
80 import binascii
81 import logging
82 import datetime
83 import time
84 import caribou
85
86 from itertools import chain
87
88 KNOBS=({'max_bounces': 3,
89 KNOBS=(
90 {
91 'max_bounces': 3,
92 'embargo_interval_seconds': 1,
93 'rubbish_interval_seconds': 10,
94 'nick': '',
95 'order_buffer_check_seconds': 5 * 60,
96 'order_buffer_expiration_seconds': 5 * 60,
97 'short_buffer_expiration_seconds': 1,
98 'short_buffer_check_interval_seconds': 1})
99 'short_buffer_check_interval_seconds': 1,
100 'peer_offline_interval_seconds': 60,
101 'peer_away_interval_seconds': 10 * 60,
102 'presence_check_seconds': 5,
103 }
104 )
105
106 class State(object):
107 def __init__(self, station, db_path=None):
(155 . 7)(166 . 7)
109 cursor = self.cursor()
110 at = []
111 if handle == None:
112 results = cursor.execute("select handle_id, address, port, updated_at from at\
113 results = cursor.execute("select handle_id, address, port, updated_at, strftime('%s', updated_at) from at\
114 order by updated_at desc").fetchall()
115 else:
116 result = cursor.execute("select handle_id from handles where handle=?",
(164 . 16)(175 . 27)
118 handle_id = result[0]
119 else:
120 return []
121 results = cursor.execute("select handle_id, address, port, updated_at from at \
122 results = cursor.execute("select handle_id, address, port, updated_at, strftime('%s', updated_at) from at \
123 where handle_id=? order by updated_at desc",
124 (handle_id,)).fetchall()
125 for result in results:
126 handle_id, address, port, updated_at = result
127 handle_id, address, port, updated_at_utc, updated_at_unixtime = result
128 h = cursor.execute("select handle from handles where handle_id=?",
129 (handle_id,)).fetchone()[0]
130 at.append({"handle": h,
131 "address": "%s:%s" % (address, port),
132 "active_at": updated_at if updated_at else "no packets received from this address"})
133 if updated_at_utc:
134 dt_format = '%Y-%m-%d %H:%M:%S.%f'
135 dt_utc = datetime.datetime.strptime(updated_at_utc, dt_format)
136 dt_local = self.utc_to_local(dt_utc)
137 updated_at = datetime.datetime.strftime(dt_local, dt_format)
138 else:
139 updated_at = "no packets received from this address"
140
141 at.append({
142 "handle": h,
143 "address": "%s:%s" % (address, port),
144 "active_at": updated_at,
145 "active_at_unixtime": int(updated_at_unixtime) if updated_at_unixtime else 0
146 })
147 return at
148
149 def import_at_and_wot(self, at_path):
(210 . 7)(232 . 7)
151 (handle_id,)).fetchone()
152
153 # if there are no AT entries for this handle, insert one
154 timestamp = datetime.datetime.now() if set_active_at else None
155 timestamp = datetime.datetime.utcnow() if set_active_at else None
156 if at_entry == None:
157 cursor.execute("insert into at(handle_id, address, port, updated_at) values(?, ?, ?, ?)",
158 (handle_id,
(357 . 7)(379 . 37)
160 peers.append(peer)
161 return peers
162
163
164
165 def handle_is_online(self, handle):
166 # last rubbish message from peer associated with handle is
167 # sufficiently recent
168 at = self.get_at(handle)[0]
169 if at["active_at_unixtime"] > time.time() - int(self.get_knob("peer_offline_interval_seconds")):
170 return True
171 else:
172 return False
173
174 def utc_to_local(self, utc_dt):
175 # get integer timestamp to avoid precision lost
176 timestamp = calendar.timegm(utc_dt.timetuple())
177 local_dt = datetime.datetime.fromtimestamp(timestamp)
178 assert utc_dt.resolution >= datetime.timedelta(microseconds=1)
179 return local_dt.replace(microsecond=utc_dt.microsecond)
180
181 def handle_is_away(self, handle):
182 # last broadcast or dm is sufficiently old
183 cursor = self.cursor()
184 away_interval_seconds = int(self.get_knob("peer_away_interval_seconds"))
185 dt = datetime.datetime.utcfromtimestamp(
186 time.time() - away_interval_seconds
187 )
188 raw_messages = cursor.execute("select message_bytes from log where created_at > ?", (dt,)).fetchall()
189 for message_bytes in raw_messages:
190 int_ts, self_chain, net_chain, speaker, body = Message._unpack_message(message_bytes[0][:])
191 if speaker == handle:
192 return False
193 return True
194
195 def get_peer_by_handle(self, handle):
196 cursor = self.cursor()
197 handle_info = cursor.execute("select handle_id, peer_id from handles where handle=?",