-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
150 lines (134 loc) · 2.53 KB
/
main.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
package main
import (
"bufio"
"encoding/json"
"fmt"
"net"
"os"
"strings"
)
type Address struct {
IPv4 string
Port string
}
type Node struct {
Connections map[string]bool
Address Address
}
type Package struct {
From string
To string
Data string
}
// ./main 8080
func init() {
if len(os.Args) != 2 {
panic("not 2 args")
}
}
func main() {
NewNode(os.Args[1]).Run(handleServer, handleClient)
}
// ipv4:port
func NewNode(address string) *Node {
splited := strings.Split(address, ":")
if len(splited) != 2 {
return nil
}
return &Node{
Address: Address{
IPv4: splited[0],
Port: ":" + splited[1],
},
Connections: make(map[string]bool),
}
}
func (node *Node) Run(handleServer func(*Node), handleClient func(*Node)) {
go handleServer(node)
handleClient(node)
}
func handleServer(node *Node) {
listen, err := net.Listen("tcp", "0.0.0.0"+node.Address.Port)
if err != nil {
panic("Listen error!")
}
defer listen.Close()
for {
conn, err := listen.Accept()
if err != nil {
break
}
go handleConnection(node, conn)
}
}
func handleConnection(node *Node, conn net.Conn) {
defer conn.Close()
var (
buffer = make([]byte, 512)
message string
pack Package
)
for {
length, err := conn.Read(buffer)
if err != nil {
break
}
message += string(buffer[:length])
}
err := json.Unmarshal([]byte(message), &pack)
if err != nil {
return
}
node.ConnectTo([]string{pack.From})
fmt.Println(pack.Data)
}
func handleClient(node *Node) {
for {
message := InputString()
splited := strings.Split(message, " ")
switch splited[0] {
case "/exit":
os.Exit(0)
case "/connect":
node.ConnectTo(splited[1:])
case "/network":
node.PrintNetwork()
default:
node.SendMessageToAll(message)
}
}
}
func (node *Node) PrintNetwork() {
for addr := range node.Connections {
fmt.Println("|", addr)
}
}
func (node *Node) ConnectTo(addresses []string) {
for _, addr := range addresses {
node.Connections[addr] = true
}
}
func (node *Node) SendMessageToAll(message string) {
var new_pack = Package{
From: node.Address.IPv4 + node.Address.Port,
Data: message,
}
for addr := range node.Connections {
new_pack.To = addr
node.Send(&new_pack)
}
}
func (node *Node) Send(pack *Package) {
conn, err := net.Dial("tcp", pack.To)
if err != nil {
delete(node.Connections, pack.To)
return
}
defer conn.Close()
json_pack, _ := json.Marshal(*pack)
conn.Write(json_pack)
}
func InputString() string {
msg, _ := bufio.NewReader(os.Stdin).ReadString('\n')
return strings.Replace(msg, "\n", "", -1)
}