blockchain/_example/mainnt.go
2024-03-31 18:46:44 +07:00

50 lines
941 B
Go

package main
import (
nt "blockchain-from-scratch/network"
"fmt"
"strings"
"time"
)
const (
TO_UPPER = iota + 1
TO_LOWER
)
const (
ADDRESS = ":8080"
)
func main() {
var (
res = new(nt.Package)
msg = "Hello, World!"
)
go nt.Listen(ADDRESS, handleServer)
time.Sleep(500 * time.Millisecond)
// send «Hello, World!»
// receive «HELLO, WORLD!»
res = nt.Send(ADDRESS, &nt.Package{
Option: TO_UPPER,
Data: msg,
})
fmt.Println(res.Data)
// send «HELLO, WORLD!»
// receive «hello, world!»
res = nt.Send(ADDRESS, &nt.Package{
Option: TO_LOWER,
Data: res.Data,
})
fmt.Println(res.Data)
}
func handleServer(conn nt.Conn, pack *nt.Package) {
nt.Handle(TO_UPPER, conn, pack, handleToUpper)
nt.Handle(TO_LOWER, conn, pack, handleToLower)
}
func handleToUpper(pack *nt.Package) string {
return strings.ToUpper(pack.Data)
}
func handleToLower(pack *nt.Package) string {
return strings.ToLower(pack.Data)
}