コード例 #1
0
ファイル: storageserver.go プロジェクト: ammarar/DS-P2
func main() {
	lsplog.SetVerbose(10)
	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)
}
コード例 #2
0
ファイル: storagetest.go プロジェクト: ammarar/DS-P2
func main() {
	lsplog.SetVerbose(10)
	ls, e := libstore.NewLibstore("localhost:9009", "localhost:9999", 0)
	e = ls.AppendToList("ammar:hey", "Helly")
	e = ls.AppendToList("ammar:hey", "Helly")
	e = ls.AppendToList("ammar:hey", "Kelly")
	lsplog.Vlogf(1, "%v", e)
	var k []string
	k, e = ls.GetList("ammar:hey")
	lsplog.Vlogf(1, "%v %v", k, e)
	e = ls.RemoveFromList("ammar:hey", "Helly")
	lsplog.Vlogf(1, "%v ", e)
	e = ls.RemoveFromList("ammar:hey", "Helly")
	lsplog.Vlogf(1, "%v ", e)
	k, e = ls.GetList("ammar:hey")
	lsplog.Vlogf(1, "%v %v", k, e)
}
コード例 #3
0
ファイル: trib-impl.go プロジェクト: yuguess/440_P2
/**@brief create a new tribserver
 * @param string
 * @param string
 * @return *tribserver
 */
func NewTribserver(storagemaster, myhostport string) *Tribserver {
	lsplog.SetVerbose(3)
	fmt.Printf("st_master:%s, port:%s\n", storagemaster, myhostport)

	var svr *Tribserver = new(Tribserver)
	var err error

	lsplog.Vlogf(3, "try to create libstore")
	// libstore.NONE forces no leases on Get and GetList requests
	svr.Store, err =
		libstore.NewLibstore(storagemaster, myhostport, libstore.NONE)

	if lsplog.CheckReport(1, err) {
		return nil
	}

	svr.Id = 1

	return svr
}
コード例 #4
0
ファイル: storageimpl.go プロジェクト: yuguess/440_P2
func NewStorageserver(master string, numnodes int, portnum int,
	nodeid uint32) *Storageserver {

	lsplog.SetVerbose(3)
	fmt.Println("Create New Storage Server")
	fmt.Printf("master:%s, numnodes:%d, portnum:%d, nodeid:%d\n",
		master, numnodes, portnum, nodeid)
	var masterNode *rpc.Client
	var regArgs storageproto.RegisterArgs
	var regReply storageproto.RegisterReply
	var storage Storageserver
	var err error
	var nodes = make(map[storageproto.Node]bool)

	storage.nodeid = nodeid
	storage.leasePool = make(map[string]leaseEntry)

	selfAddr := fmt.Sprintf("localhost:%d", portnum)

	if master == selfAddr {
		fmt.Printf("for master node\n")

		//for master node
		storage.isMaster = true
		storage.portnum = portnum
		//storage.portnum = DEFAULT_MASTER_PORT
		storage.nodes = nodes
		storage.numnodes = numnodes

		//add masternode itself to nodes table
		//hostport := fmt.Sprintf("localhost:%d", DEFAULT_MASTER_PORT)
		self := storageproto.Node{master, nodeid}
		storage.nodes[self] = true
	} else {

		masterNode, err = rpc.DialHTTP("tcp", master)
		if lsplog.CheckReport(1, err) {
			return nil
		}

		regArgs.ServerInfo.HostPort = fmt.Sprintf("localhost:%d", portnum)
		regArgs.ServerInfo.NodeID = nodeid

		//for slave node
		storage.isMaster = false
		storage.portnum = portnum

		fmt.Printf("for slave node\n")
		fmt.Printf("begin try $$$\n")

		for i := 0; (regReply.Ready == false) && (i < 10); i++ {
			fmt.Printf("try %d times\n", i)
			masterNode.Call("StorageRPC.Register", &regArgs, &regReply)
			/*
			   if lsplog.CheckReport(1, err) {
			     lsplog.Vlogf(3, "slave %d call RegisterServer %d time failed",
			                                                 i + 1, portnum)
			   }*/
			time.Sleep(1000 * time.Millisecond)
		}
	}

	storage.hash = make(map[string][]byte)

	return &storage
}