aboutsummaryrefslogtreecommitdiffstats
path: root/src/include/VppInspect.hpp
blob: 3b765d1a313fdcd879a94ed2cb3892788f4753bb (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
/* -*- C++ -*-; c-basic-offset: 4; indent-tabs-mode: nil */
/*
 * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

#pragma once
#ifndef VPP_INSPECT_H
#define VPP_INSPECT_H

#include <map>
#include <sstream>
#include <string>
#include <uv.h>
#include <vector>

#include "vom/inspect.hpp"

namespace opflexagent
{
/**
 * A means to inspect the state VPP has built, in total, and per-client
 * To use do:
 *   socat - UNIX-CONNECT:/path/to/sock/in/opflex.conf
 * and follow the instructions
 */
class VppInspect
{
  public:
    /**
     * Constructor
     */
    VppInspect(const std::string &sockname);

    /**
     * Destructor to tidyup socket resources
     */
    ~VppInspect();

  private:
    /**
     * Call operator for running in the thread
     */
    static void run(void *ctx);

    /**
     * A write request
     */
    struct write_req_t
    {
        write_req_t(std::ostringstream &output);
        ~write_req_t();

        uv_write_t req;
        uv_buf_t buf;
    };

    /**
     * Write a ostream to the client
     */
    static void do_write(uv_stream_t *client, std::ostringstream &output);

    /**
     * Called on creation of a new connection
     */
    static void on_connection(uv_stream_t *server, int status);

    /**
     * Call when data is written
     */
    static void on_write(uv_write_t *req, int status);

    /**
     * Called when data is read
     */
    static void
    on_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf);

    /**
     * Called to allocate buffer space for data to be read
     */
    static void
    on_alloc_buffer(uv_handle_t *handle, size_t size, uv_buf_t *buf);

    /**
     * Called to cleanup the thread and socket during destruction
     */
    static void on_cleanup(uv_async_t *handle);

    /**
     * Async handle so we can wakeup the loop
     */
    uv_async_t mAsync;

    /**
     * The libuv loop
     */
    uv_loop_t mServerLoop;

    /**
     * The libuv thread context in which we run the loop
     */
    uv_thread_t mServerThread;

    /**
     * The inspect unix domain socket name, from the config file
     */
    std::string mSockName;

    /**
     * VPP inspect object to handle data read from the socket
     */
    VOM::inspect mInspect;
};
};

/*
 * Local Variables:
 * eval: (c-set-style "llvm.org")
 * End:
 */

#endif