-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproj3.exs
265 lines (214 loc) · 7.58 KB
/
proj3.exs
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
defmodule Proj3 do
def main do
# Input of the nodes, number of request and Algorithm
input = System.argv()
[num_nodes, num_requests, num_fail_nodes] = input
num_nodes = String.to_integer(num_nodes)
num_requests = String.to_integer(num_requests)
num_fail_nodes = String.to_integer(num_fail_nodes)
list_of_hexValues =
Enum.map(1..(num_nodes + 1), fn nodeID ->
String.to_charlist(:crypto.hash(:sha, "#{nodeID}") |> Base.encode16())
end)
# table for linking hash with their pids
:ets.new(:indexed_actors, [:named_table, :public])
:ets.new(:indexed_pids, [:named_table, :public])
# using supervisor to initialise all the workers
children =
Enum.map(list_of_hexValues, fn hash ->
Supervisor.child_spec({Tapestryworker, []}, id: hash, restart: :permanent)
end)
new_num_node = num_nodes + 2
new_hash = String.to_charlist(:crypto.hash(:sha, "#{new_num_node}") |> Base.encode16())
children =
children ++ [Supervisor.child_spec({Tapestryworker, []}, id: new_hash, restart: :permanent)]
Supervisor.start_link(children, strategy: :one_for_one, name: Tapestrysupervisor)
result = Supervisor.which_children(Tapestrysupervisor)
Enum.map(result, fn {hash, pid, _, _} ->
:ets.insert(:indexed_actors, {hash, pid})
end)
Enum.map(result, fn {hash, pid, _, _} ->
:ets.insert(:indexed_pids, {pid, hash})
end)
list_of_pids =
Enum.map(result, fn {_, pid, _, _} ->
pid
end)
list_of_pids = list_of_pids -- [List.last(list_of_pids)]
list_without_newNode = list_of_hexValues -- [List.last(list_of_hexValues)]
create_fault_tol_table(new_hash, list_of_hexValues)
# creating routing tables for all nodes except the last node
Enum.map(list_without_newNode, fn hash_key ->
fill_routing_table(
hash_key,
list_of_hexValues -- [hash_key]
)
end)
# inserting last node as a newNode into the network
new_num_node = List.last(list_of_hexValues)
node_insertion(new_num_node, list_without_newNode)
killed_pids =
Enum.map(Enum.take_random(list_of_pids, num_fail_nodes), fn pid ->
Process.exit(pid, :kill)
pid
end)
killed_hash =
Enum.map(killed_pids, fn x ->
[{_, hash}] = :ets.lookup(:indexed_pids, x)
hash
end)
new_dest_list = list_of_hexValues -- killed_hash
# Start Hopping
Enum.map(new_dest_list, fn source_ID ->
destinationList = Enum.take_random(new_dest_list -- [source_ID], num_requests)
[{_, pid}] = :ets.lookup(:indexed_actors, source_ID)
implementing_tapestry(
source_ID,
pid,
destinationList,
new_hash,
num_requests
)
end)
hopping_list =
Enum.reduce(new_dest_list, [], fn hash_key, list ->
[{_, pid}] = :ets.lookup(:indexed_actors, hash_key)
list ++ [GenServer.call(pid, :getState)]
end)
max_hops = Enum.max(hopping_list)
IO.puts("Maximum Hops = #{max_hops}")
end
def create_fault_tol_table(new_hash, list_of_hexValues) do
:ets.new(:fault_tol_table, [:bag, :named_table, :public])
Enum.map(list_of_hexValues, fn hash_key ->
key = common_prefix(new_hash, hash_key)
:ets.insert(:fault_tol_table, {key, hash_key})
end)
end
def node_insertion(new_num_node, list_without_newNode) do
# Adapting the routing tables of all the current nodes
Enum.map(list_without_newNode, fn neighbor_hash ->
key = common_prefix(neighbor_hash, new_num_node)
handling_insertion(neighbor_hash, new_num_node, key)
end)
# Creating the routing table of the new node
fill_routing_table(new_num_node, list_without_newNode)
end
def implementing_tapestry(
node_ID,
pid,
destinationList,
new_hash,
num_requests
) do
Enum.map(destinationList, fn dest_ID ->
GenServer.cast(
pid,
{:update_next_hop, node_ID, dest_ID, new_hash, 1}
)
if num_requests > 1 and dest_ID != Enum.at(destinationList,length(destinationList)-1) do
# making one request per second
:timer.sleep(1000)
end
end)
end
def fill_routing_table(hash_key, list_of_neighbors) do
Enum.reduce(
list_of_neighbors,
:ets.new(String.to_atom("Table_#{hash_key}"), [:named_table, :public]),
fn neighbor_key, _acc ->
key = common_prefix(hash_key, neighbor_key)
handling_insertion(hash_key, neighbor_key, key)
end
)
end
def handling_insertion(hash_key, neighbor_key, key) do
if :ets.lookup(String.to_atom("Table_#{hash_key}"), key) != [] do
[{_, already_in_map_hexVal}] = :ets.lookup(String.to_atom("Table_#{hash_key}"), key)
{hash_key_integer, _} = Integer.parse(List.to_string(hash_key), 16)
{already_in_map_integer, _} = Integer.parse(List.to_string(already_in_map_hexVal), 16)
{neighbor_key_integer, _} = Integer.parse(List.to_string(neighbor_key), 16)
dist1 = abs(hash_key_integer - already_in_map_integer)
dist2 = abs(hash_key_integer - neighbor_key_integer)
if dist1 < dist2 do
:ets.insert(String.to_atom("Table_#{hash_key}"), {key, already_in_map_hexVal})
else
:ets.insert(String.to_atom("Table_#{hash_key}"), {key, neighbor_key})
end
else
:ets.insert(String.to_atom("Table_#{hash_key}"), {key, neighbor_key})
end
end
def common_prefix(hash_key, neighbor_key) do
Enum.reduce_while(neighbor_key, 0, fn char, level ->
if Enum.at(hash_key, level) == char,
do: {:cont, level + 1},
else: {:halt, {level, List.to_string([char])}}
end)
end
end
defmodule Tapestryworker do
use GenServer
def start_link(_args) do
{:ok, pid} = GenServer.start_link(__MODULE__, 1)
{:ok, pid}
end
def init(hops) do
{:ok, hops}
end
def nextHop(new_node_ID, dest_ID, new_hash, total_hops) do
[{_, pid}] = :ets.lookup(:indexed_actors, new_node_ID)
if Process.alive?(pid) != True do
[{_, new_pid}] = :ets.lookup(:indexed_actors, new_hash)
GenServer.cast(
new_pid,
{:update_fault_hop, new_node_ID, dest_ID, new_hash, total_hops + 1}
)
end
GenServer.cast(
pid,
{:update_next_hop, new_node_ID, dest_ID, new_hash, total_hops}
)
end
def handle_cast(
{:update_fault_hop, new_node_ID, dest_ID, _new_hash, total_hops},
state
) do
key = Proj3.common_prefix(new_node_ID, dest_ID)
values = :ets.lookup(:fault_tol_table, key)
list =
Enum.map(values, fn x ->
{_, hash} = x
hash
end)
if Enum.find(list, fn _x -> dest_ID end) == dest_ID do
state = Enum.max([state, total_hops])
{:noreply, state}
end
state = Enum.max([state, total_hops])
{:noreply, state}
end
def handle_cast(
{:update_next_hop, node_ID, dest_ID, new_hash, total_hops},
state
) do
key = Proj3.common_prefix(node_ID, dest_ID)
[{_, new_node_ID}] = :ets.lookup(String.to_atom("Table_#{node_ID}"), key)
state = Enum.max([state, total_hops])
if(new_node_ID == dest_ID) do
{:noreply, state}
else
nextHop(
new_node_ID,
dest_ID,
new_hash,
total_hops + 1
)
{:noreply, state}
end
end
def handle_call(:getState, _from, state) do
{:reply, state, state}
end
end
Proj3.main()