-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.fs
More file actions
128 lines (103 loc) · 3.28 KB
/
Program.fs
File metadata and controls
128 lines (103 loc) · 3.28 KB
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
open System
//open System.IO
open System.Security.Cryptography
open System.Text;
open System.Diagnostics
#r "nuget: Akka.FSharp"
#r "nuget: Akka.TestKit"
open Akka.Configuration
open Akka.FSharp
//open type System.Math;
open System.Collections.Generic
open Akka.Actor
let system = System.create "my-system" <| ConfigurationFactory.Default()
type Message =
| Message of string
let mutable numOfNodes = 0
(*
printfn "input ready"
let inputLine = Console.ReadLine()
let splitLine = (fun (line : string) -> Seq.toList (line.Split ' '))
let inputParams = splitLine inputLine
let numOfNodes = inputParams.[0] |> int
let topology = inputParams.[1]
let alg = inputParams.[2]
let proc = Process.GetCurrentProcess()
let cpuTime = proc.TotalProcessorTime
let sw = Stopwatch.StartNew()
*)
(*
*)
let convertToSHA1 (arg: string) =
System.Text.Encoding.ASCII.GetBytes arg |> (new SHA1Managed()).ComputeHash
let convertBackToString (sha1: byte[]) =
BitConverter.ToString(sha1).Replace("-", "")
// let mutable listOfActors = []
let mutable keys = []
let mutable nodes = []
let mutable (nodeMappings: int array array) = [|[||]|]
let m = 6.0
let chordActor (id:string) (topologyPosition:int list) = spawn system id <| fun mailbox ->
let rec loop() = actor {
let! msg = mailbox.Receive()
let sender = mailbox.Sender()
// handle an incoming message
return! loop() // store the new s,w into the next state of the actor
}
loop()
let rec getNodeId =
let max = 2.0**m - 1.0
let random = Random()
let nodeId = random.Next((int max))
nodeId
let rec identify (key:int) (index:int) =
if (index >= nodes.Length)
then nodes.[0]
elif key >= nodes.[index]
then nodes.[index]
else identify key (index+1)
let rec map (key:int) (nodeForKey:int) (index: int)=
if (index >= nodeMappings.Length)
then nodeMappings <- Array.append nodeMappings [|[|nodeForKey; key|]|]
else if (nodeForKey = nodeMappings.[index].[0])
then nodeMappings.[index] <- Array.append nodeMappings.[index] [|key|]
else map key nodeForKey (index+1)
let findClosestNode =
printfn "here!!"
printfn "%A" keys
for k in keys do
printfn "here"
let nodeForKey = identify k 0
map k nodeForKey 0
nodeMappings
let generateKeys =
for k in 0..numOfNodes/2 do
let keyInitializer = getNodeId
// let hash = convertBackToString(convertToSHA1(keyInitializer |> string))
// printfn "%i" keyInitializer
keys <- List.append keys [keyInitializer]
keys
let addNodesInArray =
printfn "numOfNodes2 : %i" numOfNodes
for n in 0..numOfNodes do
printfn"here"
let id = getNodeId
// let id_string = id |> string
// let actor = [chordActor id_string ]
nodes <- List.append nodes [id]
// listOfActors <- List.append listOfActors actor
[<EntryPoint>]
let main argv =
numOfNodes <- (int argv.[0])
printfn "Num of nodes: %i" numOfNodes
addNodesInArray
printfn "Nodes: %A" nodes
(*generateKeys
printfn "%A" keys
printfn"Printing list"
findClosestNode
printfn "%A" nodeMappings
for item in nodeMappings do
printfn "%A" item*)
let input2 = System.Console.ReadLine() |> ignore
0 // return an integer exit code