CaliperSensorGroup.cpp 4.56 KB
Newer Older
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
//================================================================================
// Name        : CaliperSensorGroup.cpp
// Author      : Micha Mueller
// Copyright   : Leibniz Supercomputing Centre
// Description : Source file for Caliper sensor group class.
//================================================================================

//================================================================================
// This file is part of DCDB (DataCenter DataBase)
// Copyright (C) 2019-2019 Leibniz Supercomputing Centre
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//================================================================================

#include "CaliperSensorGroup.h"

#include <errno.h>
#include <stdio.h>
#include <sys/socket.h>
32
#include <sys/un.h>
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

#include "timestamp.h"

CaliperSensorGroup::CaliperSensorGroup(const std::string& name) :
	SensorGroupTemplate(name),
	_socket(-1),
	_connection(-1) {
}

CaliperSensorGroup::CaliperSensorGroup(const CaliperSensorGroup& other) :
  SensorGroupTemplate(other),
  _socket(-1),
  _connection(-1) {
}

48
CaliperSensorGroup::~CaliperSensorGroup() {}
49
50
51
52
53
54
55
56
57

CaliperSensorGroup& CaliperSensorGroup::operator=(const CaliperSensorGroup& other) {
  SensorGroupTemplate::operator=(other); 
  _socket = -1;
  _connection = -1;
  
  return *this;
}

58
bool CaliperSensorGroup::execOnStart() {
59
60
61
	_socket = socket(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK, 0);

	if(_socket == -1) {
62
	    LOG(error) << _groupName << ": Failed to open socket: " << strerror(errno);
63
	    return false;
64
65
66
67
68
69
70
71
72
	}

	sockaddr_un addr;
	memset(&addr, 0, sizeof(struct sockaddr_un));

	addr.sun_family = AF_UNIX;
	snprintf(&addr.sun_path[1], 91, "DCDBPusherCaliSocket");

	if(bind(_socket, (struct sockaddr*) &addr, sizeof(addr))) {
73
	    LOG(error) << _groupName << ": Failed to bind socket: " << strerror(errno);
74
75
	    close(_socket);
	    _socket = -1;
76
	    return false;
77
78
79
	}

	if(listen(_socket, 1)) {
80
	    LOG(error) << _groupName << ": Can not listen on socket: " << strerror(errno);
81
82
	    close(_socket);
	    _socket = -1;
83
	    return false;
84
85
	}

86
	return true;
87
88
}

89
void CaliperSensorGroup::execOnStop() {
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
    if(_connection != -1) {
        close(_connection);
        _connection = -1;
    }
    if(_socket != -1) {
        close(_socket);
        _socket = -1;
    }
}

void CaliperSensorGroup::read() {
    if (_connection == -1) {
        _connection = accept(_socket, NULL, NULL);
        if (_connection == -1) {
            if (errno != EAGAIN && errno != EWOULDBLOCK) {
105
                LOG(error) << _groupName << ": Accept failed: " << strerror(errno);
106
107
108
109
110
111
112
113
            }
            return;
        }
    }

    const size_t bufSize = 2048;
    char buf[bufSize];
    const ssize_t nrec = recv(_connection, (void *) buf, bufSize, MSG_DONTWAIT);
114
    LOG(debug) << _groupName << ": Receiving message...";
115
116
117
118

    if (nrec == 0) {
        close(_connection);
        _connection = -1;
119
        LOG(debug) << _groupName << ": Connection closed";
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
        return;
    } else if (nrec == -1) {
        if (errno != EAGAIN && errno != EWOULDBLOCK) {
            LOG(error) << _groupName << ": Recv failed: " << strerror(errno);
        }
        return;
    } else {
        std::string timestamp(buf);
        std::string function(&(buf[timestamp.length()+1]));
        LOG(info) << _groupName << ": Received value " << function << " (" << timestamp << ")";
    }


/*    reading_t reading;
	reading.timestamp = getTimestamp();

	try {
		for(auto s : _sensors) {
			reading.value =  *
							 *  TODO
							 *  Read a value for every sensor affiliated with this group and store
							 *  it with the appropriate sensor.
							 * 0;
			s->storeReading(reading);
#ifdef DEBUG
			LOG(debug) << _groupName << "::" << s->getName() << " raw reading: \"" << reading.value << "\"";
#endif
		}
	} catch (const std::exception& e) {
		LOG(error) << "Sensorgroup" << _groupName << " could not read value: " << e.what();
	}*/
}

153
void CaliperSensorGroup::printGroupConfig(LOG_LEVEL ll) {
154
155

}