github.com/hyperledger/aries-framework-go@v0.3.2/pkg/didcomm/common/service/event.go (about)

     1  /*
     2  Copyright SecureKey Technologies Inc. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package service
     8  
     9  // StateMsgType state msg type.
    10  type StateMsgType int
    11  
    12  const (
    13  	// PreState pre state.
    14  	PreState StateMsgType = iota
    15  
    16  	// PostState post state.
    17  	PostState
    18  )
    19  
    20  // StateMsg is used in MsgEvent to pass the state details to the consumer. Refer service.Event.RegisterMsgEvent
    21  // for more details.
    22  type StateMsg struct {
    23  	// Name of the protocol.
    24  	//
    25  	// Supported protocols
    26  	//   - DID Exchange :  didexchange.DIDExchange
    27  	ProtocolName string
    28  
    29  	// type of the message (pre or post), refer service.StateMsgType
    30  	Type StateMsgType
    31  
    32  	// current state. Refer protocol RFC for possible states.
    33  	StateID string
    34  
    35  	// DIDComm message along with message type
    36  	Msg DIDCommMsg
    37  
    38  	// Properties contains value based on specific protocol. The consumers need to call the protocol client
    39  	// functions to get the data.
    40  	//
    41  	// Clients function to retrieve data based on protocol.
    42  	//   - DID Exchange :  didexchange.Event
    43  	Properties EventProperties
    44  }
    45  
    46  // DIDCommAction message type to pass events in go channels.
    47  type DIDCommAction struct {
    48  	// Name of the protocol.
    49  	//
    50  	// Supported protocols
    51  	//   - DID Exchange :  didexchange.DIDExchange
    52  	ProtocolName string
    53  
    54  	// DIDComm message
    55  	Message DIDCommMsg
    56  
    57  	// Continue function to be called by the consumer for further processing the message.
    58  	Continue func(args interface{})
    59  
    60  	// Stop invocation notifies the service that the consumer action event processing has failed or the consumer wants
    61  	// to stop the processing.
    62  	Stop func(err error)
    63  
    64  	// Properties contains value based on specific protocol. The consumers need to call the protocol client
    65  	// functions to get the data.
    66  	//
    67  	// Clients function to retrieve data based on protocol.
    68  	//   - DID Exchange :  didexchange.EventProperties
    69  	Properties EventProperties
    70  }
    71  
    72  // EventProperties type for event related data.
    73  // NOTE: Properties always should be serializable.
    74  type EventProperties interface {
    75  	All() map[string]interface{}
    76  }
    77  
    78  // Event event related apis.
    79  type Event interface {
    80  	// RegisterActionEvent on protocol messages. The events are triggered for incoming message types based on
    81  	// the protocol service. The consumer need to invoke the callback to resume processing.
    82  	// Only one channel can be registered for the action events. The function will throw error if a channel is already
    83  	// registered.
    84  	RegisterActionEvent(ch chan<- DIDCommAction) error
    85  
    86  	// UnregisterActionEvent on protocol messages. Refer RegisterActionEvent().
    87  	UnregisterActionEvent(ch chan<- DIDCommAction) error
    88  
    89  	// RegisterMsgEvent on protocol messages. The message events are triggered for incoming messages. Service
    90  	// will not expect any callback on these events unlike Action event.
    91  	RegisterMsgEvent(ch chan<- StateMsg) error
    92  
    93  	// UnregisterMsgEvent on protocol messages. Refer RegisterMsgEvent().
    94  	UnregisterMsgEvent(ch chan<- StateMsg) error
    95  }
    96  
    97  // AutoExecuteActionEvent is a utility function to execute Action events automatically. The function requires
    98  // a channel to be passed-in to listen to dispatcher.DIDCommAction and triggers the Continue function on the
    99  // action event. This is a blocking function and use this function with a goroutine.
   100  //
   101  // Usage:
   102  //  s := didexchange.New(....)
   103  //	actionCh := make(chan dispatcher.DIDCommAction)
   104  //	err = s.RegisterActionEvent(actionCh)
   105  //	go service.AutoExecuteActionEvent(actionCh)
   106  func AutoExecuteActionEvent(ch chan DIDCommAction) {
   107  	for msg := range ch {
   108  		msg.Continue(&Empty{})
   109  	}
   110  }
   111  
   112  // Empty is used if there are no arguments to Continue.
   113  type Empty struct{}