-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainnet.go
More file actions
59 lines (50 loc) · 1.27 KB
/
mainnet.go
File metadata and controls
59 lines (50 loc) · 1.27 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
package main
import (
"fmt"
"time"
"strings"
nt "./network"
)
const (
TO_UPPER = iota + 1 // Signals the numbering of constants. In this case numbering of constants starts with 1.
TO_LOWER
)
const (
ADDRESS = ":8080"
)
/*****************************************************************
The client sends two consecutive requests to the server:
first to translate the string to uppercase, and then to lowercase.
******************************************************************/
func main() {
var (
response = new(nt.Package)
msg = "Hello, World!"
)
go nt.Listen(ADDRESS, handleServer)
time.Sleep(500 * time.Millisecond)
// send "Hello, World!"
// receive "HELLO, WORLD!"
response = nt.Send(ADDRESS, &nt.Package {
Option : TO_UPPER,
Data : msg,
})
fmt.Println(response.Data)
// send «HELLO, WORLD!»
// receive «hello, world!»
response = nt.Send(ADDRESS, &nt.Package {
Option : TO_LOWER,
Data : msg,
})
fmt.Println(response.Data)
}
func handleServer(connect nt.Conn, pack *nt.Package) {
nt.Handle(TO_UPPER, connect, pack, handleToUpper)
nt.Handle(TO_LOWER, connect, pack, handleToLower)
}
func handleToUpper(pack *nt.Package) string {
return strings.ToUpper(pack.Data)
}
func handleToLower(pack *nt.Package) string {
return strings.ToLower(pack.Data)
}