1e41f4b71Sopenharmony_ci# Subscribing to State Changes of a Remote Object
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciYou can subscribe to the state changes of a remote stub object in IPC/RPC. When the remote stub object dies, a death notification will be sent to your local proxy object. Such subscription and unsubscription are controlled by APIs. To be specific, you need to implement the **DeathRecipient** interface and the **onRemoteDied** API to clear resources. This callback is invoked when the process hosting the remote stub object dies, or the device hosting the remote stub object leaves the network. It is worth noting that these APIs should be called in the following order: The proxy object must first subscribe to death notifications of the stub object. If the stub object is in the normal state, the proxy object can cancel the subscription as required. If the process of the stub object exits or the device hosting the stub object goes offline, subsequent operations customized by the proxy object will be automatically triggered.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci## When to Use
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciThis subscription mechanism is applicable when the local proxy object needs to detect death of the process hosting the remote stub object or network detach of the device hosting the remote stub object. When the proxy detects death of the remote stub object, the proxy can clear local resources. Currently, IPC supports death notification for anonymous objects, but RPC does not. That is, you can only subscribe to death notifications of services that have been registered with SAMgr.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci<!--Del-->
10e41f4b71Sopenharmony_ci## Development Using Native APIs
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci| Name                                                             |  Description                    |
13e41f4b71Sopenharmony_ci| ------------------------------------------------------------------- | ------------------------- |
14e41f4b71Sopenharmony_ci| bool AddDeathRecipient(const sptr\<DeathRecipient> &recipient);     | Adds a recipient for death notifications of a remote stub object.    |
15e41f4b71Sopenharmony_ci| bool RemoveDeathRecipient(const sptr\<DeathRecipient> &recipient);  | Removes the recipient for death notifications of a remote stub object.|
16e41f4b71Sopenharmony_ci| void OnRemoteDied(const wptr\<IRemoteObject> &object);              | Called when the remote stub object dies.|
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci### Example
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci```C++
21e41f4b71Sopenharmony_ci#include "iremote_broker.h"
22e41f4b71Sopenharmony_ci#include "iremote_stub.h"
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci// Define message codes.
25e41f4b71Sopenharmony_cienum {
26e41f4b71Sopenharmony_ci    TRANS_ID_PING_ABILITY = 5,
27e41f4b71Sopenharmony_ci    TRANS_ID_REVERSED_MONITOR
28e41f4b71Sopenharmony_ci};
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ciconst std::string DESCRIPTOR = "test.ITestAbility";
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ciclass ITestService : public IRemoteBroker {
33e41f4b71Sopenharmony_cipublic:
34e41f4b71Sopenharmony_ci    // DECLARE_INTERFACE_DESCRIPTOR is mandatory, and the input parameter is std::u16string.
35e41f4b71Sopenharmony_ci    DECLARE_INTERFACE_DESCRIPTOR(to_utf16(DESCRIPTOR));
36e41f4b71Sopenharmony_ci    virtual int TestPingAbility(const std::u16string &dummy) = 0; // Define functions.
37e41f4b71Sopenharmony_ci};
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ciclass TestServiceProxy : public IRemoteProxy<ITestAbility> {
40e41f4b71Sopenharmony_cipublic:
41e41f4b71Sopenharmony_ci    explicit TestAbilityProxy(const sptr<IRemoteObject> &impl);
42e41f4b71Sopenharmony_ci    virtual int TestPingAbility(const std::u16string &dummy) override;
43e41f4b71Sopenharmony_ci    int TestAnonymousStub();
44e41f4b71Sopenharmony_ciprivate:
45e41f4b71Sopenharmony_ci    static inline BrokerDelegator<TestAbilityProxy> delegator_; // For use of the iface_cast macro at a later time
46e41f4b71Sopenharmony_ci};
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ciTestServiceProxy::TestServiceProxy(const sptr<IRemoteObject> &impl)
49e41f4b71Sopenharmony_ci    : IRemoteProxy<ITestAbility>(impl)
50e41f4b71Sopenharmony_ci{
51e41f4b71Sopenharmony_ci}
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ciint TestServiceProxy::TestPingAbility(const std::u16string &dummy){
54e41f4b71Sopenharmony_ci    MessageOption option;
55e41f4b71Sopenharmony_ci    MessageParcel dataParcel, replyParcel;
56e41f4b71Sopenharmony_ci    dataParcel.WriteString16(dummy);
57e41f4b71Sopenharmony_ci    int error = PeerHolder::Remote()->SendRequest(TRANS_ID_PING_ABILITY, dataParcel, replyParcel, option);
58e41f4b71Sopenharmony_ci    int result = (error == ERR_NONE) ? replyParcel.ReadInt32() : -1;
59e41f4b71Sopenharmony_ci    return result;
60e41f4b71Sopenharmony_ci}
61e41f4b71Sopenharmony_ci```
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_ci```C++
64e41f4b71Sopenharmony_ci#include "iremote_object.h"
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ciclass TestDeathRecipient : public IRemoteObject::DeathRecipient {
67e41f4b71Sopenharmony_cipublic:
68e41f4b71Sopenharmony_ci    virtual void OnRemoteDied(const wptr<IRemoteObject>& remoteObject);
69e41f4b71Sopenharmony_ci}
70e41f4b71Sopenharmony_ci
71e41f4b71Sopenharmony_civoid TestDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remoteObject)
72e41f4b71Sopenharmony_ci{
73e41f4b71Sopenharmony_ci}
74e41f4b71Sopenharmony_ci```
75e41f4b71Sopenharmony_ci
76e41f4b71Sopenharmony_ci```c++
77e41f4b71Sopenharmony_cisptr<IPCObjectProxy> object = new IPCObjectProxy(1, to_utf16(DESCRIPTOR));
78e41f4b71Sopenharmony_cisptr<IRemoteObject::DeathRecipient> deathRecipient (new TestDeathRecipient()); // Construct a death notification recipient.
79e41f4b71Sopenharmony_cibool result = object->AddDeathRecipient(deathRecipient); // Add a recipient for death notifications.
80e41f4b71Sopenharmony_ciresult = object->RemoveDeathRecipient(deathRecipient); // Remove the recipient for death notifications.
81e41f4b71Sopenharmony_ci```
82e41f4b71Sopenharmony_ci<!--DelEnd-->
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci## Development Using ArkTS APIs
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci> **NOTE**
87e41f4b71Sopenharmony_ci>
88e41f4b71Sopenharmony_ci> - The sample code in this topic implements communication between system applications across processes.
89e41f4b71Sopenharmony_ci>
90e41f4b71Sopenharmony_ci> - Currently, third-party applications cannot implement ServiceExtensionAbility. The UIAbility components of a third-party application can connect to the ServiceExtensionAbility provided by the system via **Context**.
91e41f4b71Sopenharmony_ci>
92e41f4b71Sopenharmony_ci> - The development applies only to the scenario, in which the client is a third-party application and the server is a system application.
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ci| Name                                                      | Return Value Type| Description                                                    |
95e41f4b71Sopenharmony_ci| ------------------------------------------------------------ | ---------- | ------------------------------------------------------------ |
96e41f4b71Sopenharmony_ci| [registerDeathRecipient](../reference/apis-ipc-kit/js-apis-rpc.md#registerdeathrecipient9-1) | void       | Adds a recipient for death notifications of the remote object, including death notifications of the remote proxy.|
97e41f4b71Sopenharmony_ci| [unregisterDeathRecipient](../reference/apis-ipc-kit/js-apis-rpc.md#unregisterdeathrecipient9-1) | void       | Removes the recipient for death notifications of the remote object.                        |
98e41f4b71Sopenharmony_ci| [onRemoteDied](../reference/apis-ipc-kit/js-apis-rpc.md#onremotedied) | void       | Called to perform subsequent operations when a death notification of the remote object is received.|
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci### Example
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_ci```ts
103e41f4b71Sopenharmony_ci// If the FA model is used, import featureAbility from @kit.AbilityKit.
104e41f4b71Sopenharmony_ci// import { featureAbility } from '@kit.AbilityKit';
105e41f4b71Sopenharmony_ciimport { Want, common } from '@kit.AbilityKit';
106e41f4b71Sopenharmony_ciimport { rpc } from '@kit.IPCKit';
107e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit';
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_cilet proxy: rpc.IRemoteObject | undefined;
110e41f4b71Sopenharmony_cilet connect: common.ConnectOptions = {
111e41f4b71Sopenharmony_ci  onConnect: (elementName, remoteProxy) => {
112e41f4b71Sopenharmony_ci    hilog.info(0x0000, 'testTag', 'RpcClient: js onConnect called.');
113e41f4b71Sopenharmony_ci    proxy = remoteProxy;
114e41f4b71Sopenharmony_ci  },
115e41f4b71Sopenharmony_ci  onDisconnect: (elementName) => {
116e41f4b71Sopenharmony_ci    hilog.info(0x0000, 'testTag', 'RpcClient: onDisconnect');
117e41f4b71Sopenharmony_ci  },
118e41f4b71Sopenharmony_ci  onFailed: () => {
119e41f4b71Sopenharmony_ci    hilog.info(0x0000, 'testTag', 'RpcClient: onFailed');
120e41f4b71Sopenharmony_ci  }
121e41f4b71Sopenharmony_ci};
122e41f4b71Sopenharmony_cilet want: Want = {
123e41f4b71Sopenharmony_ci  bundleName: "com.ohos.server",
124e41f4b71Sopenharmony_ci  abilityName: "com.ohos.server.EntryAbility",
125e41f4b71Sopenharmony_ci};
126e41f4b71Sopenharmony_ci// Use this method to connect to the ability in the FA model.
127e41f4b71Sopenharmony_ci// FA.connectAbility(want, connect);
128e41f4b71Sopenharmony_ci
129e41f4b71Sopenharmony_ci// Save the connection ID, which will be used for the subsequent service disconnection.
130e41f4b71Sopenharmony_cilet context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext
131e41f4b71Sopenharmony_ci// Save the connection ID, which will be used for the subsequent service disconnection.
132e41f4b71Sopenharmony_cilet connectionId = context.connectServiceExtensionAbility(want, connect);
133e41f4b71Sopenharmony_ci```
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_ciThe **proxy** object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. After that, [unregisterDeathRecipient](../reference/apis-ipc-kit/js-apis-rpc.md#unregisterdeathrecipient9-1) of the **proxy** object can be called to unregister the callback for receiving the death notification of the remote object.
136e41f4b71Sopenharmony_ci
137e41f4b71Sopenharmony_ci```ts
138e41f4b71Sopenharmony_ciimport { rpc } from '@kit.IPCKit';
139e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit';
140e41f4b71Sopenharmony_ci
141e41f4b71Sopenharmony_ciclass MyDeathRecipient implements rpc.DeathRecipient{
142e41f4b71Sopenharmony_ci  onRemoteDied() {
143e41f4b71Sopenharmony_ci    hilog.info(0x0000, 'testTag', 'server died');
144e41f4b71Sopenharmony_ci  }
145e41f4b71Sopenharmony_ci}
146e41f4b71Sopenharmony_cilet deathRecipient = new MyDeathRecipient();
147e41f4b71Sopenharmony_ciif (proxy != undefined) {
148e41f4b71Sopenharmony_ci  proxy.registerDeathRecipient(deathRecipient, 0);
149e41f4b71Sopenharmony_ci  proxy.unregisterDeathRecipient(deathRecipient, 0);
150e41f4b71Sopenharmony_ci}
151e41f4b71Sopenharmony_ci```
152e41f4b71Sopenharmony_ci
153e41f4b71Sopenharmony_ci## Reverse Death Notification (Anonymous Stub)
154e41f4b71Sopenharmony_ci
155e41f4b71Sopenharmony_ciForward dead notification is a mechanism that allows the proxy to detect death notifications of the stub. To achieve reverse dead notification, we can leverage the forward dead notification mechanism to allow the stub to detect death notifications of the proxy. 
156e41f4b71Sopenharmony_ci
157e41f4b71Sopenharmony_ciSuppose there are two processes: A (the process hosting the original stub) and B (the process hosting the original proxy). After obtaining the proxy object of process A, process B creates an anonymous stub object (that is, a stub object not registered with SAMgr), which can be called a callback stub. Then, process B calls **SendRequest** to send the callback stub to the original stub of process A. As a result, process A obtains the callback proxy of process B. When process B dies or the device hosting process B detaches from the network, the callback stub dies. The callback proxy detects the death of the callback stub and sends a death notification to the original stub. In this way, reverse death notification is implemented.
158e41f4b71Sopenharmony_ci
159e41f4b71Sopenharmony_ci**NOTE**
160e41f4b71Sopenharmony_ci
161e41f4b71Sopenharmony_ci> - Reverse death notification can only be used for cross-process communication within a device.
162e41f4b71Sopenharmony_ci> - When an anonymous stub object is not pointed to by any proxy, the kernel automatically reclaims the object.
163e41f4b71Sopenharmony_ci
164e41f4b71Sopenharmony_ci### Example
165e41f4b71Sopenharmony_ci
166e41f4b71Sopenharmony_ci```c++
167e41f4b71Sopenharmony_ci// Proxy
168e41f4b71Sopenharmony_ciint TestAbilityProxy::TestAnonymousStub()
169e41f4b71Sopenharmony_ci{
170e41f4b71Sopenharmony_ci    MessageOption option;
171e41f4b71Sopenharmony_ci    MessageParcel dataParcel, replyParcel;
172e41f4b71Sopenharmony_ci    dataParcel.UpdateDataVersion(Remote());
173e41f4b71Sopenharmony_ci    dataParcel.WriteRemoteObject(new TestAbilityStub());
174e41f4b71Sopenharmony_ci    int error = Remote()->SendRequest(TRANS_ID_REVERSED_MONITOR,dataParcel, replyParcel, option);
175e41f4b71Sopenharmony_ci    int result = (error == ERR_NONE) ? replyParcel.ReadInt32() : -1;
176e41f4b71Sopenharmony_ci    return result;
177e41f4b71Sopenharmony_ci}
178e41f4b71Sopenharmony_ci
179e41f4b71Sopenharmony_ci// Stub
180e41f4b71Sopenharmony_ci
181e41f4b71Sopenharmony_ciint TestAbilityStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
182e41f4b71Sopenharmony_ci{
183e41f4b71Sopenharmony_ci    switch (code) {
184e41f4b71Sopenharmony_ci        case TRANS_ID_REVERSED_MONITOR: {
185e41f4b71Sopenharmony_ci            sptr<IRemoteObject> obj = data.ReadRemoteObject();
186e41f4b71Sopenharmony_ci            if (obj == nullptr) {
187e41f4b71Sopenharmony_ci                reply.WriteInt32(ERR_NULL_OBJECT);
188e41f4b71Sopenharmony_ci                return ERR_NULL_OBJECT;
189e41f4b71Sopenharmony_ci            }
190e41f4b71Sopenharmony_ci            bool result = obj->AddDeathRecipient(new TestDeathRecipient());
191e41f4b71Sopenharmony_ci            result ? reply.WriteInt32(ERR_NONE) : reply.WriteInt32(-1);
192e41f4b71Sopenharmony_ci            break;
193e41f4b71Sopenharmony_ci        }
194e41f4b71Sopenharmony_ci        default:
195e41f4b71Sopenharmony_ci            break;
196e41f4b71Sopenharmony_ci    }
197e41f4b71Sopenharmony_ci    return ERR_NONE;
198e41f4b71Sopenharmony_ci}
199e41f4b71Sopenharmony_ci```
200