def __init__(self, total, events, min_anon=0, pseudonyms_per_client=1, round_time_span=2.0, start_time=0, trainer=None, split_size=1): AnonymitySimulator.__init__(self, total, events, min_anon, pseudonyms_per_client, round_time_span, start_time) self.group_online = [] self.split_group = [] self.splits = {} self.join_queue = [] self.offline_clients = [] self.split_size = split_size self.member_online = {} self.round_keeper = Round_Keeper()
def __init__(self, total, events, min_anon = 0, pseudonyms_per_client = 1, round_time_span = 2.0, start_time = 0, trainer = None, split_size = 1): AnonymitySimulator.__init__(self, total, events, min_anon, pseudonyms_per_client, round_time_span, start_time) self.group_online = [] self.split_group = [] self.splits = {} self.join_queue = [] self.offline_clients = [] self.split_size = split_size self.member_online = {} self.round_keeper = Round_Keeper()
class DynamicSplitting(AnonymitySimulator): def __init__(self, total, events, min_anon = 0, pseudonyms_per_client = 1, round_time_span = 2.0, start_time = 0, trainer = None, split_size = 1): AnonymitySimulator.__init__(self, total, events, min_anon, pseudonyms_per_client, round_time_span, start_time) self.group_online = [] self.split_group = [] self.splits = {} self.join_queue = [] self.offline_clients = [] self.split_size = split_size self.member_online = {} self.round_keeper = Round_Keeper() def run(self): group = [] for client in self.clients: self.member_online[client.uid] = client.get_online() if not client.get_online(): self.offline_clients.append(client.uid) else: group.append(client.uid) self.splits[client.uid] = 0 self.split_group.append(group) self.group_online.append(True) self.round_keeper.add_group(group[:],group[:]) AnonymitySimulator.run(self) def is_member_online(self, uid): return self.member_online[uid] def process_events(self, events): events.reverse() delayed_msgs =[] unsent_msgs = [] next_time = self.round_time_span while len(events) > 0: # Move us to the period during the next event current_time = next_time next_time = events[-1][0] + self.round_time_span - (events[-1][0] % self.round_time_span) rounds = (next_time - current_time) / self.round_time_span if rounds > 1: for nym in self.pseudonyms: for cuid in nym.client_rank.keys(): if not self.is_member_online(cuid): nym.client_rank[cuid] += rounds - 1 if not self.clients[cuid].get_online(): nym.client_subrank[cuid] += rounds - 1 quit = {} join_event = False delayed_msgs = self.round_keeper.get_all_round_messages() while len(events) > 0 and events[-1][0] < next_time: event = events.pop() if event[1] == "join": if event[2] in quit: del quit[event[2]] continue self.on_join(event[0], event[2]) elif event[1] == "msg": uid = event[2][0] if uid in self.splits: gid = self.splits[uid] self.round_keeper.add_message_to_group(gid,uid,event) else: self.round_keeper.add_message_from_offline_user(event) elif event[1] == "quit": quit[event[2]] = event[0] else: assert(False) delivered = {} for gid in range(len(self.split_group)): if self.group_online[gid]: for message in self.round_keeper.get_messages_for_group(gid)[:]: if self.on_msg(message[0],message[2]): self.round_keeper.remove_message_from_group(message,gid) delivered[message[2][0]] = True if message not in delayed_msgs: self.on_time += 1 else: msg_time = message[0] + self.round_time_span - \ (message[0] % self.round_time_span) self.delayed_times.append(next_time - msg_time) self.round_keeper.end_group_round(gid) self.group_online[gid] = \ self.round_keeper.get_num_online_members_for_group(gid) \ == len(self.split_group[gid]) if not self.group_online[gid]: assert (gid != 0) else: self.round_keeper.end_global_round_for_group(gid) for nym in self.pseudonyms: if nym.uid in delivered: continue for cuid in nym.client_rank.keys(): if not self.is_member_online(cuid): nym.client_rank[cuid] += 1 if not self.clients[cuid].get_online(): nym.client_subrank[cuid] += 1 for uid, etime in quit.items(): self.on_quit(etime, uid) # No more join / quit events and there are still message posting events # add these to lost messages and break self.lost_messages = self.round_keeper.get_all_messages() def on_join(self, etime, uid): """ Handler for the client join event """ self.clients[uid].set_online(etime) if uid not in self.splits: if uid not in self.join_queue: assert(uid in self.offline_clients) self.join_queue.append(uid) if len(self.join_queue) >= self.split_size: group_idx = len(self.split_group) group = [] for g_uid in self.join_queue: self.offline_clients.remove(g_uid) self.splits[g_uid] = group_idx group.append(g_uid) self.split_group.append(group) self.group_online.append(True) self.round_keeper.add_group(group[:],group[:]) self.join_queue = [] "some clients came online and I made them a group!" else: group_idx = self.splits[uid] self.round_keeper.add_online_member_to_group(uid,group_idx) if self.round_keeper.get_num_round_members_for_group(group_idx) == \ len(self.split_group[group_idx]): self.group_online[group_idx] = True def on_quit(self, etime, uid): """ Handler for the client quit event """ self.clients[uid].set_offline(etime) if uid not in self.splits: # Dynamic splitting bootstrapping code assert (uid in self.join_queue) self.join_queue.remove(uid) elif self.splits[uid] == 0: clients = [] for client in self.clients: if client.uid in self.splits and self.splits[client.uid] == 0 \ and client.uid != uid: clients.append(client) clients.sort(key=lambda client: client.get_online_time(etime)) group_idx = len(self.split_group) self.splits[uid] = group_idx group = [uid] count = self.split_size - 1 if (2 * self.split_size - 1 < len(clients)) \ else len (clients) for idx in range(count): self.splits[clients[idx].uid] = group_idx group.append(clients[idx].uid) self.split_group.append(group) self.split_group[0] = [i for i in self.split_group[0] if i not in group] self.group_online.append(False) self.round_keeper.add_group(group[:],filter(self.is_member_online,group)) for idx in group: for jdx in group: assert(idx in self.pseudonyms[jdx].clients) else: # Terminal splitting code group_idx = self.splits[uid] self.round_keeper.remove_offline_member_from_group(uid,group_idx) def on_msg(self, etime, (uid, msg), tprint = False):
class DynamicSplitting(AnonymitySimulator): def __init__(self, total, events, min_anon=0, pseudonyms_per_client=1, round_time_span=2.0, start_time=0, trainer=None, split_size=1): AnonymitySimulator.__init__(self, total, events, min_anon, pseudonyms_per_client, round_time_span, start_time) self.group_online = [] self.split_group = [] self.splits = {} self.join_queue = [] self.offline_clients = [] self.split_size = split_size self.member_online = {} self.round_keeper = Round_Keeper() def run(self): group = [] for client in self.clients: self.member_online[client.uid] = client.get_online() if not client.get_online(): self.offline_clients.append(client.uid) else: group.append(client.uid) self.splits[client.uid] = 0 self.split_group.append(group) self.group_online.append(True) self.round_keeper.add_group(group[:], group[:]) AnonymitySimulator.run(self) def is_member_online(self, uid): return self.member_online[uid] def process_events(self, events): events.reverse() delayed_msgs = [] unsent_msgs = [] next_time = self.round_time_span while len(events) > 0: # Move us to the period during the next event current_time = next_time next_time = events[-1][0] + self.round_time_span - ( events[-1][0] % self.round_time_span) rounds = (next_time - current_time) / self.round_time_span if rounds > 1: for nym in self.pseudonyms: for cuid in nym.client_rank.keys(): if not self.is_member_online(cuid): nym.client_rank[cuid] += rounds - 1 if not self.clients[cuid].get_online(): nym.client_subrank[cuid] += rounds - 1 quit = {} join_event = False delayed_msgs = self.round_keeper.get_all_round_messages() while len(events) > 0 and events[-1][0] < next_time: event = events.pop() if event[1] == "join": if event[2] in quit: del quit[event[2]] continue self.on_join(event[0], event[2]) elif event[1] == "msg": uid = event[2][0] if uid in self.splits: gid = self.splits[uid] self.round_keeper.add_message_to_group(gid, uid, event) else: self.round_keeper.add_message_from_offline_user(event) elif event[1] == "quit": quit[event[2]] = event[0] else: assert (False) delivered = {} for gid in range(len(self.split_group)): if self.group_online[gid]: for message in self.round_keeper.get_messages_for_group( gid)[:]: if self.on_msg(message[0], message[2]): self.round_keeper.remove_message_from_group( message, gid) delivered[message[2][0]] = True if message not in delayed_msgs: self.on_time += 1 else: msg_time = message[0] + self.round_time_span - \ (message[0] % self.round_time_span) self.delayed_times.append(next_time - msg_time) self.round_keeper.end_group_round(gid) self.group_online[gid] = \ self.round_keeper.get_num_online_members_for_group(gid) \ == len(self.split_group[gid]) if not self.group_online[gid]: assert (gid != 0) else: self.round_keeper.end_global_round_for_group(gid) for nym in self.pseudonyms: if nym.uid in delivered: continue for cuid in nym.client_rank.keys(): if not self.is_member_online(cuid): nym.client_rank[cuid] += 1 if not self.clients[cuid].get_online(): nym.client_subrank[cuid] += 1 for uid, etime in quit.items(): self.on_quit(etime, uid) # No more join / quit events and there are still message posting events # add these to lost messages and break self.lost_messages = self.round_keeper.get_all_messages() def on_join(self, etime, uid): """ Handler for the client join event """ self.clients[uid].set_online(etime) if uid not in self.splits: if uid not in self.join_queue: assert (uid in self.offline_clients) self.join_queue.append(uid) if len(self.join_queue) >= self.split_size: group_idx = len(self.split_group) group = [] for g_uid in self.join_queue: self.offline_clients.remove(g_uid) self.splits[g_uid] = group_idx group.append(g_uid) self.split_group.append(group) self.group_online.append(True) self.round_keeper.add_group(group[:], group[:]) self.join_queue = [] "some clients came online and I made them a group!" else: group_idx = self.splits[uid] self.round_keeper.add_online_member_to_group(uid, group_idx) if self.round_keeper.get_num_round_members_for_group(group_idx) == \ len(self.split_group[group_idx]): self.group_online[group_idx] = True def on_quit(self, etime, uid): """ Handler for the client quit event """ self.clients[uid].set_offline(etime) if uid not in self.splits: # Dynamic splitting bootstrapping code assert (uid in self.join_queue) self.join_queue.remove(uid) elif self.splits[uid] == 0: clients = [] for client in self.clients: if client.uid in self.splits and self.splits[client.uid] == 0 \ and client.uid != uid: clients.append(client) clients.sort(key=lambda client: client.get_online_time(etime)) group_idx = len(self.split_group) self.splits[uid] = group_idx group = [uid] count = self.split_size - 1 if (2 * self.split_size - 1 < len(clients)) \ else len (clients) for idx in range(count): self.splits[clients[idx].uid] = group_idx group.append(clients[idx].uid) self.split_group.append(group) self.split_group[0] = [ i for i in self.split_group[0] if i not in group ] self.group_online.append(False) self.round_keeper.add_group(group[:], filter(self.is_member_online, group)) for idx in group: for jdx in group: assert (idx in self.pseudonyms[jdx].clients) else: # Terminal splitting code group_idx = self.splits[uid] self.round_keeper.remove_offline_member_from_group(uid, group_idx) def on_msg(self, etime, (uid, msg), tprint=False):