// 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 }
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 }
func main() { r, err := jsonrpc.Dial("tcp", "localhost:6666") if err != nil { log.Exit("Error dialing host: ", err) } remote := &TestRPCSrv{r} a := "foo" var b string err = remote.Call("testApp.PrintStuff", &a, &b) if err != nil { log.Exit("Error calling function: ", err) } log.Exit(b) }