本文整理汇总了Golang中net/rpc.HandleHTTP函数的典型用法代码示例。如果您正苦于以下问题:Golang HandleHTTP函数的具体用法?Golang HandleHTTP怎么用?Golang HandleHTTP使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了HandleHTTP函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: NewStockServer
func NewStockServer(masterHostPort, myHostPort string) (StockServer, error) {
s := rpc.NewServer()
ls, err := libstore.NewLibstore(masterHostPort, myHostPort)
if err != nil {
return nil, err
}
sMap := make(map[string]string)
ss := &stockServer{
server: s,
ls: ls,
sessionMap: sMap,
}
listener, err := net.Listen("tcp", myHostPort)
if err != nil {
return nil, err
}
log.Println("StockServer listening on address: ", myHostPort)
err = rpc.RegisterName("StockServer", stockrpc.Wrap(ss))
if err != nil {
log.Println("Failed to register StockServer RPC")
return nil, err
}
rpc.HandleHTTP()
go http.Serve(listener, nil)
return ss, nil
}
示例2: main
func main() {
fmt.Printf("node %d starts\n", nid)
node, err := paxos.NewPaxosNode(nid, numNodes, fakecallback)
if err != nil {
fmt.Println("Cannot start node.\n")
fmt.Println(err)
return
}
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", config.Nodes[nid].Port))
if err != nil {
fmt.Printf("node %d cannot listen to port:%s\n", err)
return
}
node.SetListener(&listener)
rpc.HandleHTTP()
go http.Serve(listener, nil)
time.Sleep(5 * time.Second)
for i := 0; i < 2; i++ {
c := command.Command{strconv.Itoa(nid), strconv.Itoa(i), command.Put, i, ""}
node.Replicate(&c)
}
for res := 0; res < 4; res++ {
_, ok := <-done
if !ok {
break
}
}
node.DumpLog()
fmt.Printf("node %d closes\n", nid)
}
示例3: NewTribServer
// NewTribServer creates, starts and returns a new TribServer. masterServerHostPort
// is the master storage server's host:port and port is this port number on which
// the TribServer should listen. A non-nil error should be returned if the TribServer
// could not be started.
//
// For hints on how to properly setup RPC, see the rpc/tribrpc package.
func NewTribServer(masterServerHostPort, myHostPort string) (TribServer, error) {
newStore, err := libstore.NewLibstore(masterServerHostPort, myHostPort, libstore.Never)
if err != nil {
return nil, err
}
ts := &tribServer{
myHostPort: myHostPort,
Libstore: newStore,
}
err = rpc.RegisterName("TribServer", tribrpc.Wrap(ts))
if err != nil {
return nil, err
}
rpc.HandleHTTP()
l, err := net.Listen("tcp", myHostPort)
if err != nil {
return nil, err
}
go http.Serve(l, nil)
return ts, nil
}
示例4: NewStorageServer
// NewStorageServer creates and starts a new StorageServer. masterServerHostPort
// is the master storage server's host:port address. If empty, then this server
// is the master; otherwise, this server is a slave. numNodes is the total number of
// servers in the ring. port is the port number that this server should listen on.
// nodeID is a random, unsigned 32-bit ID identifying this server.
//
// This function should return only once all storage servers have joined the ring,
// and should return a non-nil error if the storage server could not be started.
func NewStorageServer(masterServerHostPort string, numNodes, port int, nodeID uint32) (StorageServer, error) {
// Set upt this server's info
serverInfo := storagerpc.Node{HostPort: fmt.Sprintf("localhost:%d", port), NodeID: nodeID}
var ss storageServer
if masterServerHostPort == "" {
// If this is the master server, set up a list of servers
var servers = make([]storagerpc.Node, numNodes)
servers[0] = serverInfo
// Create the master server
ss = storageServer{topMap: make(map[string]interface{}), nodeID: nodeID,
servers: servers, count: 1, countLock: sync.Mutex{}, keyLocks: make(map[string]chan int)}
} else {
// Try to connect to the master at most five times
args := storagerpc.RegisterArgs{ServerInfo: serverInfo}
var reply storagerpc.RegisterReply
var err error
var master *rpc.Client
for try := 1; try <= 5; try++ {
master, err = rpc.DialHTTP("tcp", masterServerHostPort)
if err == nil {
break
}
if try == 5 {
return nil, err
}
time.Sleep(time.Millisecond * 20)
}
for i := 1; i <= 5; i++ {
master.Call("StorageServer.RegisterServer", args, &reply)
if reply.Status == storagerpc.OK {
// All servers are connected, create this slave server
ss = storageServer{topMap: make(map[string]interface{}), nodeID: nodeID,
servers: reply.Servers, count: numNodes, countLock: sync.Mutex{}, keyLocks: make(map[string]chan int)}
break
}
// Wait one second, try to connect to master again
if i == 5 {
return nil, errors.New("couldn't connect to master")
}
time.Sleep(time.Millisecond * 20)
}
}
// Start listening for connections from other storageServers and libstores
rpc.RegisterName("StorageServer", &ss)
rpc.HandleHTTP()
l, e := net.Listen("tcp", serverInfo.HostPort)
if e != nil {
return nil, errors.New("Storage server couldn't start listening")
}
go http.Serve(l, nil)
return &ss, nil
}
示例5: NewTribServer
// NewTribServer creates, starts and returns a new TribServer. masterServerHostPort
// is the master storage server's host:port and port is this port number on which
// the TribServer should listen. A non-nil error should be returned if the TribServer
// could not be started.
//
// For hints on how to properly setup RPC, see the rpc/tribrpc package.
func NewTribServer(masterServerHostPort, myHostPort string) (TribServer, error) {
tribServer := new(tribServer)
// Create the server socket that will listen for incoming RPCs.
listener, err := net.Listen("tcp", myHostPort)
if err != nil {
return nil, err
}
// Wrap the tribServer before registering it for RPC.
err = rpc.RegisterName("TribServer", tribrpc.Wrap(tribServer))
if err != nil {
return nil, err
}
// Setup the HTTP handler that will server incoming RPCs and
// serve requests in a background goroutine.
rpc.HandleHTTP()
go http.Serve(listener, nil)
storage, err := libstore.NewLibstore(masterServerHostPort, myHostPort, libstore.Never)
if err != nil {
return nil, err
}
tribServer.storage = storage
return tribServer, nil
}
示例6: main
func main() {
registerMetrics()
rpc.HandleHTTP()
if err := http.ListenAndServe(":8080", nil); err != nil {
fmt.Println(err)
}
}
示例7: NewProxy
/**
* Proxy validates the requests going into a PaxosNode and the responses coming out of it. * It logs errors that occurs during a test.
*/
func NewProxy(nodePort, myPort int) (Proxy, error) {
p := new(proxy)
p.prop = new(proposal)
p.prop.status = UNSET
p.prop.num = 0
p.prop.key = ""
p.prop.val = 0
p.err = make([]string, 0)
// Start server
l, err := net.Listen("tcp", fmt.Sprintf(":%d", myPort))
if err != nil {
LOGE.Println("Failed to listen:", err)
return nil, err
}
// Create RPC connection to paxos node.
srv, err := rpc.DialHTTP("tcp", fmt.Sprintf("localhost:%d", nodePort))
if err != nil {
LOGE.Println("Failed to dial node %d", nodePort)
return nil, err
}
p.srv = srv
// register RPC
rpc.RegisterName("PaxosNode", paxosrpc.Wrap(p))
rpc.HandleHTTP()
go http.Serve(l, nil)
// log.Printf("Proxy started")
return p, nil
}
示例8: NewServer
func NewServer() (*Server, error) {
cfg, err := config.New(os.ExpandEnv("$SUDO_USER"))
if err != nil {
return nil, err
}
err = cfg.Load()
if err != nil {
return nil, err
}
srv := &Server{
config: cfg,
done: make(chan bool),
Status: "The virtual machine has not been started",
queue: []Message{},
}
v := &VM{
server: srv,
}
srv.svc = v
rpc.Register(v)
rpc.HandleHTTP()
return srv, nil
}
示例9: main
func main() {
arith := new(Arith)
rpc.Register(arith)
rpc.HandleHTTP()
l, e := net.Listen("tcp", ":1234")
if e != nil {
log.Fatal("listen error", e)
}
go http.Serve(l, nil)
client, err := rpc.DialHTTP("tcp", "localhost:1234")
if err != nil {
log.Fatal("dialing:", err)
}
// Synchronous call
args := Args{7, 8}
var reply int
err = client.Call("Arith.Multiply", args, &reply)
if err != nil {
log.Fatal("arith error:", err)
}
fmt.Printf("Arith: %d*%d=%d\n", args.A, args.B, reply)
// Asynchronous call
quotient := new(Quotient)
divCall := client.Go("Arith.Divide", args, "ient, nil)
replyCall := <-divCall.Done // will be equal to divCall
print(replyCall)
fmt.Printf("Arith: %d*%d=%d\n", args.A, args.B, *(replyCall).Reply)
// check errors, print, etc.
}
示例10: main
func main() {
flag.Parse()
fmt.Println("Processing using flags: ", flag.Args())
arith := new(utility.Util)
rpc.Register(arith)
rpc.HandleHTTP()
var pf string = *portFlag
if !strings.HasPrefix(pf, ":") {
pf = ":" + pf
}
fmt.Println("Starting Server on port ", pf)
l, e := net.Listen("tcp", pf)
if e != nil {
log.Fatal("listen error:", e)
}
http.Serve(l, nil)
}
示例11: NewCoordinator
// NewCoordinator returns the central Coordinator that deals with initial requests from all
// users and dispatch the load to paxos servers
func NewCoordinator(myhostPort string, numPaxos int, hostMap map[int]string) (Coordinator, error) {
if numPaxos < 1 {
return nil, errors.New("numPaxos should be more than 0")
}
c := &coordinator{
numPaxos: numPaxos,
serversMutex: &sync.Mutex{},
nextPaxosID: 0,
serversMap: make(map[int]string),
hostMap: make(map[int]string),
}
for nodeID, hostPort := range hostMap {
c.hostMap[nodeID] = hostPort
}
http.HandleFunc("/", c.clientViewHandler)
go http.ListenAndServe(myhostPort, nil)
rpc.RegisterName("Coordinator", coordinatorrpc.Wrap(c))
rpc.HandleHTTP()
listener, err := net.Listen("tcp", myhostPort)
if err != nil {
return nil, err
}
go http.Serve(listener, nil)
return c, nil
}
示例12: NewCentralServer
func NewCentralServer(port, numGameServers int) (CentralServer, error) {
LOGV.Println("New Central Server is starting up")
if numGameServers < 1 {
return nil, errors.New("numGameServers must be at least 1")
}
cs := ¢ralServer{
numGameServers: numGameServers,
gameServers: make(map[uint32]*gameServer),
hostPortToGameServer: make(map[string]*gameServer),
gameServersSlice: nil,
}
// Serve up information for the game client
http.HandleFunc("/", cs.gameClientViewHandler)
go http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
rpc.RegisterName("CentralServer", centralrpc.Wrap(cs))
rpc.HandleHTTP()
l, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
return nil, err
}
go http.Serve(l, nil)
return cs, nil
}
示例13: main
func main() {
flag.Parse()
if *storageMasterNodePort == "" {
if *portnum == 0 {
*portnum = 9009
}
// Single node execution
*storageMasterNodePort = fmt.Sprintf("localhost:%d", *portnum)
if *numNodes == 0 {
*numNodes = 1
log.Println("Self-mastering, setting nodes to 1")
}
}
l, e := net.Listen("tcp", fmt.Sprintf(":%d", *portnum))
if e != nil {
log.Fatal("listen error:", e)
}
_, listenport, _ := net.SplitHostPort(l.Addr().String())
log.Println("Server starting on ", listenport)
*portnum, _ = strconv.Atoi(listenport)
ss := storageimpl.NewStorageserver(*storageMasterNodePort, *numNodes, *portnum, uint32(*nodeID))
srpc := storagerpc.NewStorageRPC(ss)
rpc.Register(srpc)
rpc.HandleHTTP()
http.Serve(l, nil)
}
示例14: main
func main() {
node, err := paxos.NewPaxosNode(nid,
numNodes,
fakecallback)
if err != nil {
fmt.Println("Cannot start node.\n")
fmt.Println(err)
return
}
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", config.Nodes[nid].Port))
if err != nil {
fmt.Printf("node %d cannot listen to port:%s\n", err)
return
}
node.SetListener(&listener)
rpc.HandleHTTP()
go http.Serve(listener, nil)
fmt.Println("Pause node.\n")
err = node.Pause()
if err != nil {
fmt.Println("Cannot Pause node.\n")
fmt.Println(err)
return
}
time.Sleep(5 * time.Second)
go func() {
time.Sleep(10 * time.Second)
fmt.Println("Resume node.\n")
err = node.Resume()
if err != nil {
fmt.Println("Cannot Resume node.\n")
fmt.Println(err)
return
}
}()
res := 0
for res < 6 {
_, ok := <-done
if ok {
res++
if res == 5 {
go func() {
c := command.Command{strconv.Itoa(nid), strconv.Itoa(0), command.Put, 0, ""}
node.Replicate(&c)
}()
}
} else {
break
}
}
if res == 6 {
fmt.Printf("\n%d receive all commands\n", nid)
} else {
fmt.Printf("%d Just break!!!!!\n", res)
}
time.Sleep(5 * time.Second)
}
示例15: kalash
func (c JoinCommand) kalash() {
log.Println("Starting kalash watcher")
shutdownCh := makeShutdownCh()
c.waitGroup.Add(1)
defer c.waitGroup.Done()
log.Println("Starting RPC server on:", c.rpcAddr)
kalashRPC := new(KalashRPC)
rpc.Register(kalashRPC)
rpc.HandleHTTP()
l, e := net.Listen("tcp", c.rpcAddr)
if e != nil {
log.Println("RPC listen error:", e)
c.watchersErrorCh <- 2
return
}
go http.Serve(l, nil)
for {
select {
case <-shutdownCh:
log.Println("Kalash watcher stopped")
return
}
}
}