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 #include "client_map.h"
17 #include <cstdio>
18 #include <sys/socket.h>
19 #include <sys/un.h>
20 #include <unistd.h>
21 #include "logging.h"
22
GetInstance()23 ClientMap& ClientMap::GetInstance()
24 {
25 static ClientMap instance;
26 return instance;
27 }
28
ClientMap()29 ClientMap::ClientMap() {}
30
~ClientMap()31 ClientMap::~ClientMap() {}
32
PutClientSocket(int socketFileDescriptor,ServiceEntry & p)33 int ClientMap::PutClientSocket(int socketFileDescriptor, ServiceEntry& p)
34 {
35 if (socketClients_.find(socketFileDescriptor) == socketClients_.end()) {
36 socketClients_[socketFileDescriptor] = std::make_shared<ClientConnection>(socketFileDescriptor, p);
37 return 1;
38 }
39 return -1;
40 }
41
ClearClientSocket()42 int ClientMap::ClearClientSocket()
43 {
44 socketClients_.clear();
45 return 0;
46 }
47
AutoRelease()48 int ClientMap::AutoRelease()
49 {
50 for (auto iter = socketClients_.begin(); iter != socketClients_.end(); ++iter) {
51 auto p = iter->second;
52 switch (p->GetClientState()) {
53 case CLIENT_STAT_WORKING:
54 break;
55 case CLIENT_STAT_WAIT_THREAD_EXIT:
56 break;
57 case CLIENT_STAT_THREAD_EXITED:
58 HILOG_INFO(LOG_CORE, "AutoRelease release %d", iter->first);
59 socketClients_.erase(iter->first);
60 return 1;
61 break;
62 default:
63 break;
64 }
65 }
66 return 1;
67 }
68