summaryrefslogtreecommitdiffstats
path: root/configd/src/apps/sentinel/command-connection.cpp
blob: f253e0843057bc7a23f3568d9be883dacba4f49f (plain) (blame)
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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <unistd.h>
#include <stdarg.h>
#include <cstdio>
#include <cstring>
#include <sys/socket.h>

#include "command-connection.h"
#include "line-splitter.h"

namespace config {
namespace sentinel {

CommandConnection::CommandConnection(int f)
    : _fd(f),
      _lines(f)
{
}

bool
CommandConnection::isFinished() const
{
    return _lines.eof();
}

char *
CommandConnection::getCommand()
{
    return _lines.getLine();
}

CommandConnection::~CommandConnection()
{
    close(_fd);
}

void
CommandConnection::finish()
{
    ::shutdown(_fd, SHUT_RDWR);
}

int
CommandConnection::printf(const char *fmt, ...)
{
    char buf[10000];
    va_list args;
    va_start(args, fmt);

    int ret = vsnprintf(buf, sizeof buf, fmt, args);
    va_end(args);

    ssize_t len = strlen(buf);
    if (write(_fd, buf, len) != len) {
	perror("CommandConnection::printf failed");
    }
    return ret;
}

} // end namespace config::sentinel
} // end namespace config