Пример #1
0
// Dial connects to a Scribe server with a given host and port endpoint.
func Dial(hostport string) (*Conn, error) {
	addr, err := net.ResolveTCPAddr("tcp", hostport)
	if err != nil {
		return nil, err
	}
	conn := &Conn{}
	if conn.transport, err = thrift.NewTNonblockingSocketAddr(addr); err != nil {
		return nil, err
	}
	conn.transport = thrift.NewTFramedTransport(conn.transport)
	protocol := thrift.NewTBinaryProtocolFactoryDefault()
	conn.client = scribe.NewScribeClientFactory(conn.transport, protocol)
	if err = conn.transport.Open(); err != nil {
		return nil, err
	}
	return conn, nil
}
Пример #2
0
// Listen binds a Scribe protocol server to bind address and dispatches incoming requests to the handler.
func Listen(bind string, handler Handler) error {

	// Resolve bind address
	addr, err := net.ResolveTCPAddr("tcp", bind)
	if err != nil {
		return err
	}

	s := &server{handler: handler}

	// Create server transport (basically a listener on a TCP port)
	s.socket, err = thrift.NewTServerSocketAddr(addr)
	if err != nil {
		return err
	}

	// Create transport factory
	s.transport = thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())

	// Create protocol factory
	s.protocol = thrift.NewTBinaryProtocolFactoryDefault()

	// Create processor
	s.processor = scribe.NewScribeProcessor(&stub{time.Now(), handler})

	// Create thrift server object
	//s.server = thrift.NewTNonblockingServer4(s.processor, s.socket, s.transport, s.protocol)
	s.server = thrift.NewTSimpleServer4(s.processor, s.socket, s.transport, s.protocol)

	// Start serving requests after we return from Listen
	defer func() {
		go func() {
			for {
				if err := s.server.Serve(); err != nil {
					handler.Error(err)
					return
				}
			}
		}()
	}()

	return nil
}