示例#1
0
// At the moment, this can start more than one Server on the same
// port, which could be a problem.
func (self *RpcService) Serve(done chan bool) {
	portString := fmt.Sprintf("%s:%d", self.IPAddress, self.Port)
	log.Println(portString)

	l, e := net.Listen("tcp", portString)
	if e != nil {
		log.Fatal("listen error:", e)
	}
	defer l.Close()

	switch self.Protocol {
	default:
		rpc.HandleHTTP() // Seems safe to call multiple times, but must
		// that precede net.Listen()?
		log.Println("Starting http server")
		http.Serve(l, nil)
	case "json":
		log.Println("Starting jsonrpc server")
		for {
			conn, err := l.Accept()
			if err != nil {
				panic(err.String())
			}
			jsonrpc.ServeConn(conn)
		}
	}
	done <- true // This may never occur.
}
示例#2
0
// rpc over tcp
func serveTCP(addr string) {
	l, err := net.Listen("tcp", addr)
	if err != nil {
		log.Fatal(err)
	}
	defer l.Close()
	for {
		conn, _ := l.Accept()
		jsonrpc.ServeConn(conn)
	}
}
示例#3
0
// rpc over udp
func serveUDP(addr string) {
	pl := new(PacketListener)
	c, err := net.ListenPacket("udp", addr)
	if err != nil {
		log.Fatal(err)
	}
	defer pl.Close()
	pl.c = c

	for {
		jsonrpc.ServeConn(pl)
	}
}
func main() {
	log.Print("Starting Server...")
	l, err := net.Listen("tcp", "localhost:1234")
	defer l.Close()
	if err != nil {
		log.Fatal(err)
	}
	log.Print("listening on: ", l.Addr())
	rpc.Register(new(RPCFunc))
	for {
		log.Print("waiting for connections ...")
		conn, err := l.Accept()
		if err != nil {
			log.Printf("accept error: %s", conn)
			continue
		}
		log.Printf("connection started: %v", conn.RemoteAddr())
		go jsonrpc.ServeConn(conn)
	}
}
示例#5
0
// rpc over tls over tcp
func serveTLS(addr string) {
	config := &tls.Config{
		Rand: rand.Reader,
		Time: time.Nanoseconds,
	}
	config.Certificates = make([]tls.Certificate, 1)
	var err os.Error
	config.Certificates[0], err = tls.LoadX509KeyPair("server.crt", "server.key")
	if err != nil {
		log.Fatal(err)
	}

	l, err := tls.Listen("tcp", ":1235", config)
	if err != nil {
		log.Fatal(err)
	}
	for {
		conn, _ := l.Accept()
		jsonrpc.ServeConn(conn)
	}
}
示例#6
0
func test_jsonrpc() {
	fmt.Println("Testing jsonrpc with TimeService...")

	cli, srv := net.Pipe()
	go jsonrpc.ServeConn(srv)

	client := jsonrpc.NewClient(cli)
	defer client.Close()

	// Synchronous calls
	args := &Args{"GMT"}
	reply := new(Reply)
	for i := 0; i < 10; i++ {
		err := client.Call("TimeService.GetTime", args, reply)
		if err != nil {
			fmt.Errorf("TimeService.GetTime: expected no error but got string %q", err.String())
		}

		fmt.Printf("time:%s\n jsonrpc.counter:%d\n", reply.Time, reply.Counter)
	}
}