当前位置: 首页>>代码示例>>Golang>>正文


Golang rpc.Server类代码示例

本文整理汇总了Golang中net/rpc.Server的典型用法代码示例。如果您正苦于以下问题:Golang Server类的具体用法?Golang Server怎么用?Golang Server使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Server类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: serve

// This serves a single RPC connection on the given RPC server on
// a random port.
func serve(server *rpc.Server) (err error) {
	if os.Getenv(MagicCookieKey) != MagicCookieValue {
		return errors.New("Please do not execute plugins directly. Packer will execute these for you.")
	}

	// If there is no explicit number of Go threads to use, then set it
	if os.Getenv("GOMAXPROCS") == "" {
		runtime.GOMAXPROCS(runtime.NumCPU())
	}

	minPort, err := strconv.ParseInt(os.Getenv("PACKER_PLUGIN_MIN_PORT"), 10, 32)
	if err != nil {
		return
	}

	maxPort, err := strconv.ParseInt(os.Getenv("PACKER_PLUGIN_MAX_PORT"), 10, 32)
	if err != nil {
		return
	}

	log.Printf("Plugin minimum port: %d\n", minPort)
	log.Printf("Plugin maximum port: %d\n", maxPort)

	// Set the RPC port range
	packrpc.PortRange(int(minPort), int(maxPort))

	var address string
	var listener net.Listener
	for port := minPort; port <= maxPort; port++ {
		address = fmt.Sprintf("127.0.0.1:%d", port)
		listener, err = net.Listen("tcp", address)
		if err != nil {
			err = nil
			continue
		}

		break
	}

	defer listener.Close()

	// Output the address to stdout
	log.Printf("Plugin address: %s\n", address)
	fmt.Println(address)
	os.Stdout.Sync()

	// Accept a connection
	log.Println("Waiting for connection...")
	conn, err := listener.Accept()
	if err != nil {
		log.Printf("Error accepting connection: %s\n", err.Error())
		return
	}

	// Serve a single connection
	log.Println("Serving a plugin connection...")
	server.ServeConn(conn)
	return
}
开发者ID:khangtoh,项目名称:packer,代码行数:61,代码来源:plugin.go

示例2: RegistFunctions

func (c *ConsumerServer) RegistFunctions(server *rpc.Server) error {
	err := server.Register(new(Call))
	if err != nil {
		log.Printf("Consumer regist object failed! err:%s \n", err)
		return err
	}
	return nil
}
开发者ID:fantasycool,项目名称:spread_compute,代码行数:8,代码来源:consumer.go

示例3: ConnectServer

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
}
开发者ID:petemoore,项目名称:rpc4,代码行数:8,代码来源:connect.go

示例4: RegisterServices

func RegisterServices(server *rpc.Server) {
	for _, s := range services {
		//TODO : the service type is as of now int, need to find out a
		// way how to get the type of an object
		server.Register(s)
	}

}
开发者ID:sac,项目名称:glusterd2,代码行数:8,代码来源:services.go

示例5: NewDryMartini

// Create a new DryMartini object with its own kademlia and RPC server
func NewDryMartini(listenStr string, keylen int) *DryMartini {
	var err error
	var s *rpc.Server
	var dm *DryMartini

	dm = new(DryMartini)

	dm.EasyNewFlowIndex = 0

	//Initialize key pair
	dm.KeyPair, err = rsa.GenerateKey(rand.Reader, keylen)
	if err != nil {
		dbg.Printf("Failed to generate key! %s", true, err)
		panic(1)
	}

	//Initialize flow struct
	dm.Bartender = make(map[UUID]MartiniPick)
	dm.Momento = make(map[UUID][]FlowIDSymmKeyPair)
	dm.MapFlowIndexToFlowID = make(map[int]FlowInfo)

	var host net.IP
	var port uint16
	host, port, err = kademlia.AddrStrToHostPort(listenStr)

	//Initialize our Kademlia
	//portStr := strconv.FormatUint(uint64(port), 10)
	//var rpcPathStr string = kademlia.RpcPath+portStr
	var rpcPathStr = "junk"
	dbg.Printf("making new Kademlia with listenStr:%s, rpcPath\n", Verbose, listenStr, rpcPathStr)

	dm.KademliaInst, s = kademlia.NewKademlia(listenStr, &rpcPathStr)
	kademlia.BucketsAsArray(dm.KademliaInst)

	//myMartiniContact <- ip, port, public key
	dm.myMartiniContact.NodeIP = host.String()
	dm.myMartiniContact.NodePort = port
	dm.myMartiniContact.PubKey = dm.KeyPair.PublicKey.N.String()
	dm.myMartiniContact.PubExp = dm.KeyPair.PublicKey.E

	dbg.Printf("NewDryMartini: making new Kademlia with NodeIP: %s. NodePort:%d\n", Verbose, dm.myMartiniContact.NodeIP, dm.myMartiniContact.NodePort)

	/*
		if Verbose {
			dbg.Printf("NodeIP: %s\n", dm.myMartiniContact.NodeIP)
			dbg.Printf("NodePort: %d\n", dm.myMartiniContact.NodePort)
			dbg.Printf("PubKey: %s\n", dm.myMartiniContact.PubKey)
			dbg.Printf("PubExp: %d\n", dm.myMartiniContact.PubKey)
		}*/
	//register
	err = s.Register(dm)
	if err != nil {
		dbg.Printf("Failed to register Drymartini! %s", true, err)
		panic(1)
	}

	return dm
}
开发者ID:johhud1,项目名称:KademliaMixNetwork,代码行数:59,代码来源:drymartini.go

示例6: Serve

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))
	}
}
开发者ID:ironzhang,项目名称:golang,代码行数:9,代码来源:server.go

示例7: registerComponent

// registerComponent registers a single Packer RPC component onto
// the RPC server. If id is true, then a unique ID number will be appended
// onto the end of the endpoint.
//
// The endpoint name is returned.
func registerComponent(server *rpc.Server, name string, rcvr interface{}, id bool) string {
	endpoint := name
	if id {
		fmt.Sprintf("%s.%d", endpoint, atomic.AddUint64(&endpointId, 1))
	}

	server.RegisterName(endpoint, rcvr)
	return endpoint
}
开发者ID:Nitron,项目名称:packer,代码行数:14,代码来源:server.go

示例8: waitForConnExit

func waitForConnExit(c net.Conn, server *rpc.Server) (ret chan bool) {
	ret = make(chan bool)
	go func() {
		tcpConn := c.(*net.TCPConn)
		tcpConn.SetKeepAlive(true)
		server.ServeConn(c)
		ret <- true
	}()
	return ret
}
开发者ID:tartaruszen,项目名称:dbstream,代码行数:10,代码来源:scheduler.go

示例9: ServeRedis

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)
	}
}
开发者ID:icexin,项目名称:raftkv,代码行数:10,代码来源:redis.go

示例10: ServeMsgpack

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)
	}
}
开发者ID:icexin,项目名称:raftkv,代码行数:10,代码来源:msgpack.go

示例11: DispatchForever

func DispatchForever(connch <-chan net.Conn, srv *rpc.Server, clientch chan<- *rpc.Client) {
	for conn := range connch {
		muxed, err := muxconn.Split(conn, 2)
		if err != nil {
			log.Println("birpc: Failed to mux incoming connection from", conn.RemoteAddr().String(), "to", conn.LocalAddr().String(), ", dropping")
			continue
		}
		// Server on first muxed conn, client on second
		go srv.ServeConn(muxed[0])
		clientch <- rpc.NewClient(muxed[1])
	}
}
开发者ID:funkygao,项目名称:govtil,代码行数:12,代码来源:birpc.go

示例12: NewJSONRPCHandler

// NewJSONRPCHandler makes a JSON-RPC handler for s.
func NewJSONRPCHandler(s *rpc.Server) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		defer r.Body.Close()
		w.Header().Set("Content-Type", "application/json")
		var b bytes.Buffer
		var codec = jsonrpc.NewServerCodec(&readWriteCloser{r: r.Body, w: &b})
		if err := s.ServeRequest(codec); err != nil {
			panic(err)
		}
		codec.Close()
		io.Copy(w, &b)
	})
}
开发者ID:willfaught,项目名称:services,代码行数:14,代码来源:server.go

示例13: NewServerCodec

// NewServerCodec returns a new rpc.ServerCodec using JSON-RPC 2.0 on conn,
// which will use srv to execute batch requests.
//
// If srv is nil then rpc.DefaultServer will be used.
func NewServerCodec(conn io.ReadWriteCloser, srv *rpc.Server) rpc.ServerCodec {
	if srv == nil {
		srv = rpc.DefaultServer
	}
	srv.Register(JSONRPC2{})
	return &serverCodec{
		dec:     json.NewDecoder(conn),
		enc:     json.NewEncoder(conn),
		c:       conn,
		srv:     srv,
		pending: make(map[uint64]*json.RawMessage),
	}
}
开发者ID:plumbum,项目名称:rpc-codec,代码行数:17,代码来源:server.go

示例14: RegistFunctions

func (p *ProducerServer) RegistFunctions(server *rpc.Server) error {
	err := server.Register(new(Call))
	if err != nil {
		log.Printf("register function failed!err:%s \n", err)
		return err
	}
	err = server.Register(new(PMSync))
	if err != nil {
		log.Printf("regist PMSync failed! err:%s \n", err)
		return err
	}
	return nil
}
开发者ID:fantasycool,项目名称:spread_compute,代码行数:13,代码来源:producer.go

示例15: Make

//
// the application wants to create a paxos peer.
// the ports of all the paxos peers (including this one)
// are in peers[]. this servers port is peers[me].
//
func Make(peers []string, me int, rpcs *rpc.Server) *Paxos {
	px := &Paxos{}
	px.peers = peers
	px.me = me

	// Your initialization code here.

	if rpcs != nil {
		// caller will create socket &c
		rpcs.Register(px)
	} else {
		rpcs = rpc.NewServer()
		rpcs.Register(px)

		// prepare to receive connections from clients.
		// change "unix" to "tcp" to use over a network.
		os.Remove(peers[me]) // only needed for "unix"
		l, e := net.Listen("unix", peers[me])
		if e != nil {
			log.Fatal("listen error: ", e)
		}
		px.l = l

		// please do not change any of the following code,
		// or do anything to subvert it.

		// create a thread to accept RPC connections
		go func() {
			for px.dead == false {
				conn, err := px.l.Accept()
				if err == nil && px.dead == false {
					if px.unreliable && (rand.Int63()%1000) < 100 {
						// discard the request.
						conn.Close()
					} else if px.unreliable && (rand.Int63()%1000) < 200 {
						// process the request but force discard of reply.
						c1 := conn.(*net.UnixConn)
						f, _ := c1.File()
						err := syscall.Shutdown(int(f.Fd()), syscall.SHUT_WR)
						if err != nil {
							fmt.Printf("shutdown: %v\n", err)
						}
						px.rpcCount++
						go rpcs.ServeConn(conn)
					} else {
						px.rpcCount++
						go rpcs.ServeConn(conn)
					}
				} else if err == nil {
					conn.Close()
				}
				if err != nil && px.dead == false {
					fmt.Printf("Paxos(%v) accept: %v\n", me, err.Error())
				}
			}
		}()
	}

	return px
}
开发者ID:kedebug,项目名称:golang-programming,代码行数:65,代码来源:paxos.go


注:本文中的net/rpc.Server类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。