// 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 %v)", 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") }
// 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") }
func RECEIVE_SENDMSG(cd *goipmsg.ClientData, ipmsg *goipmsg.IPMSG) error { sl := append(Messages, cd) Messages = sl cmd := cd.Command if cmd.Get(goipmsg.SENDCHECK) { num := cd.PacketNum err := ipmsg.SendMSG(cd.Addr, strconv.Itoa(num), goipmsg.RECVMSG) return err } return nil }
// Switchinput dispatches actions via input func SwitchInput(ipmsg *goipmsg.IPMSG, input string, quit chan string) { 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...") ipmsg.Close() quit <- "quitting" case "join": Join(ipmsg) case "list": List(ipmsg) case "send": Send(ipmsg) case "read": Read(ipmsg) } }
func RECEIVE_BR_ENTRY(cd *goipmsg.ClientData, ipmsg *goipmsg.IPMSG) error { Users[cd.Key()] = cd ipmsg.SendMSG(cd.Addr, ipmsg.Myinfo(), goipmsg.ANSENTRY) return nil }