parent
04e650933a
commit
7115aa98ff
@ -0,0 +1,49 @@ |
||||
package message |
||||
|
||||
import ( |
||||
"context" |
||||
"fmt" |
||||
"log" |
||||
"time" |
||||
|
||||
proto "github.com/golang/protobuf/proto" |
||||
"google.golang.org/grpc" |
||||
) |
||||
|
||||
// Client is the client model for client service.
|
||||
type Client struct { |
||||
clientServiceClient proto.ClientServiceClient |
||||
opts []grpc.DialOption |
||||
conn *grpc.ClientConn |
||||
} |
||||
|
||||
// NewClient setups a Client given ip and port.
|
||||
func NewClient(ip string) *Client { |
||||
client := Client{} |
||||
client.opts = append(client.opts, grpc.WithInsecure()) |
||||
var err error |
||||
client.conn, err = grpc.Dial(fmt.Sprintf("%s:%s", ip, Port), client.opts...) |
||||
if err != nil { |
||||
log.Fatalf("fail to dial: %v", err) |
||||
return nil |
||||
} |
||||
|
||||
client.clientServiceClient = proto.NewClientServiceClient(client.conn) |
||||
return &client |
||||
} |
||||
|
||||
// Close closes the Client.
|
||||
func (client *Client) Close() { |
||||
client.conn.Close() |
||||
} |
||||
|
||||
// Process processes message.
|
||||
func (client *Client) Process(message *Message) *Response { |
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
||||
defer cancel() |
||||
response, err := client.clientServiceClient.Process(ctx, message) |
||||
if err != nil { |
||||
log.Fatalf("Getting error when processing message: %s", err) |
||||
} |
||||
return response |
||||
} |
@ -0,0 +1,44 @@ |
||||
package message |
||||
|
||||
// This client service will replace the other client service.
|
||||
// This client service will use unified Message.
|
||||
// TODO(minhdoan): Refactor and clean up the other client service.
|
||||
import ( |
||||
context "context" |
||||
"log" |
||||
"net" |
||||
|
||||
"google.golang.org/grpc" |
||||
) |
||||
|
||||
// Constants for client service port.
|
||||
const ( |
||||
Port = "30000" |
||||
) |
||||
|
||||
// Server is the Server struct for client service package.
|
||||
type Server struct { |
||||
Port string |
||||
} |
||||
|
||||
func (s *Server) Process(ctx context.Context, message *Message) (*Response, error) { |
||||
} |
||||
|
||||
// Start starts the Server on given ip and port.
|
||||
func (s *Server) Start(ip, port string) (*grpc.Server, error) { |
||||
addr := net.JoinHostPort("", s.Port) |
||||
lis, err := net.Listen("tcp", addr) |
||||
if err != nil { |
||||
log.Fatalf("failed to listen: %v", err) |
||||
} |
||||
var opts []grpc.ServerOption |
||||
grpcServer := grpc.NewServer(opts...) |
||||
RegisterClientServiceServer(grpcServer, s) |
||||
go grpcServer.Serve(lis) |
||||
return grpcServer, nil |
||||
} |
||||
|
||||
// New creates new Server which implements ClientServiceServer interface.
|
||||
func NewServer() *Server { |
||||
return &Server{} |
||||
} |
Loading…
Reference in new issue