// The newserver is the actual server where the paxos node runs and also keeps // information on clients func NewServer(myhostPort, coordinatorHostPort string, numPaxos, srvId int, hostMap map[int]string) (Server, error) { if numPaxos < 1 { return nil, errors.New("numPaxos should be more than 0") } s := &server{ hostMap: make(map[int]string), clients: make(map[int]*client), clientMutex: sync.Mutex{}, numClients: 0, srvId: srvId, bidChan: make(chan string), } c, err := rpc.DialHTTP("tcp", coordinatorHostPort) if err != nil { fmt.Println(err) return nil, err } args := &coordinatorrpc.RegisterPaxosServerArgs{srvId} var reply coordinatorrpc.RegisterPaxosServerReply reply.Status = coordinatorrpc.NotReady for reply.Status != coordinatorrpc.OK { err := c.Call("Coordinator.RegisterPaxosServer", args, &reply) if err != nil { fmt.Println(err) return nil, err } time.Sleep(time.Duration(rand.Int()%100) * time.Millisecond) } pn, err := paxos.NewPaxosNode(myhostPort, hostMap, numPaxos, srvId, numRetries, false) if err != nil { fmt.Println("Error starting paxos", err) return nil, err } s.pn = pn for nodeID, hostPort := range hostMap { s.hostMap[nodeID] = hostPort } // go s.clientHandler() http.HandleFunc("/bid/", s.bidHandler) http.HandleFunc("/view/", s.viewHandler) go http.ListenAndServe(myhostPort, nil) go s.broadcast() return s, nil }
func main() { flag.Parse() portStrings := strings.Split(*ports, ",") hostMap := make(map[int]string) if *proxyNode >= 0 { if *proxyNode == *nodeID { // set up proxy routing for i, port := range portStrings { if i == *proxyNode { hostMap[i] = "localhost:" + port } else { hostMap[i] = fmt.Sprintf("localhost:%d", *proxyPort) } } } else { for i, port := range portStrings { if i == *proxyNode { hostMap[*proxyNode] = fmt.Sprintf("localhost:%d", *proxyPort) } else { hostMap[i] = "localhost:" + port } } } } else { for i, port := range portStrings { hostMap[i] = "localhost:" + port } } // fmt.Println("print hostport") // for i, port := range hostMap { // fmt.Println(i, port) // } // Create and start the Paxos Node. _, err := paxos.NewPaxosNode(hostMap[*nodeID], hostMap, *numNodes, *nodeID, *numRetries, false) if err != nil { log.Fatalln("Failed to create paxos node:", err) } // Run the paxos node forever. select {} }
func main() { hmap := map[int]string{ 0: ":8010", 1: ":8012", 2: ":8013", 3: ":8015"} num := flag.Int("num", 0, "the number of the paxos node for this server") port := flag.String("port", "foo", "a string") flag.Parse() node, _ := paxos.NewPaxosNode(hmap[*num], hmap, 4, *num, 4, false) server := &TodoServer{port: *port, node: node} server.setPaxos("topitem", "0") server.start(strings.Join([]string{":", *port}, "")) }