The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
woop/api/proto/message/client.go

48 lines
1.1 KiB

package message
import (
"context"
"fmt"
"log"
"time"
"google.golang.org/grpc"
)
// Client is the client model for client service.
type Client struct {
clientServiceClient 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 = 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
}