Exemplo n.º 1
0
func ConnectServer(addr string, s *rpc.Server) error {
	if conn, err := net.Dial("tcp", addr); err == nil {
		s.ServeCodec(NewServerCodec(conn))
	} else {
		return err
	}
	return nil
}
Exemplo n.º 2
0
func Serve(s *rpc.Server, l net.Listener) {
	for {
		conn, err := l.Accept()
		if err != nil {
			log.Fatal("rpc.Serve: accept:", err.Error())
		}
		go s.ServeCodec(NewServerCodec(conn))
	}
}
Exemplo n.º 3
0
func ServeMsgpack(l net.Listener, s *rpc.Server) error {
	for {
		conn, err := l.Accept()
		if err != nil {
			return err
		}
		codec := codec.MsgpackSpecRpc.ServerCodec(conn, &msgpackHandle)
		go s.ServeCodec(codec)
	}
}
Exemplo n.º 4
0
func ServeRedis(l net.Listener, s *rpc.Server) error {
	for {
		conn, err := l.Accept()
		if err != nil {
			return err
		}
		codec := NewRedisServerCodec(conn)
		go s.ServeCodec(codec)
	}
}
Exemplo n.º 5
0
//function for handling RPC connection
func handle(server *rpc.Server, conn net.Conn) {
	fmt.Println("start handle")
	//defer conn.Close() //make sure connection gets closed
	remote := conn.RemoteAddr().String() + " --> " + conn.LocalAddr().String()
	fmt.Println("==conn " + remote)
	//requests
	//doRequests(conn)
	// time.Sleep(3 * time.Second)

	fmt.Println("==conn " + remote)
	server.ServeCodec(jsonrpc.NewServerCodec(conn))
	fmt.Println("==discon " + remote)
	fmt.Println("end handle")
}
Exemplo n.º 6
0
func (c *Simple) waitForConnections(rpcs *rpc.Server) {
	for {
		conn, err := c.listener.Accept()
		if err == nil {
			if *use_codec {
				//rpcCodec := codec.GoRpc.ServerCodec(conn, &mh)
				rpcCodec := codec.MsgpackSpecRpc.ServerCodec(conn, &mh)
				go rpcs.ServeCodec(rpcCodec)
			} else {
				go rpcs.ServeConn(conn)
			}
		} else {
			// handle error
			//fmt.Println("ERROR: ", err)
		}
	}
}
Exemplo n.º 7
0
func (s *Server) handleConnection(conn net.Conn) {
	// We create a new server each time so that we can have access to the
	// underlying connection. The standard rpc package does not give us access
	// to the calling connection :/
	var server *rpc.Server = rpc.NewServer()

	// Get a free service from the pool.
	var service *Discovery
	select {
	case service = <-s.servicePool:
		// Success
	default:
		service = newDiscoveryService(s)
	}

	// Set up the service variables.
	service.init(conn, atomic.AddInt32(&s.nextConnId, 1))

	// If debugging is enabled, log all rpc traffic.
	var rwc io.ReadWriteCloser = conn
	if *debug {
		rwc = &debugInput{conn}
	}

	// Set up the rpc service and start serving the connection.
	server.Register(service)
	server.ServeCodec(jsonrpc.NewServerCodec(rwc))

	// Connection has disconnected. Remove any registered services.
	s.removeAll(service)

	// Reset the service state.
	service.init(nil, -1)

	select {
	case s.servicePool <- service:
		// Success
	default:
		// Buffer is full
	}
}
Exemplo n.º 8
0
func Server() {
	var err error
	var l net.Listener
	l, err = net.Listen("tcp", ":1234")
	if err != nil {
		log.Fatal("listen error:", err)
	}
	defer l.Close()
	var s *rpc.Server
	s = rpc.NewServer()
	s.Register(new(Arith))
	for {
		var err error
		var c net.Conn
		c, err = l.Accept()
		log.Println(c.RemoteAddr())
		if err != nil {
			log.Fatal("listen error:", err)
		}
		go s.ServeCodec(jsonrpc.NewServerCodec(c))
	}
}
Exemplo n.º 9
0
func (control *Control) handle(
	conn_fd int,
	server *rpc.Server) {

	control_file := os.NewFile(uintptr(conn_fd), "control")
	defer control_file.Close()

	// Read single header.
	// Our header is exactly 9 characters, and we
	// expect the last character to be a newline.
	// This is a simple plaintext protocol.
	header_buf := make([]byte, 9, 9)
	n, err := control_file.Read(header_buf)
	if n != 9 || header_buf[8] != '\n' {
		if err != nil {
			control_file.Write([]byte(err.Error()))
		} else {
			control_file.Write([]byte("invalid header"))
		}
		return
	}
	header := string(header_buf)

	// We read a special header before diving into RPC
	// mode. This is because for the novmrun case, we turn
	// the socket into a stream of input/output events.
	// These are simply JSON serialized versions of the
	// events for the guest RPC interface.

	if header == "NOVM RUN\n" {

		decoder := utils.NewDecoder(control_file)
		encoder := utils.NewEncoder(control_file)

		var start noguest.StartCommand
		err := decoder.Decode(&start)
		if err != nil {
			// Poorly encoded command.
			encoder.Encode(err.Error())
			return
		}

		// Grab our client.
		client, err := control.Ready()
		if err != nil {
			encoder.Encode(err.Error())
			return
		}

		// Call start.
		result := noguest.StartResult{}
		err = client.Call("Server.Start", &start, &result)
		if err != nil {
			encoder.Encode(err.Error())
			return
		}

		// Save our pid.
		pid := result.Pid
		inputs := make(chan error)
		outputs := make(chan error)
		exitcode := make(chan int)

		// This indicates we're okay.
		encoder.Encode(nil)

		// Wait for the process to exit.
		go func() {
			wait := noguest.WaitCommand{
				Pid: pid,
			}
			var wait_result noguest.WaitResult
			err := client.Call("Server.Wait", &wait, &wait_result)
			if err != nil {
				exitcode <- 1
			} else {
				exitcode <- wait_result.Exitcode
			}
		}()

		// Read from stdout & stderr.
		go func() {
			read := noguest.ReadCommand{
				Pid: pid,
				N:   4096,
			}
			var read_result noguest.ReadResult
			for {
				err := client.Call("Server.Read", &read, &read_result)
				if err != nil {
					inputs <- err
					return
				}
				err = encoder.Encode(read_result.Data)
				if err != nil {
					inputs <- err
					return
				}
			}
		}()

		// Write to stdin.
		go func() {
			write := noguest.WriteCommand{
				Pid: pid,
			}
			var write_result noguest.WriteResult
			for {
				err := decoder.Decode(&write.Data)
				if err != nil {
					outputs <- err
					return
				}
				err = client.Call("Server.Write", &write, &write_result)
				if err != nil {
					outputs <- err
					return
				}
			}
		}()

		// Wait till exit.
		status := <-exitcode
		encoder.Encode(status)

		// Wait till EOF.
		<-inputs

		// Send a notice and close the socket.
		encoder.Encode(nil)

	} else if header == "NOVM RPC\n" {

		// Run as JSON RPC connection.
		codec := jsonrpc.NewServerCodec(control_file)
		server.ServeCodec(codec)
	}
}