Esempio n. 1
0
// This method is used for creating connections between nodes at start up
func (ss *Storageserver) Connect(addr net.Addr) {
	ss.nodeData.HostPort = addr.String()
	if ss.numnodes == 0 { // slave logic here
		log.Printf("Connecting as slave")
		for { // Register with the master until it's ready
			master, err := rpc.DialHTTP("tcp", ss.master)
			if err != nil {
				log.Printf("Failed to connect to the master; retrying: %s", err.String())
				time.Sleep(1000000000)
			} else {
				reply := new(storageproto.RegisterReply)
				err = master.Call("StorageRPC.Register", &storageproto.RegisterArgs{ss.nodeData}, reply)
				if err != nil {
					log.Printf("Something went wrong with the call; retrying")
				} else if reply.Ready {
					ss.servers = make([]interface{}, len(reply.Clients))
					for i := 0; i < len(reply.Clients); i++ {
						ss.servers[i] = &serverData{nil, reply.Clients[i]}
						ss.serversMap[reply.Clients[i].NodeID] = ss.servers[i].(*serverData)
					}
					break
				}
				time.Sleep(1000000000)
			}
		}
	} else { // master case
		ss.servers = make([]interface{}, ss.numnodes)
		ss.servers[ss.numnodes-1] = &serverData{nil, ss.nodeData}
		ss.serversMap[ss.nodeData.NodeID] = ss.servers[ss.numnodes-1].(*serverData)
		ss.numnodes--
	}
	ss.cond.L.Lock()
	defer ss.cond.L.Unlock()
	log.Printf("Connecting to other servers")
	for ss.numnodes > 0 { //master has to wait until all other nodes have registered
		ss.cond.Wait()
	}
	for i := 0; i < len(ss.servers); i++ { // make rpc connection to all nodes
		server := ss.servers[i].(*serverData)
		if server.clientInfo.NodeID == ss.nodeData.NodeID {
			continue
		}
		for {
			rpc, err := rpc.DialHTTP("tcp", server.clientInfo.HostPort)
			if err != nil {
				log.Printf("Problem making rpc connection")
			} else {
				server.rpc = rpc
				log.Printf("Made connection to %s", server.clientInfo.HostPort)
				break
			}
			time.Sleep(1000000000)
		}
	}
	ss.sortServers()
}
Esempio n. 2
0
func (me *WorkerDaemon) report(coordinator string, port int) {
	client, err := rpc.DialHTTP("tcp", coordinator)
	if err != nil {
		log.Println("dialing coordinator:", err)
		return
	}

	hostname, err := os.Hostname()
	if err != nil {
		log.Println("hostname", err)
		return
	}

	cname, err := net.LookupCNAME(hostname)
	if err != nil {
		log.Println("cname", err)
		return
	}
	cname = strings.TrimRight(cname, ".")
	req := Registration{
		Address: fmt.Sprintf("%v:%d", cname, port),
		Name:    fmt.Sprintf("%s:%d", hostname, port),
		Version: Version(),
	}

	rep := 0
	err = client.Call("Coordinator.Register", &req, &rep)
	if err != nil {
		log.Println("coordinator rpc error:", err)
	}
}
Esempio n. 3
0
func GetAllClientsByService(classname string) (clientList []*rpc.Client) {
	var newClient *rpc.Client
	var err os.Error
	serviceList := GetAllServiceProviders(classname)

	for i, s := range serviceList {
		hostString := fmt.Sprintf("%s:%d", s.IPAddress, s.Port)
		protocol := strings.ToLower(s.Protocol) // to be safe
		switch protocol {
		default:
			newClient, err = rpc.DialHTTP("tcp", hostString)
		case "json":
			newClient, err = jsonrpc.Dial("tcp", hostString)
		}

		if err != nil {
			LogWarn(fmt.Sprintf("Found %d nodes to provide service %s requested on %s, but failed to connect to #%d.",
				len(serviceList), classname, hostString, i))
			//NewError(NO_CLIENT_PROVIDES_SERVICE, classname)
			continue
		}
		clientList = append(clientList, newClient)
	}
	return
}
Esempio n. 4
0
// This is simple today - it returns the first listed service that matches the request
// Load balancing needs to be applied here somewhere.
func GetRandomClientByService(classname string) (*rpc.Client, os.Error) {
	var newClient *rpc.Client
	var err os.Error
	serviceList := GetAllServiceProviders(classname)

	if len(serviceList) > 0 {
		chosen := rand.Int() % len(serviceList)
		s := serviceList[chosen]

		hostString := fmt.Sprintf("%s:%d", s.IPAddress, s.Port)
		protocol := strings.ToLower(s.Protocol) // to be safe
		switch protocol {
		default:
			newClient, err = rpc.DialHTTP("tcp", hostString)
		case "json":
			newClient, err = jsonrpc.Dial("tcp", hostString)
		}

		if err != nil {
			LogWarn(fmt.Sprintf("Found %d nodes to provide service %s requested on %s, but failed to connect.",
				len(serviceList), classname, hostString))
			return nil, NewError(NO_CLIENT_PROVIDES_SERVICE, classname)
		}

	} else {
		LogWarn(fmt.Sprintf("Found no node to provide service %s.", classname))
		return nil, NewError(NO_CLIENT_PROVIDES_SERVICE, classname)
	}
	return newClient, nil
}
Esempio n. 5
0
func monitorServices() {
	for {
		skylib.LoadConfig()
		for _, v := range skylib.NS.Services {
			if (v.Port != *skylib.Port) || (v.IPAddress != *skylib.BindIP) {
				portString := fmt.Sprintf("%s:%d", v.IPAddress, v.Port)
				x, err := rpc.DialHTTP("tcp", portString)
				if err != nil {
					log.Println("BAD CON:", err)
					v.RemoveFromConfig()
					skylib.Errors.Add(1)
					break
				}
				hc := skylib.HeartbeatRequest{Timestamp: time.Seconds()}
				hcr := skylib.HeartbeatResponse{}
				err = x.Call("Service.Ping", hc, &hcr)
				if err != nil {
					log.Println(err.String())
					skylib.Errors.Add(1)
				}
				x.Close()
				skylib.Requests.Add(1)
			}
		}
		syscall.Sleep(2000 * 1000000) // sleep then do it again!
	}
}
Esempio n. 6
0
// This is simple today - it returns the first listed service that matches the request
// Load balancing needs to be applied here somewhere.
func GetRandomClientByProvides(provides string) (*rpc.Client, os.Error) {
	var providesList = make([]*Service, 0)

	var newClient *rpc.Client
	var err os.Error

	for _, v := range NS.Services {
		if v != nil {
			if v.Provides == provides {
				providesList = append(providesList, v)
			}

		}
	}

	if len(providesList) > 0 {
		random := rand.Int() % len(providesList)
		s := providesList[random]

		portString := fmt.Sprintf("%s:%d", s.IPAddress, s.Port)
		newClient, err = rpc.DialHTTP("tcp", portString)
		if err != nil {
			LogError(WARN, fmt.Sprintf("Found %d Clients to service %s request.", len(providesList), provides))
			return nil, NewError(NO_CLIENT_PROVIDES_SERVICE, provides)
		}

	} else {
		return nil, NewError(NO_CLIENT_PROVIDES_SERVICE, provides)
	}
	return newClient, nil
}
Esempio n. 7
0
func NewProxyStore(addr string) *ProxyStore {
	client, err := rpc.DialHTTP("tcp", addr)
	if err != nil {
		log.Println("ProxyStore:", err)
	}
	return &ProxyStore{urls: NewURLStore(""), client: client}
}
Esempio n. 8
0
func runMapper(reducer string, mFunc Mapper) {
	c, err := rpc.DialHTTP("tcp", reducer)
	if err != nil {
		log.Fatal("Connecting to Reducer: ", err)
	}
	defer c.Close()
	if err := c.Call("Reducer.Hello", &e{}, &e{}); err != nil {
		log.Fatal("Reducer.Hello: ", err)
	}
	var calls []*rpc.Call
	for {
		var w interface{}
		if err := c.Call("Reducer.GetWork", &e{}, &w); err != nil {
			if err.String() == ErrDone.String() {
				break
			}
			log.Fatal("Reducer.GetWork: ", err)
		}
		r, err := mFunc(w)
		if err != nil {
			log.Fatal("Mapper: ", err)
		}
		call := c.Go("Reducer.SendResult", &r, &e{}, nil)
		calls = append(calls, call)
	}
	for _, call := range calls {
		<-call.Done
	}
	if err := c.Call("Reducer.Goodbye", &e{}, &e{}); err != nil {
		log.Fatal("Reducer.Goodbye: ", err)
	}
	log.Print("Mapper exited cleanly; shutting down")
}
Esempio n. 9
0
func main() {
	client, err := rpc.DialHTTP("tcp", "localhost:1234")
	if err != nil {
		log.Exit("dialing:", err)
	}
	args := &arith.Args{7, 8}
	reply := new(arith.Reply)
	err = client.Call("Arith.Add", args, reply)
	if err != nil {
		log.Exit("arith error:", err)
	}
	fmt.Printf("Arith: %d+%d=%d\n", args.A, args.B, reply.C)
}
Esempio n. 10
0
func client(host string, port int) {
	client, err := rpc.DialHTTP("tcp", host+":"+fmt.Sprintf("%d", port))
	if err != nil {
		log.Exit("dialing:", err)
	}

	var reply string
	err = client.Call("ProcessStarter.Start", processMap["server"], &reply)
	if err != nil {
		log.Exit("error:", err)
	}
	//    fmt.Printf("Arith: %d*%d=%d", args.A, args.B, *reply)

}
Esempio n. 11
0
//main for the matrix client
func main() {
	client, err := rpc.DialHTTP("tcp", "localhost:1234")
	if err != nil {
		log.Fatal("dialing:", err)
	}
	cw := NewClientWrapper(client)
	commands := make_commands(cw)
	sin := bufio.NewReader(os.Stdin)
	var l string
	for err != os.EOF {
		l, err = sin.ReadString('\n')
		e := commands.Parseln(l)
		if e.String() == "Closing" {
			break
		}
		if e != nil {
			fmt.Fprintln(os.Stderr, e)
		}
	}
}
Esempio n. 12
0
func main() {
	flag.Parse()

	if *help {
		PrintUsage()
		return
	}

	// Build a slice of RPC clients, as specified by the user as arguments
	workers := make([]*Worker, 0, 5)

	for idx, arg := range flag.Args() {
		log.Printf("Opening RPC connection to %s", arg)
		client, err := rpc.DialHTTP("tcp", arg)
		log.Printf("New RPC connection %p", client)

		if err != nil {
			log.Fatalf("Could not connect to client %s: %s", arg, err)
		}

		id := fmt.Sprintf("%s:%d", arg, idx)
		worker := &Worker{arg, id, client, nil, nil, 0, nil}
		workers = append(workers, worker)
	}

	if !*modeStressConn && !*modeStressReqs && !*modeManual {
		log.Fatalf("No mode selected, please supply one of -stressconn, -stressreqs or -manual")
	}

	if *modeManual {
		RunManualBenchmark(workers)
	}

	if *modeStressConn {
		StressTestConnections(workers)
	}

	if *modeStressReqs {
		StressTestRequests(workers)
	}
}
Esempio n. 13
0
func Monitor(addr string) {
	if addr != "" {
		var err os.Error
		client, err = rpc.DialHTTP("tcp", addr)
		if err != nil {
			log.Println(err)
		}
	}
	t := time.NewTicker(period)
	for {
		select {
		case <-t.C:
			output()
		case s := <-In:
			c, ok := counters[s]
			if !ok {
				c = &counter{}
				counters[s] = c
			}
			c.period++
		}
	}
}
Esempio n. 14
0
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
}
Esempio n. 15
0
// This is simple today - it returns the first listed service that matches the request
// Load balancing needs to be applied here somewhere.
func GetRandomClientByProvides(provides string) (*rpc.Client, os.Error) {
	var newClient *rpc.Client
	var err os.Error
	serviceList := GetServiceProviders(provides)

	if len(serviceList) > 0 {
		chosen := rand.Int() % len(serviceList)
		s := serviceList[chosen]

		hostString := fmt.Sprintf("%s:%d", s.IPAddress, s.Port)
		newClient, err = rpc.DialHTTP("tcp", hostString)
		if err != nil {
			LogWarn(fmt.Sprintf("Found %d Services to service %s request on %s.",
				len(serviceList), provides, hostString))
			return nil, NewError(NO_CLIENT_PROVIDES_SERVICE, provides)
		}

	} else {
		LogWarn(fmt.Sprintf("Found no Service to service %s request.", provides))
		return nil, NewError(NO_CLIENT_PROVIDES_SERVICE, provides)
	}
	return newClient, nil
}
Esempio n. 16
0
func (me *mirrorConnections) fetchWorkers() (newMap map[string]bool) {
	newMap = map[string]bool{}
	client, err := rpc.DialHTTP("tcp", me.coordinator)
	if err != nil {
		log.Println("dialing coordinator:", err)
		return newMap
	}
	defer client.Close()
	req := 0
	rep := Registered{}
	err = client.Call("Coordinator.List", &req, &rep)
	if err != nil {
		log.Println("coordinator rpc error:", err)
		return newMap
	}

	for _, v := range rep.Registrations {
		newMap[v.Address] = true
	}
	if len(newMap) == 0 {
		log.Println("coordinator has no workers for us.")
	}
	return newMap
}
Esempio n. 17
0
File: main.go Progetto: nf/webfilter
func main() {
	flag.Parse()
	client, err := rpc.DialHTTP("tcp", *masterAddr)
	if err != nil {
		log.Fatal("Dial:", err)
	}
	in := bufio.NewReader(os.Stdin)
	for {
		host, _, err := in.ReadLine()
		if err != nil {
			log.Fatal("Read:", err)
		}
		var ok bool
		err = client.Call("Master.Validate", host, &ok)
		if err != nil {
			log.Fatal("Call:", err)
		}
		if ok {
			os.Stdout.Write([]byte("OK\n"))
		} else {
			os.Stdout.Write([]byte("ERR\n"))
		}
	}
}
Esempio n. 18
0
func NewClient(addr string) (*Client, os.Error) {
	self := new(Client)
	client, err := rpc.DialHTTP("tcp", addr)
	self.client = client
	return self, err
}
Esempio n. 19
0
func main() {
	log.SetOutput(os.Stderr)

	nakedclient, err := rpc.DialHTTP("tcp", "localhost:"+strconv.Itoa(registry.Port))
	if err != nil {
		log.Fatal("dialing:", err)
	}

	client := regclient.NewClientWrapper(nakedclient)

	cmds := parser.NewCommands()

	bind := func(args []string) os.Error {
		if len(args) == 2 && args[0] != "" && args[1] != "" {
			key := args[0]
			value := args[1]
			_, err = client.Bind(key, ([]byte)(value))
			if err == nil {
				fmt.Println("Successful Binding for KEY: " + key + " VALUE: " + value)
			} else {
				log.Println("Bind Failure: " + err.String())
			}
			return nil
		}
		return os.NewError("Invalid Arguments")
	}

	cmds.AddInputCommand("bind", bind)

	lookup := func(args []string) os.Error {
		if len(args) == 1 && args[0] != "" {
			var value interface{}
			key := args[0]
			value, err = client.Lookup(key, value)
			if err == nil {
				result := (string)(value.([]byte))
				fmt.Println("KEY: " + key + " Bound to VALUE: " + result)
			} else {
				log.Println("Lookup Failure: " + err.String())
			}
			return nil
		}
		return os.NewError("Invalid Arguments")
	}

	cmds.AddInputCommand("lookup", lookup)

	remove := func(args []string) os.Error {
		if len(args) == 1 && args[0] != "" {
			key := args[0]
			_, err = client.Remove(key, 0)
			if err == nil {
				fmt.Println("Removed KEY: " + key)
			} else {
				log.Println("Remove Failure: " + err.String())
			}
			return nil
		}
		return os.NewError("Invalid Arguments")
	}

	cmds.AddInputCommand("remove", remove)

	close_str := "CLOSE_PROGRAM"

	close_func := func() os.Error {
		err = client.Close()
		if err == nil {
			fmt.Println("successful close")
		} else {
			log.Fatal("Close Failure: " + err.String())
		}
		return os.NewError(close_str)
	}

	cmds.AddCommand("close", close_func)

	help := func() os.Error {
		fmt.Println("Command:\t\tDescription:")
		fmt.Println("")
		fmt.Println("help\t\t\tdisplay a list of commands")
		fmt.Println("close\t\t\tclose the scession")
		fmt.Println("bind: KEY, VALUE\tbind the KEY to the VALUE")
		fmt.Println("lookup: KEY\t\tfind the VALUE for th KEY")
		fmt.Println("remove: KEY\t\tremove the KEY and the VALUE")
		return nil
	}

	cmds.AddCommand("help", help)

	sin := bufio.NewReader(os.Stdin)
	var line string
	var error os.Error
	for error != os.EOF {
		fmt.Println("")
		line, error = sin.ReadString('\n')
		e := cmds.Parseln(line)
		if e != nil {
			if e.String() == close_str {
				return
			} else {
				log.Println(e.String())
			}
		}
	}

}
Esempio n. 20
0
func main() {
	flag.Parse()
	err := getConfig()
	if err != nil {
		err = genConfig()
		if err != nil {
			panic(err)
		}
		fmt.Println("\"" + user + "\":\"" + sekrit + "\"")
		return
	}
	r, e := rpc.DialHTTP("tcp", host)
	if e != nil {
		fmt.Println("Cannot connect to server: " + host)
		os.Exit(-1)
	}
	remote := &LunchServer{r, Auth{Name: "User"}}

	if *seats != 0 {
		if !remote.drive(*seats) {
			fmt.Println("Drive Failed")
		}
		return
	}

	if *walk {
		if !remote.undrive() {
			fmt.Println("UnDrive Failed")
		}
		return
	}
	if !*unvote && flag.NArg() == 0 {
		places := remote.displayPlaces()
		if places != nil {
			for _, p := range *places {
				fmt.Println(p.String())
			}
		}
		return
	}

	var dest uint = 0
	if *add && !*del {
		name := strings.Join(flag.Args(), " ")
		dest = remote.addPlace(name)
		return
	}

	if dest == 0 {
		dest, _ = strconv.Atoui(flag.Arg(0))
	}

	if *del {
		if !remote.delPlace(dest) {
			fmt.Println("Could not delete. This can happen if there are still votes on the place or if you did not nominate it.")
		}
		return
	}

	if *unvote {
		if !remote.unvote() {
			fmt.Println("Unvoting Failed")
		}
		return
	} else {
		if !remote.vote(dest) {
			fmt.Println("Vote Failed")
		}
		return
	}
}
Esempio n. 21
0
func main() {
	flag.Parse()
	if flag.NArg() < 2 {
		log.Fatal("Insufficient arguments to client")
	}

	cmd := flag.Arg(0)

	serverPort := fmt.Sprintf("%d", *portnum)
	client, err := rpc.DialHTTP("tcp", net.JoinHostPort(*serverAddress, serverPort))
	if err != nil {
		log.Fatal("Could not connect to server:", err)
	}

	cmdlist := []cmd_info{
		{"p", "StorageRPC.Put", 2},
		{"g", "StorageRPC.Get", 1},
		{"la", "StorageRPC.AppendToList", 2},
		{"lr", "StorageRPC.RemoveFromList", 2},
		{"lg", "StorageRPC.GetList", 1},
	}

	cmdmap := make(map[string]cmd_info)
	for _, j := range cmdlist {
		cmdmap[j.cmdline] = j
	}

	ci, found := cmdmap[cmd]
	if !found {
		log.Fatal("Unknown command ", cmd)
	}
	if flag.NArg() < (ci.nargs + 1) {
		log.Fatal("Insufficient arguments for ", cmd)
	}

	// This is a little ugly, but it's quick to code. :)
	// What's the saying?  "Do what I say, not what I do."
	var putargs *storageproto.PutArgs
	getargs := &storageproto.GetArgs{flag.Arg(1), false, storageproto.Client{"", 0}}
	getreply := &storageproto.GetReply{}
	putreply := &storageproto.PutReply{}
	getlistreply := &storageproto.GetListReply{}
	if ci.nargs == 2 {
		putargs = &storageproto.PutArgs{flag.Arg(1), flag.Arg(2)}
	}
	var status int
	switch cmd {
	case "g":
		err = client.Call(ci.funcname, getargs, getreply)
		status = getreply.Status
	case "lg":
		err = client.Call(ci.funcname, getargs, getlistreply)
		status = getlistreply.Status
	case "p", "la", "lr":
		err = client.Call(ci.funcname, putargs, putreply)
		status = putreply.Status
	}
	if err != nil {
		fmt.Println(ci.funcname, " failed: ", err)
	} else if status != storageproto.OK {
		fmt.Print("error\t", flag.Arg(1), "\t")
		switch status {
		case storageproto.EKEYNOTFOUND:
			fmt.Println("key not found")
		case storageproto.EITEMNOTFOUND:
			fmt.Println("item not found")
		case storageproto.EPUTFAILED:
			fmt.Println("put failed")
		case storageproto.EITEMEXISTS:
			fmt.Println("Item already exists in list")
		}
	} else {
		switch cmd {
		case "g":
			fmt.Println(flag.Arg(1), "\t", getreply.Value)
		case "lg":
			fmt.Println(flag.Arg(1), "\t", strings.Join(getlistreply.Value, "\t"))
		case "p", "la", "lr":
			fmt.Println(ci.funcname, " succeeded")
		}
	}
}