本文整理汇总了Golang中net/rpc.Server.Register方法的典型用法代码示例。如果您正苦于以下问题:Golang Server.Register方法的具体用法?Golang Server.Register怎么用?Golang Server.Register使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net/rpc.Server
的用法示例。
在下文中一共展示了Server.Register方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: 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)
}
}
示例3: 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
}
示例4: 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
}
示例5: 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
}
示例6: 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),
}
}
示例7: NewPolicyAgent
// NewPolicyMgr Creates a new policy manager
func NewPolicyAgent(agent *OfnetAgent, rpcServ *rpc.Server) *PolicyAgent {
policyAgent := new(PolicyAgent)
// initialize
policyAgent.agent = agent
policyAgent.Rules = make(map[string]*PolicyRule)
policyAgent.DstGrpFlow = make(map[string]*ofctrl.Flow)
// Register for Master add/remove events
rpcServ.Register(policyAgent)
// done
return policyAgent
}
示例8: NewVrouter
// Create a new vrouter instance
func NewVrouter(agent *OfnetAgent, rpcServ *rpc.Server) *Vrouter {
vrouter := new(Vrouter)
// Keep a reference to the agent
vrouter.agent = agent
// Create a route table and my router mac
vrouter.routeTable = make(map[string]*OfnetRoute)
vrouter.flowDb = make(map[string]*ofctrl.Flow)
vrouter.portVlanFlowDb = make(map[uint32]*ofctrl.Flow)
vrouter.myRouterMac, _ = net.ParseMAC("00:00:11:11:11:11")
// Register for Route rpc callbacks
rpcServ.Register(vrouter)
return vrouter
}
示例9: 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.stateMap = make(map[int]State)
px.maxCanDisregard = -1
px.lastDoneSignalled = -1
flag.Parse()
px.me = me
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 {
glog.Fatalf("listen error: ", e)
}
px.l = l
// create a thread to accept RPC connections
go func() {
for px.isdead() == false {
conn, err := px.l.Accept()
if err == nil && px.isdead() == false {
atomic.AddInt32(&px.rpcCount, 1)
go rpcs.ServeConn(conn)
} else if err == nil {
conn.Close()
}
if err != nil && px.isdead() == false {
//////fmt.Printf("Paxos(%v) accept: %v\n", me, err.Error())
}
}
}()
}
return px
}
示例10: StartDNS
func StartDNS(servers []string, rpcs *rpc.Server) *DNSserver {
// call gob.Register on structures you want
// Go's RPC library to marshall/unmarshall.
gob.Register(DNSserver{})
gob.Register(GetServerArgs{})
gob.Register(GetServerReply{})
dns := new(DNSserver)
dns.servers = servers
dns.address = portDNS()
if rpcs != nil {
// caller will create socket &c
rpcs.Register(dns)
} else {
rpcs = rpc.NewServer()
rpcs.Register(dns)
os.Remove(dns.address)
l, e := net.Listen("unix", dns.address)
if e != nil {
log.Fatal("listen error: ", e)
}
dns.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 {
conn, err := dns.l.Accept()
if err == nil {
go rpcs.ServeConn(conn)
}
if err != nil {
fmt.Printf("DNSerror: %v\n", err.Error())
}
}
}()
}
return dns
}
示例11: handleConnection
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
}
}
示例12: NewVxlan
// Create a new vxlan instance
func NewVxlan(agent *OfnetAgent, rpcServ *rpc.Server) *Vxlan {
vxlan := new(Vxlan)
// Keep a reference to the agent
vxlan.agent = agent
// init DBs
vxlan.macRouteDb = make(map[string]*MacRoute)
vxlan.vlanDb = make(map[uint16]*Vlan)
vxlan.macFlowDb = make(map[string]*ofctrl.Flow)
vxlan.portVlanFlowDb = make(map[uint32]*ofctrl.Flow)
log.Infof("Registering vxlan RPC calls")
// Register for Route rpc callbacks
err := rpcServ.Register(vxlan)
if err != nil {
log.Fatalf("Error registering vxlan RPC")
}
return vxlan
}
示例13: Server
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))
}
}
示例14: StartWhanauPaxos
func StartWhanauPaxos(servers []string, me int, uid string,
rpcs *rpc.Server) *WhanauPaxos {
wp := new(WhanauPaxos)
if rpcs != nil {
// caller will create socket &c
rpcs.Register(wp)
} else {
rpcs := rpc.NewServer()
rpcs.Register(wp)
}
newservers := make([]string, len(servers))
for i, _ := range servers {
newservers[i] = port(uid+"-wp", i)
}
wp.handledRequests = make(map[int64]interface{})
wp.px = paxos.Make(newservers, me, nil)
wp.db = make(map[KeyType]TrueValueType)
wp.pending_writes = make(map[PendingInsertsKey]string)
wp.currSeq = 0
wp.currView = 0
wp.me = me
wp.myaddr = servers[me]
wp.uid = uid
gob.Register(Op{})
gob.Register(PaxosGetArgs{})
gob.Register(PaxosPutArgs{})
gob.Register(PaxosGetReply{})
gob.Register(PaxosPutReply{})
gob.Register(PaxosPendingInsertsArgs{})
gob.Register(PaxosPendingInsertsReply{})
return wp
}
示例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, network bool, tag string) *Paxos {
px := &Paxos{}
// Read memory options
px.persistent = persistent
px.recovery = recovery
px.dbUseCompression = dbUseCompression
px.dbUseCache = dbUseCache
px.dbCacheSize = dbCacheSize
px.writeToMemory = writeToMemory
// Network stuff
px.peers = peers
px.me = me
px.network = network
px.reachable = make([]bool, len(px.peers))
for i := range px.peers {
px.reachable[i] = true
}
px.deaf = false
// Paxos state
px.instances = make(map[int]Proposal)
px.leader = make(map[int]int)
px.proposed = make(map[int]bool)
px.maxInstance = -1
px.done = make(map[int]int)
for i := 0; i < len(px.peers); i++ {
px.done[i] = -1
}
px.doneChannels = make(map[int]chan bool)
// Persistence stuff
waitChan := make(chan int)
go func() {
waitChan <- 1
px.startup(tag)
}()
<-waitChan
if rpcs != nil {
// caller will create socket &c
if !printRPCerrors {
disableLog()
rpcs.Register(px)
enableLog()
} else {
rpcs.Register(px)
}
} else {
rpcs = rpc.NewServer()
if !printRPCerrors {
disableLog()
rpcs.Register(px)
enableLog()
} else {
rpcs.Register(px)
}
// prepare to receive connections from clients.
// change "unix" to "tcp" to use over a network.
if px.network {
l, e := net.Listen("tcp", ":"+strconv.Itoa(startport))
if e != nil {
log.Fatal("listen error: ", e)
}
px.l = l
} else {
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.deaf || (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.
if !px.network {
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)
//.........这里部分代码省略.........