github.com/CyCoreSystems/ari@v4.8.4+incompatible/endpoint.go (about)

     1  package ari
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  )
     7  
     8  // EndpointIDSeparator seperates the ID components of the endpoint ID
     9  const EndpointIDSeparator = "|" //TODO: confirm separator isn't terrible
    10  
    11  // Endpoint represents a communication path to an Asterisk server
    12  // for endpoint resources
    13  type Endpoint interface {
    14  
    15  	// List lists the endpoints
    16  	List(filter *Key) ([]*Key, error)
    17  
    18  	// List available endpoints for a given endpoint technology
    19  	ListByTech(tech string, filter *Key) ([]*Key, error)
    20  
    21  	// Get returns a handle to the endpoint for further operations
    22  	Get(key *Key) *EndpointHandle
    23  
    24  	// Data returns the state of the endpoint
    25  	Data(key *Key) (*EndpointData, error)
    26  }
    27  
    28  // NewEndpointKey returns the key for the given endpoint
    29  func NewEndpointKey(tech, resource string, opts ...KeyOptionFunc) *Key {
    30  	return NewKey(EndpointKey, endpointKeyID(tech, resource), opts...)
    31  }
    32  
    33  func endpointKeyID(tech, resource string) string {
    34  	return tech + "/" + resource
    35  }
    36  
    37  // EndpointData describes an external device which may offer or accept calls
    38  // to or from Asterisk.  Devices are defined by a technology/resource pair.
    39  //
    40  // Allowed states:  'unknown', 'offline', 'online'
    41  type EndpointData struct {
    42  	// Key is the cluster-unique identifier for this Endpoint
    43  	Key *Key `json:"key"`
    44  
    45  	ChannelIDs []string `json:"channel_ids"`     // List of channel Ids which are associated with this endpoint
    46  	Resource   string   `json:"resource"`        // The endpoint's resource name
    47  	State      string   `json:"state,omitempty"` // The state of the endpoint
    48  	Technology string   `json:"technology"`      // The technology of the endpoint (e.g. SIP, PJSIP, DAHDI, etc)
    49  }
    50  
    51  // ID returns the ID for the endpoint
    52  func (ed *EndpointData) ID() string {
    53  	return ed.Technology + EndpointIDSeparator + ed.Resource
    54  }
    55  
    56  // FromEndpointID converts the endpoint ID to the tech, resource pair.
    57  func FromEndpointID(id string) (tech string, resource string, err error) {
    58  	items := strings.Split(id, EndpointIDSeparator)
    59  	if len(items) < 2 {
    60  		err = errors.New("Endpoint ID is not in tech" + EndpointIDSeparator + "resource format")
    61  		return
    62  	}
    63  
    64  	if len(items) > 2 {
    65  		// huge programmer error here, we want to handle it
    66  		// tempted to panic here...
    67  		err = errors.New("EndpointIDSeparator is conflicting with tech and resource identifiers")
    68  		return
    69  	}
    70  
    71  	tech = items[0]
    72  	resource = items[1]
    73  	return
    74  }
    75  
    76  // NewEndpointHandle creates a new EndpointHandle
    77  func NewEndpointHandle(key *Key, e Endpoint) *EndpointHandle {
    78  	return &EndpointHandle{
    79  		key: key,
    80  		e:   e,
    81  	}
    82  }
    83  
    84  // An EndpointHandle is a reference to an endpoint attached to
    85  // a transport to an asterisk server
    86  type EndpointHandle struct {
    87  	key *Key
    88  	e   Endpoint
    89  }
    90  
    91  // ID returns the identifier for the endpoint
    92  func (eh *EndpointHandle) ID() string {
    93  	return eh.key.ID
    94  }
    95  
    96  // Key returns the key for the endpoint
    97  func (eh *EndpointHandle) Key() *Key {
    98  	return eh.key
    99  }
   100  
   101  // Data returns the state of the endpoint
   102  func (eh *EndpointHandle) Data() (*EndpointData, error) {
   103  	return eh.e.Data(eh.key)
   104  }