-
Notifications
You must be signed in to change notification settings - Fork 0
/
project1.ex
270 lines (224 loc) · 7.38 KB
/
project1.ex
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
266
267
268
269
270
defmodule Project1 do
@moduledoc """
Documentation for Project1.
"""
@doc """
Hello world.
## Examples
iex> Project1.hello
:world
"""
def main(args) do
input=Enum.at(args, 0)
if input=~"." do
pid = spawn(Project1 ,:setup_client, [input])
send pid, {:starting_client, "ok to start client"}
receive do
{:stop_client, msg} -> "stop"
end
else
setup_server(input)
end
end
def setup_client(input) do
unless Node.alive?() do
local_node_name = generate_name_client("client")
{:ok, _} = Node.start(local_node_name)
end
#cookie = Application.get_env("server", :cookie)
Node.set_cookie(:"Nikhil-Kanika-cookie")
Node.connect(String.to_atom("server@#{input}"))
IO.puts "Client connected to Server IP #{input}"
end
def setup_server(input) do
lzero=String.to_integer(input)
unless Node.alive?() do
local_node_name = generate_name_server("server")
{:ok, _} = Node.start(local_node_name)
end
Node.set_cookie(:"Nikhil-Kanika-cookie")
#cookie = Application.get_env("server", :cookie)
IO.puts "Server Started"
serverstart(lzero)
end
def generate_name_server(appname) do
{:ok,host}= :inet.gethostname
{:ok,{a,b,c,d}} = :inet.getaddr(host, :inet)
if a==127 do
{:ok, list_ips} = :inet.getif()
ip=list_ips
|> Enum.at(0)
|> elem(0)
|> :inet_parse.ntoa
|> IO.iodata_to_binary
else
ip=Integer.to_string(a)<>"."<>Integer.to_string(b)<>"."<>Integer.to_string(c)<>"."<>Integer.to_string(d)
end
IO.puts "Server IP #{ip}"
String.to_atom("#{appname}1@#{ip}")
end
def generate_name_client(appname) do
{:ok,host}= :inet.gethostname
{:ok,{a,b,c,d}} = :inet.getaddr(host, :inet)
if a==127 do
{:ok, list_ips} = :inet.getif()
ip=list_ips
|> Enum.at(0)
|> elem(0)
|> :inet_parse.ntoa
|> IO.iodata_to_binary
else
ip=Integer.to_string(a)<>"."<>Integer.to_string(b)<>"."<>Integer.to_string(c)<>"."<>Integer.to_string(d)
end
hex = :erlang.monotonic_time() |>
:erlang.phash2(256) |>
Integer.to_string(16)
IO.puts "Client started with IP Address: #{ip}"
#String.to_atom("#{appname}-#{hex}@#{machine}")
String.to_atom("#{appname}-#{hex}@#{ip}")
end
def serverstart(lzero) do
tzero="0"
tzero=generateZeroStr(tzero,lzero)
nodeList = Project1.iterateNodesList(lzero,tzero)
Project1.parentReceiver(nodeList,lzero,tzero)
end
def parentReceiver(prevNodeList,lzero,tzero) do
receive do
{:parentOk, x} ->
pid = Node.spawn(x,Project1,:intermediate,[self(),x,lzero,tzero])
global_counter_length= :ets.update_counter(:countTable,"Length_counter",{2,1})
#IO.puts "im global counter when new node added #{global_counter_length}"#<>inspect global_counter_length
send pid, {:ok, global_counter_length}
after
2_000 ->
presentList=Node.list ++ [Node.self()]
if prevNodeList === presentList do
else
newlyAddedNodeList = presentList -- prevNodeList
prevNodeList = prevNodeList ++ newlyAddedNodeList
Enum.each(newlyAddedNodeList,fn x -> eachNode(x,lzero,tzero) end)
end
end
parentReceiver(prevNodeList,lzero,tzero)
end
def iterateNodesList(lzero, tzero) do
nlist=Node.list
nlist=nlist++[Node.self()]
count_table = :ets.new(:countTable, [:named_table])
:ets.insert(:countTable,{"Length_counter",0})
Enum.each(nlist,fn x -> eachNode(x,lzero,tzero) end)
nlist
end
def findCore(x) do
pid = Node.spawn(x,Project1,:getCoresCount,[self()])
send pid, {:start, "find cores"}
tempCore=0
receive do
{:noOfCores, c} -> tempCore= c
#IO.puts "im getting cores #{c}"
end
tempCore
end
def getCoresCount(parentProcessID) do
receive do
{:start, msg} ->
c = :erlang.system_info(:logical_processors_available)
send parentProcessID, {:noOfCores, c}
end
end
def eachNode(x,lzero,tzero) do
parentProcessID=self()
cores_count= findCore(x)
Enum.each(1..cores_count, fn(k) ->
pid = Node.spawn(x,Project1,:intermediate,[parentProcessID,x,lzero,tzero])
global_counter_length= :ets.update_counter(:countTable,"Length_counter",{2,1})
#IO.puts "im global counter on server start #{global_counter_length}"#<>inspect global_counter_length
send pid, {:ok, global_counter_length}#########
#global_counter_length=global_counter_length+1
end)
end
def intermediate(parentProcessID,x,lzero,tzero) do
receive do
{:ok,length} ->
workUnit=length * 200000
findBitCoin(length, lzero,tzero,workUnit)
send parentProcessID, {:parentOk,x}
end
end
def findBitCoin(length,lzero,tzero,workUnit) when workUnit <= 1 do
end
def findBitCoin(length,lzero,tzero,workUnit) do
randInput = Project1.generateInput(length)
hashInput = Project1.calculateHash(randInput)
if Project1.check_leading_zeros(hashInput,tzero,lzero) ==true do
IO.puts randInput<>" "<>hashInput
else
#IO.puts "not a match"
end
findBitCoin(length,lzero,tzero,workUnit-1)
end
def generateZeroStr(msg, n) when n <= 1 do
msg
end
def generateZeroStr(msg, n) do
msg=msg<>"0"
generateZeroStr(msg, n - 1)
end
def base62(num_bytes \\16) do
Project1.random_bytes(num_bytes)
|> Base.encode64(padding: false)
|> String.replace(~r/[+\/]/, Project1.random_char())
end
def random_bytes(num) do
:crypto.strong_rand_bytes(num)
end
@base62_alphabet 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
def random_char do
Enum.random(@base62_alphabet) |> to_string
end
def generateInput(length) do
"kanikagupta" <> Project1.base62(length)
end
def calculateHash(input) do
#IO.puts "gererating random hash inside hash"
:crypto.hash(:sha256, input) |> Base.encode16
end
def check_leading_zeros(input, tzero,lzero) do
#IO.puts "Checking leading zeroes"
linput=String.slice(input,0..lzero-1)
#IO.puts "Checking leading zeroes linput = " <> linput
if tzero==linput do
true
else
false
end
end
def generateInputString(length, type \\ :all) do
#IO.puts "Node is alive #{Node.alive?}"
#IO.puts "Im before alphas"
alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
numbers = "0123456789"
#characters = "!@#$%^&*(){}[]\|;:\'\'.,<>?/+="
lists =
cond do
type == :alpha -> alphabets <> String.downcase(alphabets)
type == :numeric -> numbers
type == :upcase -> alphabets
type == :downcase -> String.downcase(alphabets)
true -> alphabets <> String.downcase(alphabets) <> numbers
end
|> String.split("", trim: true)
do_generateInputString(length, lists)
end
@doc false
defp get_range(length) when length > 1, do: (1..length)
defp get_range(length), do: [1]
@doc false
defp do_generateInputString(length, lists) do
IO.puts "Im before random"
get_range(length)
|> Enum.reduce([], fn(_, acc) -> [Enum.random(lists) | acc] end)
|> Enum.join("")
end
end