# megaco_user

### MODULE

megaco_user

### MODULE SUMMARY

Callback module for users of the Megaco application

### DESCRIPTION

This module defines the callback behaviour of Megaco users. A megaco_user compliant callback module must export the following functions:

- handle_connect/2
- handle_disconnect/3
- handle_syntax_error/3
- handle_message_error/3
- handle_trans_request/3
- handle_trans_long_request/3
- handle_trans_reply/4
- handle_trans_ack/4
- handle_unexpected_trans/3
- handle_trans_request_abort/4

The semantics of them and their exact signatures are explained below. There are a couple data types that are common for many of the functions. These are explained here:

`conn_handle()`
  Is the 'megaco_conn_handle' record initially returned by megaco:connect/4. It identifies a "virtual" connection and may be reused after a reconnect (disconnect + connect).
`protocol_version()`
  Is the actual protocol version. In most cases the protocol version is retreived from the processed message, but there are exceptions:
  - When handle_connect/2 is triggered by an explicit call to megaco:connect/4.
  - handle_disconnect/3
  - handle_syntax_error/3
  In these cases, the ProtocolVersion default version is obtained from the static connection configuration:
  - megaco:conn_info(ConnHandle, protocol_version).
`error_descr()`
  An 'ErrorDescriptor' record.

The `user_args` configuration parameter which may be used to extend the argument list of the callback functions. For example, the handle_connect function takes by default two arguments:

- handle_connect(Handle, Version)

but if the `user_args` parameter is set to a longer list, such as `[SomePid,SomeTableRef]`, the callback function is expected to have these (in this case two) extra arguments last in the argument list:

- handle_connect(Handle, Version, SomePid, SomeTableRef)

### EXPORTS

`handle_connect(ConnHandle, ProtocolVersion) -> ok | error | {error,ErrorDescr}`

Types:

`ConnHandle = conn_handle()`
`ProtocolVersion = protocol_version()`
`ErrorDescr = error_descr()`

Invoked when a new connection is established

Connections may either be established by an explicit call to megaco:connect/4 or implicitely at the first invokaction of megaco:receive_message/3.

Normally a Media Gateway (MG) connects explicitly while a Media Gateway Controller (MGC) connects implicitly.

At the Media Gateway Controller (MGC) side it is possible to reject a connection request (and send a message error reply to the gateway) by returning `{error, ErrorDescr}` or simply `error` which generates an error descriptor with code 402 (unauthorized) and reason "Connection refused by user" (this is also the case for all unknown results, such as exit signals or throw).

`handle_disconnect(ConnHandle, ProtocolVersion, Reason) -> ok`

Types:

`ConnHandle = conn_handle()`
`ProtocolVersion = protocol_version()`
`Reason = term()`

Invoked when a connection is teared down

The disconnect may either be made explicitely by a call to megaco:disconnect/2 or implicitely when the control process of the connection dies.

`handle_syntax_error(ReceiveHandle, ProtocolVersion, DefaultED) -> reply | {reply,ED} | no_reply | {no_reply,ED}`

Types:

`ReceiveHandle = receive_handle()`
`receive_handle() = #megaco_receive_handle{}`
`ProtocolVersion = protocol_version()`
`DefaultED = error_descr()`
`ED = error_descr()`

Invoked when a received message had syntax errors

Incoming messages is delivered by megaco:receive_message/4 and normally decoded successfully. But if the decoding failed this function is called in order to decide if the originator should get a reply message (reply) or if the reply silently should be discarded (no_reply).

Syntax errors are detected locally on this side of the protocol and may have many causes, e.g. a malfunctioning transport layer, wrong encoder/decoder selected, bad configuration of the selected encoder/decoder etc.

The error descriptor defaults to `DefaultED`, but can be overridden with an alternate one by returning `{reply,ED}` or `{no_reply,ED}` instead of `reply` and `no_reply` respectively.

Any other return values (including exit signals or throw) and the `DefaultED` will be used.

`handle_message_error(ConnHandle, ProtocolVersion, ErrorDescr) -> ok`

Types:

`ConnHandle = conn_handle()`
`ProtocolVersion = protocol_version()`
`ErrorDescr = error_descr()`

Invoked when a received message just contains an error instead of a list of transactions.

Incoming messages is delivered by megaco:receive_message/4 and successfully decoded. Normally a message contains a list of transactions, but it may instead contain an ErrorDescriptor on top level of the message.

Message errors are detected remotely on the other side of the protocol. And you probably don't want to reply to it, but it may indicate that you have outstanding transactions that not will get any response (request -> reply; reply -> ack).

`handle_trans_request(ConnHandle, ProtocolVersion, ActionRequests) -> pending() | reply() | ignore_trans_request`

Types:

`ConnHandle = conn_handle()`
`ProtocolVersion = protocol_version()`
`ActionRequests = [#'ActionRequest'{}]`
`pending() = {pending, req_data()}`
`req_data() = term()`
`reply() = {ack_action(), actual_reply()} | {ack_action(), actual_reply(), send_options()}`
`ack_action() = discard_ack | {handle_ack, ack_data()} | {handle_pending_ack, ack_data()} | {handle_sloppy_ack, ack_data()}`
`actual_reply() = [#'ActionReply'{}] | error_descr()`
`ack_data() = term()`
`send_options() = [send_option()]`
`send_option() = {reply_timer, megaco_timer()} | {send_handle, term()} | {protocol_version, integer()}`

Invoked for each transaction request

Incoming messages is delivered by megaco:receive_message/4 and successfully decoded. Normally a message contains a list of transactions and this function is invoked for each TransactionRequest in the message.

This function takes a list of 'ActionRequest' records and has three main options:

`Return ignore_trans_request`
  Decide that these action requests shall be ignored completely.
`Return pending()`
  Decide that the processing of these action requests will take a long time and that the originator should get an immediate 'TransactionPending' reply as interim response. The actual processing of these action requests instead should be delegated to the the handle_trans_long_request/3 callback function with the req_data() as one of its arguments.
`Return reply()`
  Process the action requests and either return an error_descr() indicating some fatal error or a list of action replies (wildcarded or not).
  If for some reason megaco is unable to deliver the reply, the reason for this will be passed to the user via a call to the callback function handle_trans_ack, unless `ack_action() = discard_ack`.
  The ack_action() is either:
  `discard_ack`
    Meaning that you don't care if the reply is acknowledged or not.
  `{handle_ack, ack_data()} | {handle_ack, ack_data(), send_options()}`
    Meaning that you want an immediate acknowledgement when the other part receives this transaction reply. When the acknowledgement eventually is received, the handle_trans_ack/4 callback function will be invoked with the ack_data() as one of its arguments. ack_data() may be any Erlang term.
  `{handle_pending_ack, ack_data()} | {handle_pending_ack, ack_data(), send_options()}`
    This has the same effect as the above, if and only if megaco has sent atleast one pending message for this request (during the processing of the request). If no pending message has been sent, then immediate acknowledgement will not be requested.
    Note that this only works as specified if the `sent_pending_limit` config option has been set to an integer value.
  `{handle_sloppy_ack, ack_data()}| {handle_sloppy_ack, ack_data(), send_options()}`
    Meaning that you want an acknowledgement sometime. When the acknowledgement eventually is received, the handle_trans_ack/4 callback function will be invoked with the ack_data() as one of its arguments. ack_data() may be any Erlang term.

Any other return values (including exit signals or throw) will result in an error descriptor with code 500 (internal gateway error) and the module name (of the callback module) as reason.

`handle_trans_long_request(ConnHandle, ProtocolVersion, ReqData) -> reply()`

Types:

`ConnHandle = conn_handle()`
`ProtocolVersion = protocol_version()`
`ReqData = req_data()`
`req_data() = term()`
`reply() = {ack_action(), actual_reply()} | {ack_action(), actual_reply(), send_options()}`
`ack_action() = discard_ack | {handle_ack, ack_data()} | {handle_sloppy_ack, ack_data()}`
`actual_reply() = [#'ActionReply'{}] | error_descr()`
`ack_data() = term()`
`send_options() = [send_option()]`
`send_option() = {reply_timer, megaco_timer()} | {send_handle, term()} | {protocol_version, integer()}`

Optionally invoked for a time consuming transaction request

If this function gets invoked or not is controlled by the reply from the preceeding call to handle_trans_request/3. The handle_trans_request/3 function may decide to process the action requests itself or to delegate the processing to this function.

The req_data() argument to this function is the Erlang term returned by handle_trans_request/3.

Any other return values (including exit signals or throw) will result in an error descriptor with code 500 (internal gateway error) and the module name (of the callback module) as reason.

`handle_trans_reply(ConnHandle, ProtocolVersion, UserReply, ReplyData) -> ok`

Types:

`ConnHandle = conn_handle()`
`ProtocolVersion = protocol_version()`
`UserReply = success() | failure()`
`success() = {ok, [#'ActionReply'{}]}`
`failure() = message_error() | user_cancel_error() | send_error() | other_error()`
`message_error() = {error, error_descr()}`
`user_cancel_error() = {error, user_cancel_reason()}`
`user_cancel_reason() = {user_cancel, reason_for_user_cancel()}`
`reason_for_user_cancel() = term()`
`send_error() = {error, send_reason()}`
`send_reason() = send_cancelled_reason() | send_failed_reason()`
`send_cancelled_reason() = {send_message_cancelled, reason_for_send_cancel()}`
`reason_for_send_cancel() = term()`
`send_failed_reason() = {send_message_failed, reason_for_send_failure()}`
`reason_for_send_failure() = term()`
`other_error() = {error, other_reason()}`
`other_reason() = term()`
`ReplyData = reply_data()`
`reply_data() = term()`

Optionally invoked for a transaction reply

The sender of a transaction request has the option of deciding, whether the originating Erlang process should synchronously wait (megaco:call/3) for a reply or if the message should be sent asynchronously (megaco:cast/3) and the processing of the reply should be delegated this callback function.

The ReplyData defaults to megaco:lookup(ConnHandle, reply_data), but may be explicitely overridden by a megaco:cast/3 option in order to forward info about the calling context of the originating process.

At success(), the UserReply contains a list of 'ActionReply' records possibly containing error indications.

A message_error(), indicates that the remote user has replied with an explicit transactionError.

A `user_cancel_error()`, indicates that the request has been canceled by the user. `reason_for_user_cancel()` is the reason given in the call to the cancel function.

A `send_error()`, indicates that the send function of the megaco transport callback module failed to send the request. There are two separate cases: `send_cancelled_reason()` and `send_failed_reason()`. The first is the result of the send function returning `{cancel, Reason}` and the second is some other kind of erroneous return value. See the send_message function for more info.

An other_error(), indicates some other error such as timeout.

`handle_trans_ack(ConnHandle, ProtocolVersion, AckStatus, AckData) -> ok`

Types:

`ConnHandle = conn_handle()`
`ProtocolVersion = protocol_version()`
`AckStatus = ok | user_cancel() | send_error() | other_error()`
`user_cancel() = {error, user_cancel_reason()}`
`user_cancel_reason() = {user_cancel, reason_for_user_cancel()}`
`send_error() = {error, send_reason()}`
`send_reason() = send_cancelled_reason() | send_failed_reason()`
`send_cancelled_reason() = {send_message_cancelled, reason_for_send_cancel()}`
`reason_for_send_cancel() = term()`
`send_failed_reason() = {send_message_failed, reason_for_send_failure()}`
`reason_for_send_failure() = term()`
`other_error() = {error, other_reason()}`
`other_reason() = term()`
`AckData = ack_data()`
`ack_data() = term()`

Optionally invoked for a transaction acknowledgement

If this function gets invoked or not, is controlled by the reply from the preceeding call to handle_trans_request/3. The handle_trans_request/3 function may decide to return {handle_ack, ack_data()} or {handle_sloppy_ack, ack_data()} meaning that you need an immediate acknowledgement of the reply and that this function should be invoked to handle the acknowledgement.

The ack_data() argument to this function is the Erlang term returned by handle_trans_request/3.

If the AckStatus is ok, it is indicating that this is a true acknowledgement of the transaction reply.

If the AckStatus is {error, Reason}, it is an indication that the acknowledgement or even the reply (for which this is an acknowledgement) was not delivered, but there is no point in waiting any longer for it to arrive. This happens when:

`reply_timer`
  The `reply_timer` eventually times out.
reply send failure
  When megaco failes to send the reply (see handle_trans_reply), for whatever reason.
cancel
  The user has explicitly cancelled the wait (megaco:cancel/2).

`handle_unexpected_trans(ReceiveHandle, ProtocolVersion, Trans) -> ok`

Types:

`ReceiveHandle = receive_handle()`
`receive_handle() = #megaco_receive_handle{}`
`ProtocolVersion = protocol_version()`
`Trans = 'TransactionPending' | 'TransactionReply' | 'TransactionResponseAck'`

Invoked when a unexpected message is received

If a reply to a request is not received in time, the megaco stack removes all info about the request from it's tables. If a reply should arrive after this has been done the app has no way of knowing where to send this message. The message is delivered to the "user" by calling this function on the local node (the node which has the link).

`handle_trans_request_abort(ReceiveHandle, ProtocolVersion, TransNo, Pid) -> ok`

Types:

`ReceiveHandle = receive_handle()`
`receive_handle() = #megaco_receive_handle{}`
`ProtocolVersion = protocol_version()`
`TransNo = integer()`
`Pid = undefined | pid()`

Invoked when a transaction request has been aborted

This function is invoked if the originating pending limit has been exceeded. This usually means that a request has taken abnormally long time to complete.

### AUTHORS

Håkan Mattsson - support@erlang.ericsson.se

megaco 3.5
Copyright © 1991-2006 Ericsson AB