func startHTTPJSONRPC() (string, *mockSessionStatePluginProxy) { encr := encrypter.New(&key.PublicKey, nil) encr.Key = symkey ee := encoding.NewJsonEncoder() ee.SetEncrypter(encr) mockProxy := &mockProxy{e: ee} mockCollectorProxy := &mockCollectorProxy{e: ee} rpc.RegisterName("Collector", mockCollectorProxy) rpc.RegisterName("Processor", mockProxy) rpc.RegisterName("Publisher", mockProxy) session := &mockSessionStatePluginProxy{e: ee} rpc.RegisterName("SessionState", session) rpc.HandleHTTP() l, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { panic(err) } go func() { http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { defer req.Body.Close() w.Header().Set("Content-Type", "application/json") res := plugin.NewRPCRequest(req.Body).Call() io.Copy(w, res) }) http.Serve(l, nil) }() return l.Addr().String(), session }
// NewLibstore creates a new instance of a TribServer's libstore. masterServerHostPort // is the master storage server's host:port. myHostPort is this Libstore's host:port // (i.e. the callback address that the storage servers should use to send back // notifications when leases are revoked). // // The mode argument is a debugging flag that determines how the Libstore should // request/handle leases. If mode is Never, then the Libstore should never request // leases from the storage server (i.e. the GetArgs.WantLease field should always // be set to false). If mode is Always, then the Libstore should always request // leases from the storage server (i.e. the GetArgs.WantLease field should always // be set to true). If mode is Normal, then the Libstore should make its own // decisions on whether or not a lease should be requested from the storage server, // based on the requirements specified in the project PDF handout. Note that the // value of the mode flag may also determine whether or not the Libstore should // register to receive RPCs from the storage servers. // // To register the Libstore to receive RPCs from the storage servers, the following // line of code should suffice: // // rpc.RegisterName("LeaseCallbacks", librpc.Wrap(libstore)) // // Note that unlike in the NewTribServer and NewStorageServer functions, there is no // need to create a brand new HTTP handler to serve the requests (the Libstore may // simply reuse the TribServer's HTTP handler since the two run in the same process). func NewLibstore(masterServerHostPort, myHostPort string, mode LeaseMode) (Libstore, error) { fmt.Println("New an libstore") client, err := rpc.DialHTTP("tcp", masterServerHostPort) if err != nil { // log.Fatalln("dialing:", err) return nil, errors.New("meet error when DialHTTP") } args := &storagerpc.GetServersArgs{} reply := &storagerpc.GetServersReply{} num := 0 for reply.Status != storagerpc.OK { if num >= 5 { return nil, errors.New("cannot find the storage server") } err = client.Call("StorageServer.GetServers", args, reply) if err != nil { return nil, errors.New("meet error when client call") } if reply.Status != storagerpc.OK { time.Sleep(1000 * time.Millisecond) } num++ } libstore := &libstore{ Servers: reply.Servers, // keyValueMutex: make(map[string]*sync.Mutex), // keyListMutex: make(map[string]*sync.Mutex), mutex: &sync.Mutex{}, // client: client, keyValue: make(map[string]*valuelib), keylist: make(map[string]*listlib), keyValudhistory: make(map[string]*list.List), keyListhistory: make(map[string]*list.List), keyValueMutex: make(map[string]*sync.Mutex), keyListMutex: make(map[string]*sync.Mutex), mode: mode, clientmap: make(map[string]*rpc.Client), myHostPort: myHostPort, } libstore.clientmap[masterServerHostPort] = client err = rpc.RegisterName("LeaseCallbacks", librpc.Wrap(libstore)) // retry until register sucessfully for err != nil { err = rpc.RegisterName("LeaseCallbacks", librpc.Wrap(libstore)) } go libstore.epochtime() return libstore, nil // return nil, errors.New("not implemented") }
// NewLibstore creates a new instance of a TribServer's libstore. masterServerHostPort // is the master storage server's host:port. myHostPort is this Libstore's host:port // (i.e. the callback address that the storage servers should use to send back // notifications when leases are revoked). // // The mode argument is a debugging flag that determines how the Libstore should // request/handle leases. If mode is Never, then the Libstore should never request // leases from the storage server (i.e. the GetArgs.WantLease field should always // be set to false). If mode is Always, then the Libstore should always request // leases from the storage server (i.e. the GetArgs.WantLease field should always // be set to true). If mode is Normal, then the Libstore should make its own // decisions on whether or not a lease should be requested from the storage server, // based on the requirements specified in the project PDF handout. Note that the // value of the mode flag may also determine whether or not the Libstore should // register to receive RPCs from the storage servers. // // To register the Libstore to receive RPCs from the storage servers, the following // line of code should suffice: // // rpc.RegisterName("LeaseCallbacks", librpc.Wrap(libstore)) // // Note that unlike in the NewTribServer and NewStorageServer functions, there is no // need to create a brand new HTTP handler to serve the requests (the Libstore may // simply reuse the TribServer's HTTP handler since the two run in the same process). func NewLibstore(masterServerHostPort, myHostPort string, mode LeaseMode) (Libstore, error) { client, err := rpc.DialHTTP("tcp", masterServerHostPort) if err != nil { return nil, errors.New("Error DialHTTP in NewLibstore") } args := &storagerpc.GetServersArgs{} reply := &storagerpc.GetServersReply{} storageReady := false for i := 0; i < 5; i++ { err = client.Call("StorageServer.GetServers", args, reply) if err != nil { return nil, errors.New("Error Call in NewLibstore") } if reply.Status == storagerpc.OK { storageReady = true break } else { time.Sleep(1 * time.Second) } } if storageReady == false { return nil, errors.New("Storage not ready after 5 trial in NewLibstore") } ls := &libstore{ StorageServers: reply.Servers, connectionMap: make(map[string]*rpc.Client), HostPort: myHostPort, CacheValueSet: make(map[string]*valueCache), CacheListSet: make(map[string]*listCache), queryGetQueue: make(map[string]*list.List), queryListQueue: make(map[string]*list.List), queryGetMutex: make(map[string]*sync.Mutex), queryListMutex: make(map[string]*sync.Mutex), mutex: &sync.Mutex{}, mode: mode, } ls.connectionMap[masterServerHostPort] = client err = rpc.RegisterName("LeaseCallbacks", librpc.Wrap(ls)) for err != nil { fmt.Println("Error LeaseCallbacks in NewLibstore") // return nil, errors.New("Error LeaseCallbacks in NewLibstore") err = rpc.RegisterName("LeaseCallbacks", librpc.Wrap(ls)) } go func() { for { select { case <-time.After(time.Duration(storagerpc.LeaseSeconds/2) * time.Second): ls.updateCache() case <-time.After(time.Duration(storagerpc.QueryCacheSeconds/2) * time.Second): ls.updateQueue() } } }() return ls, nil }
func NewCentralServer(port, numGameServers int) (CentralServer, error) { LOGV.Println("New Central Server is starting up") if numGameServers < 1 { return nil, errors.New("numGameServers must be at least 1") } cs := ¢ralServer{ numGameServers: numGameServers, gameServers: make(map[uint32]*gameServer), hostPortToGameServer: make(map[string]*gameServer), gameServersSlice: nil, } // Serve up information for the game client http.HandleFunc("/", cs.gameClientViewHandler) go http.ListenAndServe(fmt.Sprintf(":%d", port), nil) rpc.RegisterName("CentralServer", centralrpc.Wrap(cs)) rpc.HandleHTTP() l, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) if err != nil { return nil, err } go http.Serve(l, nil) return cs, nil }
func main() { scribeService := new(scribeServiceImplementation) rpc.RegisterName("Thrift", &scribe.ScribeServer{Implementation: scribeService}) flag.IntVar(&port, "p", 1463, "Scribe Listen Port") flag.StringVar(&kafka_hostname, "o", "localhost:9092", "host:port string for the kafka server") flag.IntVar(&partition, "r", 1, "partition to publish to") flag.IntVar(&buffer_size, "s", 10, "Buffer Size") flag.IntVar(&buffer_time, "t", 10, "Buffer Time") flag.Parse() fmt.Printf("Quiet! I'm trying to listen to port %d and send to kafka at %s", port, kafka_hostname) ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) if err != nil { log.Println(err) } for { conn, err := ln.Accept() if err != nil { fmt.Printf("ERROR: %+v\n", err) continue } fmt.Printf("New connection %+v\n", conn) go rpc.ServeCodec(thrift.NewServerCodec(thrift.NewFramedReadWriteCloser(conn, 0), thrift.NewBinaryProtocol(true, false))) } }
func NewStockServer(masterHostPort, myHostPort string) (StockServer, error) { s := rpc.NewServer() ls, err := libstore.NewLibstore(masterHostPort, myHostPort) if err != nil { return nil, err } sMap := make(map[string]string) ss := &stockServer{ server: s, ls: ls, sessionMap: sMap, } listener, err := net.Listen("tcp", myHostPort) if err != nil { return nil, err } log.Println("StockServer listening on address: ", myHostPort) err = rpc.RegisterName("StockServer", stockrpc.Wrap(ss)) if err != nil { log.Println("Failed to register StockServer RPC") return nil, err } rpc.HandleHTTP() go http.Serve(listener, nil) return ss, nil }
func NewLoadBalancer(hostPort string) (LoadBalancer, error) { loadBalancer := new(loadBalancer) loadBalancer.hostPort = hostPort loadBalancer.numCurrentNodes = 0 loadBalancer.nodes = make([]loadbalancerrpc.Node, loadbalancerrpc.InitCliNum) loadBalancer.nodesClientNum = make([]int, loadbalancerrpc.InitCliNum) loadBalancer.nodesFailed = make([]bool, loadbalancerrpc.InitCliNum) loadBalancer.nodesFailedNumber = 0 loadBalancer.numOKs = 0 listener, err := net.Listen("tcp", hostPort) if err != nil { return nil, err } // Wrap the tribServer before registering it for RPC. err = rpc.RegisterName("LoadBalancer", loadbalancerrpc.Wrap(loadBalancer)) if err != nil { return nil, err } rpc.HandleHTTP() go http.Serve(listener, nil) return loadBalancer, nil }
func Setup(objSrv objectserver.ObjectServer, lg *log.Logger) *htmlWriter { objectServer = objSrv logger = lg rpc.RegisterName("ObjectServer", new(rpcType)) http.HandleFunc("/GetObjects", getObjectsHandler) return &htmlWriter{} }
/** * Same as rpc.RegisterName, but wil panic if anything goes wrong */ func RPCRegisterName(name string, rcvr interface{}) bool { err := rpc.RegisterName(name, rcvr) if err != nil { return false } return true }
func NewLibpaxos(nodeID uint32, hostport string, allNodes []paxosrpc.Node) (Libpaxos, error) { lp := &libpaxos{ allNodes: allNodes, majorityCount: len(allNodes)/2 + 1, myNode: paxosrpc.Node{nodeID, hostport}, nodes: make(map[uint32]*node), newValueCh: make(chan *paxosrpc.ProposalValue), highestProposalNumberSeen: &paxosrpc.ProposalNumber{0, nodeID}, slotBox: NewSlotBox(), triggerHandlerCallCh: make(chan struct{}, 1000), newValuesQueue: list.New(), } for _, node := range allNodes { lp.nodes[node.ID] = NewNode(node) } // Start the RPC handlers rpc.RegisterName("PaxosNode", paxosrpc.Wrap(lp)) rpc.HandleHTTP() l, err := net.Listen("tcp", fmt.Sprintf(hostport)) if err != nil { return nil, err } go http.Serve(l, nil) go lp.controller() if DUMP_SLOTS { // Writes the contents of slotbox to file at fixed intervals go lp.dumpSlots() } return lp, nil }
// NewTribServer creates, starts and returns a new TribServer. masterServerHostPort // is the master storage server's host:port and port is this port number on which // the TribServer should listen. A non-nil error should be returned if the TribServer // could not be started. // // For hints on how to properly setup RPC, see the rpc/tribrpc package. func NewTribServer(masterServerHostPort, myHostPort string) (TribServer, error) { tribServer := new(tribServer) ls, err := libstore.NewLibstore(masterServerHostPort, myHostPort, libstore.Normal) if err != nil { return nil, errors.New("Libstore failed.") } tribServer.lib_store = ls // Create the server socket that will listen for incoming RPCs. _, port, _ := net.SplitHostPort(myHostPort) listener, err := net.Listen("tcp", fmt.Sprintf(":%s", port)) if err != nil { return nil, err } // Wrap the tribServer before registering it for RPC. err = rpc.RegisterName("TribServer", tribrpc.Wrap(tribServer)) 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) return tribServer, nil // return nil, errors.New("not implemented") }
func NewCentral(port int) (Central, error) { common.LOGV.Println("$PearCentral on ", port) c := central{} c.port = port c.clientIdCnt = 0 c.docMap = make(map[string]map[string]bool) c.serverMap = make(map[string]map[string]bool) c.connMap = make(map[string]*rpc.Client) c.myHostPort = fmt.Sprintf("localhost:%d", port) // Create the server socket that will listen for incoming RPCs. listener, err := net.Listen("tcp", c.myHostPort) if err != nil { return nil, err } // Wrap the tribServer before registering it for RPC. err = rpc.RegisterName("PearCentral", centralrpc.Wrap(&c)) 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) http.HandleFunc("/", c.NewClient) http.ListenAndServe(":"+strconv.Itoa(port), nil) return &c, nil }
// NewCoordinator returns the central Coordinator that deals with initial requests from all // users and dispatch the load to paxos servers func NewCoordinator(myhostPort string, numPaxos int, hostMap map[int]string) (Coordinator, error) { if numPaxos < 1 { return nil, errors.New("numPaxos should be more than 0") } c := &coordinator{ numPaxos: numPaxos, serversMutex: &sync.Mutex{}, nextPaxosID: 0, serversMap: make(map[int]string), hostMap: make(map[int]string), } for nodeID, hostPort := range hostMap { c.hostMap[nodeID] = hostPort } http.HandleFunc("/", c.clientViewHandler) go http.ListenAndServe(myhostPort, nil) rpc.RegisterName("Coordinator", coordinatorrpc.Wrap(c)) rpc.HandleHTTP() listener, err := net.Listen("tcp", myhostPort) if err != nil { return nil, err } go http.Serve(listener, nil) return c, nil }
func initTribServer(masterServerHostPort string, tribServerPort int) error { // l, err := net.Listen("tcp", tribServerHostPort) // if err != nil { // LOGE.Println("Failed to listen:", err) // return nil, err // } tribServerHostPort := net.JoinHostPort("localhost", strconv.Itoa(tribServerPort)) proxyCounter, err := proxycounter.NewProxyCounter(masterServerHostPort, tribServerHostPort) if err != nil { LOGE.Println("Failed to setup test:", err) return err } pc = proxyCounter rpc.RegisterName("StorageServer", storagerpc.Wrap(pc)) //rpc.HandleHTTP() //go http.Serve(l, nil) // Create and start the TribServer. tribServer, err := tribserver.NewTribServer(masterServerHostPort, tribServerPort) if err != nil { LOGE.Println("Failed to create TribServer:", err) return err } ts = tribServer //rpc.RegisterName("TribServer", tribrpc.Wrap(ts)) return nil }
// NewTribServer creates, starts and returns a new TribServer. masterServerHostPort // is the master storage server's host:port and port is this port number on which // the TribServer should listen. A non-nil error should be returned if the TribServer // could not be started. // // For hints on how to properly setup RPC, see the rpc/tribrpc package. func NewTribServer(masterServerHostPort, myHostPort string) (TribServer, error) { newStore, err := libstore.NewLibstore(masterServerHostPort, myHostPort, libstore.Never) if err != nil { return nil, err } ts := &tribServer{ myHostPort: myHostPort, Libstore: newStore, } err = rpc.RegisterName("TribServer", tribrpc.Wrap(ts)) if err != nil { return nil, err } rpc.HandleHTTP() l, err := net.Listen("tcp", myHostPort) if err != nil { return nil, err } go http.Serve(l, nil) return ts, nil }
// NewTribServer creates, starts and returns a new TribServer. masterServerHostPort // is the master storage server's host:port and port is this port number on which // the TribServer should listen. A non-nil error should be returned if the TribServer // could not be started. // // For hints on how to properly setup RPC, see the rpc/tribrpc package. func NewTribServer(masterServerHostPort, myHostPort string) (TribServer, error) { libStore, err := libstore.NewLibstore(masterServerHostPort, myHostPort, libstore.Never) //fmt.Println("Creating new tribServer...") if err != nil { return nil, err } var tribServ tribServer = tribServer{ libStore: libStore, } l, err2 := net.Listen("tcp", myHostPort) if err2 != nil { fmt.Println(err2) return nil, err2 } //go http.Serve(l, nil) err1 := rpc.RegisterName("TribServer", tribrpc.Wrap(&tribServ)) if err1 != nil { fmt.Println(err1) return nil, err1 } rpc.HandleHTTP() go http.Serve(l, nil) return &tribServ, nil }
func NewPaxos(myHostPort string, ID int, serverHostPorts []string, test bool) (Paxos, error) { thisPaxos := new(paxos) thisPaxos.ID = ID thisPaxos.currentRound = 0 thisPaxos.highestSeenProposal = 0 thisPaxos.proposalNum = 0 thisPaxos.value = "" thisPaxos.myHostPort = myHostPort thisPaxos.serverHostPorts = serverHostPorts thisPaxos.logs = make(map[int]string) if !test { err := rpc.RegisterName("Paxos", paxosrpc.Wrap(thisPaxos)) if err != nil { return nil, err } } //dial all other paxos and create a list of them to call. err := thisPaxos.DialAllServers() for i := 0; i < 5; i++ { time.Sleep(time.Second) err = thisPaxos.DialAllServers() if err == nil { break } if i == 4 { return nil, errors.New("dial all servers error") } } return thisPaxos, nil }
// NewStorageServer creates and starts a new StorageServer. masterServerHostPort // is the master storage server's host:port address. If empty, then this server // is the master; otherwise, this server is a slave. numNodes is the total number of // servers in the ring. port is the port number that this server should listen on. // nodeID is a random, unsigned 32-bit ID identifying this server. // // This function should return only once all storage servers have joined the ring, // and should return a non-nil error if the storage server could not be started. func NewStorageServer(masterServerHostPort string, numNodes, port int, nodeID uint32) (StorageServer, error) { // Set upt this server's info serverInfo := storagerpc.Node{HostPort: fmt.Sprintf("localhost:%d", port), NodeID: nodeID} var ss storageServer if masterServerHostPort == "" { // If this is the master server, set up a list of servers var servers = make([]storagerpc.Node, numNodes) servers[0] = serverInfo // Create the master server ss = storageServer{topMap: make(map[string]interface{}), nodeID: nodeID, servers: servers, count: 1, countLock: sync.Mutex{}, keyLocks: make(map[string]chan int)} } else { // Try to connect to the master at most five times args := storagerpc.RegisterArgs{ServerInfo: serverInfo} var reply storagerpc.RegisterReply var err error var master *rpc.Client for try := 1; try <= 5; try++ { master, err = rpc.DialHTTP("tcp", masterServerHostPort) if err == nil { break } if try == 5 { return nil, err } time.Sleep(time.Millisecond * 20) } for i := 1; i <= 5; i++ { master.Call("StorageServer.RegisterServer", args, &reply) if reply.Status == storagerpc.OK { // All servers are connected, create this slave server ss = storageServer{topMap: make(map[string]interface{}), nodeID: nodeID, servers: reply.Servers, count: numNodes, countLock: sync.Mutex{}, keyLocks: make(map[string]chan int)} break } // Wait one second, try to connect to master again if i == 5 { return nil, errors.New("couldn't connect to master") } time.Sleep(time.Millisecond * 20) } } // Start listening for connections from other storageServers and libstores rpc.RegisterName("StorageServer", &ss) rpc.HandleHTTP() l, e := net.Listen("tcp", serverInfo.HostPort) if e != nil { return nil, errors.New("Storage server couldn't start listening") } go http.Serve(l, nil) return &ss, nil }
// NewTribServer creates, starts and returns a new TribServer. masterServerHostPort // is the master storage server's host:port and port is this port number on which // the TribServer should listen. A non-nil error should be returned if the TribServer // could not be started. // // For hints on how to properly setup RPC, see the rpc/tribrpc package. func NewTribServer(masterServerHostPort, myHostPort string) (TribServer, error) { tribServer := new(tribServer) // Create the server socket that will listen for incoming RPCs. listener, err := net.Listen("tcp", myHostPort) if err != nil { return nil, err } // Wrap the tribServer before registering it for RPC. err = rpc.RegisterName("TribServer", tribrpc.Wrap(tribServer)) 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) storage, err := libstore.NewLibstore(masterServerHostPort, myHostPort, libstore.Never) if err != nil { return nil, err } tribServer.storage = storage return tribServer, nil }
/** * Proxy validates the requests going into a PaxosNode and the responses coming out of it. * It logs errors that occurs during a test. */ func NewProxy(nodePort, myPort int) (Proxy, error) { p := new(proxy) p.prop = new(proposal) p.prop.status = UNSET p.prop.num = 0 p.prop.key = "" p.prop.val = 0 p.err = make([]string, 0) // Start server l, err := net.Listen("tcp", fmt.Sprintf(":%d", myPort)) if err != nil { LOGE.Println("Failed to listen:", err) return nil, err } // Create RPC connection to paxos node. srv, err := rpc.DialHTTP("tcp", fmt.Sprintf("localhost:%d", nodePort)) if err != nil { LOGE.Println("Failed to dial node %d", nodePort) return nil, err } p.srv = srv // register RPC rpc.RegisterName("PaxosNode", paxosrpc.Wrap(p)) rpc.HandleHTTP() go http.Serve(l, nil) // log.Printf("Proxy started") return p, nil }
//Current setting: all settings are static func NewLeaseNode(nodeID int, numNodes int) (LeaseNode, error) { node := &leaseNode{ nodeID: nodeID, port: config.Nodes[nodeID].Port, numNodes: numNodes, addrport: config.Nodes[nodeID].Address + ":" + strconv.Itoa(config.Nodes[nodeID].Port), isMaster: false, nextBallot: nodeID, masterLeaseLen: 0, renewLeaseLen: 0, acceptLeaseLen: 0, Nh: 0, Na: 0, peers: make([]Node, numNodes), } for i := 0; i < numNodes; i++ { node.peers[i].HostPort = config.Nodes[i].Address + ":" + strconv.Itoa(config.Nodes[i].Port) node.peers[i].NodeID = i } if err := rpc.RegisterName("LeaseNode", leaserpc.Wrap(node)); err != nil { return nil, err } go node.leaseManager() return node, nil }
// NewTribServer creates, starts and returns a new TribServer. masterServerHostPort // is the master storage server's host:port and port is this port number on which // the TribServer should listen. A non-nil error should be returned if the TribServer // could not be started. // // For hints on how to properly setup RPC, see the rpc/tribrpc package. func NewTribServer(masterServerHostPort, myHostPort string) (TribServer, error) { fmt.Println("tribserver being connection!") server := new(tribServer) lib, err := libstore.NewLibstore(masterServerHostPort, myHostPort, libstore.Normal) if err != nil { fmt.Println("failed to connect!") //fmt.Println(err) return nil, err } server.lib = lib server.id = 0 server.hostport = myHostPort // listen for incoming RPC listener, err := net.Listen("tcp", myHostPort) if err != nil { fmt.Println("Listen error!") return nil, err } // warp the tribserver err = rpc.RegisterName("TribServer", tribrpc.Wrap(server)) if err != nil { fmt.Println("RegisterName error!") return nil, err } rpc.HandleHTTP() go http.Serve(listener, nil) fmt.Println("server started!!!!!!!") return server, nil }
// NewTribServer creates, starts and returns a new TribServer. masterServerHostPort // is the master storage server's host:port and port is this port number on which // the TribServer should listen. A non-nil error should be returned if the TribServer // could not be started. // // For hints on how to properly setup RPC, see the rpc/tribrpc package. func NewTribServer(masterServerHostPort, myHostPort string) (TribServer, error) { tribServer := &tribServer{} var newError error tribServer.libstore, newError = libstore.NewLibstore(masterServerHostPort, myHostPort, libstore.Normal) if newError != nil { fmt.Println(newError) } err := rpc.RegisterName("TribServer", tribrpc.Wrap(tribServer)) if err != nil { return nil, err } rpc.HandleHTTP() listener, err := net.Listen("tcp", ":"+strings.Split(myHostPort, ":")[1]) if err != nil { return nil, err } go http.Serve(listener, nil) fmt.Println("TribServer Created!", myHostPort) return tribServer, nil }
// NewTribServer creates, starts and returns a new TribServer. masterServerHostPort // is the master storage server's host:port and port is this port number on which // the TribServer should listen. A non-nil error should be returned if the TribServer // could not be started. // // For hints on how to properly setup RPC, see the rpc/tribrpc package. func NewTribServer(masterServerHostPort, myHostPort string) (TribServer, error) { LOGE.Printf("NewTribServer:: masterServerHostPort=%s myHostPort=%s\n", masterServerHostPort, myHostPort) server := new(tribServer) server.hostPort = myHostPort server.index = 0 lib, err := libstore.NewLibstore(masterServerHostPort, myHostPort, libstore.Normal) if err != nil { return nil, err } server.lib = lib listener, err := net.Listen("tcp", myHostPort) if err != nil { return nil, err } err = rpc.RegisterName("TribServer", tribrpc.Wrap(server)) if err != nil { fmt.Println("RegisterName error!") return nil, err } rpc.HandleHTTP() go http.Serve(listener, nil) return server, nil }
// NewTribServer creates, starts and returns a new TribServer. masterServerHostPort // is the master storage server's host:port and port is this port number on which // the TribServer should listen. A non-nil error should be returned if the TribServer // could not be started. // // For hints on how to properly setup RPC, see the rpc/tribrpc package. func NewTribServer(masterServerHostPort, myHostPort string) (TribServer, error) { ts := new(tribServer) // Create the libstore for this server ls, err := libstore.NewLibstore(masterServerHostPort, myHostPort, libstore.Never) if err != nil { fmt.Println("Failed to create libstore") return nil, errors.New("Couldn't start libstore for Tribserver") } ts.ls = ls // Start listening for connections from TribClients rpc.RegisterName("TribServer", tribrpc.Wrap(ts)) rpc.HandleHTTP() l, e := net.Listen("tcp", myHostPort) if e != nil { fmt.Println("Failed to listen with Tribserver") return nil, errors.New("Tribserver couldn't start listening") } go http.Serve(l, nil) return ts, nil // Get rid of this. I didn't want to keep commenting out fmt for testing when I didn't use it fmt.Println("this is here so I don't throw an error for not using fmt") return nil, nil }
func NewGobbyServer(nodeID int, numNodes int) (Gobbyserver, error) { server := new(gobbyserver) server.addrport = config.Nodes[nodeID].Address + ":" + strconv.Itoa(config.Nodes[nodeID].Port) server.store = make(map[string]*kstate) server.commandLog = make([]*command.Command, 0) server.nextIndex = 0 server.replyChs = make(map[int]chan *gobbyrpc.GobbyReply) server.pending = NewQueue() if pnode, err := paxos.NewPaxosNode(nodeID, numNodes, server.getCommand); err != nil { return nil, err } else { server.paxosnode = pnode } if lnode, err := lease.NewLeaseNode(nodeID, numNodes); err != nil { return nil, err } else { server.leasenode = lnode } if err := rpc.RegisterName("gobbyServer", gobbyrpc.Wrap(server)); err != nil { return nil, err } else { go ReplicateRoutine(server.pending, server.paxosnode) return server, nil } //TODO:Now the listener is in the lease node, and we should let gobbyserver contain listener }
func NewLoggerPlugin(logger LoggerPlugin) (*LoggerPluginServer, error) { client, err := rpc.DialHTTP("tcp", control.CONTROL_PORT_CORE) if err != nil { return nil, err } name := id() rpc.RegisterName(name, logger) rpc.HandleHTTP() listener, port, err := getListener() if err != nil { return nil, err } s := &LoggerPluginServer{ Port: port, Name: name, listener: listener, readyChan: make(chan *rpc.Call, 1), client: client} // handle exactly one manager ready reply go func() { for call := range s.readyChan { if call.Error != nil { log.Fatal(call.Error) } close(s.readyChan) } }() return s, nil }
// NewStorageServer creates and starts a new StorageServer. masterServerHostPort // is the master storage server's host:port address. If empty, then this server // is the master; otherwise, this server is a slave. numNodes is the total number of // servers in the ring. port is the port number that this server should listen on. // nodeID is a random, unsigned 32-bit ID identifying this server. // // This function should return only once all storage servers have joined the ring, // and should return a non-nil error if the storage server could not be started. func NewStorageServer(masterServerHostPort string, numNodes, port int, nodeID uint32) (StorageServer, error) { //Create Server Object server := createStorageServer(numNodes, nodeID) // Register RPCs rpc.RegisterName("StorageServer", storagerpc.Wrap(server)) rpc.HandleHTTP() laddr := fmt.Sprintf(":%d", port) l, e := net.Listen("tcp", laddr) if e != nil { return nil, errors.New("RPC Issue Storage Server\n") } go http.Serve(l, nil) if masterServerHostPort == "" { //add itself to the empty server ring hostport := net.JoinHostPort("localhost", strconv.Itoa(port)) server.ring = append(server.ring, storagerpc.Node{hostport, nodeID}) if numNodes > 1 { <-server.allConnected } } else { master, err := rpc.DialHTTP("tcp", masterServerHostPort) if err != nil { return nil, err } hostport := net.JoinHostPort("localhost", strconv.Itoa(port)) node := storagerpc.Node{hostport, nodeID} args := &storagerpc.RegisterArgs{node} var reply storagerpc.RegisterReply // Keep trying to register itself for { if err := master.Call("StorageServer.RegisterServer", args, &reply); err != nil { return nil, err } if reply.Status != storagerpc.NotReady { break } else { time.Sleep(time.Duration(1) * time.Second) } } server.ring = reply.Servers } for _, node := range server.ring { server.nodeIDs = append(server.nodeIDs, int(node.NodeID)) } print(len(server.nodeIDs)) sort.Ints(server.nodeIDs) go incTime(server) return server, nil }
// Returns a new RPC Server func New(database db.DB, sock string, mc chan<- int, fc chan<- int) *Server { if err := rpc.RegisterName("S", NewService(database, mc, fc)); err != nil { Fatal("[RPC]", err) } rpc.HandleHTTP() return &Server{db: database, socket: sock, Ctrl: make(chan int)} }
func Setup(imdb *scanner.ImageDataBase, lg *log.Logger) { rpcObj := rpcType{ imageDataBase: imdb, logger: lg} rpc.RegisterName("ImageServer", &rpcObj) srpcObj := srpcType(rpcObj) srpc.RegisterName("ImageServer", &srpcObj) }