github.com/glide-im/glide@v1.6.0/im_service/client/gateway_rpc_impl.go (about)

     1  package client
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"errors"
     7  	"fmt"
     8  	"github.com/glide-im/glide/im_service/proto"
     9  	"github.com/glide-im/glide/pkg/gate"
    10  	"github.com/glide-im/glide/pkg/messages"
    11  	"github.com/glide-im/glide/pkg/rpc"
    12  	"strings"
    13  )
    14  
    15  const (
    16  	errRpcInvocation = "gate invocation error: "
    17  )
    18  
    19  type IMServiceError struct {
    20  	Code    int32
    21  	Message string
    22  }
    23  
    24  func (e *IMServiceError) Error() string {
    25  	return fmt.Sprintf("IM Service Error: %d, %s", e.Code, e.Message)
    26  }
    27  
    28  // IsRpcInvocationError
    29  // Rpc invocation failed errors are returned by the gate client when the rpc call fails.
    30  func IsRpcInvocationError(err error) bool {
    31  	return err != nil && strings.HasPrefix(err.Error(), errRpcInvocation)
    32  }
    33  
    34  type GatewayRpcImpl struct {
    35  	gate *GatewayRpcClient
    36  }
    37  
    38  func NewGatewayRpcImplWithClient(client *rpc.BaseClient) *GatewayRpcImpl {
    39  	return &GatewayRpcImpl{
    40  		gate: &GatewayRpcClient{
    41  			cli: client,
    42  		},
    43  	}
    44  }
    45  
    46  func NewGatewayRpcImpl(opts *rpc.ClientOptions) (*GatewayRpcImpl, error) {
    47  	cli, err := rpc.NewBaseClient(opts)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	return NewGatewayRpcImplWithClient(cli), nil
    52  }
    53  
    54  func (i *GatewayRpcImpl) SetClientID(old gate.ID, new_ gate.ID) error {
    55  	response := proto.Response{}
    56  	ctx := context.TODO()
    57  	request := proto.UpdateClient{
    58  		Type:  proto.UpdateClient_UpdateID,
    59  		Id:    string(old),
    60  		NewId: string(new_),
    61  	}
    62  	return i.gate.UpdateClient(ctx, &request, &response)
    63  }
    64  
    65  func (i *GatewayRpcImpl) UpdateClient(id gate.ID, info *gate.ClientSecrets) error {
    66  	response := proto.Response{}
    67  	ctx := context.TODO()
    68  	request := proto.UpdateClient{
    69  		Type:   proto.UpdateClient_UpdateSecret,
    70  		Id:     string(id),
    71  		Secret: info.MessageDeliverSecret,
    72  	}
    73  	return i.gate.UpdateClient(ctx, &request, &response)
    74  }
    75  
    76  func (i *GatewayRpcImpl) ExitClient(id gate.ID) error {
    77  	response := proto.Response{}
    78  	ctx := context.TODO()
    79  	request := proto.UpdateClient{
    80  		Type: proto.UpdateClient_Close,
    81  		Id:   string(id),
    82  	}
    83  	return i.gate.UpdateClient(ctx, &request, &response)
    84  }
    85  
    86  func (i *GatewayRpcImpl) EnqueueMessage(id gate.ID, message *messages.GlideMessage) error {
    87  
    88  	marshal, err := json.Marshal(message)
    89  	if err != nil {
    90  		return err
    91  	}
    92  	ctx := context.TODO()
    93  	request := proto.EnqueueMessageRequest{
    94  		Id:  string(id),
    95  		Msg: marshal,
    96  	}
    97  	response := proto.Response{}
    98  	err = i.gate.EnqueueMessage(ctx, &request, &response)
    99  	if err != nil {
   100  		return errors.New(errRpcInvocation + err.Error())
   101  	}
   102  	return getResponseError(&response)
   103  }
   104  
   105  func (i *GatewayRpcImpl) Close() error {
   106  	return i.gate.cli.Close()
   107  }
   108  
   109  func getResponseError(response *proto.Response) error {
   110  	if proto.Response_ResponseCode(response.GetCode()) != proto.Response_OK {
   111  		return &IMServiceError{
   112  			Code:    response.GetCode(),
   113  			Message: response.GetMsg(),
   114  		}
   115  	}
   116  	return nil
   117  }