// Initialize proxy and tribserver func initTribserver(storage string, server string, myhostport string) net.Listener { // Start proxy l, err := net.Listen("tcp", server) if err != nil { log.Fatal("listen error:", err) } pc = proxycounter.NewProxyCounter(storage, server) if pc == nil { fmt.Println("Could not start proxy") return nil } srpc := storagerpc.NewStorageRPC(pc) rpc.Register(srpc) rpc.HandleHTTP() go http.Serve(l, nil) // Start tribserver ts = tribimpl.NewTribserver(server, myhostport) if ts == nil { fmt.Println("Could not start tribserver") return nil } rpc.Register(ts) return l }
func main() { fmt.Println("Starting Server...") winProc := new(Win32_Process) rpc.Register(winProc) winSvc := new(Win32_Service) rpc.Register(winSvc) //setup IP, port for the server tcpAddr, err := net.ResolveTCPAddr("tcp", ":1234") checkError(err) //start to listen listener, err := net.ListenTCP("tcp", tcpAddr) checkError(err) fmt.Println("Server Started") for { conn, err := listener.Accept() if err != nil { continue } jsonrpc.ServeConn(conn) } }
func main() { flag.Parse() myhostport := fmt.Sprintf("localhost:%d", *portnum) masterStorage := fmt.Sprintf("localhost:%d", *masterStoragePort) ownStorage := fmt.Sprintf("localhost:%d", *ownStoragePort) l, e := net.Listen("tcp", myhostport) if e != nil { log.Fatal("listen error:", e) } _, listenport, _ := net.SplitHostPort(l.Addr().String()) //log.Println("Server starting on ", listenport) *portnum, _ = strconv.Atoi(listenport) as, lp, err := airlineimpl.NewAirlineServer(*airlineID, myhostport, masterStorage, ownStorage, strings.Split(*replicas, "-"), *pos) if err != nil { fmt.Println(err) return } arpc := airlinerpc.NewAirlineRPC(as) rpc.Register(arpc) rpc.Register(lp) rpc.HandleHTTP() http.Serve(l, nil) }
func main() { db, e := sql.Open("mysql", "root@/scan?charset=utf8") defer db.Close() if e != nil { fmt.Println("Не удалось подключится к БД: ", e) } bs := new(storage.BlockStorage) bs.DB = db is := new(storage.IpStorage) is.DB = db rpc.Register(bs) rpc.Register(is) tcpAddr, err := net.ResolveTCPAddr("tcp", ":1234") checkError(err) listener, err := net.ListenTCP("tcp", tcpAddr) checkError(err) for { conn, err := listener.Accept() if err != nil { continue } go func(conn net.Conn) { jsonrpc.ServeConn(conn) // conn.CloseWrite() }(conn) } }
func main() { tuple := new(Tuple) hashtable := new(Hashtable) rpc.Register(tuple) rpc.Register(hashtable) tcpAddr, err := net.ResolveTCPAddr("tcp", ":1234") checkError(err) fmt.Println(tcpAddr) listener, err := net.ListenTCP("tcp", tcpAddr) checkError(err) /* This works: rpc.Accept(listener) */ /* and so does this: */ for { conn, err := listener.Accept() if err != nil { continue } rpc.ServeConn(conn) } fmt.Println("END OF MAIN") }
func Server() { Init() rpc.Register(new(ServerStruct)) rpc.Register(new(PortfolioStruct)) rpc.HandleHTTP() fmt.Println("Server listining on 9999 port.....") err := http.ListenAndServe(":9999", nil) if err != nil { fmt.Println(err) return } }
func main() { port_flag := flag.Int("port", PORT_DEFAULT, "Specify port number to listen on.") root_flag := flag.String("root", "", "Specify root working directory for Forge.") jobs_flag := flag.Int("jobs", runtime.NumCPU(), "Specify number of jobs to run simultaneously.") flag.Parse() var port int port = PORT_DEFAULT if *port_flag > 0 && *port_flag < 65535 { port = *port_flag } var jobs int jobs = runtime.NumCPU() if *jobs_flag > 0 { jobs = *jobs_flag } runtime.GOMAXPROCS(jobs) var forge_root *string = new(string) *forge_root = ROOT_DEFAULT root_env := os.Getenv("FORGE_ROOT") if root_env != "" { forge_root = &root_env } if *root_flag != "" { forge_root = root_flag } sem := make(chan int, jobs) config := tasks.Config{Root: *forge_root} task := tasks.Task{Semaphore: sem, Config: &config} file := files.File{Rootdir: *forge_root} util := util.Util{Jobs: jobs} rpc.Register(&task) rpc.Register(&file) rpc.Register(&util) port_spec := fmt.Sprintf(":%d", port) tcp_addr, err := net.ResolveTCPAddr("tcp", port_spec) checkError(err) listener, err := net.ListenTCP("tcp", tcp_addr) checkError(err) rpc.Accept(listener) }
func main() { rpc.Register(new(Echo)) rpc.Register(new(Sum)) rpc.HandleHTTP() l, e := net.Listen("tcp", ":1234") if e != nil { log.Fatal("listen error:", e) } http.Serve(l, nil) }
func StartRPC() { helen := new(Helen) event := new(Event) rpc.Register(helen) rpc.Register(event) rpc.HandleHTTP() l, err := net.Listen("tcp", config.Constants.RPCAddr) if err != nil { logrus.Fatal(err) } logrus.Info("Started RPC on ", config.Constants.RPCAddr) logrus.Fatal(http.Serve(l, nil)) }
func main() { flag.Parse() runtime.GOMAXPROCS(*procs) if *cpuprofile != "" { f, err := os.Create(*cpuprofile) if err != nil { log.Fatal(err) } pprof.StartCPUProfile(f) interrupt := make(chan os.Signal, 1) signal.Notify(interrupt) go catchKill(interrupt) } log.Printf("Server starting on port %d\n", *portnum) replicaId, nodeList := registerWithMaster(fmt.Sprintf("%s:%d", *masterAddr, *masterPort)) if *doEpaxos { log.Println("Starting Egalitarian Paxos replica...") rep := epaxos.NewReplica(replicaId, nodeList, *thrifty, *exec, *dreply, *beacon, *durable) rpc.Register(rep) } else if *doMencius { log.Println("Starting Mencius replica...") rep := mencius.NewReplica(replicaId, nodeList, *thrifty, *exec, *dreply, *durable) rpc.Register(rep) } else if *doGpaxos { log.Println("Starting Generalized Paxos replica...") rep := gpaxos.NewReplica(replicaId, nodeList, *thrifty, *exec, *dreply) rpc.Register(rep) } else { log.Println("Starting classic Paxos replica...") rep := paxos.NewReplica(replicaId, nodeList, *thrifty, *exec, *dreply, *durable) rpc.Register(rep) } rpc.HandleHTTP() //listen for RPC on a different port (8070 by default) l, err := net.Listen("tcp", fmt.Sprintf(":%d", *portnum+1000)) if err != nil { log.Fatal("listen error:", err) } http.Serve(l, nil) }
func (m *MasterServer) RegistFunction(server *rpc.Server) error { err := rpc.Register(globalCache) if err != nil { log.Printf("regist failed! err:%s \n", err) } return err }
// register RPC handler class func RegisterRPCHandler(rcvr interface{}) error { if serverInstance == nil { return errorf(errServerNotInit) } if serverInstance.rpcsvr == nil { if *confRPCHost == "" { return errorf(errMissingFlag, FlagRPCHost) } addr, err := fixHostIp(*confRPCHost) if err != nil { return errorf(errWrongHostAddr, *confRPCHost) } err = rpc.Register(rcvr) if err != nil { return errorf("Cannot Resgister RPC service: %s", err) } handler := rpcHandler{} serverInstance.rpcsvr = &RPCServer{ TCPServer{ addr: addr, handler: &handler, useTls: false, // rpc service do not use tls because it's in internal network }, } } return nil }
func do_server() int { g_config.read() if g_config.ForceDebugOutput != "" { // forcefully enable debugging and redirect logging into the // specified file *g_debug = true f, err := os.Create(g_config.ForceDebugOutput) if err != nil { panic(err) } log.SetOutput(f) } addr := *g_addr if *g_sock == "unix" { addr = get_socket_filename() if file_exists(addr) { log.Printf("unix socket: '%s' already exists\n", addr) return 1 } } g_daemon = new_daemon(*g_sock, addr) if *g_sock == "unix" { // cleanup unix socket file defer os.Remove(addr) } rpc.Register(new(RPC)) g_daemon.loop() return 0 }
func main() { // Create Babel service objects svc := new(gen.UserService) svc.SvcObj = NewUserServiceImpl() // Register the service with RPC rpc.Register(svc) // Register the service with Babel babel.Register(svc) // Set up Babel HTTP handlers and serve HTTP babel.HandleHTTP() http.Handle("/test/", http.StripPrefix("/test/", http.FileServer(http.Dir("../test")))) go func() { log.Fatal(http.ListenAndServe(":8333", nil)) }() //rpc.HandleHTTP(rpc.DefaultRPCPath, rpc.DefaultDebugPath) // set up network listener for json rpc l, e := net.Listen("tcp", ":8222") if e != nil { log.Fatal("listen error:", e) } for { conn, err := l.Accept() if err != nil { log.Fatal(err) } go jsonrpc.ServeConn(conn) } }
//启动HTTP服务监控指定端口 func ListenAndServer(port string) { // {{{ executer := new(CmdExecuter) rpc.Register(executer) l.Infoln("Worker is running Port:", port) tcpAddr, err := net.ResolveTCPAddr("tcp", port) checkErr(err) listener, err := net.ListenTCP("tcp", tcpAddr) checkErr(err) go func() { for { conn, err := listener.Accept() if err != nil { continue } go func() { rpc.ServeConn(conn) }() } }() } // }}}
func main() { winProc := new(Win32_Process) rpc.Register(winProc) winSvc := new(Win32_Service) rpc.Register(winSvc) rpc.HandleHTTP() fmt.Println("Starting...") err := http.ListenAndServe(":1234", nil) if err != nil { fmt.Println(err.Error()) } }
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) time.Sleep(5 * time.Second) client, err := rpc.DialHTTP("tcp", "127.0.0.1"+":1234") if err != nil { log.Fatal("dialing:", err) } args := &Args{7, 8} //调用参数,其实没做什么用 reply := make([]string, 10) err = client.Call("Arith.Multiply", args, &reply) if err != nil { log.Fatal("arith error:", err) } log.Println(reply) }
func main() { arith := new(Arith) rpc.Register(arith) tcpAddr, err := net.ResolveTCPAddr("tcp", ":1234") checkError(err) listener, err := net.ListenTCP("tcp", tcpAddr) checkError(err) /* This works: rpc.Accept(listener) */ /* and so does this: */ for { conn, err := listener.Accept() if err != nil { continue } jsonrpc.ServeConn(conn) } }
func rpcInit() error { cmd := new(Cmd) rpc.Register(cmd) l, err := net.Listen("unix", GlobalOpt.UnixSocket) if err != nil { return err } go func() { var tempDelay time.Duration for { conn, err := l.Accept() if err != nil { if tempDelay == 0 { tempDelay = 5 * time.Millisecond } else { tempDelay *= 2 } if max := 1 * time.Second; tempDelay > max { tempDelay = max } time.Sleep(tempDelay) continue } tempDelay = 0 go func() { rpc.ServeConn(conn) }() } }() return nil }
func main() { var err error var clientConn net.Conn var errLoggedListen, errLoggedClientConn = false, false runtime.GOMAXPROCS(4) if err = rpc.Register(rpcService); err != nil { os.Exit(1) } for { if rpcListener == nil { if rpcListener, err = net.Listen("tcp", ":9669"); err != nil { rpcListener = nil if !errLoggedListen { errLoggedListen = true logf("Failed to establish TCP listener, will keep retrying. Error: %+v", err) } } else { errLoggedListen = false logf("TCP listener established.") } } if rpcListener != nil { if clientConn, err = rpcListener.Accept(); err == nil { errLoggedClientConn = false go rpc.ServeConn(clientConn) } else if !errLoggedClientConn { errLoggedClientConn = true rpcListener.Close() rpcListener = nil logf("Failed to accept TCP connection, will re-establish TCP listener. Error: %+v", err) } } } }
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 } } }
// Main function func main() { loadConfig() loadDict3() fingertable = make(map[int]NodeInfo) next = 0 if entrypt == "null" { createChordRing() } else { joinChordRing(entrypt) } stabilize() check_predecessor() fixFingers() getDict3FromSuccessor() dic3 := new(DIC3) rpc.Register(dic3) tcpAddr, err := net.ResolveTCPAddr(protocol, port) checkError(err) fmt.Println("Server started........") listener, err := net.ListenTCP(protocol, tcpAddr) checkError(err) for { conn, err := listener.Accept() if err != nil { continue } jsonrpc.ServeConn(conn) } }
func main() { flag.Parse() rpc.Register(new(Worker)) l, e := net.Listen("tcp", ":"+strconv.Itoa(*port)) if e != nil { log.Fatal("listen error:", e) } log.Print("listening for connections on ", l.Addr()) if *connect != "" { log.Printf("calling %s\n", *connect) conn, err := net.Dial("tcp", *connect) if err != nil { log.Fatalf("failed to connect to %s: %s", *connect, err) } addr, _, err := net.SplitHostPort(conn.LocalAddr().String()) if err != nil { log.Fatalf("failed to get the local address: %s", err) } laddr := net.JoinHostPort(addr, strconv.Itoa(*port)) client := rpc.NewClient(conn) var res struct{} client.Call("WorkerList.Add", laddr, &res) client.Close() } rpc.Accept(l) }
func NewRpcService( outputCapture terminal.OutputCapture, terminalOutputSwitch terminal.TerminalOutputSwitch, cliConfig core_config.Repository, repoLocator api.RepositoryLocator, newCmdRunner CommandRunner, logger trace.Printer, ) (*CliRpcService, error) { rpcService := &CliRpcService{ RpcCmd: &CliRpcCmd{ PluginMetadata: &plugin.PluginMetadata{}, outputCapture: outputCapture, terminalOutputSwitch: terminalOutputSwitch, cliConfig: cliConfig, repoLocator: repoLocator, newCmdRunner: newCmdRunner, logger: logger, }, } err := rpc.Register(rpcService.RpcCmd) if err != nil { return nil, err } return rpcService, nil }
func TestRPCServer(t *testing.T) { initLog("test", "debug") testRPC := new(Arith) err := rpc.Register(testRPC) if err != nil { t.Fatal(err) } handler := rpcHandler{} svr := &RPCServer{ TCPServer{ addr: testRPCHost, handler: &handler, useTls: false, }, } err = svr.Start() if err != nil { t.Fatal(err) } time.Sleep(time.Millisecond * 300) validateRPCServer(t, testRPCHost, "Arith.Multiply") }
// Initialise Server, registering RPC service and binding to port 1234 func (t *Server) init() { t.Status = true rpc.Register(t) tcpAddr, err := net.ResolveTCPAddr("tcp", ":1234") t.checkError(err) listener, err := net.ListenTCP("tcp", tcpAddr) t.checkError(err) // create new map for tracking routes t.Routes = make(map[int][]*Tram) t.Clients = make(map[string]*Record) rpc.Accept(listener) // case want to add additional socket accounting // for { // conn, err := listener.Accept() // if err != nil { // continue // } // rpc.ServeConn(conn) // } }
func main() { f, err := os.OpenFile("server0.log", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666) if err != nil { fmt.Printf("error opening file: %v", err) } // don't forget to close it defer f.Close() log.SetOutput(f) log.SetLevel(log.DebugLevel) globalData, err := platform.CreateGlobalData() if err != nil { log.Fatal(err) return } runtime.GOMAXPROCS(runtime.NumCPU()) c, err := service.NewContester("server.ini", globalData) if err != nil { log.Fatal(err) return } rpc.Register(c) for { if err = rpc4go.ConnectServer(c.ServerAddress, rpc.DefaultServer); err != nil { log.Error(err) time.Sleep(time.Second * 5) } } }
func main() { srver := new(Server).Initi() rpc.Register(srver) litsner := connect() go handler(srver, &litsner) }
func MakeLockService(servers []string, me int) *LockService { gob.Register(Op{}) ls := new(LockService) ls.me = me ls.servers = servers ls.max = -1 ls.locks = make(map[int]int) ls.requests = make(chan Request, 256) go ls.dequeueRequests() rpc.Register(ls) ls.px = MakePaxos(servers, me) rpc.HandleHTTP() listener, err := net.Listen("tcp", servers[me]) if err != nil { panic(err) } http.Serve(listener, nil) return ls }
func Start() { if !g.Config().Rpc.Enabled { return } addr := g.Config().Rpc.Listen tcpAddr, err := net.ResolveTCPAddr("tcp", addr) if err != nil { log.Fatalf("net.ResolveTCPAddr fail: %s", err) } listener, err := net.ListenTCP("tcp", tcpAddr) if err != nil { log.Fatalf("listen %s fail: %s", addr, err) } else { log.Println("rpc listening", addr) } rpc.Register(new(Graph)) go func() { var tempDelay time.Duration // how long to sleep on accept failure for { conn, err := listener.Accept() if err != nil { if tempDelay == 0 { tempDelay = 5 * time.Millisecond } else { tempDelay *= 2 } if max := 1 * time.Second; tempDelay > max { tempDelay = max } time.Sleep(tempDelay) continue } tempDelay = 0 go func() { e := connects.insert(conn) defer connects.remove(e) rpc.ServeConn(conn) }() } }() select { case <-Close_chan: log.Println("api recv sigout and exit...") listener.Close() Close_done_chan <- 1 connects.Lock() for e := connects.list.Front(); e != nil; e = e.Next() { e.Value.(net.Conn).Close() } connects.Unlock() return } }