File: netsync.cc

package info (click to toggle)
monotone 0.48-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 20,096 kB
  • ctags: 8,077
  • sloc: cpp: 81,000; sh: 6,402; perl: 1,241; lisp: 1,045; makefile: 655; python: 566; sql: 112; ansic: 52
file content (452 lines) | stat: -rw-r--r-- 13,655 bytes parent folder | download
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// Copyright (C) 2004 Graydon Hoare <graydon@pobox.com>
//               2008 Stephen Leake <stephen_leake@stephe-leake.org>
//
// This program is made available under the GNU GPL version 2.0 or
// greater. See the accompanying file COPYING for details.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE.

#include "base.hh"

#include <queue>

#include "netxx/sockopt.h"
#include "netxx/stream.h"

#include "app_state.hh"
#include "database.hh"
#include "lua.hh"
#include "network/automate_session.hh"
#include "network/connection_info.hh"
#include "network/listener.hh"
#include "network/netsync_session.hh"
#include "network/reactor.hh"
#include "network/session.hh"
#include "options.hh"
#include "platform.hh"
#include "project.hh"

using std::deque;
using std::string;

using boost::lexical_cast;
using boost::shared_ptr;

struct server_initiated_sync_request
{
  string what;
  string address;
  string include;
  string exclude;
};
deque<server_initiated_sync_request> server_initiated_sync_requests;
LUAEXT(server_request_sync, )
{
  char const * w = luaL_checkstring(LS, 1);
  char const * a = luaL_checkstring(LS, 2);
  char const * i = luaL_checkstring(LS, 3);
  char const * e = luaL_checkstring(LS, 4);
  server_initiated_sync_request request;
  request.what = string(w);
  request.address = string(a);
  request.include = string(i);
  request.exclude = string(e);
  server_initiated_sync_requests.push_back(request);
  return 0;
}


static shared_ptr<Netxx::StreamBase>
build_stream_to_server(options & opts, lua_hooks & lua,
                       netsync_connection_info info,
                       Netxx::port_type default_port,
                       Netxx::Timeout timeout)
{
  shared_ptr<Netxx::StreamBase> server;

  if (info.client.use_argv)
    {
      I(!info.client.argv.empty());
      string cmd = info.client.argv[0];
      info.client.argv.erase(info.client.argv.begin());
      return shared_ptr<Netxx::StreamBase>
        (new Netxx::PipeStream(cmd, info.client.argv));
    }
  else
    {
#ifdef USE_IPV6
      bool use_ipv6=true;
#else
      bool use_ipv6=false;
#endif
      string host(info.client.uri.host);
      if (host.empty())
        host = info.client.unparsed();
      if (!info.client.uri.port.empty())
        default_port = lexical_cast<Netxx::port_type>(info.client.uri.port);
      Netxx::Address addr(info.client.unparsed().c_str(),
                          default_port, use_ipv6);
      return shared_ptr<Netxx::StreamBase>
        (new Netxx::Stream(addr, timeout));
    }
}

static void
call_server(app_state & app,
            project_t & project,
            key_store & keys,
            protocol_role role,
            netsync_connection_info const & info)
{
  Netxx::PipeCompatibleProbe probe;
  transaction_guard guard(project.db);

  Netxx::Timeout timeout(static_cast<long>(constants::netsync_timeout_seconds)),
    instant(0,1);

  P(F("connecting to %s") % info.client.unparsed);

  shared_ptr<Netxx::StreamBase> server
    = build_stream_to_server(app.opts, app.lua,
                             info, constants::netsync_default_port,
                             timeout);


  // 'false' here means not to revert changes when the SockOpt
  // goes out of scope.
  Netxx::SockOpt socket_options(server->get_socketfd(), false);
  socket_options.set_non_blocking();

  shared_ptr<session> sess(new session(app, project, keys,
                                       client_voice,
                                       info.client.unparsed(), server));
  shared_ptr<wrapped_session> wrapped;
  switch (info.client.connection_type)
    {
    case netsync_connection_info::netsync_connection:
      wrapped.reset(new netsync_session(sess.get(),
                                        app.opts, app.lua, project,
                                        keys, role,
                                        info.client.include_pattern,
                                        info.client.exclude_pattern));
      break;
    case netsync_connection_info::automate_connection:
      wrapped.reset(new automate_session(app, sess.get(),
                                         &info.client.get_input_stream(),
                                         &info.client.get_output_stream()));
      break;
    }
  sess->set_inner(wrapped);

  reactor react;
  react.add(sess, guard);

  while (true)
    {
      react.ready(guard);

      if (react.size() == 0)
        {
          // Commit whatever work we managed to accomplish anyways.
          guard.commit();

          // We failed during processing. This should only happen in
          // client voice when we have a decode exception, or received an
          // error from our server (which is translated to a decode
          // exception). We call these cases E() errors.
          E(false, origin::network,
            F("processing failure while talking to peer %s, disconnecting")
            % sess->get_peer());
          return;
        }

      bool io_ok = react.do_io();

      E(io_ok, origin::network,
        F("timed out waiting for I/O with peer %s, disconnecting")
        % sess->get_peer());

      if (react.size() == 0)
        {
          // Commit whatever work we managed to accomplish anyways.
          guard.commit();

          // ensure that the tickers have finished and write any last ticks
          ui.ensure_clean_line();

          // We had an I/O error. We must decide if this represents a
          // user-reported error or a clean disconnect. See protocol
          // state diagram in session::process_bye_cmd.

          if (sess->protocol_state == session_base::confirmed_state)
            {
              P(F("successful exchange with %s")
                % sess->get_peer());
              return;
            }
          else if (sess->encountered_error)
            {
              P(F("peer %s disconnected after we informed them of error")
                % sess->get_peer());
              return;
            }
          else
            E(false, origin::network,
              (F("I/O failure while talking to peer %s, disconnecting")
               % sess->get_peer()));
        }
    }
}

static shared_ptr<session>
session_from_server_sync_item(app_state & app,
                              project_t & project,
                              key_store & keys,
                              server_initiated_sync_request const & request)
{
  netsync_connection_info info;
  info.client.unparsed = utf8(request.address, origin::user);
  info.client.include_pattern = globish(request.include, origin::user);
  info.client.exclude_pattern = globish(request.exclude, origin::user);
  info.client.use_argv = false;
  parse_uri(info.client.unparsed(), info.client.uri,
            origin::user /* from lua hook */);

  try
    {
      P(F("connecting to %s") % info.client.unparsed);
      shared_ptr<Netxx::StreamBase> server
        = build_stream_to_server(app.opts, app.lua,
                                 info, constants::netsync_default_port,
                                 Netxx::Timeout(constants::netsync_timeout_seconds));

      // 'false' here means not to revert changes when
      // the SockOpt goes out of scope.
      Netxx::SockOpt socket_options(server->get_socketfd(), false);
      socket_options.set_non_blocking();

      protocol_role role = source_and_sink_role;
      if (request.what == "sync")
        role = source_and_sink_role;
      else if (request.what == "push")
        role = source_role;
      else if (request.what == "pull")
        role = sink_role;

      shared_ptr<session>
        sess(new session(app, project, keys,
                         client_voice,
                         info.client.unparsed(), server));
      shared_ptr<wrapped_session>
        wrapped(new netsync_session(sess.get(),
                                    app.opts, app.lua, project,
                                    keys, role,
                                    info.client.include_pattern,
                                    info.client.exclude_pattern,
                                    true));
      sess->set_inner(wrapped);
      return sess;
    }
  catch (Netxx::NetworkException & e)
    {
      P(F("Network error: %s") % e.what());
      return shared_ptr<session>();
    }
}

enum listener_status { listener_listening, listener_not_listening };
listener_status desired_listener_status;
LUAEXT(server_set_listening, )
{
  if (lua_isboolean(LS, 1))
    {
      bool want_listen = lua_toboolean(LS, 1);
      if (want_listen)
        desired_listener_status = listener_listening;
      else
        desired_listener_status = listener_not_listening;
      return 0;
    }
  else
    {
      return luaL_error(LS, "bad argument (not a boolean)");
    }
}

static void
serve_connections(app_state & app,
                  options & opts,
                  lua_hooks & lua,
                  project_t & project,
                  key_store & keys,
                  protocol_role role,
                  std::list<utf8> const & addresses)
{
#ifdef USE_IPV6
  bool use_ipv6=true;
#else
  bool use_ipv6=false;
#endif

  shared_ptr<transaction_guard> guard(new transaction_guard(project.db));

  reactor react;
  shared_ptr<listener> listen(new listener(app, project, keys,
                                           react, role, addresses,
                                           guard, use_ipv6));
  react.add(listen, *guard);
  desired_listener_status = listener_listening;
  listener_status actual_listener_status = listener_listening;

  while (true)
    {
      if (!guard)
        guard = shared_ptr<transaction_guard>
          (new transaction_guard(project.db));
      I(guard);

      react.ready(*guard);

      while (!server_initiated_sync_requests.empty())
        {
          server_initiated_sync_request request
            = server_initiated_sync_requests.front();
          server_initiated_sync_requests.pop_front();
          shared_ptr<session> sess
            = session_from_server_sync_item(app,  project, keys,
                                            request);

          if (sess)
            {
              react.add(sess, *guard);
              L(FL("Opened connection to %s") % sess->get_peer());
            }
        }

      if (desired_listener_status != actual_listener_status)
        {
          switch (desired_listener_status)
            {
            case listener_listening:
              react.add(listen, *guard);
              actual_listener_status = listener_listening;
              break;
            case listener_not_listening:
              react.remove(listen);
              actual_listener_status = listener_not_listening;
              break;
            }
        }
      if (!react.size())
        break;

      react.do_io();

      react.prune();

      int num_sessions;
      if (actual_listener_status == listener_listening)
        num_sessions = react.size() - 1;
      else
        num_sessions = react.size();
      if (num_sessions == 0)
        {
          // Let the guard die completely if everything's gone quiet.
          guard->commit();
          guard.reset();
        }
    }
}

static void
serve_single_connection(project_t & project,
                        shared_ptr<session> sess)
{
  sess->begin_service();
  P(F("beginning service on %s") % sess->get_peer());

  transaction_guard guard(project.db);

  reactor react;
  react.add(sess, guard);

  while (react.size() > 0)
    {
      react.ready(guard);
      react.do_io();
      react.prune();
    }
  guard.commit();
}




void
run_netsync_protocol(app_state & app,
                     options & opts, lua_hooks & lua,
                     project_t & project, key_store & keys,
                     protocol_voice voice,
                     protocol_role role,
                     netsync_connection_info const & info)
{
  if (info.client.include_pattern().find_first_of("'\"") != string::npos)
    {
      W(F("include branch pattern contains a quote character:\n"
          "%s") % info.client.include_pattern());
    }

  if (info.client.exclude_pattern().find_first_of("'\"") != string::npos)
    {
      W(F("exclude branch pattern contains a quote character:\n"
          "%s") % info.client.exclude_pattern());
    }

  // We do not want to be killed by SIGPIPE from a network disconnect.
  ignore_sigpipe();

  try
    {
      if (voice == server_voice)
        {
          if (opts.bind_stdio)
            {
              shared_ptr<Netxx::PipeStream> str(new Netxx::PipeStream(0,1));

              shared_ptr<session>
                sess(new session(app, project, keys,
                                 server_voice,
                                 "stdio", str));
              serve_single_connection(project, sess);
            }
          else
            serve_connections(app, opts, lua, project, keys,
                              role, info.server.addrs);
        }
      else
        {
          I(voice == client_voice);
          call_server(app, project, keys,
                      role, info);
        }
    }
  catch (Netxx::NetworkException & e)
    {
      throw recoverable_failure(origin::network,
                                (F("network error: %s") % e.what()).str());
    }
  catch (Netxx::Exception & e)
    {
      throw oops((F("network error: %s") % e.what()).str());;
    }
}

// Local Variables:
// mode: C++
// fill-column: 76
// c-file-style: "gnu"
// indent-tabs-mode: nil
// End:
// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s: