Example #1
0
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
}
Example #2
0
func main() {
	fmt.Printf("node %d starts\n", nid)
	node, err := paxos.NewPaxosNode(nid, numNodes, fakecallback)
	if err != nil {
		fmt.Println("Cannot start node.\n")
		fmt.Println(err)
		return
	}
	listener, err := net.Listen("tcp", fmt.Sprintf(":%d", config.Nodes[nid].Port))
	if err != nil {
		fmt.Printf("node %d cannot listen to port:%s\n", err)
		return
	}
	node.SetListener(&listener)
	rpc.HandleHTTP()
	go http.Serve(listener, nil)

	time.Sleep(5 * time.Second)

	for i := 0; i < 2; i++ {
		c := command.Command{strconv.Itoa(nid), strconv.Itoa(i), command.Put, i, ""}
		node.Replicate(&c)
	}

	for res := 0; res < 4; res++ {
		_, ok := <-done
		if !ok {
			break
		}
	}
	node.DumpLog()
	fmt.Printf("node %d closes\n", nid)
}
Example #3
0
// 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
}
Example #4
0
// 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
}
Example #5
0
// 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
}
Example #6
0
func main() {
	registerMetrics()
	rpc.HandleHTTP()
	if err := http.ListenAndServe(":8080", nil); err != nil {
		fmt.Println(err)
	}
}
Example #7
0
/**
 * 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
}
Example #8
0
func NewServer() (*Server, error) {
	cfg, err := config.New(os.ExpandEnv("$SUDO_USER"))
	if err != nil {
		return nil, err
	}

	err = cfg.Load()
	if err != nil {
		return nil, err
	}

	srv := &Server{
		config: cfg,
		done:   make(chan bool),
		Status: "The virtual machine has not been started",
		queue:  []Message{},
	}

	v := &VM{
		server: srv,
	}
	srv.svc = v

	rpc.Register(v)
	rpc.HandleHTTP()

	return srv, nil
}
Example #9
0
func main() {
	arith := new(Arith)
	rpc.Register(arith)
	rpc.HandleHTTP()
	l, e := net.Listen("tcp", ":1234")
	if e != nil {
		log.Fatal("listen error", e)
	}
	go http.Serve(l, nil)

	client, err := rpc.DialHTTP("tcp", "localhost:1234")
	if err != nil {
		log.Fatal("dialing:", err)
	}

	// Synchronous call
	args := Args{7, 8}
	var reply int
	err = client.Call("Arith.Multiply", args, &reply)
	if err != nil {
		log.Fatal("arith error:", err)
	}
	fmt.Printf("Arith: %d*%d=%d\n", args.A, args.B, reply)

	// Asynchronous call
	quotient := new(Quotient)
	divCall := client.Go("Arith.Divide", args, &quotient, nil)
	replyCall := <-divCall.Done // will be equal to divCall
	print(replyCall)
	fmt.Printf("Arith: %d*%d=%d\n", args.A, args.B, *(replyCall).Reply)
	// check errors, print, etc.
}
Example #10
0
func main() {

	flag.Parse()

	fmt.Println("Processing using flags: ", flag.Args())

	arith := new(utility.Util)
	rpc.Register(arith)

	rpc.HandleHTTP()

	var pf string = *portFlag

	if !strings.HasPrefix(pf, ":") {
		pf = ":" + pf
	}

	fmt.Println("Starting Server on port ", pf)
	l, e := net.Listen("tcp", pf)
	if e != nil {
		log.Fatal("listen error:", e)
	}
	http.Serve(l, nil)

}
Example #11
0
// 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 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 := &centralServer{
		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() {
	flag.Parse()
	if *storageMasterNodePort == "" {
		if *portnum == 0 {
			*portnum = 9009
		}
		// Single node execution
		*storageMasterNodePort = fmt.Sprintf("localhost:%d", *portnum)
		if *numNodes == 0 {
			*numNodes = 1
			log.Println("Self-mastering, setting nodes to 1")
		}
	}
	l, e := net.Listen("tcp", fmt.Sprintf(":%d", *portnum))
	if e != nil {
		log.Fatal("listen error:", e)
	}
	_, listenport, _ := net.SplitHostPort(l.Addr().String())
	log.Println("Server starting on ", listenport)
	*portnum, _ = strconv.Atoi(listenport)
	ss := storageimpl.NewStorageserver(*storageMasterNodePort, *numNodes, *portnum, uint32(*nodeID))

	srpc := storagerpc.NewStorageRPC(ss)
	rpc.Register(srpc)
	rpc.HandleHTTP()
	http.Serve(l, nil)
}
Example #14
0
func main() {
	node, err := paxos.NewPaxosNode(nid,
		numNodes,
		fakecallback)
	if err != nil {
		fmt.Println("Cannot start node.\n")
		fmt.Println(err)
		return
	}
	listener, err := net.Listen("tcp", fmt.Sprintf(":%d", config.Nodes[nid].Port))
	if err != nil {
		fmt.Printf("node %d cannot listen to port:%s\n", err)
		return
	}
	node.SetListener(&listener)
	rpc.HandleHTTP()
	go http.Serve(listener, nil)
	fmt.Println("Pause node.\n")
	err = node.Pause()
	if err != nil {
		fmt.Println("Cannot Pause node.\n")
		fmt.Println(err)
		return
	}

	time.Sleep(5 * time.Second)
	go func() {
		time.Sleep(10 * time.Second)
		fmt.Println("Resume node.\n")
		err = node.Resume()
		if err != nil {
			fmt.Println("Cannot Resume node.\n")
			fmt.Println(err)
			return
		}
	}()

	res := 0
	for res < 6 {
		_, ok := <-done
		if ok {
			res++
			if res == 5 {
				go func() {
					c := command.Command{strconv.Itoa(nid), strconv.Itoa(0), command.Put, 0, ""}
					node.Replicate(&c)
				}()
			}
		} else {
			break
		}
	}

	if res == 6 {
		fmt.Printf("\n%d receive all commands\n", nid)
	} else {
		fmt.Printf("%d Just break!!!!!\n", res)
	}
	time.Sleep(5 * time.Second)
}
Example #15
0
func (c JoinCommand) kalash() {
	log.Println("Starting kalash watcher")

	shutdownCh := makeShutdownCh()

	c.waitGroup.Add(1)

	defer c.waitGroup.Done()

	log.Println("Starting RPC server on:", c.rpcAddr)

	kalashRPC := new(KalashRPC)
	rpc.Register(kalashRPC)
	rpc.HandleHTTP()
	l, e := net.Listen("tcp", c.rpcAddr)
	if e != nil {
		log.Println("RPC listen error:", e)
		c.watchersErrorCh <- 2
		return
	}

	go http.Serve(l, nil)

	for {
		select {
		case <-shutdownCh:
			log.Println("Kalash watcher stopped")
			return
		}
	}
}
Example #16
0
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
}
// 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
}
Example #18
0
func main() {
	flag.Parse()

	log.Printf("Master starting on port %d\n", *portnum)
	log.Printf("...waiting for %d replicas\n", *numNodes)

	master := &Master{*numNodes,
		make([]string, 0, *numNodes),
		make([]string, 0, *numNodes),
		make([]int, 0, *numNodes),
		new(sync.Mutex),
		make([]*rpc.Client, *numNodes),
		make([]bool, *numNodes),
		make([]bool, *numNodes)}

	rpc.Register(master)
	rpc.HandleHTTP()
	l, err := net.Listen("tcp", fmt.Sprintf(":%d", *portnum))
	if err != nil {
		log.Fatal("Master listen error:", err)
	}

	go master.run()

	http.Serve(l, nil)
}
Example #19
0
// 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
}
Example #20
0
// 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
}
Example #21
0
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
}
Example #22
0
func (k *Kademlia) bootUp(listenAddr string, peerAddr string) (err error) {
	rpc.Register(k)
	rpc.HandleHTTP()

	l, err := net.Listen("tcp", listenAddr)
	if err != nil {
		log.Fatal("Listen: ", err)
		return
	}

	// Serve forever.
	go http.Serve(l, nil)

	_, err = SendPing(k, peerAddr)
	if err != nil {
		log.Fatal("Initial ping error: ", err)
		return
	}

	_, err = IterativeFindNode(k, k.NodeID)
	if err != nil {
		log.Fatal("Bootstrap find_node error: ", err)
	}
	return
}
Example #23
0
// Initialize proxy and tribserver
func initTribserver(storage string, server string, myhostport string) net.Listener {
	// Start proxy
	l, err := net.Listen("tcp", server)
	if err != nil {
		log.Fatal("listen error:", err)
	}
	pc = proxycounter.NewProxyCounter(storage, server)
	if pc == nil {
		fmt.Println("Could not start proxy")
		return nil
	}
	srpc := storagerpc.NewStorageRPC(pc)
	rpc.Register(srpc)
	rpc.HandleHTTP()
	go http.Serve(l, nil)

	// Start tribserver
	ts = tribimpl.NewTribserver(server, myhostport)
	if ts == nil {
		fmt.Println("Could not start tribserver")
		return nil
	}
	rpc.Register(ts)
	return l
}
Example #24
0
// 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) {
	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
}
Example #26
0
func main() {
	arith := new(Arith)

	rpc.Register(arith) // 注册服务
	rpc.HandleHTTP()    // 注册协议

	l, e := net.Listen("tcp", ":1234")
	if e != nil {
		log.Fatal("listen error:", e)
	}
	go http.Serve(l, nil)
	time.Sleep(5 * time.Second)

	client, err := rpc.DialHTTP("tcp", "127.0.0.1"+":1234")
	if err != nil {
		log.Fatal("dialing:", err)
	}

	args := &Args{7, 8} //调用参数,其实没做什么用
	reply := make([]string, 10)
	err = client.Call("Arith.Multiply", args, &reply)
	if err != nil {
		log.Fatal("arith error:", err)
	}
	log.Println(reply)
}
Example #27
0
func main() {
	flag.Parse()
	tricorder.RegisterFlags()
	if os.Geteuid() == 0 {
		fmt.Fprintln(os.Stderr, "Do not run the Image Server as root")
		os.Exit(1)
	}
	setupTls(*caFile, *certFile, *keyFile)
	circularBuffer := logbuf.New(*logbufLines)
	logger := log.New(circularBuffer, "", log.LstdFlags)
	objSrv, err := filesystem.NewObjectServer(*objectDir, logger)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Cannot create ObjectServer\t%s\n", err)
		os.Exit(1)
	}
	imdb, err := scanner.LoadImageDataBase(*imageDir, objSrv, logger)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Cannot load image database\t%s\n", err)
		os.Exit(1)
	}
	tricorder.RegisterMetric("/image-count",
		func() uint { return imdb.CountImages() },
		units.None, "number of images")
	imageserverRpcd.Setup(imdb, logger)
	rpcHtmlWriter := objectserverRpcd.Setup(objSrv, logger)
	rpc.HandleHTTP()
	httpd.AddHtmlWriter(imdb)
	httpd.AddHtmlWriter(rpcHtmlWriter)
	httpd.AddHtmlWriter(circularBuffer)
	if err = httpd.StartServer(*portNum, imdb, false); err != nil {
		fmt.Fprintf(os.Stderr, "Unable to create http server\t%s\n", err)
		os.Exit(1)
	}
}
Example #28
0
func NewDttServer(masterServerHostPort, myHostPort string, allHostPorts []string, numSrvs, srvID int, replace bool) (DttServer, error) {

	// TODO: filter out the server's own hostport string
	ts := new(dttServer)
	ts.Sjob_log = make(map[int]*LogEntry)
	ts.Sjob_log_chan = make(map[int]chan struct{})
	ts.Sjob_logID = 0
	ts.Sjob_currID = 0
	ts.LogMutex = new(sync.Mutex)
	ts.TxMutex = new(sync.Mutex)
	// Create random seed for randomness
	rand.Seed(time.Now().UTC().UnixNano())

	//fmt.Printf("Listeneing")
	// Create the server socket that will listen for incoming RPCs.
	listener, err := net.Listen("tcp", myHostPort)
	if err != nil {
		return nil, err
	}

	http.HandleFunc("/", handlerGeneric)

	TS = ts

	// Handle RPC calls for Libstore
	rpc.HandleHTTP()

	// Setup the HTTP handler that will
	// serve requests in a background goroutine.
	go http.Serve(listener, nil)

	return ts, nil
}
Example #29
0
func MakeLockService(servers []string, me int) *LockService {
	gob.Register(Op{})

	ls := new(LockService)
	ls.me = me
	ls.servers = servers
	ls.max = -1
	ls.locks = make(map[int]int)
	ls.requests = make(chan Request, 256)

	go ls.dequeueRequests()

	rpc.Register(ls)
	ls.px = MakePaxos(servers, me)
	rpc.HandleHTTP()
	listener, err := net.Listen("tcp", servers[me])

	if err != nil {
		panic(err)
	}

	http.Serve(listener, nil)

	return ls
}
Example #30
0
func main() {
	flag.Parse()
	myhostport := fmt.Sprintf("localhost:%d", *portnum)
	masterStorage := fmt.Sprintf("localhost:%d", *masterStoragePort)
	ownStorage := fmt.Sprintf("localhost:%d", *ownStoragePort)

	l, e := net.Listen("tcp", myhostport)
	if e != nil {
		log.Fatal("listen error:", e)
	}
	_, listenport, _ := net.SplitHostPort(l.Addr().String())
	//log.Println("Server starting on ", listenport)
	*portnum, _ = strconv.Atoi(listenport)
	as, lp, err := airlineimpl.NewAirlineServer(*airlineID, myhostport, masterStorage, ownStorage, strings.Split(*replicas, "-"), *pos)
	if err != nil {
		fmt.Println(err)
		return
	}

	arpc := airlinerpc.NewAirlineRPC(as)
	rpc.Register(arpc)
	rpc.Register(lp)
	rpc.HandleHTTP()
	http.Serve(l, nil)
}