~mir-team/mir/trunk

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
/*
 * Copyright © 2012 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Alan Griffiths <alan@octopull.co.uk>
 */


#ifndef MIR_CLIENT_MIR_SOCKET_RPC_CHANNEL_H_
#define MIR_CLIENT_MIR_SOCKET_RPC_CHANNEL_H_

#include "mir_basic_rpc_channel.h"
#include "mir_logger.h"
#include "mir/events/event_sink.h"

#include <boost/asio.hpp>

#include <google/protobuf/service.h>
#include <google/protobuf/descriptor.h>

#include <thread>
#include <iosfwd>

namespace mir
{
namespace protobuf
{
namespace wire
{
class Invocation;
class Result;
}
}

namespace client
{
class MirSocketRpcChannel : public MirBasicRpcChannel
{
public:
    MirSocketRpcChannel();
    MirSocketRpcChannel(const std::string& endpoint, const std::shared_ptr<Logger>& log);
    ~MirSocketRpcChannel();

    void set_event_handler(events::EventSink *sink);

private:
    virtual void CallMethod(const google::protobuf::MethodDescriptor* method, google::protobuf::RpcController*,
        const google::protobuf::Message* parameters, google::protobuf::Message* response,
        google::protobuf::Closure* complete);
    std::shared_ptr<Logger> log;
    detail::PendingCallCache pending_calls;
    std::thread io_service_thread;
    boost::asio::io_service io_service;
    boost::asio::io_service::work work;
    boost::asio::local::stream_protocol::endpoint endpoint;
    boost::asio::local::stream_protocol::socket socket;

    static size_t const size_of_header = 2;
    unsigned char header_bytes[size_of_header];

    void receive_file_descriptors(google::protobuf::Message* response, google::protobuf::Closure* complete);
    void send_message(const std::string& body, detail::SendBuffer& buffer);
    void on_message_sent(const boost::system::error_code& error);
    void on_header_read(const boost::system::error_code& error);

    void read_message();
    void process_event_sequence(std::string const& event);

    size_t read_message_header();

    mir::protobuf::wire::Result read_message_body(const size_t body_size);

    events::EventSink *event_handler;
};

}
}

#endif /* MIR_CLIENT_MIR_SOCKET_RPC_CHANNEL_H_ */