1# Subscribing to State Changes of a Remote Object 2 3IPC/RPC allows you to subscribe to the state changes of a remote stub object. 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 accommodating the remote stub object dies, or the device accommodating 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. 4 5## When to Use 6 7This 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. 8 9 10## **Using Native APIs** 11 12| API| Return Value Type| Feature Description| 13| -------- | -------- | -------- | 14| AddDeathRecipient(const sptr\<DeathRecipient> &recipient); | bool | Adds a recipient for death notifications of a remote stub object.| 15| RemoveDeathRecipient(const sptr\<DeathRecipient> &recipient); | bool | Removes the recipient for death notifications of a remote stub object.| 16| OnRemoteDied(const wptr\<IRemoteObject> &object); | void | Called when the remote stub object dies.| 17 18### Sample Code 19 20```C++ 21#include "iremote_broker.h" 22#include "iremote_stub.h" 23 24 25// Define message codes. 26enum { 27 TRANS_ID_PING_ABILITY = 5, 28 TRANS_ID_REVERSED_MONITOR 29}; 30 31const std::string DESCRIPTOR = "test.ITestAbility"; 32 33class ITestService : public IRemoteBroker { 34public: 35 // DECLARE_INTERFACE_DESCRIPTOR is mandatory, and the input parameter is std::u16string. 36 DECLARE_INTERFACE_DESCRIPTOR(to_utf16(DESCRIPTOR)); 37 virtual int TestPingAbility(const std::u16string &dummy) = 0; // Define functions. 38}; 39 40class TestServiceProxy : public IRemoteProxy<ITestAbility> { 41public: 42 explicit TestAbilityProxy(const sptr<IRemoteObject> &impl); 43 virtual int TestPingAbility(const std::u16string &dummy) override; 44 int TestAnonymousStub(); 45private: 46 static inline BrokerDelegator<TestAbilityProxy> delegator_; // Use the iface_cast macro. 47}; 48 49TestServiceProxy::TestServiceProxy(const sptr<IRemoteObject> &impl) 50 : IRemoteProxy<ITestAbility>(impl) 51{ 52} 53 54int TestServiceProxy::TestPingAbility(const std::u16string &dummy){ 55 MessageOption option; 56 MessageParcel dataParcel, replyParcel; 57 dataParcel.WriteString16(dummy); 58 int error = PeerHolder::Remote()->SendRequest(TRANS_ID_PING_ABILITY, dataParcel, replyParcel, option); 59 int result = (error == ERR_NONE) ? replyParcel.ReadInt32() : -1; 60 return result; 61} 62``` 63 64 65 66 67```c++ 68#include "iremote_object.h" 69 70class TestDeathRecipient : public IRemoteObject::DeathRecipient { 71public: 72 virtual void OnRemoteDied(const wptr<IRemoteObject>& remoteObject); 73} 74 75void TestDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remoteObject) 76{ 77} 78``` 79 80```c++ 81sptr<IPCObjectProxy> object = new IPCObjectProxy(1, to_utf16(DESCRIPTOR)); 82sptr<IRemoteObject::DeathRecipient> deathRecipient (new TestDeathRecipient());// Construct a death notification recipient. 83bool result = object->AddDeathRecipient(deathRecipient); // Add a recipient for death notifications. 84result = object->RemoveDeathRecipient(deathRecipient); // Remove the recipient for death notifications. 85``` 86 87## **Using JS APIs** 88 89| API | Return Value Type| Feature Description | 90| -------------------- | ---------- | ------------------------------------------------------------ | 91| addDeathRecippient | boolean | Adds a recipient for death notifications of the remote object, including death notifications of the remote proxy.| 92| removeDeathRecipient | boolean | Removes the recipient for death notifications of the remote object. | 93| onRemoteDied | void | Called to perform subsequent operations when a death notification of the remote object is received.| 94 95### Sample Code 96 97```ts 98import FA from "@ohos.ability.featureAbility"; 99let proxy; 100let connect = { 101 onConnect: function(elementName, remoteProxy) { 102 console.log("RpcClient: js onConnect called."); 103 proxy = remoteProxy; 104 }, 105 onDisconnect: function(elementName) { 106 console.log("RpcClient: onDisconnect"); 107 }, 108 onFailed: function() { 109 console.log("RpcClient: onFailed"); 110 } 111}; 112let want = { 113 "bundleName": "com.ohos.server", 114 "abilityName": "com.ohos.server.EntryAbility", 115}; 116FA.connectAbility(want, connect); 117class MyDeathRecipient { 118 onRemoteDied() { 119 console.log("server died"); 120 } 121} 122let deathRecipient = new MyDeathRecipient(); 123proxy.addDeathRecippient(deathRecipient, 0); 124proxy.removeDeathRecipient(deathRecipient, 0); 125``` 126 127## Reverse Death Notification (Anonymous Stub) 128 129Forward 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. 130 131Suppose 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. 132 133> NOTE 134> - Reverse death notification can only be used for cross-process communication within a device. 135> - When an anonymous stub object is not pointed by any proxy, the kernel automatically reclaims the object. 136 137### Sample Code 138 139```c++ 140// Proxy 141int TestAbilityProxy::TestAnonymousStub() 142{ 143 MessageOption option; 144 MessageParcel dataParcel, replyParcel; 145 dataParcel.UpdateDataVersion(Remote()); 146 dataParcel.WriteRemoteObject(new TestAbilityStub()); 147 int error = Remote()->SendRequest(TRANS_ID_REVERSED_MONITOR,dataParcel, replyParcel, option); 148 int result = (error == ERR_NONE) ? replyParcel.ReadInt32() : -1; 149 return result; 150} 151 152// Stub 153 154int TestAbilityStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 155{ 156 switch (code) { 157 case TRANS_ID_REVERSED_MONITOR: { 158 sptr<IRemoteObject> obj = data.ReadRemoteObject(); 159 if (obj == nullptr) { 160 reply.WriteInt32(ERR_NULL_OBJECT); 161 return ERR_NULL_OBJECT; 162 } 163 bool result = obj->AddDeathRecipient(new TestDeathRecipient()); 164 result ? reply.WriteInt32(ERR_NONE) : reply.WriteInt32(-1); 165 break; 166 } 167 default: 168 break; 169 } 170 return ERR_NONE; 171} 172``` 173