func main() { args := os.Args if len(args) != 4 { fmt.Println("Usage: [aserver UDP ip:port] [fserver RPC ip:port] [secret]") os.Exit(-1) } aserverUDPIpPort := args[1] fserverRCPIpPort := args[2] secret := parseSecretArg(args[3]) udpListener := clientServerUtils.InitUDPConn(aserverUDPIpPort) //Initialize hash table of (udpIpPort, nonce) key-value pairs var nonceMap clientServerUtils.ConcurrentMap nonceMap.Map = make(map[string]int64) //Start listen/receive connection loop for { var buf [1024]byte msgLen, clientUDPAddr, err := udpListener.ReadFromUDP(buf[:]) if err != nil { fmt.Println("Error on ReadFromUDP: ", err) } else { go handleUDPConn(udpListener, clientUDPAddr, fserverRCPIpPort, nonceMap, secret, buf[0:msgLen]) } } udpListener.Close() }
//Generate new nonce for udpAddr and add to nonceMap func generateNewNonce(udpAddr *net.UDPAddr, nonceMap clientServerUtils.ConcurrentMap) int64 { nonce := rand.Int63() nonceMap.Lock() nonceMap.Map[udpAddr.String()] = nonce nonceMap.Unlock() return nonce }
func handleUDPConn(udpListener net.UDPConn, clientUDPAddr *net.UDPAddr, fserverRCPIpPort string, nonceMap clientServerUtils.ConcurrentMap, secret int64, msgFromClient []byte) { nonceMap.RLock() clientNonce := nonceMap.Map[clientUDPAddr.String()] nonceMap.RUnlock() var receivedHashMsg clientServerUtils.HashMessage err := json.Unmarshal(msgFromClient, &receivedHashMsg) if err != nil { sendNewNonce(udpListener, clientUDPAddr, nonceMap) } else if clientNonce == 0 { errMsg := clientServerUtils.ErrMessage{"unknown remote client address"} sendErrMessage(udpListener, clientUDPAddr, errMsg) } else if isValidHashMessage(receivedHashMsg, clientNonce, secret) { replyWithFortuneInfoMessage(udpListener, clientUDPAddr, fserverRCPIpPort) } else { errMsg := clientServerUtils.ErrMessage{"unexpected hash value"} sendErrMessage(udpListener, clientUDPAddr, errMsg) } }