-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_go.go
248 lines (228 loc) · 6.14 KB
/
client_go.go
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
package main
import (
"../bclass"
"bufio"
"flag"
"fmt"
"github.com/arcaneiceman/GoVector/govec"
"github.com/gonum/matrix/mat64"
"io/ioutil"
"net"
"os"
"strconv"
"strings"
)
const BUFFSIZE = 1048576
var (
cnum int = 0
modeldeg int = 2
modellam float64 = 0.01
name string
inputargs []string
myaddr *net.TCPAddr
svaddr *net.TCPAddr
model bclass.Model
logger *govec.GoLog
x *mat64.Dense
y *mat64.Dense
xt *mat64.Dense
yt *mat64.Dense
l *net.TCPListener
gmodel bclass.GlobalModel
gempty bclass.GlobalModel
isjoining bool = true
)
type message struct {
Id int
NodeIp string
NodeName string
Type string
C int
D int
Model bclass.Model
GModel bclass.GlobalModel
}
func main() {
//Parsing inputargs
parseArgs()
//Initialize stuff
model = bclass.RegLSBasisC(x, y, modellam, modeldeg)
//Initialize TCP Connection and listener
l, _ = net.ListenTCP("tcp", myaddr)
fmt.Printf("Node initialized as %v.\n", name)
go listener()
for isjoining {
requestJoin()
}
//Main function of this server
for {
parseUserInput()
}
}
func listener() {
for {
conn, err := l.AcceptTCP()
checkError(err)
if err == nil {
go connHandler(conn)
}
}
}
func connHandler(conn *net.TCPConn) {
p := make([]byte, BUFFSIZE)
conn.Read(p)
var msg message
logger.UnpackReceive("Received message", p, &msg)
switch msg.Type {
case "test_request":
// server is asking me to test
conn.Write([]byte("OK"))
go testModel(msg.Id, msg.Model)
case "global_grant":
// server is sending global model
conn.Write([]byte("OK"))
gmodel = msg.GModel
fmt.Printf("\n <-- Pulled global model from server.\nEnter command: ")
default:
// respond to ping
conn.Write([]byte("Unknown command."))
}
conn.Close()
}
func parseUserInput() {
var ident string
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter command: ")
text, _ := reader.ReadString('\n')
//Windows adds its own strange carriage return, the following lines fix it
if text[len(text)-2] == '\r' {
ident = text[0 : len(text)-2]
} else {
ident = text[0 : len(text)-1]
}
switch ident {
case "read":
x = readData(inputargs[3])
y = readData(inputargs[4])
fmt.Printf(" --- Local data updated.\n")
case "train":
model = bclass.RegLSBasisC(x, y, modellam, modeldeg)
yh := model.Predict(x)
c, d := bclass.TestResults(yh, y)
fmt.Printf(" --- Local model accuracy on local data is: %v.\n", float64(c)/float64(d))
case "push":
yh := model.Predict(x)
c, d := bclass.TestResults(yh, y)
requestCommit(c, d)
case "pull":
requestGlobal()
case "valid":
yh := gmodel.Predict(x)
c, d := bclass.TestResults(yh, y)
fmt.Printf(" --- Global model accuracy on local data is: %v.\n", float64(c)/float64(d))
case "test":
yh := model.Predict(xt)
c, d := bclass.TestResults(yh, yt)
fmt.Printf(" --- Local model accuracy on test data is: %v.\n", float64(c)/float64(d))
case "testg":
yh := gmodel.Predict(xt)
c, d := bclass.TestResults(yh, yt)
fmt.Printf(" --- Global model accuracy on test data is: %v.\n", float64(c)/float64(d))
case "who":
fmt.Printf("%v\n", name)
default:
fmt.Printf(" Command not recognized: %v.\n\n", ident)
fmt.Printf(" Choose from the following commands\n")
fmt.Printf(" read -- Read data from disk\n")
fmt.Printf(" push -- Push trained model to server\n")
fmt.Printf(" pull -- Obtain global model from server\n")
fmt.Printf(" train -- Train model from data (reports error)\n")
fmt.Printf(" valid -- Validate global model with local data\n")
fmt.Printf(" test -- Test local model with test data\n")
fmt.Printf(" testg -- Test global model with test data\n")
fmt.Printf(" who -- Print node name\n\n")
}
}
func requestJoin() {
//msg := message{cnum, myaddr.String(), name, "join_request", 0, 0, model, gempty}
msg := message{cnum, myaddr.String(), name, "join_request", 0, 0, model, gempty}
fmt.Printf(" --> Asking server to join.")
tcpSend(msg)
}
func requestCommit(c, d int) {
cnum++
msg := message{cnum, myaddr.String(), name, "commit_request", c, d, model, gempty}
fmt.Printf(" --> Pushing local model to server.")
tcpSend(msg)
}
func requestGlobal() {
msg := message{cnum, myaddr.String(), name, "global_request", 0, 0, model, gempty}
fmt.Printf(" --> Requesting global model from server.")
tcpSend(msg)
}
func testModel(id int, testmodel bclass.Model) {
fmt.Printf("\n <-- Received test requset.\nEnter command: ")
yh := testmodel.Predict(x)
c, d := bclass.TestResults(yh, y)
msg := message{id, myaddr.String(), name, "test_complete", c, d, testmodel, gempty}
fmt.Printf("\n --> Sending completed test requset.")
tcpSend(msg)
fmt.Printf("Enter command: ")
}
func tcpSend(msg message) {
p := make([]byte, BUFFSIZE)
conn, err := net.DialTCP("tcp", nil, svaddr)
checkError(err)
outbuf := logger.PrepareSend(msg.Type, msg)
_, err = conn.Write(outbuf)
checkError(err)
n, _ := conn.Read(p)
if string(p[:n]) == "OK" {
fmt.Printf(" [OK]\n")
} else if string(p[:n]) == "Joined" {
fmt.Printf(" [OK]\n")
isjoining = false
} else {
fmt.Printf(" [NO!]\n *** Request was denied by server: %v.\nEnter command: ", string(p[:n]))
}
}
func readData(filename string) *mat64.Dense {
dat, err := ioutil.ReadFile(filename)
checkError(err)
array := strings.Split(string(dat), "\n")
r := len(array) - 1
temp := strings.Split(array[0], ",")
c := len(temp)
vdat := make([]float64, c*r)
for i := 0; i < r; i++ {
temp = strings.Split(array[i], ",")
for j := 0; j < c; j++ {
vdat[i*c+j], _ = strconv.ParseFloat(temp[j], 64)
}
}
return mat64.NewDense(r, c, vdat)
}
func parseArgs() {
flag.Parse()
inputargs = flag.Args()
var err error
if len(inputargs) < 2 {
fmt.Printf("Not enough inputs.\n")
return
}
name = inputargs[0]
myaddr, err = net.ResolveTCPAddr("tcp", inputargs[1])
checkError(err)
svaddr, err = net.ResolveTCPAddr("tcp", inputargs[2])
x = readData(inputargs[3])
y = readData(inputargs[4])
xt = readData(inputargs[5])
yt = readData(inputargs[6])
logger = govec.Initialize(inputargs[0], inputargs[7])
}
func checkError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
//os.Exit(1)
}
}