@@ -167,6 +167,7 @@ func (u *URLDispatch) NewCourier(addr *url.URL,
167167 backoffHandle : backoffHandler ,
168168 transfer : u .cfg .TransferLog ,
169169 subscribers : subscribers ,
170+ rawConn : conn ,
170171 }, nil
171172
172173 default :
@@ -179,187 +180,40 @@ func (u *URLDispatch) NewCourier(addr *url.URL,
179180// CourierDispatch interface.
180181var _ CourierDispatch = (* URLDispatch )(nil )
181182
182- // CourierAddr is a fully validated courier address (including protocol specific
183- // validation).
184- type CourierAddr interface {
185- // Url returns the url.URL representation of the courier address.
186- Url () * url.URL
187-
188- // NewCourier generates a new courier service handle.
189- NewCourier (ctx context.Context , cfg * CourierCfg ,
190- recipient Recipient ) (Courier , error )
191- }
192-
193- // ParseCourierAddrString parses a proof courier address string and returns a
194- // protocol specific courier address instance.
195- func ParseCourierAddrString (addr string ) (CourierAddr , error ) {
196- // Parse URI.
183+ // ParseCourierAddress attempts to parse the given string as a proof courier
184+ // address, validates that all required fields are present and ensures the
185+ // protocol is one of the supported protocols.
186+ func ParseCourierAddress (addr string ) (* url.URL , error ) {
197187 urlAddr , err := url .ParseRequestURI (addr )
198188 if err != nil {
199189 return nil , fmt .Errorf ("invalid proof courier URI address: %w" ,
200190 err )
201191 }
202192
203- return ParseCourierAddrUrl (* urlAddr )
204- }
205-
206- // ParseCourierAddrUrl parses a proof courier address url.URL and returns a
207- // protocol specific courier address instance.
208- func ParseCourierAddrUrl (addr url.URL ) (CourierAddr , error ) {
209- // Create new courier addr based on URL scheme.
210- switch addr .Scheme {
211- case HashmailCourierType :
212- return NewHashMailCourierAddr (addr )
213- case UniverseRpcCourierType :
214- return NewUniverseRpcCourierAddr (addr )
215- }
216-
217- return nil , fmt .Errorf ("unknown courier address protocol " +
218- "(consider updating tapd): %v" , addr .Scheme )
219- }
220-
221- // HashMailCourierAddr is a hashmail protocol specific implementation of the
222- // CourierAddr interface.
223- type HashMailCourierAddr struct {
224- addr url.URL
225- }
226-
227- // Url returns the url.URL representation of the hashmail courier address.
228- func (h * HashMailCourierAddr ) Url () * url.URL {
229- return & h .addr
230- }
231-
232- // NewCourier generates a new courier service handle.
233- func (h * HashMailCourierAddr ) NewCourier (_ context.Context , cfg * CourierCfg ,
234- recipient Recipient ) (Courier , error ) {
235-
236- backoffHandle := NewBackoffHandler (
237- cfg .HashMailCfg .BackoffCfg , cfg .TransferLog ,
238- )
239-
240- hashMailCfg := HashMailCourierCfg {
241- ReceiverAckTimeout : cfg .HashMailCfg .ReceiverAckTimeout ,
242- }
243-
244- hashMailBox , err := NewHashMailBox (& h .addr )
245- if err != nil {
246- return nil , fmt .Errorf ("unable to make mailbox: %v" ,
247- err )
248- }
249-
250- subscribers := make (
251- map [uint64 ]* fn.EventReceiver [fn.Event ],
252- )
253- return & HashMailCourier {
254- cfg : & hashMailCfg ,
255- backoffHandle : backoffHandle ,
256- recipient : recipient ,
257- mailbox : hashMailBox ,
258- subscribers : subscribers ,
259- }, nil
260- }
261-
262- // NewHashMailCourierAddr generates a new hashmail courier address from a given
263- // URL. This function also performs hashmail protocol specific address
264- // validation.
265- func NewHashMailCourierAddr (addr url.URL ) (* HashMailCourierAddr , error ) {
266- if addr .Scheme != HashmailCourierType {
267- return nil , fmt .Errorf ("expected hashmail courier protocol: %v" ,
268- addr .Scheme )
269- }
270-
271- // We expect the port number to be specified for a hashmail service.
272- if addr .Port () == "" {
273- return nil , fmt .Errorf ("hashmail proof courier URI address " +
274- "port unspecified" )
275- }
276-
277- return & HashMailCourierAddr {
278- addr ,
279- }, nil
280- }
281-
282- // UniverseRpcCourierAddr is a universe RPC protocol specific implementation of
283- // the CourierAddr interface.
284- type UniverseRpcCourierAddr struct {
285- addr url.URL
286- }
287-
288- // Url returns the url.URL representation of the courier address.
289- func (h * UniverseRpcCourierAddr ) Url () * url.URL {
290- return & h .addr
291- }
292-
293- // NewCourier generates a new courier service handle.
294- func (h * UniverseRpcCourierAddr ) NewCourier (_ context.Context ,
295- cfg * CourierCfg , recipient Recipient ) (Courier , error ) {
296-
297- backoffHandle := NewBackoffHandler (
298- cfg .UniverseRpcCfg .BackoffCfg , cfg .TransferLog ,
299- )
300-
301- // Ensure that the courier address is a universe RPC address.
302- if h .addr .Scheme != UniverseRpcCourierType {
303- return nil , fmt .Errorf ("unsupported courier protocol: %v" ,
304- h .addr .Scheme )
305- }
306-
307- // Connect to the universe RPC server.
308- dialOpts , err := serverDialOpts ()
309- if err != nil {
193+ if err := ValidateCourierAddress (urlAddr ); err != nil {
310194 return nil , err
311195 }
312196
313- serverAddr := fmt .Sprintf (
314- "%s:%s" , h .addr .Hostname (), h .addr .Port (),
315- )
316- conn , err := grpc .Dial (serverAddr , dialOpts ... )
317- if err != nil {
318- return nil , err
319- }
320-
321- client := unirpc .NewUniverseClient (conn )
322-
323- // Instantiate the events subscribers map.
324- subscribers := make (
325- map [uint64 ]* fn.EventReceiver [fn.Event ],
326- )
327-
328- return & UniverseRpcCourier {
329- recipient : recipient ,
330- client : client ,
331- backoffHandle : backoffHandle ,
332- transfer : cfg .TransferLog ,
333- subscribers : subscribers ,
334- rawConn : conn ,
335- }, nil
197+ return urlAddr , nil
336198}
337199
338- // NewUniverseRpcCourierAddr generates a new universe RPC courier address from a
339- // given URL. This function also performs protocol specific address validation .
340- func NewUniverseRpcCourierAddr (addr url.URL ) ( * UniverseRpcCourierAddr , error ) {
200+ // ValidateCourierAddress validates that all required fields are present and
201+ // ensures the protocol is one of the supported protocols .
202+ func ValidateCourierAddress (addr * url.URL ) error {
341203 // We expect the port number to be specified.
342204 if addr .Port () == "" {
343- return nil , fmt .Errorf ("proof courier URI address port " +
344- "unspecified" )
205+ return fmt .Errorf ("proof courier URI address port unspecified" )
345206 }
346207
347- return & UniverseRpcCourierAddr {
348- addr ,
349- }, nil
350- }
351-
352- // NewCourier instantiates a new courier service handle given a service URL
353- // address.
354- func NewCourier (ctx context.Context , addr url.URL , cfg * CourierCfg ,
355- recipient Recipient ) (Courier , error ) {
208+ switch addr .Scheme {
209+ case HashmailCourierType , UniverseRpcCourierType :
210+ // Valid and known courier address protocol.
211+ return nil
356212
357- courierAddr , err := ParseCourierAddrUrl ( addr )
358- if err != nil {
359- return nil , err
213+ default :
214+ return fmt . Errorf ( "unknown courier address protocol " +
215+ "(consider updating tapd): %v" , addr . Scheme )
360216 }
361-
362- return courierAddr .NewCourier (ctx , cfg , recipient )
363217}
364218
365219// ProofMailbox represents an abstract store-and-forward mailbox that can be
@@ -564,7 +418,7 @@ func (h *HashMailBox) RecvAck(ctx context.Context, sid streamID) error {
564418 return fmt .Errorf ("expected ack, got %x" , msg .Msg )
565419}
566420
567- // CleanUp atempts to tear down the mailbox as specified by the passed sid.
421+ // CleanUp attempts to tear down the mailbox as specified by the passed sid.
568422func (h * HashMailBox ) CleanUp (ctx context.Context , sid streamID ) error {
569423 streamAuth := & hashmailrpc.CipherBoxAuth {
570424 Desc : & hashmailrpc.CipherBoxDesc {
0 commit comments