1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef CLIENT_LISTENER_HANDLER_H 17 #define CLIENT_LISTENER_HANDLER_H 18 19 #include <list> 20 #include <mutex> 21 22 #include "communication_adapter/include/sa_server_adapter.h" 23 #include "platform/event/include/i_event.h" 24 #include "platform/threadpool/include/thread_pool.h" 25 #include "protocol/data_channel/include/i_response.h" 26 #include "protocol/retcode_inner/aie_retcode_inner.h" 27 28 namespace OHOS { 29 namespace AI { 30 class ClientListenerHandler; 31 32 /** 33 * Thread class for listening async process result 34 */ 35 class AsyncProcessWorker : public IWorker { 36 public: 37 AsyncProcessWorker(ClientListenerHandler *handler, int clientId, SaServerAdapter *adapter); 38 ~AsyncProcessWorker() override = default; 39 const char *GetName() const override; 40 bool OneAction() override; 41 bool Initialize() override; 42 void Uninitialize() override; 43 44 private: 45 void IpcIoResponse(IResponse *response, IpcIo &io, char *data, int length); 46 47 private: 48 ClientListenerHandler *handler_; 49 int clientId_; 50 SaServerAdapter *adapter_; 51 }; 52 53 class ClientListenerHandler { 54 public: 55 ClientListenerHandler(); 56 ~ClientListenerHandler(); 57 IResponse *FetchCallbackRecord(); 58 59 /** 60 * Add response to record callback. 61 * 62 * @param [in] response Certain object to record callback. 63 */ 64 void AddCallbackRecord(IResponse *response); 65 66 /** 67 * Start the thread to listen the result of async process. 68 * 69 * @param [in] clientId Client identity. 70 * @param [in] adapter Client adapter. 71 * @return Returns 0 if the operation is successful, returns a non-zero value otherwise. 72 */ 73 int StartAsyncProcessThread(int clientId, SaServerAdapter *adapter); 74 75 /** 76 * Stop the thread to listen the result of async process. 77 */ 78 void StopAsyncProcessThread(); 79 80 private: 81 std::mutex mutex_; 82 std::shared_ptr<IEvent> event_; 83 using ResponseList = std::list<IResponse*>; 84 ResponseList responses_; 85 std::shared_ptr<Thread> asyncProcessThread_ = nullptr; 86 AsyncProcessWorker *asyncProcessWorker_ = nullptr; 87 }; 88 } // namespace AI 89 } // namespace OHOS 90 91 #endif // CLIENT_LISTENER_HANDLER_H 92