//initialize all variablesmessage.
func (mul *Multicast) Init(c *message.CalendarInfo) {
	port := "9200"
	mul.Passer.owner = mul
	mul.SeqNum = 0
	mul.Info = c
	mul.GroupState = make(map[string]string)
	mul.RequestWait = make(map[string]int)
	mul.HoldbackQueue = make(map[string]customtypes.SortByStamp)
	mul.RequestStamp = make(map[string]uint64)
	// mul.ToFront = make(calendar.Message)
	for key, _ := range mul.Info.GroupMember {
		//		num := len(mul.Info.GroupMember[key])
		mul.GroupState[key] = "wait"
		mul.RequestWait[key] = 0
		mul.HoldbackQueue[key] = make([]customtypes.TsMessage, 0)
		mul.RequestStamp[key] = 0
	}
	fmt.Println(mul.GroupState["group1"])
	mul.LockQueue = make(chan customtypes.TsMessage, 1024)
	mul.CalendarQueue = make(chan customtypes.TsMessage, 1024)
	mul.VoteQueue = make(chan customtypes.TsMessage, 1024)
	mul.GroupInfoQueue = make(chan customtypes.TsMessage, 1024)
	cryptowrapper.GenerateRSAKey()
	go mul.rcvPrint()
	go mul.portListener(":" + port)
	//	go mul.rcvFromFront()
	go mul.LockRcv()
	go mul.UpdateRcv()
	go mul.VoteRcv()
	go mul.GroupInfoRcv()
}
func main() {
	announce_client.InitAnnounceUDP()
	timestamp.InitTimeStamp()
	cryptowrapper.GenerateRSAKey()
	var c message.CalendarInfo
	//    c.Init(strings.Trim(cmd[0], "\n"))
	c.Init()
	var mul tcpNet.Multicast
	mul.Init(&c)

	reader := bufio.NewReader(os.Stdin)
	commands, _ := reader.ReadString('\n')
	cmd := strings.Split(commands, " ")

	//commands, _ = reader.ReadString('\n')
	for commands != "exit" {
		cmd = strings.Split(commands, " ")
		switch cmd[0] {
		case "CreateGroup":
			mul.AddGroup(strings.Trim(cmd[1], "\n")) //group name
		case "join":
			mul.SendJoinRequest(strings.Trim(cmd[1], "\n"), strings.Trim(cmd[2], "\n"), strings.Trim(cmd[3], "\n")) //dest, group, invite_code
		case "VoteRequest":
			mul.VoteRequest(strings.Trim(cmd[1], "\n"), strings.Trim(cmd[2], "\n")) //(mem, group string)
		case "VoteReply":
			mul.VoteReply(strings.Trim(cmd[1], "\n"), strings.Trim(cmd[2], "\n")) //dest, decision string
		case "csRequest":
			mul.LockRequest(strings.Trim(cmd[1], "\n")) //group
		case "csRelease":
			mul.LockRelease(strings.Trim(cmd[1], "\n"))
		case "operation":
			mul.UpdateSend(strings.Trim(cmd[1], "\n"), strings.Trim(cmd[2], "\n")) //group, content
		// case "uni": mul.SendUniMessage("lock_request", strings.Trim(cmd[1], "\n"),strings.Trim(cmd[2], "\n"),5000)
		// case "multi": mul.SendMulticast("lock_reply", strings.Trim(cmd[1], "\n"),strings.Trim(cmd[2], "\n"),5000)
		// case "request": mul.LockRequest(strings.Trim(cmd[1], "\n"))
		// case "release": mul.LockRelease(strings.Trim(cmd[1], "\n"))
		// case "join": mul.VoteRequest(strings.Trim(cmd[1], "\n"), strings.Trim(cmd[2], "\n"))
		// case "info": go mul.SendInfo(strings.Trim(cmd[1], "\n"), strings.Trim(cmd[2], "\n"))
		case "exit":
			mul.Info.WriteToFile("communication/message/config.txt")
			os.Exit(0)
		default:
			mul.Info.WriteToFile("communication/message/config.txt")
			os.Exit(0)
		}
		commands, _ = reader.ReadString('\n')
		commands = strings.Trim(commands, "\n")
		fmt.Println(commands)
	}

}
func runServer(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory, addr string, secure bool) error {
	var transport thrift.TServerTransport
	var err error
	if secure {
		cfg := new(tls.Config)
		if cert, err := tls.LoadX509KeyPair("server.crt", "server.key"); err == nil {
			cfg.Certificates = append(cfg.Certificates, cert)
		} else {
			return err
		}
		transport, err = thrift.NewTSSLServerSocket(addr, cfg)
	} else {
		transport, err = thrift.NewTServerSocket(addr)
	}

	if err != nil {
		return err
	}
	fmt.Printf("%T\n", transport)

	announce_client.InitAnnounceUDP()
	fmt.Println("111")
	timestamp.InitTimeStamp()
	fmt.Println("222")
	cryptowrapper.GenerateRSAKey()
	fmt.Println("333")
	var c message.CalendarInfo
	c.Init()
	fmt.Println("444")
	var mul tcpNet.Multicast
	mul.Init(&c)
	fmt.Println("555")
	handler := NewMyServiceHandler()
	processor := calendar.NewMyServiceProcessor(handler)
	server := thrift.NewTSimpleServer4(processor, transport, transportFactory, protocolFactory)

	fmt.Println("Starting the simple server... on ", addr)
	return server.Serve()
}