Ejemplo n.º 1
0
func main() {
	scribeService := new(scribeServiceImplementation)
	rpc.RegisterName("Thrift", &scribe.ScribeServer{Implementation: scribeService})
	flag.IntVar(&port, "p", 1463, "Scribe Listen Port")
	flag.StringVar(&kafka_hostname, "o", "localhost:9092", "host:port string for the kafka server")
	flag.IntVar(&partition, "r", 1, "partition to publish to")
	flag.IntVar(&buffer_size, "s", 10, "Buffer Size")
	flag.IntVar(&buffer_time, "t", 10, "Buffer Time")

	flag.Parse()

	fmt.Printf("Quiet! I'm trying to listen to port %d and send to kafka at %s", port, kafka_hostname)
	ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
	if err != nil {
		log.Println(err)
	}
	for {
		conn, err := ln.Accept()
		if err != nil {
			fmt.Printf("ERROR: %+v\n", err)
			continue
		}
		fmt.Printf("New connection %+v\n", conn)
		go rpc.ServeCodec(thrift.NewServerCodec(thrift.NewFramedReadWriteCloser(conn, 0), thrift.NewBinaryProtocol(true, false)))
	}
}
Ejemplo n.º 2
0
func main() {
	bind_addr := flag.String("bind_ip", "127.0.0.1", "bind ip address")
	http_port := flag.Int("http_port", 9999, "listen http port")
	rpc_port := flag.Int("rpc_port", 9998, "listen rpc port")
	flag.Parse()

	go func() {
		addr, _ := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", *bind_addr, *rpc_port))
		listener, _ := net.ListenTCP("tcp", addr)
		rpcservice := new(RPCService)
		rpc.Register(rpcservice)
		rpc.HandleHTTP()
		for {
			conn, _ := listener.Accept()
			go rpc.ServeCodec(jsonrpc.NewServerCodec(conn))
		}
	}()

	web.Get("/api/topics/([a-zA-Z0-9_\\-]+)/subscribers/([a-zA-Z0-9_\\-]+)/messages", APIGetTopicMessages)
	web.Post("/api/topics/([a-zA-Z0-9_\\-]+)/subscribers/([a-zA-Z0-9_\\-]+)/messages", APIPostTopicMessage)
	web.Get("/api/topics/([a-zA-Z0-9_\\-]+)", APIGetTopic)
	web.Post("/api/topics/([a-zA-Z0-9_\\-]+)", APIUpdateTopic)
	//web.Get("/api/topics", APIGetTopics)
	web.Get("/api/subscribers/([a-zA-Z0-9_\\-]+)", APIGetSubscriber)
	web.Post("/api/subscribers/([a-zA-Z0-9_\\-]+)", APIUpdateSubscriber)
	//web.Get("/api/topics/(.+)/subscribers/(.+)", APIGetTopicSubscriber)
	//web.Get("/api/topics/(.+)/subscribers", APIGetTopicSubscribers)

	web.Run(fmt.Sprintf("%s:%d", *bind_addr, *http_port))

}
Ejemplo n.º 3
0
func (self *rpcHandler) ServeHTTP(c http.ResponseWriter, req *http.Request) {
	conn, _, err := c.(http.Hijacker).Hijack()
	if err != nil {
		relog.Error("rpc hijacking %s: %v", req.RemoteAddr, err)
		return
	}
	io.WriteString(conn, "HTTP/1.0 "+connected+"\n\n")
	rpc.ServeCodec(self.cFactory(NewBufferedConnection(conn)))
}
Ejemplo n.º 4
0
func (svr *server) Accept() {
	conn, _ := svr.ln.Accept()

	svr.cn = conn
	log.Println("using custom codec for server")
	//rpcCodec := msgpack.NewCustomRPCServerCodec(conn, nil)
	rpcCodec := codec.MsgpackSpecRpc.ServerCodec(conn, h)
	go rpc.ServeCodec(rpcCodec)
}
Ejemplo n.º 5
0
func StartRPC(url, queue string) {
	rpc.Register(new(Fumble))
	conn, err := amqp.Dial(url)
	if err != nil {
		log.Fatal(err)
	}

	serverCodec, err := amqprpc.NewServerCodec(conn, queue, amqprpc.JSONCodec{})
	rpc.ServeCodec(serverCodec)
}
Ejemplo n.º 6
0
func runPipeServer(protoDebug bool) {
	protocol := new(protocol.Protocol)
	rpc.Register(protocol)
	pp, err := createPipePair(os.Stdin, os.Stdout, protoDebug)
	if err != nil {
		logger.Warning(fmt.Sprintf("Failed to create pipe pair: %s", err))
		return
	}
	codec := jsonrpc.NewServerCodec(pp)
	logger.Info("Starting...")
	rpc.ServeCodec(codec)
}
Ejemplo n.º 7
0
func ExampleNewServerCodec() {
	conn, err := amqp.Dial("amqp://*****:*****@localhost:5672/")
	if err != nil {
		log.Fatal(err)
	}

	serverCodec, err := amqprpc.NewServerCodec(conn, "rpc_queue", amqprpc.GobCodec{})
	if err != nil {
		log.Fatal(err)
	}

	go rpc.ServeCodec(serverCodec)
}
Ejemplo n.º 8
0
func runServer(port string) {
	// Set processor
	rpc.RegisterName("Thrift", &echo.EchoServer{Implementation: new(echoHandler)})
	ln, _ := net.Listen("tcp", port)

	for {
		conn, _ := ln.Accept()
		go func(c net.Conn) {
			t := thrift.NewTransport(thrift.NewFramedReadWriteCloser(c, 0), thrift.BinaryProtocol)
			rpc.ServeCodec(thrift.NewServerCodec(t))
		}(conn)
	}
}
Ejemplo n.º 9
0
func myAccept(l net.Listener) {
	conn, err := l.Accept()
	if err != nil {
		fmt.Println("Accept error: ", err)
		return
	}

	// 创建一个自定义的ServerCode, 实际上这些代码就从go源码中获取出来的
	buf := bufio.NewWriter(conn)
	codec := &myServerCodec{conn, gob.NewDecoder(conn), gob.NewEncoder(buf), buf}

	//运行连接
	go rpc.ServeCodec(codec)
}
Ejemplo n.º 10
0
// Start up the RPC Server
func server(registry *Registry, port int) error {
	rpc.Register(registry)
	rpc.HandleHTTP()
	ln, err := net.Listen("tcp", util.Address("", port))
	if err != nil {
		return err
	}
	go func() {
		for {
			conn, _ := ln.Accept()
			rpc.ServeCodec(jsonrpc.NewServerCodec(conn))
		}
	}()
	return nil
}
Ejemplo n.º 11
0
func (self *rpcHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	if req.Method != "CONNECT" {
		w.Header().Set("Content-Type", "text/plain; charset=utf-8")
		w.WriteHeader(http.StatusMethodNotAllowed)
		io.WriteString(w, "405 must CONNECT\n")
		return
	}
	conn, _, err := w.(http.Hijacker).Hijack()
	if err != nil {
		log.Printf("rpc hijacking %s: %v", req.RemoteAddr, err)
		return
	}
	io.WriteString(conn, "HTTP/1.0 "+connected+"\n\n")
	rpc.ServeCodec(self.cFactory(NewBufferedConnection(conn)))
}
Ejemplo n.º 12
0
func startServer() {
	rpc.RegisterName("Thrift", new(TestService))

	var l net.Listener
	l, serverAddr = listenTCP()
	log.Println("Test RPC server listening on", serverAddr)
	go func() {
		for {
			conn, err := l.Accept()
			if err != nil {
				panic(err)
			}
			go rpc.ServeCodec(NewServerCodec(NewFramedReadWriteCloser(conn, 0), NewBinaryProtocol(true, false)))
		}
	}()
}
Ejemplo n.º 13
0
// startRPCServer registers the master consumer for plugins. The master consumer allows plugins to
// communicate with the bot, allowing access to connected channels, users and registered modules.
// Conventionally, it uses a json codec to serve.
func (b *Bot) startRPCServer() {
	rpc.RegisterName("Master", BotAPI{b})
	master, err := net.Listen("tcp", ":0")
	b.ListenPort = strconv.Itoa(master.Addr().(*net.TCPAddr).Port)
	rlog.Info("Bot", "Listening on port: "+b.ListenPort)

	if err != nil {
		rlog.Error("Bot", err.Error())
	}

	// Start accepting connections
	go func() {
		for {
			conn, _ := master.Accept()
			go rpc.ServeCodec(RpcCodecServer(conn))
		}
	}()
}
Ejemplo n.º 14
0
func main() {
	scribeService := new(scribeServiceImplementation)
	rpc.RegisterName("Thrift", &scribe.ScribeServer{scribeService})

	ln, err := net.Listen("tcp", ":1463")
	if err != nil {
		panic(err)
	}
	for {
		conn, err := ln.Accept()
		if err != nil {
			fmt.Printf("ERROR: %+v\n", err)
			continue
		}
		fmt.Printf("New connection %+v\n", conn)
		go rpc.ServeCodec(thrift.NewServerCodec(thrift.NewFramedReadWriteCloser(conn, 0), thrift.NewBinaryProtocol(true, false)))
	}
}
Ejemplo n.º 15
0
func Run(mdmanager *metadata.Manager) {
	fmt.Print("Runnig server\n")

	handler.RegisterAll(mdmanager)
	ln, err := net.Listen("tcp", fmt.Sprintf(":%d", aether.DEFAULT_PORT))

	if err != nil {
		panic(fmt.Sprintf("Error %s", err))
	}

	for {
		conn, err := ln.Accept()
		if err == nil {
			rpcCodec := msgpack.NewRPCServerCodec(conn, nil)
			rpc.ServeCodec(rpcCodec)
		}

	}
}
Ejemplo n.º 16
0
func startServer() {
	arith := new(Arith)

	rpc.Register(arith)

	//rpc.HandleHTTP (rpc.DefaultRPCPath, rpc.DefaultDebugPath);

	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 rpc.ServeCodec(jsonrpc.NewServerCodec(conn))
	}
}
Ejemplo n.º 17
0
// Serve a port with a protobuf rpc server. Set brokered to true if you are using a broker
func Serve(addr string, brokered bool) (io.Closer, error) {
	// Socket to talk to clients
	conn, err := context.NewSocket(zmq.DEALER)
	if err != nil {
		return nil, err
	}

	if brokered {
		err = conn.Connect("tcp://" + addr)
	} else {
		err = conn.Bind("tcp://" + addr)
	}
	if err != nil {
		return nil, err
	}

	// log.Println("Created Server", conn, addr)
	server := NewServerCodec(conn)
	go rpc.ServeCodec(server)
	return server, nil
}
Ejemplo n.º 18
0
func startAccept(args []string) (reply *Reply) {
	if len(args) != 1 {
		fmt.Println("Need one args <method>")
		reply.StateCode = 503
		reply.Error = "Args error"
		return reply
	}
	l, e := net.Listen(TCP, SERVER_ADDR)
	if e != nil {
		fmt.Println("Listen error", e)
		reply.StateCode = 503
		reply.Error = "Listen error"
		return reply
	}

	fmt.Println("Server: listened on PORT  ", SERVER_PORT)

	go func() {
		for {
			conn, e := l.Accept()
			if e != nil {
				fmt.Println("Accept err", e)
				conn.Close()
				continue
			}
			fmt.Println("Accept From:", conn.RemoteAddr())
			if conn != nil {
				go func() {
					rpc.ServeCodec(jsonrpc.NewServerCodec(conn))
					conn.Close()
				}()
			}
		}
	}()

	RegisterRPC()

	return nil

}
Ejemplo n.º 19
0
func main() {
	port, ok := conf.Conf[util.CONF_KEY_SERVER_LISTEN_PORT]
	if !ok {
		util.StartupFatalErr(errors.New("Must contain " + util.CONF_KEY_SERVER_LISTEN_PORT + " in conf"))
	}

	fs := new(feedservice.FeedService)
	rpc.Register(fs)

	l, err := net.Listen("tcp", ":"+port)
	util.StartupFatalErr(err)
	log.Info("server start at port: " + port)

	for {
		conn, err := l.Accept()
		log.Infof("conn from remote: %+v", conn.RemoteAddr())
		if err != nil {
			log.Errorf("accept error: %s", err.Error())
		}
		go rpc.ServeCodec(jsonrpc.NewServerCodec(conn))
	}
}
Ejemplo n.º 20
0
//启动客户端聊天,设置收信息端口
func startAccept(args []string) *Reply {
	if len(args) != 2 {
		fmt.Println("Need 2 args <method> <port>")
		return nil
	}
	port = args[1]
	clientAddr := getClientAddr()
	l, e := net.Listen(TCP, clientAddr)
	if e != nil {
		fmt.Println("Listen error", e)
		return nil
	}

	fmt.Println("Clinet: listening on PORT ", port)

	go func() {
		for {
			conn, e := l.Accept()
			if e != nil {
				fmt.Println("Accept err", e)
				conn.Close()
				continue
			}
			if conn != nil {
				fmt.Println("Accept From:", conn.RemoteAddr())
				go func() {
					rpc.ServeCodec(jsonrpc.NewServerCodec(conn))
					conn.Close()
				}()
			}
		}
	}()

	RegisterRPC()

	return nil

}
Ejemplo n.º 21
0
func main() {

	var (
		v  interface{}
		r  io.Reader
		w  io.Writer
		b  []byte
		mh codec.MsgpackHandle
	)

	dec := codec.NewDecoder(r, &mh)
	dec = codec.NewDecoderBytes(b, &mh)
	err := dec.Decode(&v)

	enc := codec.NewEncoder(w, &mh)
	enc = codec.NewEncoderBytes(&b, &mh)
	err = enc.Encode(v)

	checkError(err)

	service := ":30001"
	tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
	checkError(err)
	listener, err := net.ListenTCP("tcp", tcpAddr)
	checkError(err)

	for {
		conn, err := listener.Accept()
		if err != nil {
			continue
		}
		fmt.Println("Get connection !")
		//go handleClient(conn)
		rpcCodec := codec.GoRpc.ServerCodec(conn, &mh)
		// OR rpcCodec := codec.MsgpackSpecRpc.ServerCodec()
		rpc.ServeCodec(rpcCodec)
	}
}
Ejemplo n.º 22
0
func main() {

	echo := &Echo{}
	rpc.Register(echo)

	ln, e := net.Listen("tcp", ":9000")
	if e != nil {
		log.Fatal("listen error:", e)
	}

	log.Println("rpc server starting")

	for {
		conn, err := ln.Accept()
		if err != nil {
			log.Println(err)
			continue
		}
		var h codec.MsgpackHandle
		c := codec.MsgpackSpecRpc.ServerCodec(conn, &h)

		go rpc.ServeCodec(c)
	}
}
Ejemplo n.º 23
0
// ServeConnContext is ServeConn with given context provided
// within parameters for compatible RPC methods.
func ServeConnContext(ctx context.Context, conn io.ReadWriteCloser) {
	rpc.ServeCodec(NewServerCodecContext(ctx, conn, nil))
}
Ejemplo n.º 24
0
// ServeConn runs the Thrift RPC server on a single connection. ServeConn blocks,
// serving the connection until the client hangs up. The caller typically invokes
// ServeConn in a go statement.
func ServeConn(conn io.ReadWriteCloser, protocol Protocol) {
	rpc.ServeCodec(NewServerCodec(conn, protocol))
}
Ejemplo n.º 25
0
// ServeConn runs the Protobuf-RPC server on a single connection.
// ServeConn blocks, serving the connection until the client hangs up.
// The caller typically invokes ServeConn in a go statement.
func ServeConn(conn io.ReadWriteCloser) {
	rpc.ServeCodec(NewServerCodec(conn))
}
Ejemplo n.º 26
0
func handle(conn net.Conn) {
	buf := bufio.NewWriter(conn)
	srv := &gobServerCodec{conn, gob.NewDecoder(conn), gob.NewEncoder(buf), buf}
	rpc.ServeCodec(srv)
}
Ejemplo n.º 27
0
// ServeConn runs the Thrift RPC server on a single connection. ServeConn blocks,
// serving the connection until the client hangs up. The caller typically invokes
// ServeConn in a go statement.
func ServeConn(conn Transport) {
	rpc.ServeCodec(NewServerCodec(conn))
}
Ejemplo n.º 28
0
Archivo: lb.go Proyecto: dgryski/dlb
func main() {

	var optCfgFile = flag.String("cfg", "", "config file")

	flag.Parse()

	if *optCfgFile == "" {
		log.Fatal("no config file found")
	}

	cfgFile, err := os.Open(*optCfgFile)
	if err != nil {
		log.Fatal(err)
	}

	dec := json.NewDecoder(cfgFile)

	var cfg Cfg

	if err := dec.Decode(&cfg); err != nil {
		log.Fatal(err)
	}

	pools := make(map[string]*Pool)

	for _, pool := range cfg.Pools {
		for _, m := range pool.Machines {
			pool.totalWeight += m.Weight
			packAddr(m.packedAddr[:], m.Addr)
		}

		log.Println("service=", []byte(pool.Service))

		pool.serviceCrc = crc32.ChecksumIEEE([]byte(pool.Service))

		pools[pool.Service] = pool

		log.Printf("pool: %s (%x) loaded\n", pool.Service, pool.serviceCrc)
	}

	/*
		        // tcp support
			for _, pool := range pools {

				if pool.PackPort != 0 {
					go func(pool *Pool) {
						ln, e := net.Listen("tcp", ":"+strconv.Itoa(pool.PackPort))
						if e != nil {
							log.Fatal("listen error:", e)
						}

						log.Println("tcp server starting")

						for {
							conn, err := ln.Accept()
							if err != nil {
								log.Println(err)
								continue
							}
							go func(c net.Conn) {
								m := cfg.Pools[0].selectMachine()
								c.Write([]byte(m.packedAddr[:]))
								c.Close()
							}(conn)
						}
					}(pool)
				}
			}
	*/

	go func(port int, pools map[string]*Pool) {

		pconn, e := net.ListenPacket("udp", ":"+strconv.Itoa(port))
		if e != nil {
			log.Fatal("udp listen error:", e)
		}

		log.Println("udp server starting on port", port)

		for {
			var b [255]byte
			n, addr, err := pconn.ReadFrom(b[:])
			if err != nil {
				log.Println(err)
				continue
			}

			if n < 3 || n != int(b[2])+3 {
				log.Println("bad packet from ", addr)
				continue
			}

			req := binary.BigEndian.Uint16(b[:2])
			s := string(b[3:n])

			p := pools[s]

			go func(pconn net.PacketConn, addr net.Addr, service string, req uint16, p *Pool) {

				var b [12]byte

				binary.BigEndian.PutUint16(b[:2], req)
				reqCrc := crc32.ChecksumIEEE([]byte(service))
				binary.BigEndian.PutUint32(b[2:], reqCrc)

				if p != nil {
					m := p.selectMachine()
					copy(b[6:], m.packedAddr[:])
				} else {
					log.Println("service lookup for", s, "failed -- returning error")
				}

				n, err := pconn.WriteTo(b[:], addr)

				if n != len(b) || err != nil {
					log.Println("error sending packet:", n, "/", len(b), "bytes written, err=", err)
				}
			}(pconn, addr, s, req, p)
		}
	}(cfg.Port, pools)

	// msgpack rpc server
	lbns := &LBNS{pools}
	rpc.Register(lbns)

	ln, e := net.Listen("tcp", ":50000")
	if e != nil {
		log.Fatal("listen error:", e)
	}

	log.Println("rpc server starting")

	for {
		conn, err := ln.Accept()
		if err != nil {
			log.Println(err)
			continue
		}
		var h codec.MsgpackHandle
		c := codec.MsgpackSpecRpc.ServerCodec(conn, &h)

		go rpc.ServeCodec(c)
	}
}