-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·105 lines (85 loc) · 2.48 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import random
try:
import psyco
psyco.full()
print 'PYSCO ENABLED!!!'
except:
print 'PYSCO NOT ENABLED!!!'
from events import handlers
from globals import *
# create a full have list for the seed
have_list = [PIECE_SIZE for i in range(NUM_PIECES)]
if not NO_SEED_TEST:
wq.enqueue([0, 'ADD_NODE', 1001, 'priority', 'eternal_seed', 0, have_list, SEED_SPEED])
if SEED_LEAVE_TEST:
wq.enqueue([SEED_LEAVE, 'REMOVE_NODE', 1001])
# Open the output files to store the logs
# This just creates the files that are then appended to by the log events
#fpf = open(file_progress_file, 'w')
locf = open(local_file, 'w')
globf = open(global_file, 'w')
distf = open(distance_file, 'w')
pcf = open(piece_count_file, 'w')
#cdf = open(curr_down_file, 'w')
#plf = open(priority_list_file, 'w')
# Close the output files that store the logs
#fpf.close()
locf.close()
globf.close()
distf.close()
pcf.close()
#cdf.close()
#plf.close()
#wq.enqueue([0, 'LOG', 'file_progress', 1001, fpf])
# Initialize the work queue with ADD_NODE operations.
start_times = [random.randint(0,100) for i in range(NUM_NODES)]
start_times.sort()
# Compute the number of pieces each node gets in the NO_SEED_TEST.
have_step = (NUM_PIECES / NUM_NODES)+275
pieces = range(NUM_PIECES)
if LOAD_RATES == True:
sf = open(statefile, 'r')
else:
sf = open(statefile, 'w')
sf.close()
# Check to see if we should be loading node rates from a file
for i in range(NUM_NODES):
have = []
if NO_SEED_TEST:
# Distribute the pieces.
if len(pieces) > have_step:
piece_ids = pieces[0:have_step]
del pieces[0:have_step]
elif len(pieces) == 0:
piece_ids = []
else:
piece_ids = pieces
pieces = []
if i == NUM_NODES - 1:
# The last node gets whatever is left.
piece_ids += pieces
for j in range(NUM_PIECES):
if j in piece_ids:
have.append(PIECE_SIZE)
else:
have.append(0)
if LOAD_RATES == True:
node_id = sf.readline()
node_id = int(node_id)
start_time = sf.readline()
start_time = int(start_time)
down_rate = sf.readline()
down_rate = float(down_rate)
up_rate = sf.readline()
up_rate = float(up_rate)
wq.enqueue([start_time, 'ADD_NODE', node_id, 'priority', LEECHER_ALTRUISM, 0, have, [down_rate, up_rate]])
else:
wq.enqueue([start_times[i], 'ADD_NODE', i, 'priority', LEECHER_ALTRUISM, 0, have, None])
wq.enqueue([101, 'CHECK_DEAD'])
wq.enqueue([5000, 'KILL_SIM'])
sf.close()
# Main queue loop
while not wq.empty():
cur_event = wq.dequeue()
# Call the event handler
handlers[cur_event[1]](cur_event)