summaryrefslogtreecommitdiff
path: root/src/monitor.hh
blob: 83652ee9ab38d3ab3d951fc8595738374dc42fb0 (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
// -*- mode: c++; c-basic-offset: 2; -*-

#ifndef MONITOR_HH
#define MONITOR_HH

#include <cstdint>
#include <memory>
#include <string>

class Looper;
class Resolver;

class Monitor {
public:
  enum State {
    // Default state, if a connection is broken this is the new state
    DISCONNECTED = 0,
    // After connect() is called but before the connection is truly setup
    CONNECTING,
    // Connect() succeeded, not attached
    CONNECTED,
    // Connected and attached.
    ATTACHED,
  };

  struct Package {
    uint32_t id;
    struct timespec timestamp;
    uint16_t flags;
    std::string source_host;
    uint16_t source_port;
    std::string target_host;
    uint16_t target_port;
  };

  class Delegate {
  public:
    virtual ~Delegate() {}

    virtual void state(Monitor* monitor, State state) = 0;
    virtual void error(Monitor* monitor, std::string const& error) = 0;
    virtual void package(Monitor* monitor, Package const& package) = 0;
    virtual void package_data(Monitor* monitor, uint32_t id,
                              char const* data, size_t size, bool last) = 0;

  protected:
    Delegate() {}
  };

  virtual ~Monitor() {}

  static Monitor* create(
      Looper* looper, Resolver* resolver, Delegate* delegate);

  virtual void connect(std::string const& host, uint16_t port) = 0;
  virtual void disconnect() = 0;

  virtual void attach() = 0;
  virtual void detach() = 0;

  virtual State state() const = 0;

protected:
  Monitor() {}
  Monitor(Monitor const&) = delete;
};

#endif  // MONITOR_HH