func NewServer(centralHostPort string, port int) (Server, error) { LOGV.Println("PearServer on", port) ps := server{} ps.centralHostPort = centralHostPort ps.myHostPort = fmt.Sprintf("localhost:%d", port) ps.port = port ps.clients = make(map[string]*client) ps.documents = make(map[string]map[string]bool) ps.connMap = make(map[string]*rpc.Client) ps.docToServerMap = make(map[string]map[string]bool) // Create the server socket that will listen for incoming RPCs. listener, err := net.Listen("tcp", ps.myHostPort) if err != nil { return nil, err } // Wrap the tribServer before registering it for RPC. err = rpc.RegisterName("PearServer", serverrpc.Wrap(&ps)) if err != nil { return nil, err } // Setup the HTTP handler that will server incoming RPCs and // serve requests in a background goroutine. rpc.HandleHTTP() go http.Serve(listener, nil) // err = coordinatorInit(&ps, ps.myHostPort) err = participantInit(&ps, ps.myHostPort) if err != nil { common.LOGE.Println(err) return nil, err } http.Handle("/", websocket.Handler(ps.clientConnHandler)) go http.ListenAndServe(":"+strconv.Itoa(port), nil) return &ps, nil }
func initCentralTest(server, myhostport string) (*centralTester, error) { tester := new(centralTester) tester.myHostPort = myhostport // tester.recvRevoke = make(map[string]bool) // tester.compRevoke = make(map[string]bool) // Create RPC connection to storage server. srv, err := rpc.DialHTTP("tcp", server) if err != nil { return nil, fmt.Errorf("could not connect to server %s", server) } rpc.RegisterName("PearServer", serverrpc.Wrap(tester)) rpc.HandleHTTP() l, err := net.Listen("tcp", fmt.Sprintf(":%d", *portnum)) if err != nil { LOGE.Fatalln("Failed to listen:", err) } go http.Serve(l, nil) tester.srv = srv return tester, nil }