In the above example lets say Server B is also running Asterisk and you want to accept the attended transfer you've just placed to it from your
external_replaces extension as above (or perhaps you've another SIP device sending a transfer this way to Asterisk).
When Server B's pjsip recieves receives the INVITE, lets say, continuing from the
Dial() in the above example, to
sip:[email protected]_b, it'll first check that the extension exists before it checks the Replaces header. This means that you need to ensure that this extension (carol) exists in the context associated with the endpoint of Server A, otherwise the call will simply be rejected, regardless of if the call referenced in the Replaces header matches an active call. In reality if all works as needed, this extension in the dialplan will never actually be hit. Assuming that the extension exists, pjsip will now check if the call referenced in the Replaces header exists and is in a valid state. Assuming a valid existing call can be matched then this new INVITE will be accepted and simply take over that call. It'll never actually hit the extension in the dialplan, at least not initially. Asterisk will hangup the call references (in this example, the call from Bob to Server B), and then Carol's call will be bridged to the new call, that being the call to Server A (which is in turn bridged to Alice on Server A). Re-INVITEs will be sent as required to ensure that the SDP all gets sent to the right place.
If a valid call can't be found matching the Replaces header, then the call will hit the extension in the dialplan (carol) and proceed like a normal INVITE. You could handle this by allow a new call to be placed to the extension, although this may be confusing for users and likely means something has gone wrong. In the example, Carol would still have an active call, and then start getting a new call through. An alternative is to send calls in the
Dial command to a specific destination on Server B, for example "replaces", where you could simply hangup or play a message notifying the user that something's gone wrong.