Beispiel #1
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
}
Beispiel #2
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
}
// 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
}
// 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
}
Beispiel #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) {
	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) {
	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
}
Beispiel #7
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
}
// 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")
}
Beispiel #9
0
// Test libstore returns nil when it cannot connect to the server
func testNonexistentServer() {
	if l, err := libstore.NewLibstore(fmt.Sprintf("localhost:%d", *portnum), fmt.Sprintf("localhost:%d", *portnum), libstore.Normal); l == nil || err != nil {
		fmt.Println("PASS")
		passCount++
	} else {
		LOGE.Println("FAIL: libstore does not return a non-nil error when it cannot connect to nonexistent storage server")
		failCount++
	}
	cleanupLibstore(nil)
}
Beispiel #10
0
// Initialize proxy and libstore
func initLibstore(storage, server, myhostport string, alwaysLease bool) (net.Listener, error) {
	l, err := net.Listen("tcp", server)
	if err != nil {
		LOGE.Println("Failed to listen:", err)
		return nil, err
	}

	// The ProxyServer acts like a "StorageServer" in the system, but also has some
	// additional functionalities that allow us to enforce the number of RPCs made
	// to the storage server, etc.
	proxyCounter, err := proxycounter.NewProxyCounter(storage, server)
	if err != nil {
		LOGE.Println("Failed to setup test:", err)
		return nil, err
	}
	pc = proxyCounter

	// Normally the StorageServer would register itself to receive RPCs,
	// but we don't call NewStorageServer here, do we need to do it here instead.
	rpc.RegisterName("StorageServer", storagerpc.Wrap(pc))

	// Normally the TribServer would call the two methods below when it is first
	// created, but these tests mock out the TribServer all together, so we do
	// it here instead.
	rpc.HandleHTTP()
	go http.Serve(l, nil)

	var leaseMode libstore.LeaseMode
	if alwaysLease {
		leaseMode = libstore.Always
	} else if myhostport == "" {
		leaseMode = libstore.Never
	} else {
		leaseMode = libstore.Normal
	}

	// Create and start the Libstore.
	libstore, err := libstore.NewLibstore(server, myhostport, leaseMode)
	if err != nil {
		LOGE.Println("Failed to create Libstore:", err)
		return nil, err
	}
	ls = libstore
	return l, nil
}
Beispiel #11
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) {
	ls, err := libstore.NewLibstore(masterServerHostPort, myHostPort, libstore.Normal)
	if err != nil {
		return nil, err
	}
	ts := new(tribServer)
	ts.ls = ls
	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(ts))
	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 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)
	if err != nil {
		return nil, errors.New("Creating new libstore failed")
	}
	newTribServer := &tribServer{
		ls: libstore,
	}

	listener, err := net.Listen("tcp", myHostPort)
	if err != nil {
		return nil, err
	}

	err = rpc.RegisterName("TribServer", tribrpc.Wrap(newTribServer))
	if err != nil {
		return nil, err
	}
	rpc.HandleHTTP()
	go http.Serve(listener, nil)

	return newTribServer, nil
}
Beispiel #13
0
func main() {
	flag.Parse()
	if flag.NArg() < 2 {
		flag.Usage()
		os.Exit(1)
	}

	cmdmap := make(map[string]cmdInfo)
	for k, v := range cmdList {
		cmdmap[k] = cmdInfo{cmdline: k, nargs: v}
	}

	cmd := flag.Arg(0)
	ci, found := cmdmap[cmd]
	if !found {
		flag.Usage()
		os.Exit(1)
	}
	if flag.NArg() < (ci.nargs + 1) {
		flag.Usage()
		os.Exit(1)
	}

	var leaseCallbackAddr string
	if *handleLeases {
		// Setup an HTTP handler to receive remote lease revocation requests.
		// The student's libstore implementation is resonsible for calling
		// rpc.RegisterName("LeaseCallbacks", librpc.Wrap(libstore)) to finish
		// the setup.
		l, err := net.Listen("tcp", ":0")
		if err != nil {
			log.Fatalln("Failed to listen:", err)
		}
		_, listenPort, _ := net.SplitHostPort(l.Addr().String())
		leaseCallbackAddr = net.JoinHostPort("localhost", listenPort)
		rpc.HandleHTTP()
		go http.Serve(l, nil)
	}

	var leaseMode libstore.LeaseMode
	if *handleLeases && *forceLease {
		leaseMode = libstore.Always
	} else if leaseCallbackAddr == "" {
		leaseMode = libstore.Never
	} else {
		leaseMode = libstore.Normal
	}

	masterHostPort := net.JoinHostPort(*serverAddress, strconv.Itoa(*port))
	ls, err := libstore.NewLibstore(masterHostPort, leaseCallbackAddr, leaseMode)
	if err != nil {
		log.Fatalln("Failed to create libstore:", err)
	}

	for i := 0; i < *numTimes; i++ {
		switch cmd {
		case "g":
			val, err := ls.Get(flag.Arg(1))
			if err != nil {
				fmt.Println("ERROR:", err)
			} else {
				fmt.Println(val)
			}
		case "lg":
			val, err := ls.GetList(flag.Arg(1))
			if err != nil {
				fmt.Println("ERROR:", err)
			} else {
				for _, i := range val {
					fmt.Println(i)
				}
			}
		case "p", "la", "lr":
			var err error
			switch cmd {
			case "p":
				err = ls.Put(flag.Arg(1), flag.Arg(2))
			case "la":
				err = ls.AppendToList(flag.Arg(1), flag.Arg(2))
			case "lr":
				err = ls.RemoveFromList(flag.Arg(1), flag.Arg(2))
			}
			if err == nil {
				fmt.Println("OK")
			} else {
				fmt.Println("ERROR:", err)
			}
		}
	}

	if *handleLeases {
		fmt.Println("Waiting 20 seconds for lease callbacks...")
		time.Sleep(20 * time.Second)
	}
}