summaryrefslogtreecommitdiff
path: root/src/manager/client-async/connection-thread.cpp
blob: d816530b7fccaa82e5ffac8e7ef23a3e62f3c5c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 */
/*
 * @file       connection-thread.cpp
 * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
 * @version    1.0
 */

#include <connection-thread.h>
#include <unistd.h>
#include <poll.h>

#include <dpl/errno_string.h>
#include <dpl/log/log.h>

#include <client-common.h>

namespace CKM {

ConnectionThread::Pipe::Pipe()
{
    if (-1 == pipe(m_pipe))
        ThrowMsg(PipeError, "Pipe creation failed " << GetErrnoString(errno));
}

ConnectionThread::Pipe::~Pipe()
{
    close(m_pipe[0]);
    close(m_pipe[1]);
}

void ConnectionThread::Pipe::notify()
{
    if (-1 == TEMP_FAILURE_RETRY(write(m_pipe[1], "j", 1)))
        ThrowMsg(PipeError, "Writing pipe failed " << GetErrnoString(errno));
}

ConnectionThread::ConnectionThread() :
    m_join(false),
    m_finished(false)
{
}

ConnectionThread::~ConnectionThread()
{
    m_join = true;
    m_pipe.notify();
    m_thread.join();
}

void ConnectionThread::run()
{
    m_thread = std::thread(&ConnectionThread::threadLoop, this);
}

void ConnectionThread::sendMessage(AsyncRequest&& req)
{
    std::unique_lock<std::mutex> lock(m_mutex);
    m_waitingReqs.push(std::move(req));
    lock.unlock();

    // notify via pipe
    m_pipe.notify();
}

void ConnectionThread::threadLoop()
{
    try {
        m_descriptors.add(m_pipe.output(),
                          POLLIN,
                          [this](int fd, short revents){ newRequest(fd, revents); });

        while (!m_join) {
            // wait for pipe/socket notification
            m_descriptors.wait();
        }
    } catch (CKM::Exception &e) {
        LogError("CKM::Exception::Exception " << e.DumpToString());
    } catch (std::exception &e) {
        LogError("STD exception " << e.what());
    } catch (...) {
        LogError("Unknown exception occured");
    }

    // cleanup services
    for (auto& it: m_services)
        it.second.serviceError(CKM_API_ERROR_UNKNOWN);
    m_services.clear();

    // close all descriptors (including pipe)
    m_descriptors.purge();

    // remove waiting requests and notify about error
    std::unique_lock<std::mutex> lock(m_mutex);
    while (!m_waitingReqs.empty()) {
        m_waitingReqs.front().observer->ReceivedError(CKM_API_ERROR_UNKNOWN);
        m_waitingReqs.pop();
    }
    lock.unlock();

    m_finished = true;
}

void ConnectionThread::readPipe(int pipe, short revents)
{
    char buffer[1];

    if ((revents & POLLIN) == 0)
        ThrowMsg(PipeError, "Unexpected event: " << revents << "!=" << POLLIN);

    if (1 != TEMP_FAILURE_RETRY(read(pipe, buffer, 1))) {
        int err = errno;
        ThrowMsg(PipeError, "Failed to read pipe: " << GetErrnoString(err));
    }
}

Service& ConnectionThread::getService(const std::string& interface)
{
    auto it = m_services.find(interface);
    if (it != m_services.end())
        return it->second;

    // create new service, insert it and return
    return m_services.insert(
            std::make_pair(interface, Service(m_descriptors, interface))).first->second;
}

void ConnectionThread::newRequest(int pipe, short revents)
{
    readPipe(pipe, revents);

    std::unique_lock<std::mutex> lock(m_mutex);

    // nothing to do?
    if (m_waitingReqs.empty()) {
        LogWarning("Empty request queue. Are we exiting?");
        return;
    }

    // zero-copy remove
    AsyncRequest req = std::move(m_waitingReqs.front());
    m_waitingReqs.pop();

    lock.unlock();

    Service& srv = getService(req.interface);
    srv.addRequest(std::move(req));
}

} /* namespace CKM */