megaco_user(3)
π NAME π
megaco_user β bestie, this is the callback module for users of the Megaco application π¦. It's an Erlang module definition, so yes it's sitting in section 3 of the manual, you found exactly the right page, slay π
.
β¨ DESCRIPTION β¨
okay so here's the whole vibe: this module defines the callback behaviour of Megaco users. A megaco_user compliant callback module must export the following functions, no skipping, periodt:
handle_connect/2,3handle_disconnect/3handle_syntax_error/3,4handle_message_error/3,4handle_trans_request/3,4handle_trans_long_request/3,4handle_trans_reply/4,5handle_trans_ack/4,5handle_unexpected_trans/3,4handle_trans_request_abort/4,5handle_segment_reply/5,6
The semantics of them and their exact signatures are explained below π (yes, all of them, we're doing this together bestie π«Ά).
There's also the user_args configuration parameter, which may be used to extend the argument list of the callback functions. Example: handle_connect 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)
so extra args go on the end, like the little friends they are πΈ.
NOTE: most of the functions below have an optionalExtraargument (e.g.handle_unexpected_trans/4). The functions which take this argument will be called if and only if one of the functionsreceive_message/5orprocess_received_message/5was called with theExtraargument different thanignore_extra. iykyk π
π DATA TYPES π
action_request() = #'ActionRequest'{}
action_reply() = #'ActionReply'{}
error_desc() = #'ErrorDescriptor'{}
segment_no() = integer()
conn_handle() = #megaco_conn_handle{}
conn_handle()β the record initially returned bymegaco:connect/4,5. It identifies a "virtual" connection and may be reused after a reconnect (disconnect + connect). Reuse, recycle, iconic behaviour honestly β»οΈ.
protocol_version() = integer()
Is the actual protocol version π₯. In most cases the protocol version is retrieved from the processed message, but there are exceptions, and yes you need to know them:
- When
handle_connect/2,3is triggered by an explicit call tomegaco:connect/4,5. handle_disconnect/3handle_syntax_error/3
In these cases, the ProtocolVersion default version is obtained from the static connection configuration:
megaco:conn_info(ConnHandle, protocol_version).
π DATA TYPES π
conn_handle() :: #megaco_conn_handle{}.megaco_timer() ::infinity | non_neg_integer() | #megaco_incr_timer{}.receive_handle() :: #megaco_receive_handle{}.
π CALLBACKS π
okay bestie, deep breath, this is the main character section π.
handle_connect(ConnHandle, ProtocolVersion, Extra)
Invoked when a new connection is established π
Connections may either be established by an explicit call to megaco:connect/4 or implicitly at the first invocation 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" β and yes, that's also the case for all unknown results, such as exit signals or throw. So if you return something weird, congrats, you ghosted them with a 402 π.
See note above about the Extra argument in handle_message_error/4.
handle_connect/3 (with Extra) can also be called as a result of a call to the megaco:connect/5 function (if that function is called with the Extra argument different than ignore_extra).
handle_disconnect(ConnHandle, ProtocolVersion, Reason)
Invoked when a connection is teared down π
The disconnect may either be made explicitly by a call to megaco:disconnect/2 or implicitly when the control process of the connection dies.
handle_message_error(ConnHandle, ProtocolVersion, ErrorDescr, Extra)
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).
See note above about the Extra argument in handle_message_error/4.
handle_segment_reply(ConnHandle, ProtocolVersion, TransNo, SegNo, SegCompl, Extra)
This function is called when a segment reply has been received if the segment_reply_ind config option has been set to true.
This is in effect a progress report π.
See note above about the Extra argument in handle_segment_reply/6.
handle_syntax_error(ReceiveHandle, ProtocolVersion, DefaultED, Extra)
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.
See note above about the Extra argument in handle_syntax_error/4.
handle_trans_ack(ConnHandle, ProtocolVersion, AckStatus, AckData, Extra)
Optionally invoked for a transaction acknowledgement.
If this function gets invoked or not, is controlled by the reply from the preceding 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β thereply_timereventually times out.- reply send failure β when megaco fails to send the reply (see
handle_trans_reply), for whatever reason. cancelβ the user has explicitly cancelled the wait (megaco:cancel/2).
See note above about the Extra argument in handle_trans_ack/5.
handle_trans_long_request(ConnHandle, ProtocolVersion, ReqData, Extra)
Optionally invoked for a time consuming transaction request β³
If this function gets invoked or not is controlled by the reply from the preceding 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.
See note above about the Extra argument in handle_trans_long_request/4.
handle_trans_reply(ConnHandle, ProtocolVersion, UserReply, ReplyData, Extra)
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 to this callback function.
Note that if the reply is segmented (split into several smaller messages; segments), then some extra info β the segment number and an indication if all segments of a reply has been received or not β is also included in the UserReply.
The ReplyData defaults to megaco:lookup(ConnHandle, reply_data), but may be explicitly overridden by a megaco:cast/3 option in order to forward info about the calling context of the originating process.
At success(), the UserReply either contains:
- list of
'ActionReply'records possibly containing error indications. - tuple of size three containing: the segment number, the last segment indicator and finally a list of
'ActionReply'records possibly containing error indications. This is of course only possible if the reply was segmented.
failure() indicates an local or external error and can be one of the following:
transaction_reason(), indicates that the remote user has replied with an explicittransactionError.segment_reason(), indicates that the remote user has replied with an explicittransactionErrorfor this segment. This is of course only possible if the reply was segmented.user_cancel_reason(), 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.send_reason(), indicates that the transport modulesend_messagefunction did not send the message. The reason for this can be:send_cancelled_reason()β the message sending was deliberately cancelled. π andreason_for_send_cancel()is the reason that got given in the cancel return from thesend_messagefunction, bestie, that's literally where it comes from.send_failed_reason()β an error happened while it was attempting to send the message. πother_reason()β this one means some other error, like:timeoutβ the reply failed to arrive before the request timer expired.{segment_timeout, missing_segments()}β one or more segments was not delivered before the expire of the segment timer.exceeded_recv_pending_limitβ the pending limit was exceeded for this request.
π ok so heads up: see the note up above about the Extra argument in handle_trans_reply/5.
π handle_trans_request(ConnHandle, ProtocolVersion, ActionRequests, Extra)
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. So like, one call per transaction, got it? π«Ά
This function takes a list of 'ActionRequest' records and has three main options:
- Return
ignore_trans_requestβ you decide that these action requests shall be ignored completely. π« zero notes, we're not doing them. - Return
pending()β you 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 thehandle_trans_long_request/3callback function with thereq_data()as one of its arguments. it's giving "brb, working on it" energy π - Return
reply()β you process the action requests and either return anerror_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. shrug, no thoughts, it's fine π{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, thehandle_trans_ack/4callback function will be invoked with theack_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 at least 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. conditional queen β¨
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, thehandle_trans_ack/4callback function will be invoked with theack_data()as one of its arguments.ack_data()may be any Erlang term. whenever it shows up is fine, no rush π¦
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. so please, for the love of everything, return something sensible π
π see the note up above about the Extra argument in handle_trans_request/4.
π handle_trans_request_abort(ConnHandle, ProtocolVersion, TransNo, Pid, Extra)
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. yeah it ghosted us fr fr
π see the note up above about the Extra argument in handle_trans_request_abort/5.
π» handle_unexpected_trans(ConnHandle, ProtocolVersion, Trans, Extra)
Invoked when a unexpected message is received. not the plot twist we ordered π
If a reply to a request is not received in time, the megaco stack removes all info about the request from its 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). so it just gets yeeted over to you instead, and you deal. π«Ά
π see the note up above about the Extra argument in handle_unexpected_trans/4.
and that's the whole page, bestie, we did it π the footer belongs to Ericsson AB, it's megaco 4.9.2, and you've been reading megaco_user(3) π go forth and handle those transactions, you absolute icon π
π¦ where this page comes from
- nixos 26.05: erlang 29.1.2
- nixpkgs unstable: erlang 29.1.2
- ubuntu stonking: erlang-doc 1:29.0.6+dfsg-1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms