forked from masanorih/go-ipmsg
/
main.go
226 lines (205 loc) · 4.56 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
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
package main
import (
"fmt"
"net"
"os"
"os/user"
"strconv"
"github.com/Songmu/prompter"
"github.com/k0kubun/pp"
goipmsg "github.com/FlowingSPDG/go-ipmsg"
)
var (
commands = []string{"help", "quit", "join", "list", "send", "read"}
)
func main() {
ipmsg := setup()
defer ipmsg.Close()
input := make(chan string)
next := make(chan string)
quit := make(chan struct{})
recv := make(chan goipmsg.ClientData)
// main loop or receive event from channel
go func() {
for {
var str string
var cd goipmsg.ClientData
select {
case str = <-input:
SwitchInput(ipmsg, str, quit)
next <- ""
case cd = <-recv:
pp.Println("recv=", cd)
}
}
}()
// get command from stdin
go func() {
for {
input <- (&prompter.Prompter{
Choices: commands,
Default: "list",
Message: "ipmsg>",
IgnoreCase: true,
}).Prompt()
<-next
}
}()
// recv message from socket
go func() {
for {
cd, err := ipmsg.RecvMSG()
if err != nil {
panic(err)
}
//pp.Println("recv=", cd.String())
recv <- *cd
}
}()
<-quit
os.Exit(1)
}
func setup() *goipmsg.IPMSG {
conf := goipmsg.NewIPMSGConf()
user, err := user.Current()
if err != nil {
panic(err)
}
conf.UserName = user.Username
conf.NickName = user.Username
conf.GroupName = user.Gid
hostname, err := os.Hostname()
if err != nil {
panic(err)
}
conf.HostName = hostname
ipmsg, err := goipmsg.NewIPMSG(conf)
if err != nil {
panic(err)
}
ev := goipmsg.NewEventHandler()
ev.Debug = false
// those are defined at handler.go
ev.Regist(goipmsg.BR_ENTRY, RECEIVE_BR_ENTRY)
ev.Regist(goipmsg.ANSENTRY, RECEIVE_ANSENTRY)
ev.Regist(goipmsg.SENDMSG, RECEIVE_SENDMSG)
ipmsg.AddEventHandler(ev)
return ipmsg
}
// SwitchInput dispatches actions via input
func SwitchInput(ipmsg *goipmsg.IPMSG, input string, quit chan struct{}) {
switch input {
case "help":
fmt.Println("usage:")
fmt.Println("\thelp: show this help.")
fmt.Println("\tquit: quit this programme.")
fmt.Println("\tjoin: let others know U.")
fmt.Println("\tlist: list up users U know.")
fmt.Println("\tsend: send message to user.")
fmt.Println("\tread: read received message.")
case "quit":
fmt.Println("quitting...")
Quit(ipmsg)
ipmsg.Close()
quit <- struct{}{}
case "join":
Join(ipmsg)
case "list":
List(ipmsg)
case "send":
Send(ipmsg)
case "read":
Read(ipmsg)
}
}
// Read ipmsg=?
func Read(ipmsg *goipmsg.IPMSG) {
if len(messages) == 0 {
fmt.Println("There is no message to read.")
} else {
for _, v := range messages {
fmt.Printf("From: %s\n", v.Key())
fmt.Printf("Date: %v\n", v.Time)
fmt.Printf("Message: %s\n\n", v.Option)
}
// clear all datas
messages = []*goipmsg.ClientData{}
}
}
// Send get specified user from prompt and actually send it to the target
func Send(ipmsg *goipmsg.IPMSG) {
userIdx := []string{}
i := 0
m := make(map[int]string)
for k := range users {
i++
fmt.Printf("%d %v\n", i, k)
userIdx = append(userIdx, strconv.Itoa(i))
m[i] = k
}
chosen := (&prompter.Prompter{
Choices: userIdx,
UseDefault: false,
Message: "Choose the user to send message>",
IgnoreCase: true,
}).Prompt()
i, _ = strconv.Atoi(chosen)
key := m[i]
promptMessage := fmt.Sprintf("Enter message(to %s)", key)
message := prompter.Prompt(promptMessage, "")
cd := users[key]
addr := cd.Addr
cmd := goipmsg.SENDMSG
cmd.SetOpt(goipmsg.SECRET)
err := ipmsg.SendMSG(addr, message, cmd)
if err != nil {
panic(err)
}
fmt.Println("sent SENDMSG")
}
// List print out known users
func List(ipmsg *goipmsg.IPMSG) {
if len(users) == 0 {
fmt.Println("There is no users.")
} else {
fmt.Println("known users below.")
i := 0
for k := range users {
i++
fmt.Printf("\t%d %v\n", i, k)
}
}
}
// Join sends BR_ENTRY packet to the broadcast address
func Join(ipmsg *goipmsg.IPMSG) {
addr := brAddr(ipmsg)
cmd := goipmsg.BR_ENTRY
cmd.SetOpt(goipmsg.BROADCAST)
err := ipmsg.SendMSG(addr, ipmsg.Myinfo(), cmd)
if err != nil {
panic(err)
}
fmt.Println("sent BR_ENTRY")
}
// Quit sends BR_EXIT packet to the broadcast address
func Quit(ipmsg *goipmsg.IPMSG) {
addr := brAddr(ipmsg)
cmd := goipmsg.BR_EXIT
cmd.SetOpt(goipmsg.BROADCAST)
err := ipmsg.SendMSG(addr, ipmsg.Myinfo(), cmd)
if err != nil {
panic(err)
}
fmt.Println("sent BR_EXIT")
}
// brAddr retrieves broadcast address
func brAddr(ipmsg *goipmsg.IPMSG) *net.UDPAddr {
broadcast := net.IPv4(255, 255, 255, 255)
port := ipmsg.Conf.Port
str := fmt.Sprintf("%v:%d", broadcast.String(), port)
udpAddr, err := net.ResolveUDPAddr("udp4", str)
if err != nil {
panic(err)
}
return udpAddr
}