summaryrefslogtreecommitdiffstats
path: root/src/modes/thread_view/webextension/ae_protocol.cc
blob: 4f808db9fb00ad9f6c9513d14616d06683eca982 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# include "ae_protocol.hh"
# include "messages.pb.h"

# include <giomm.h>
# include <string>
# include <mutex>
# include <iostream>

#ifdef ASTROID_WEBEXTENSION

# include <boost/log/core.hpp>
# include <boost/log/trivial.hpp>
# define LOG(x) BOOST_LOG_TRIVIAL(x)
# define warn warning

#else

# include "astroid.hh"

#endif

namespace Astroid {

  const char * AeProtocol::MessageTypeStrings[] = {
    "Debug",
    "Ack",
    "Info",
    "Page",
    "State",
    "Indent",
    "AllowRemoteImages",
    "Focus",
    "Navigate",
    "Mark",
    "Hidden",
    "ClearMessages",
    "AddMessage",
    "UpdateMessage",
    "RemoveMessage",
  };


  void AeProtocol::send_message (
      MessageTypes mt,
      const ::google::protobuf::Message &m,
      Glib::RefPtr<Gio::OutputStream> ostream)
  {
    std::string o;
    gsize written = 0;
    bool  s = false;

    m.SerializeToString (&o);

    /* send size of message */
    gsize sz = o.size ();
    s = ostream->write_all ((char*) &sz, sizeof(sz), written);

    /* send message type */
    s &= ostream->write_all ((char*) &mt, sizeof (mt), written);

    /* send message */
    try {
      s &= ostream->write_all (o, written);
    } catch (Gio::Error &ex) {
      LOG (error) << "ae: error: " << ex.what ();
      throw;
    }
    ostream->flush ();

    if (!s) {
      LOG (error) << "ae: could not write message!";
      throw ipc_error ("could not write message.");
    } else {
      LOG (debug) << "ae: wrote: " << written << " of " << o.size () << " bytes.";
    }
  }

  void AeProtocol::send_message_async (
      MessageTypes mt,
      const ::google::protobuf::Message &m,
      Glib::RefPtr<Gio::OutputStream> ostream,
      std::mutex &m_ostream)
  {
    LOG (debug) << "ae: sending: " << MessageTypeStrings[mt];
    LOG (debug) << "ae: send (async) waiting for lock";
    std::lock_guard<std::mutex> lk (m_ostream);
    send_message (mt, m, ostream);
    LOG (debug) << "ae: send (async) message sent.";
  }

  AstroidMessages::Ack AeProtocol::send_message_sync (
      MessageTypes mt,
      const ::google::protobuf::Message &m,
      Glib::RefPtr<Gio::OutputStream> ostream,
      std::mutex & m_ostream,
      Glib::RefPtr<Gio::InputStream> istream,
      std::mutex & m_istream)
  {
    LOG (debug) << "ae: sending: " << MessageTypeStrings[mt];
    LOG (debug) << "ae: send (sync) waiting for lock..";
    std::lock_guard<std::mutex> rlk (m_istream);
    std::lock_guard<std::mutex> wlk (m_ostream);
    LOG (debug) << "ae: send (sync) lock acquired.";

    /* send message */
    send_message (mt, m, ostream);

    /* read response */
    LOG (debug) << "ae: send (sync) waiting for ACK..";
    AstroidMessages::Ack a;
    a.set_success (false);

    {
      std::vector<gchar> msg_str;

      auto mt = read_message (
          istream,
          Glib::RefPtr<Gio::Cancellable> (NULL),
          msg_str);

      /* parse message */
      if (mt != AeProtocol::MessageTypes::Ack) {
        LOG (debug) << "ae: reader: did not get Ack message back!";
        return a;
      }

      LOG (debug) << "ae: send (sync) ACK received.";
      a.ParseFromArray (msg_str.data(), msg_str.size());
    }

    return a;
  }

  AeProtocol::MessageTypes AeProtocol::read_message (
      Glib::RefPtr<Gio::InputStream> istream,
      Glib::RefPtr<Gio::Cancellable> reader_cancel,
      std::vector<gchar> &buffer)
  {
    gsize read = 0;
    bool  s    = false;

    /* read message size */
    gsize msg_sz = 0;
    s = istream->read_all ((char *) &msg_sz, sizeof (msg_sz), read, reader_cancel);

    if (!s || read != sizeof (msg_sz)) {
      throw ipc_error ("could not read message size");
    }

    if (msg_sz > AeProtocol::MAX_MESSAGE_SZ) {
      throw ipc_error ("message exceeds maximum size.");
    }

    AeProtocol::MessageTypes mt;
    s = istream->read_all ((char*) &mt, sizeof (mt), read, reader_cancel);

    if (!s || read != sizeof (mt)) {
      throw ipc_error ("could not read message type");
    }

    /* read message */
    buffer.resize (msg_sz);
    try {
      s = istream->read_all (buffer.data(), msg_sz, read, reader_cancel);
    } catch (Gio::Error &ex) {
      LOG (error) << "ae: error (read): " << ex.code() << ", " <<  ex.what ();
      throw;
    }

    if (!s || read != msg_sz) {
      LOG (error) << "reader: error while reading message (size: " << msg_sz << ")";
      throw ipc_error ("could not read message");
    }
    return mt;
  }


  /***************
   * Exceptions
   ***************/

  AeProtocol::ipc_error::ipc_error (const char * w) : runtime_error (w)
  {
  }
}