|
| 1 | +package gorums |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/google/uuid" |
| 10 | + "google.golang.org/grpc" |
| 11 | + "google.golang.org/grpc/credentials/insecure" |
| 12 | + "google.golang.org/grpc/metadata" |
| 13 | + "google.golang.org/protobuf/reflect/protoreflect" |
| 14 | +) |
| 15 | + |
| 16 | +type ReplySpecHandler func([]protoreflect.ProtoMessage) (protoreflect.ProtoMessage, bool) |
| 17 | + |
| 18 | +type ClientResponse struct { |
| 19 | + broadcastID string |
| 20 | + data protoreflect.ProtoMessage |
| 21 | +} |
| 22 | + |
| 23 | +type ClientRequest struct { |
| 24 | + broadcastID string |
| 25 | + doneChan chan protoreflect.ProtoMessage |
| 26 | + handler ReplySpecHandler |
| 27 | +} |
| 28 | + |
| 29 | +type ClientServer struct { |
| 30 | + respChan chan *ClientResponse |
| 31 | + reqChan chan *ClientRequest |
| 32 | + resps map[string][]protoreflect.ProtoMessage |
| 33 | + doneChans map[string]chan protoreflect.ProtoMessage |
| 34 | + handlers map[string]func(resps []protoreflect.ProtoMessage) (protoreflect.ProtoMessage, bool) |
| 35 | + listenAddr string |
| 36 | +} |
| 37 | + |
| 38 | +func NewClientServer(listenAddr string) (*ClientServer, net.Listener, error) { |
| 39 | + srv := &ClientServer{ |
| 40 | + respChan: make(chan *ClientResponse, 10), |
| 41 | + reqChan: make(chan *ClientRequest), |
| 42 | + resps: make(map[string][]protoreflect.ProtoMessage), |
| 43 | + doneChans: make(map[string]chan protoreflect.ProtoMessage), |
| 44 | + handlers: make(map[string]func(resps []protoreflect.ProtoMessage) (protoreflect.ProtoMessage, bool)), |
| 45 | + } |
| 46 | + lis, err := net.Listen("tcp", listenAddr) |
| 47 | + for err != nil { |
| 48 | + return nil, nil, err |
| 49 | + } |
| 50 | + srv.listenAddr = lis.Addr().String() |
| 51 | + go srv.handle() |
| 52 | + return srv, lis, nil |
| 53 | +} |
| 54 | + |
| 55 | +func (srv *ClientServer) AddRequest(ctx context.Context, in protoreflect.ProtoMessage, handler ReplySpecHandler) (chan protoreflect.ProtoMessage, QuorumCallData) { |
| 56 | + broadcastID := uuid.New().String() |
| 57 | + cd := QuorumCallData{ |
| 58 | + Message: in, |
| 59 | + Method: "protos.UniformBroadcast.SaveStudent", |
| 60 | + |
| 61 | + BroadcastID: broadcastID, |
| 62 | + Sender: BroadcastClient, |
| 63 | + OriginAddr: srv.listenAddr, |
| 64 | + } |
| 65 | + doneChan := make(chan protoreflect.ProtoMessage) |
| 66 | + srv.reqChan <- &ClientRequest{ |
| 67 | + broadcastID: broadcastID, |
| 68 | + doneChan: doneChan, |
| 69 | + handler: handler, |
| 70 | + } |
| 71 | + return doneChan, cd |
| 72 | +} |
| 73 | + |
| 74 | +func (srv *ClientServer) AddResponse(ctx context.Context, resp protoreflect.ProtoMessage) error { |
| 75 | + md, ok := metadata.FromIncomingContext(ctx) |
| 76 | + if !ok { |
| 77 | + return fmt.Errorf("no metadata") |
| 78 | + } |
| 79 | + broadcastID := "" |
| 80 | + val := md.Get(BroadcastID) |
| 81 | + if val != nil && len(val) >= 1 { |
| 82 | + broadcastID = val[0] |
| 83 | + } |
| 84 | + if broadcastID == "" { |
| 85 | + return fmt.Errorf("no broadcastID") |
| 86 | + } |
| 87 | + srv.respChan <- &ClientResponse{ |
| 88 | + broadcastID: broadcastID, |
| 89 | + data: resp, |
| 90 | + } |
| 91 | + return nil |
| 92 | +} |
| 93 | + |
| 94 | +func (srv *ClientServer) handle() { |
| 95 | + for { |
| 96 | + select { |
| 97 | + case resp := <-srv.respChan: |
| 98 | + if _, ok := srv.resps[resp.broadcastID]; !ok { |
| 99 | + continue |
| 100 | + } |
| 101 | + srv.resps[resp.broadcastID] = append(srv.resps[resp.broadcastID], resp.data) |
| 102 | + response, done := srv.handlers[resp.broadcastID](srv.resps[resp.broadcastID]) |
| 103 | + if done { |
| 104 | + srv.doneChans[resp.broadcastID] <- response |
| 105 | + close(srv.doneChans[resp.broadcastID]) |
| 106 | + delete(srv.resps, resp.broadcastID) |
| 107 | + delete(srv.doneChans, resp.broadcastID) |
| 108 | + delete(srv.handlers, resp.broadcastID) |
| 109 | + } |
| 110 | + case req := <-srv.reqChan: |
| 111 | + srv.resps[req.broadcastID] = make([]protoreflect.ProtoMessage, 0) |
| 112 | + srv.doneChans[req.broadcastID] = req.doneChan |
| 113 | + srv.handlers[req.broadcastID] = req.handler |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func ConvertToType[T protoreflect.ProtoMessage](handler func([]T) (T, bool)) func(d []protoreflect.ProtoMessage) (protoreflect.ProtoMessage, bool) { |
| 119 | + return func(d []protoreflect.ProtoMessage) (protoreflect.ProtoMessage, bool) { |
| 120 | + data := make([]T, len(d)) |
| 121 | + for i, elem := range d { |
| 122 | + data[i] = elem.(T) |
| 123 | + } |
| 124 | + return handler(data) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func ServerClientRPC(method string) func(addr, broadcastID string, in protoreflect.ProtoMessage, opts ...grpc.CallOption) (any, error) { |
| 129 | + return func(addr, broadcastID string, in protoreflect.ProtoMessage, opts ...grpc.CallOption) (any, error) { |
| 130 | + tmp := strings.Split(method, ".") |
| 131 | + m := "" |
| 132 | + if len(tmp) >= 1 { |
| 133 | + m = tmp[len(tmp)-1] |
| 134 | + } |
| 135 | + method = "/protos.ClientServer/Client" + m |
| 136 | + cc, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 137 | + if err != nil { |
| 138 | + return nil, err |
| 139 | + } |
| 140 | + out := new(any) |
| 141 | + md := metadata.Pairs(BroadcastID, broadcastID) |
| 142 | + ctx := metadata.NewOutgoingContext(context.Background(), md) |
| 143 | + err = cc.Invoke(ctx, method, in, out, opts...) |
| 144 | + if err != nil { |
| 145 | + return nil, err |
| 146 | + } |
| 147 | + return nil, nil |
| 148 | + } |
| 149 | +} |
0 commit comments