Ejemplo n.º 1
0
func connectContext(
	broker *muxBroker,
	ctx *context.Shared, args *ContextSharedArgs) (io.Closer, error) {
	closer := &multiCloser{}

	// Setup Directory
	conn, err := broker.Dial(args.DirectoryId)
	if err != nil {
		return closer, err
	}
	client := rpc.NewClient(conn)
	closer.Closers = append(closer.Closers, client)
	ctx.Directory = &Directory{
		Broker: broker,
		Client: client,
		Name:   "Directory",
	}

	// Setup Ui
	conn, err = broker.Dial(args.UiId)
	if err != nil {
		return closer, err
	}
	client = rpc.NewClient(conn)
	closer.Closers = append(closer.Closers, client)
	ctx.Ui = &Ui{
		Client: client,
		Name:   "Ui",
	}

	return closer, nil
}
Ejemplo n.º 2
0
Archivo: rpc.go Proyecto: jyzhe/beehive
func newRPCClient(addr string) (client *rpcClient, err error) {
	client = &rpcClient{
		addr: addr,
	}

	cmdConn, err := net.DialTimeout("tcp", addr, maxWait)
	if err != nil {
		return nil, err
	}
	client.cmd = rpc.NewClient(cmdConn)

	raftConn, err := net.DialTimeout("tcp", addr, maxWait)
	if err != nil {
		client.raft = client.cmd
	} else {
		client.raft = rpc.NewClient(raftConn)
	}

	prioConn, err := net.DialTimeout("tcp", addr, maxWait)
	if err != nil {
		client.prio = client.raft
	} else {
		client.prio = rpc.NewClient(prioConn)
	}

	msgConn, err := net.DialTimeout("tcp", addr, maxWait)
	if err != nil {
		client.msg = client.cmd
	} else {
		client.msg = rpc.NewClient(msgConn)
	}

	return client, nil
}
Ejemplo n.º 3
0
func TestXRPC(t *testing.T) {
	inconn, outconn := vtesting.SelfConnection()
	ins, outs, err := MuxPairs(inconn, outconn, 2, false)
	if err != nil {
		t.Error("MuxPairs failed: ", err)
	}

	type pair struct {
		In  net.Conn
		Out net.Conn
	}

	pairs := make([]pair, 2)
	pairs[0].In = ins[0]
	pairs[1].In = ins[1]
	pairs[0].Out = outs[0]
	pairs[1].Out = outs[1]

	for _, p := range pairs {
		if p.In.LocalAddr().String() != p.Out.RemoteAddr().String() &&
			p.In.LocalAddr().String() != "" {
			t.Error("Address mismatch: ", p.In.LocalAddr(), " != ", p.Out.RemoteAddr())
		}
		if p.In.RemoteAddr().String() != p.Out.LocalAddr().String() &&
			p.In.RemoteAddr().String() != "" {
			t.Error("Address mismatch: ", p.In.RemoteAddr(), " != ", p.Out.LocalAddr())
		}
	}

	srv := rpc.NewServer()
	srv.Register(new(vtesting.RPCRecv))
	go srv.ServeConn(pairs[0].In)
	go srv.ServeConn(pairs[1].Out)
	client1 := rpc.NewClient(pairs[0].Out)
	defer client1.Close()
	client2 := rpc.NewClient(pairs[1].In)
	defer client2.Close()

	sdata1 := "abc"
	sdata2 := "123"
	rdata1 := ""
	rdata2 := ""

	call1 := client1.Go("RPCRecv.Echo", &sdata1, &rdata1, nil)
	call2 := client2.Go("RPCRecv.Echo", &sdata2, &rdata2, nil)
	<-call2.Done
	<-call1.Done
	if sdata1 != rdata1 || sdata2 != rdata2 {
		t.Error("XRPC failed")
	}
}
Ejemplo n.º 4
0
func sendConfiguration(nodesToConfigure []string, newConfig *server.ServerNodeConfiguration) (err error) {
	for _, node := range nodesToConfigure {
		netconn, localerr := net.Dial("tcp", node)
		//rpcclient, localerr := rpc.Dial("tcp", node)
		if localerr != nil {
			fmt.Printf("Error connecting to node %v: %v\n", node, localerr)
			err = localerr
		} else {
			rpcclient := rpc.NewClient(netconn)

			reply := &server.ConfigChangeResults{}

			localerr = rpcclient.Call("RPCHandler.ConfigurationChange", newConfig, reply)
			if localerr != nil {
				fmt.Printf("Error configuring node %v: %v\n", node, localerr)
				err = localerr
				return err
			}

			if reply.Result.Code != rapi.RI_SUCCESS {
				fmt.Printf("Error applying configuration to node %v: %v\n", node, reply.Result.Description)
			} else {
				fmt.Printf("Configuration set successfully on node %v\n", node)
			}
		}
	}

	return err
}
Ejemplo n.º 5
0
func BenchmarkNetrpcByteSlice(b *testing.B) {
	connC, connS := getTcpPipe(b)
	defer connC.Close()
	defer connS.Close()

	s := rpc.NewServer()
	if err := s.Register(&NetrpcService{}); err != nil {
		b.Fatalf("Error when registering rpc service: %s", err)
	}
	go s.ServeConn(connS)

	c := rpc.NewClient(connC)
	defer c.Close()

	req := []byte("byte slice byte slice aaa bbb ccc foobar")
	b.SetParallelism(250)
	b.ResetTimer()
	b.RunParallel(func(pb *testing.PB) {
		var resp []byte
		for i := 0; pb.Next(); i++ {
			if err := c.Call("NetrpcService.ByteSlice", req, &resp); err != nil {
				b.Fatalf("Unexpected error when calling NetrpcService.ByteSlice(%q): %s", req, err)
			}
			if !bytes.Equal(resp, req) {
				b.Fatalf("Unexpected response: %q. Expected %q", resp, req)
			}
		}
	})
}
Ejemplo n.º 6
0
func (me *Coordinator) logHandler(w http.ResponseWriter, req *http.Request) {
	_, conn, err := me.getHost(req)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintf(w, "<html><head><title>Termite worker error</title></head>")
		fmt.Fprintf(w, "<body>Error: %s</body></html>", err.Error())
		return
	}
	sz := int64(500 * 1024)
	sizeStr, ok := req.URL.Query()["size"]
	if ok {
		fmt.Scanf(sizeStr[0], "%d", &sz)
	}

	logReq := LogRequest{Whence: os.SEEK_END, Off: -sz, Size: sz}
	logRep := LogResponse{}
	client := rpc.NewClient(conn)
	err = client.Call("Worker.Log", &logReq, &logRep)
	client.Close()
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintf(w, "<html><head><title>Termite worker error</title></head>")
		fmt.Fprintf(w, "<body>Error: %s</body></html>", err.Error())
		return
	}

	w.Header().Set("Content-Type", "text/plain")
	w.Write(logRep.Data)
	return
}
Ejemplo n.º 7
0
func (s *ResourceProviderServer) Input(
	args *ResourceProviderInputArgs,
	reply *ResourceProviderInputResponse) error {
	conn, err := s.Broker.Dial(args.InputId)
	if err != nil {
		*reply = ResourceProviderInputResponse{
			Error: NewBasicError(err),
		}
		return nil
	}
	client := rpc.NewClient(conn)
	defer client.Close()

	input := &UIInput{
		Client: client,
		Name:   "UIInput",
	}

	config, err := s.Provider.Input(input, args.Config)
	*reply = ResourceProviderInputResponse{
		Config: config,
		Error:  NewBasicError(err),
	}

	return nil
}
Ejemplo n.º 8
0
// Client returns an RPC client for the plugin.
//
// Subsequent calls to this will return the same RPC client.
func (c *Client) Client() (*rpc.Client, error) {
	addr, err := c.Start()
	if err != nil {
		return nil, err
	}

	c.l.Lock()
	defer c.l.Unlock()

	if c.client != nil {
		return c.client, nil
	}

	conn, err := net.Dial(addr.Network(), addr.String())
	if err != nil {
		return nil, err
	}

	if tcpConn, ok := conn.(*net.TCPConn); ok {
		// Make sure to set keep alive so that the connection doesn't die
		tcpConn.SetKeepAlive(true)
	}

	c.client = rpc.NewClient(conn)
	return c.client, nil
}
Ejemplo n.º 9
0
// NewClient returns a client connection
func NewClient(dsn string, timeout time.Duration) (*Client, error) {
	connection, err := net.DialTimeout("tcp", dsn, timeout)
	if err != nil {
		return nil, err
	}
	return &Client{connection: rpc.NewClient(connection)}, nil
}
Ejemplo n.º 10
0
// StartProvider start a provider-style plugin application at the given path and
// args, and returns an RPC client that communicates with the plugin using gob
// encoding over the plugin's Stdin and Stdout.  The writer passed to output
// will receive output from the plugin's stderr.  Closing the RPC client
// returned from this function will shut down the plugin application.
func StartProvider(output io.Writer, path string, args ...string) (*rpc.Client, error) {
	pipe, err := start(makeCommand(output, path, args))
	if err != nil {
		return nil, err
	}
	return rpc.NewClient(pipe), nil
}
Ejemplo n.º 11
0
func Dial(socketPath string) (*rpc.Client, error) {
	conn, err := npipe.Dial(socketPath)
	if err != nil {
		return nil, err
	}
	return rpc.NewClient(conn), nil
}
Ejemplo n.º 12
0
// NewClient returns a new rpc.Client that communicates with the given command over stdin/stdout.
func NewClient(cmd *exec.Cmd) (*rpc.Client, error) {
	conn, err := newConn(cmd)
	if err != nil {
		return nil, err
	}
	return rpc.NewClient(conn), nil
}
Ejemplo n.º 13
0
func (pc *PluginClient) Start() error {
	if pc.proc != nil {
		return common.NewPluginError("plugin is already started")
	}

	pc.proc = exec.Command(pc.File)
	stderr, _ := pc.proc.StderrPipe()
	stdin, _ := pc.proc.StdinPipe()
	stdout, _ := pc.proc.StdoutPipe()

	fmt.Printf("Starting plugin %s\n", pc.File)
	err := pc.proc.Start()

	if err != nil {
		return err
	}

	go common.StdErrForward(stderr)

	pio := common.NewPluginIO(stdout, stdin)
	pc.mpx = ipc.NewMultiplex(pio)

	rpcwriter := pc.mpx.RawWriterChannel("rpcw")
	rpcreader := pc.mpx.RawReaderChannel("rpcr")

	pc.RPC = rpc.NewClient(common.NewPluginIO(rpcreader, rpcwriter))

	if !pc.VerifyRPC() {
		return common.NewPluginError("plugin is not a valid plugin or is not responding")
	}

	return nil
}
Ejemplo n.º 14
0
// connects to the RPC service on remote server
func DialRPC(addr *net.TCPAddr) (*rpc.Client, error) {
	conn, err := DialHandler(addr, rpcHandlerId)
	if err != nil {
		return nil, err
	}
	return rpc.NewClient(conn), nil
}
Ejemplo n.º 15
0
// Shuffle initiates a random exchange of peer profiles with a neighbour of
// greatest age.
func (c *Cyclon) Shuffle() {
	c.neighbsmu.Lock()
	if len(c.neighbs) == 0 {
		c.neighbsmu.Unlock()
		return
	}

	// Increase the age of all neighbours and our service
	for _, p := range c.neighbs {
		p.Put(age, p.Get(age).(int)+1)
	}
	c.serviceAge++

	// Pop the neighbour that we're going to shuffle with
	q := c.neighbs.PopOldest()

	// Construct the offer. This doesn't remove entries from c.neighbs
	offer := c.neighbs.Sample(c.shuflen - 1)

	c.neighbsmu.Unlock()

	// Calling another cyclon over the network can take a while
	// so we keep our cache unlocked while doing this.
	var answer []pnet.Peer
	conn, err := c.protonet.Dial(q)
	if err == nil {
		cl := rpc.NewClient(conn)
		cl.Call("CyclonRPC.HandleShuffle", append(offer, c.me), &answer)
		conn.Close()
	}

	c.neighbsmu.Lock()
	c.updateCache(answer, offer)
	c.neighbsmu.Unlock()
}
Ejemplo n.º 16
0
// NewClient instantiates a Client. The ID string is used for
// identifying the client in remote logs.
func NewClient(c io.ReadWriteCloser, id string) *Client {
	return &Client{
		client:  rpc.NewClient(c),
		id:      id,
		timings: stats.NewTimerStats(),
	}
}
Ejemplo n.º 17
0
func newNativeClient(address string, timeout time.Duration, t plugin.PluginType, pub *rsa.PublicKey, secure bool) (*PluginNativeClient, error) {
	// Attempt to dial address error on timeout or problem
	conn, err := net.DialTimeout("tcp", address, timeout)
	// Return nil RPCClient and err if encoutered
	if err != nil {
		return nil, err
	}
	r := rpc.NewClient(conn)
	p := &PluginNativeClient{
		connection: r,
		pluginType: t,
		timeout:    timeout,
	}

	p.encoder = encoding.NewGobEncoder()

	if secure {
		key, err := encrypter.GenerateKey()
		if err != nil {
			return nil, err
		}
		encrypter := encrypter.New(pub, nil)
		encrypter.Key = key
		p.encrypter = encrypter
		p.encoder.SetEncrypter(encrypter)
	}

	return p, nil
}
Ejemplo n.º 18
0
// TestGoRPC tests that you can run go RPC over the pipe,
// and that overlapping bi-directional communication is working
// (write while a blocking read is in progress).
func TestGoRPC(t *testing.T) {
	address := `\\.\pipe\TestRPC`
	ln, err := Listen(address)
	if err != nil {
		t.Fatal("Error listening on %q: %v", address, err)
	}
	defer ln.Close()
	server := rpc.NewServer()
	service := &RPCService{}
	server.Register(service)
	go server.Accept(ln)
	var conn *PipeConn
	conn, err = Dial(address)
	if err != nil {
		t.Fatal("Error dialing %q: %v", address, err)
	}
	client := rpc.NewClient(conn)
	defer client.Close()
	req := "dummy"
	resp := ""
	if err = client.Call("RPCService.GetResponse", req, &resp); err != nil {
		t.Fatal("Error calling RPCService.GetResponse: %v", err)
	}
	if req != resp {
		t.Fatal("Unexpected result (expected: %q, got: %q)", req, resp)
	}
}
Ejemplo n.º 19
0
func main() {
	newServer := rpc.NewServer()
	newServer.Register(new(Arith))

	l, e := net.Listen("tcp", "127.0.0.1:1234")
	if e != nil {
		log.Fatalf("net.Listen tcp :0:%v", e)
	}

	go newServer.Accept(l)
	newServer.HandleHTTP("/foo", "/bar")
	time.Sleep(2 * time.Second)

	address, err := net.ResolveTCPAddr("tcp", "127.0.0.1:1234")
	if err != nil {
		panic(err)
	}
	conn, _ := net.DialTCP("tcp", nil, address)
	defer conn.Close()

	client := rpc.NewClient(conn)
	defer client.Close()

	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)
}
Ejemplo n.º 20
0
func RpcClient(network, address string, timeout time.Duration) (*rpc.Client, error) {
	conn, err := net.DialTimeout(network, address, timeout)
	if err != nil {
		return nil, err
	}
	return rpc.NewClient(conn), nil
}
Ejemplo n.º 21
0
func Handle(listener *Listener, coordinator Coordinator) {
	coordinator.Register()

	for {
		conn, err := listener.Accept()
		if err != nil {
			log.Printf("Error: %s\n", err)
			continue
		}
		class, err := readOneByte(conn)
		if err != nil {
			conn.Close()
		}

		switch class {
		case 'm':
			log.Printf("Minion has connected.\n")
			mConn := conn.(*Conn)
			go coordinator.Handle(rpc.NewClient(conn), mConn)
		case 'r':
			log.Printf("Administrator has connected.\n")
			go rpc.ServeConn(conn)
		default:
			log.Printf("No idea what that was. Closing. Value %c\n", class)
			conn.Close()
		}
	}
}
Ejemplo n.º 22
0
func benchmarkEchoGobRPC(b *testing.B, size int) {
	var client *rpc.Client
	benchmarkEcho(b, size, listenAndServeGobRPC,
		func(addr net.Addr) {
			var err error
			conn, err := tls.Dial(addr.Network(), addr.String(), clientTLSConfig)
			if err != nil {
				b.Fatal(err)
			}
			client = rpc.NewClient(conn)
			if err != nil {
				b.Fatal(err)
			}
		},
		func() {
			if err := client.Close(); err != nil {
				b.Fatal(err)
			}
		},
		func(echoMsg string) string {
			args := EchoRequest{Msg: echoMsg}
			reply := EchoResponse{}
			if err := client.Call("Echo.Echo", &args, &reply); err != nil {
				b.Fatal(err)
			}
			return reply.Msg
		},
	)
}
Ejemplo n.º 23
0
// NewClient creates a client from an already-open connection-like value.
// Dial is typically used instead.
func NewClient(conn io.ReadWriteCloser) (*Client, error) {
	// Create the yamux client so we can multiplex
	mux, err := yamux.Client(conn, nil)
	if err != nil {
		conn.Close()
		return nil, err
	}

	// Connect to the control stream.
	control, err := mux.Open()
	if err != nil {
		mux.Close()
		return nil, err
	}

	// Create the broker and start it up
	broker := newMuxBroker(mux)
	go broker.Run()

	// Build the client using our broker and control channel.
	return &Client{
		broker:  broker,
		control: rpc.NewClient(control),
	}, nil
}
Ejemplo n.º 24
0
func (lsm *LocalServiceManager) call(serviceMethod string, args interface{}, reply interface{}) error {
	lsm.mu.Lock()
	defer lsm.mu.Unlock()

	if lsm.client == nil {
		exe, err := osext.Executable()
		if err != nil {
			return err
		}

		listener, err := net.Listen("tcp", "127.0.0.1:0")
		if err != nil {
			return err
		}
		defer listener.Close()

		runner := exec.Command(exe, "service-runner", "--address", listener.Addr().String(), "--state-file", lsm.stateFile)
		runner.Stdout = os.Stdout
		runner.Stderr = os.Stderr
		go func() {
			runner.Run()
			lsm.mu.Lock()
			lsm.client = nil
			lsm.mu.Unlock()
		}()

		conn, err := listener.Accept()
		if err != nil {
			return err
		}
		lsm.client = rpc.NewClient(conn)
	}

	return lsm.client.Call(serviceMethod, args, reply)
}
Ejemplo n.º 25
0
func BenchmarkNetrpcInt(b *testing.B) {
	connC, connS := getTcpPipe(b)
	defer connC.Close()
	defer connS.Close()

	s := rpc.NewServer()
	if err := s.Register(&NetrpcService{}); err != nil {
		b.Fatalf("Error when registering rpc service: %s", err)
	}
	go s.ServeConn(connS)

	c := rpc.NewClient(connC)
	defer c.Close()

	b.SetParallelism(250)
	b.ResetTimer()
	b.RunParallel(func(pb *testing.PB) {
		var x int
		for i := 0; pb.Next(); i++ {
			if err := c.Call("NetrpcService.Int", i, &x); err != nil {
				b.Fatalf("Unexpected error when calling NetrpcService.Int(%d): %s", i, err)
			}
			if i != x {
				b.Fatalf("Unexpected response: %d. Expected %d", x, i)
			}
		}
	})
}
Ejemplo n.º 26
0
func NewRemoteFileSystem(url string) (filesystem.FileSystem, error) {
	// Resolve/Validate URL?
	uri, err := neturl.Parse(url)

	if err != nil {
		return nil, err
	}

	// Check for host:port part
	host := uri.Host
	p := "8123"

	if strings.Contains(host, ":") {
		host, p, err = net.SplitHostPort(host)
	}
	port, _ := strconv.Atoi(p)

	// Create RPC server
	var client *rpc.Client
	conn, err := tls.Dial("tcp", fmt.Sprintf("%s:%d", host, port), pki.MirrorConfig.ClientTlsConfig)

	// TODO: How to terminate connection when done - we want to keep open during course of events?
	//defer conn.Close()
	if err != nil {
		log.Fatalf("client: dial: %s", err)
		conn.Close()
	}
	log.Println("client: connected to: ", conn.RemoteAddr())
	client = rpc.NewClient(conn)
	return RemoteFileSystem{rootUrl: *uri, client: client}, err
}
Ejemplo n.º 27
0
func Test_RpcServiceServesRequests(t *testing.T) {
	f := NewRpcFixture(t)
	defer f.Close()

	serviceWasCalled := false
	svc := MockRpcService{
		callback: func(text string, result *string) error {
			serviceWasCalled = true
			(*result) = strings.ToUpper(text)
			return nil
		},
	}

	err := f.rpcServer.Register(&svc)
	require.NoError(t, err)

	conn, err := f.rpcServer.Connect()
	require.NoError(t, err)
	defer conn.Close()

	client := rpc.NewClient(conn)
	defer client.Close()

	var reply string
	err = client.Call("MockRpcService.Test", "lowercase", &reply)

	assert.NoError(t, err)
	assert.Equal(t, "LOWERCASE", reply)
	assert.True(t, serviceWasCalled)
}
Ejemplo n.º 28
0
Archivo: peers.go Proyecto: vgp/gogp
func ServeForever(addr string, seeds []string) {
	// RPC server
	srv := rpc.NewServer()
	ps := PeerServer{&sync.Mutex{}, make([]peer, 0)}
	srv.Register(&ps)

	// Connection server
	listener, err := net.Listen("tcp", addr)
	if err != nil {
		log.Fatal("gogp/peers: Could not set up listen: ", err)
	}
	defer listener.Close()

	// Incoming connections from connection server
	go func() {
		for {
			conn, err := listener.Accept()
			if err != nil {
				log.Fatal("Couldn't receive connection")
			}
			conns, err := muxconn.Split(conn, 2)
			if err != nil {
				log.Fatal("Couldn't mux an inbound connection")
			}
			go srv.ServeConn(conns[0])
			p := peer{rpc.NewClient(conns[1]), time.Now()}
			func() {
				ps.mutex.Lock()
				defer ps.mutex.Unlock()
				ps.peers = append(ps.peers, p)
			}()
		}
	}()

}
Ejemplo n.º 29
0
func main() {
	flag.Parse()

	rpc.Register(new(Worker))
	l, e := net.Listen("tcp", ":"+strconv.Itoa(*port))
	if e != nil {
		log.Fatal("listen error:", e)
	}
	log.Print("listening for connections on ", l.Addr())

	if *connect != "" {
		log.Printf("calling %s\n", *connect)
		conn, err := net.Dial("tcp", *connect)
		if err != nil {
			log.Fatalf("failed to connect to %s: %s", *connect, err)
		}
		addr, _, err := net.SplitHostPort(conn.LocalAddr().String())
		if err != nil {
			log.Fatalf("failed to get the local address: %s", err)
		}
		laddr := net.JoinHostPort(addr, strconv.Itoa(*port))
		client := rpc.NewClient(conn)
		var res struct{}
		client.Call("WorkerList.Add", laddr, &res)
		client.Close()
	}

	rpc.Accept(l)
}
Ejemplo n.º 30
0
func BenchmarkPingPong_2MB_netrpc(b *testing.B) {
	ready := make(chan bool)
	done := make(chan bool)
	addr := "127.0.0.1:12346"
	go runServer(b, addr, ready, done)

	<-ready // Block until server is ready.

	connC, err := net.DialTimeout("tcp", addr, 10*time.Second)
	if err != nil {
		b.Fatalf("cannot dial. Error: %v", err)
		return
	}
	defer connC.Close()

	c := rpc.NewClient(connC)
	defer c.Close()

	req := NewRequest(mb2)
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		var reply PostingList
		if err := c.Call("PostingList.PingPong", req, &reply); err != nil {
			b.Fatalf("While running request: %v", err)
			return
		}
	}
	b.StopTimer()
	done <- true
}