-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client_for_next.fsx
93 lines (77 loc) · 2.97 KB
/
Client_for_next.fsx
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
#r "nuget: Akka.FSharp"
#r "nuget: Akka.TestKit"
#r "nuget: Akka.Remote"
#load "FindCoin.fsx"
open System
open Akka.FSharp
open Akka.Remote
open Akka.Configuration
open FindCoin
let configuration =
ConfigurationFactory.ParseString(
@"akka {
actor {
provider = ""Akka.Remote.RemoteActorRefProvider, Akka.Remote""
}
remote {
helios.tcp {
port = 2552
hostname = localhost
}
}
}")
let clientSystem = System.create "Client" configuration
let serverIP = "akka.tcp://[email protected]:9003/user/server"
let serverRef = select (serverIP) clientSystem
let actor_num = 10
type MessageSystem =
| TransitMsg of int * string * string
let myActor (mailbox: Actor<_>) =
let rec loop() = actor {
let! TransitMsg(n, content, param) = mailbox.Receive()
let sender = mailbox.Sender()
match content with
| "go to work" -> printfn "local actor %d start to work" n ; FindCoin.findCoin(param, 7)
| _ -> printfn "actor don't understand"
let returnMsg = sprintf "bitcoin;%d;%s;" n "bincoin sequence"
sender <! returnMsg
return! loop()
}
loop()
let myMonitor (mailbox: Actor<string>) =
let mutable i = 0
let mutable n = 0
let mutable actori = 0
let actorArray = Array.create actor_num (spawn clientSystem "myActor" myActor)
{0..actor_num-1} |> Seq.iter (fun a ->
actorArray.[a] <- spawn clientSystem (string a) myActor
()
)
// let serverRef = select (parseMsg.[1]) myMonitor
let rec loop() =
actor {
let! msg = mailbox.Receive()
let sender = mailbox.Sender()
let parseMsg = msg.Split ';'
// "content;id/addr;paramter"
match parseMsg.[0] with
| "start" -> n <- int(parseMsg.[2]);{i..i+n-1} |> Seq.iter(fun a ->
actorArray.[a] <! TransitMsg(a, "go to work", "init string")
()
); i <- n+i
| "bitcoin" -> printfn "bitcoin: %s" parseMsg.[2];
actori <- int(parseMsg.[1]);
actorArray.[actori] <! TransitMsg(actori, "go to work", parseMsg.[2]);
let remoteMsg = sprintf "remote bitcoin; ;parseMsg.[2]"
serverRef <! remoteMsg
| "find nothing" -> actori <- int(parseMsg.[1]);
actorArray.[actori] <! TransitMsg(actori, "go to work", parseMsg.[2]);
| _ -> printfn "manager doesn't understand"
return! loop()
}
loop()
let clientRef = spawn clientSystem "client" myMonitor
let myIP = "akka.tcp://[email protected]:2552/user/client"
let regisMsg = sprintf "register;%s; " myIP
serverRef <! regisMsg;;
Console.ReadLine()