@@ -72,6 +72,113 @@ type Courier interface {
7272 Close () error
7373}
7474
75+ // CourierCfg contains general config parameters applicable to all proof
76+ // couriers.
77+ type CourierCfg struct {
78+ // HashMailCfg contains hashmail protocol specific config parameters.
79+ HashMailCfg * HashMailCourierCfg
80+
81+ // UniverseRpcCfg contains universe RPC protocol specific config
82+ // parameters.
83+ UniverseRpcCfg * UniverseRpcCourierCfg
84+
85+ // TransferLog is a log for recording proof delivery and retrieval
86+ // attempts.
87+ TransferLog TransferLog
88+ }
89+
90+ // CourierDispatch is an interface that abstracts away the different proof
91+ // courier services that are supported.
92+ type CourierDispatch interface {
93+ // NewCourier instantiates a new courier service handle given a service
94+ // URL address.
95+ NewCourier (addr * url.URL , recipient Recipient ) (Courier , error )
96+ }
97+
98+ // URLDispatch is a proof courier dispatch that uses the courier address URL
99+ // scheme to determine which courier service to use.
100+ type URLDispatch struct {
101+ cfg * CourierCfg
102+ }
103+
104+ // NewCourierDispatch creates a new proof courier dispatch.
105+ func NewCourierDispatch (cfg * CourierCfg ) * URLDispatch {
106+ return & URLDispatch {
107+ cfg : cfg ,
108+ }
109+ }
110+
111+ // NewCourier instantiates a new courier service handle given a service URL
112+ // address.
113+ func (u * URLDispatch ) NewCourier (addr * url.URL ,
114+ recipient Recipient ) (Courier , error ) {
115+
116+ subscribers := make (map [uint64 ]* fn.EventReceiver [fn.Event ])
117+
118+ // Create new courier addr based on URL scheme.
119+ switch addr .Scheme {
120+ case HashmailCourierType :
121+ cfg := u .cfg .HashMailCfg
122+ backoffHandler := NewBackoffHandler (
123+ cfg .BackoffCfg , u .cfg .TransferLog ,
124+ )
125+
126+ hashMailCfg := HashMailCourierCfg {
127+ ReceiverAckTimeout : cfg .ReceiverAckTimeout ,
128+ }
129+
130+ hashMailBox , err := NewHashMailBox (addr )
131+ if err != nil {
132+ return nil , fmt .Errorf ("unable to make mailbox: %v" ,
133+ err )
134+ }
135+
136+ return & HashMailCourier {
137+ cfg : & hashMailCfg ,
138+ backoffHandle : backoffHandler ,
139+ recipient : recipient ,
140+ mailbox : hashMailBox ,
141+ subscribers : subscribers ,
142+ }, nil
143+
144+ case UniverseRpcCourierType :
145+ cfg := u .cfg .UniverseRpcCfg
146+ backoffHandler := NewBackoffHandler (
147+ cfg .BackoffCfg , u .cfg .TransferLog ,
148+ )
149+
150+ // Connect to the universe RPC server.
151+ dialOpts , err := serverDialOpts ()
152+ if err != nil {
153+ return nil , err
154+ }
155+
156+ serverAddr := fmt .Sprintf ("%s:%s" , addr .Hostname (), addr .Port ())
157+ conn , err := grpc .Dial (serverAddr , dialOpts ... )
158+ if err != nil {
159+ return nil , err
160+ }
161+
162+ client := unirpc .NewUniverseClient (conn )
163+
164+ return & UniverseRpcCourier {
165+ recipient : recipient ,
166+ client : client ,
167+ backoffHandle : backoffHandler ,
168+ transfer : u .cfg .TransferLog ,
169+ subscribers : subscribers ,
170+ }, nil
171+
172+ default :
173+ return nil , fmt .Errorf ("unknown courier address protocol " +
174+ "(consider updating tapd): %v" , addr .Scheme )
175+ }
176+ }
177+
178+ // A compile-time assertion to ensure that the URLDispatch meets the
179+ // CourierDispatch interface.
180+ var _ CourierDispatch = (* URLDispatch )(nil )
181+
75182// CourierAddr is a fully validated courier address (including protocol specific
76183// validation).
77184type CourierAddr interface {
@@ -126,10 +233,12 @@ func (h *HashMailCourierAddr) Url() *url.URL {
126233func (h * HashMailCourierAddr ) NewCourier (_ context.Context , cfg * CourierCfg ,
127234 recipient Recipient ) (Courier , error ) {
128235
129- backoffHandle := NewBackoffHandler (cfg .BackoffCfg , cfg .TransferLog )
236+ backoffHandle := NewBackoffHandler (
237+ cfg .HashMailCfg .BackoffCfg , cfg .TransferLog ,
238+ )
130239
131240 hashMailCfg := HashMailCourierCfg {
132- ReceiverAckTimeout : cfg .ReceiverAckTimeout ,
241+ ReceiverAckTimeout : cfg .HashMailCfg . ReceiverAckTimeout ,
133242 }
134243
135244 hashMailBox , err := NewHashMailBox (& h .addr )
@@ -185,7 +294,9 @@ func (h *UniverseRpcCourierAddr) Url() *url.URL {
185294func (h * UniverseRpcCourierAddr ) NewCourier (_ context.Context ,
186295 cfg * CourierCfg , recipient Recipient ) (Courier , error ) {
187296
188- backoffHandle := NewBackoffHandler (cfg .BackoffCfg , cfg .TransferLog )
297+ backoffHandle := NewBackoffHandler (
298+ cfg .UniverseRpcCfg .BackoffCfg , cfg .TransferLog ,
299+ )
189300
190301 // Ensure that the courier address is a universe RPC address.
191302 if h .addr .Scheme != UniverseRpcCourierType {
@@ -251,22 +362,6 @@ func NewCourier(ctx context.Context, addr url.URL, cfg *CourierCfg,
251362 return courierAddr .NewCourier (ctx , cfg , recipient )
252363}
253364
254- // CourierCfg contains general config parameters applicable to all proof
255- // couriers.
256- type CourierCfg struct {
257- // ReceiverAckTimeout is the maximum time we'll wait for the receiver to
258- // acknowledge the proof.
259- ReceiverAckTimeout time.Duration
260-
261- // BackoffCfg configures the behaviour of the proof delivery
262- // functionality.
263- BackoffCfg * BackoffCfg
264-
265- // TransferLog is a log for recording proof delivery and retrieval
266- // attempts.
267- TransferLog TransferLog
268- }
269-
270365// ProofMailbox represents an abstract store-and-forward mailbox that can be
271366// used to send/receive proofs.
272367type ProofMailbox interface {
0 commit comments