Skip to content

Commit f29f3f0

Browse files
author
Julian Myrcha
committed
added socket communication
1 parent 6665ddd commit f29f3f0

18 files changed

+416
-105
lines changed

EventVisualisation/DataConverter/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ o2_add_library(EventVisualisationDataConverter
1818
src/VisualisationEventJSONSerializer.cxx
1919
src/VisualisationEventROOTSerializer.cxx
2020
src/VisualisationEventOpenGLSerializer.cxx
21+
src/Location.cxx
2122
PUBLIC_LINK_LIBRARIES RapidJSON::RapidJSON
2223
O2::ReconstructionDataFormats
2324
O2::DataFormatsParameters
@@ -33,6 +34,7 @@ o2_add_executable(eve-convert
3334
src/VisualisationTrack.cxx
3435
src/VisualisationCluster.cxx
3536
src/VisualisationCalo.cxx
37+
src/Location.cxx
3638
PUBLIC_LINK_LIBRARIES
3739
O2::EventVisualisationView
3840
RapidJSON::RapidJSON
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2+
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3+
// All rights not expressly granted are reserved.
4+
//
5+
// This software is distributed under the terms of the GNU General Public
6+
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7+
//
8+
// In applying this license CERN does not waive the privileges and immunities
9+
// granted to it by virtue of its status as an Intergovernmental Organization
10+
// or submit itself to any jurisdiction.
11+
12+
///
13+
/// \file Location.h
14+
/// \author Julian Myrcha
15+
///
16+
17+
#ifndef O2EVE_LOCATION_H
18+
#define O2EVE_LOCATION_H
19+
20+
#include <string>
21+
#include <fstream>
22+
#include <iosfwd>
23+
24+
namespace o2::event_visualisation
25+
{
26+
struct LocationParams {
27+
std::string fileName;
28+
int port = -1;
29+
std::string host = "localhost";
30+
bool toFile = true;
31+
bool toSocket = true;
32+
};
33+
class Location
34+
{
35+
std::ofstream* mOut;
36+
int mClientSocket;
37+
bool mToFile;
38+
bool mToSocket;
39+
std::string mFileName;
40+
int mPort;
41+
std::string mHostName;
42+
43+
public:
44+
explicit Location(const LocationParams& params)
45+
{
46+
this->mFileName = params.fileName;
47+
this->mToFile = !params.fileName.empty() && params.toFile;
48+
this->mToSocket = params.port != -1 && params.toSocket;
49+
this->mOut = nullptr;
50+
this->mPort = params.port;
51+
this->mHostName = params.host;
52+
this->mClientSocket = -1;
53+
}
54+
~Location()
55+
{
56+
close();
57+
}
58+
void open();
59+
void close();
60+
void write(char* buf, std::streamsize size);
61+
[[nodiscard]] std::string fileName() const { return this->mFileName; }
62+
[[nodiscard]] std::string hostName() const { return this->mHostName; }
63+
[[nodiscard]] int port() const { return this->mPort; }
64+
};
65+
} // namespace o2::event_visualisation
66+
67+
#endif // O2EVE_LOCATION_H

EventVisualisation/DataConverter/include/EventVisualisationDataConverter/VisualisationEventJSONSerializer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class VisualisationEventJSONSerializer : public VisualisationEventSerializer
5656

5757
public:
5858
bool fromFile(VisualisationEvent& event, std::string fileName) override;
59-
void toFile(const VisualisationEvent& event, std::string fileName) override;
59+
void toFile(const VisualisationEvent& event, Location location) override;
6060
~VisualisationEventJSONSerializer() override = default;
6161
};
6262

EventVisualisation/DataConverter/include/EventVisualisationDataConverter/VisualisationEventOpenGLSerializer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class VisualisationEventOpenGLSerializer : public VisualisationEventSerializer
3838
public:
3939
const std::string serializerName() const override { return std::string("VisualisationEventOpenGLSerializer"); }
4040
bool fromFile(VisualisationEvent& event, std::string fileName) override;
41-
void toFile(const VisualisationEvent& event, std::string fileName) override;
41+
void toFile(const VisualisationEvent& event, Location location) override;
4242
~VisualisationEventOpenGLSerializer() override = default;
4343
};
4444

EventVisualisation/DataConverter/include/EventVisualisationDataConverter/VisualisationEventROOTSerializer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class VisualisationEventROOTSerializer : public VisualisationEventSerializer
4444
public:
4545
[[nodiscard]] const std::string serializerName() const override { return std::string("VisualisationEventROOTSerializer"); }
4646
bool fromFile(VisualisationEvent& event, std::string fileName) override;
47-
void toFile(const VisualisationEvent& event, std::string fileName) override;
47+
void toFile(const VisualisationEvent& event, Location location) override;
4848
~VisualisationEventROOTSerializer() override = default;
4949
};
5050

EventVisualisation/DataConverter/include/EventVisualisationDataConverter/VisualisationEventSerializer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define O2EVE_VISUALISATIONEVENTSERIALIZER_H
1818

1919
#include "EventVisualisationDataConverter/VisualisationEvent.h"
20+
#include "EventVisualisationDataConverter/Location.h"
2021
#include <string>
2122
#include <map>
2223

@@ -45,7 +46,7 @@ class VisualisationEventSerializer
4546
static o2::dataformats::GlobalTrackID deserialize(unsigned source, unsigned index, unsigned flags);
4647
static VisualisationEventSerializer* getInstance(std::string ext) { return instances[ext]; }
4748
virtual bool fromFile(VisualisationEvent& event, std::string fileName) = 0;
48-
virtual void toFile(const VisualisationEvent& event, std::string fileName) = 0;
49+
virtual void toFile(const VisualisationEvent& event, Location location) = 0;
4950
virtual const std::string serializerName() const = 0;
5051
virtual ~VisualisationEventSerializer() = default;
5152
};
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2+
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3+
// All rights not expressly granted are reserved.
4+
//
5+
// This software is distributed under the terms of the GNU General Public
6+
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7+
//
8+
// In applying this license CERN does not waive the privileges and immunities
9+
// granted to it by virtue of its status as an Intergovernmental Organization
10+
// or submit itself to any jurisdiction.
11+
12+
///
13+
/// \file Location.cxx
14+
/// \author Julian Myrcha
15+
///
16+
17+
#include "EventVisualisationDataConverter/Location.h"
18+
#include <fairlogger/Logger.h>
19+
#include <sys/socket.h>
20+
#include <unistd.h>
21+
#include <netdb.h>
22+
23+
using namespace std;
24+
namespace o2::event_visualisation
25+
{
26+
void Location::open()
27+
{
28+
if (this->mToFile) {
29+
this->mOut = new std::ofstream(mFileName, std::ios::out | std::ios::binary);
30+
}
31+
if (this->mToSocket) {
32+
// resolve host name
33+
sockaddr_in serverAddress; // NOLINT(*-pro-type-member-init)
34+
serverAddress.sin_family = AF_INET;
35+
serverAddress.sin_port = htons(this->mPort); // Port number
36+
37+
// ask once
38+
static auto server = gethostbyname(this->mHostName.c_str());
39+
if (server == nullptr) {
40+
fprintf(stderr, "ERROR, no such host\n");
41+
return;
42+
};
43+
44+
bcopy((char*)server->h_addr,
45+
(char*)&serverAddress.sin_addr.s_addr,
46+
server->h_length);
47+
48+
// Connect to the server
49+
this->mClientSocket = socket(AF_INET, SOCK_STREAM, 0);
50+
if (this->mClientSocket == -1) {
51+
LOGF(info, "Error creating socket");
52+
return;
53+
}
54+
55+
if (connect(this->mClientSocket, (struct sockaddr*)&serverAddress,
56+
sizeof(serverAddress)) == -1) {
57+
LOGF(info, "Error connecting to %s:%d", this->mHostName.c_str(), this->mPort);
58+
::close(this->mClientSocket);
59+
this->mClientSocket = -1;
60+
return;
61+
}
62+
try {
63+
char buf[256] = "SEND:";
64+
strncpy(buf + 6, this->mFileName.c_str(), sizeof(buf) - 7);
65+
strncpy(buf + sizeof(buf) - 6, "ALICE", 6);
66+
auto real = send(this->mClientSocket, buf, sizeof(buf), 0);
67+
if (real != sizeof(buf)) {
68+
throw real;
69+
}
70+
} catch (...) {
71+
::close(this->mClientSocket);
72+
this->mClientSocket = -1;
73+
LOGF(info, "Error sending file name to %s:%d", this->mHostName.c_str(), this->mPort);
74+
}
75+
}
76+
}
77+
78+
void Location::close()
79+
{
80+
if (this->mToFile && this->mOut) {
81+
this->mOut->close();
82+
delete this->mOut;
83+
this->mOut = nullptr;
84+
}
85+
if (this->mToSocket && this->mClientSocket != -1) {
86+
::close(this->mClientSocket);
87+
this->mClientSocket = -1;
88+
}
89+
}
90+
91+
void Location::write(char* buf, std::streamsize size)
92+
{
93+
if (size == 0) {
94+
return;
95+
}
96+
if (this->mToFile && this->mOut) {
97+
this->mOut->write(buf, size);
98+
}
99+
if (this->mToSocket && this->mClientSocket != -1) {
100+
try {
101+
auto real = send(this->mClientSocket, buf, size, 0);
102+
if (real != size) {
103+
throw real;
104+
}
105+
} catch (...) {
106+
::close(this->mClientSocket);
107+
this->mClientSocket = -1;
108+
LOGF(info, "Error sending data to %s:%d", this->mHostName.c_str(), this->mPort);
109+
}
110+
}
111+
}
112+
113+
} // namespace o2::event_visualisation

EventVisualisation/DataConverter/src/VisualisationEventJSONSerializer.cxx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ using namespace rapidjson;
2929
namespace o2::event_visualisation
3030
{
3131

32-
void VisualisationEventJSONSerializer::toFile(const VisualisationEvent& event, std::string fileName)
32+
void VisualisationEventJSONSerializer::toFile(const VisualisationEvent& event, Location location)
3333
{
34+
std::string fileName = location.fileName();
3435
std::string json = toJson(event);
3536
std::ofstream out(fileName);
3637
out << json;

EventVisualisation/DataConverter/src/VisualisationEventOpenGLSerializer.cxx

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <iostream>
2121
#include <fstream>
2222
#include <filesystem>
23+
#include "EventVisualisationDataConverter/Location.h"
2324

2425
namespace o2::event_visualisation
2526
{
@@ -82,16 +83,19 @@ const auto CALT = "CALT"; // calo PID
8283

8384
const auto FINE = "FINE"; //
8485

85-
void VisualisationEventOpenGLSerializer::toFile(const VisualisationEvent& event, std::string fileName)
86+
void VisualisationEventOpenGLSerializer::toFile(const VisualisationEvent& event, Location location)
8687
{
88+
std::string fileName = location.fileName();
8789
static const std::vector<std::string> det_coma = {
8890
"ITS", "TPC", "TRD", "TOF", "PHS", "CPV", "EMC", "HMP", "MFT", "MCH", "MID", "ZDC", "FT0", "FV0", "FDD", "ITS-TPC",
8991
"TPC-TOF", "TPC-TRD", "MFT-MCH", "ITS-TPC-TRD", "ITS-TPC-TOF", "TPC-TRD-TOF", "MFT-MCH-MID", "ITS-TPC-TRD-TOF", "ITS-AB", "CTP",
9092
"MCH-MID"};
9193
std::ostringstream buf;
9294
constexpr auto SIGSIZE = 512;
9395
unsigned char data[SIGSIZE];
94-
std::ofstream out(fileName, std::ios::out | std::ios::binary);
96+
// std::ofstream out(fileName, std::ios::out | std::ios::binary);
97+
98+
location.open();
9599
// head --bytes 512 fileName.eve
96100
buf << "eve" << std::endl;
97101
buf << "version=1.00" << std::endl;
@@ -104,7 +108,7 @@ void VisualisationEventOpenGLSerializer::toFile(const VisualisationEvent& event,
104108
memcpy((char*)&data[0], buf.str().c_str(), SIGSIZE);
105109
data[SIGSIZE - 2] = '\n';
106110
data[SIGSIZE - 1] = 0;
107-
out.write((char*)&data[0], SIGSIZE); // <----0 SIGN
111+
location.write((char*)&data[0], SIGSIZE); // <----0 SIGN
108112

109113
const auto trackNo = event.getTracksSpan().size();
110114
int phsCount = 0;
@@ -140,7 +144,7 @@ void VisualisationEventOpenGLSerializer::toFile(const VisualisationEvent& event,
140144
head[Header::emcCount] = emcCount;
141145
head[Header::primaryVertex] = event.getPrimaryVertex();
142146
head[Header::tfCounter] = event.getTfCounter();
143-
out.write(static_cast<char*>(chunkHEAD), chunkSize(chunkHEAD)); // <----1 HEAD
147+
location.write(static_cast<char*>(chunkHEAD), chunkSize(chunkHEAD)); // <----1 HEAD
144148
free(chunkHEAD);
145149
}
146150

@@ -171,15 +175,15 @@ void VisualisationEventOpenGLSerializer::toFile(const VisualisationEvent& event,
171175
celm[index] = track.getClusterCount();
172176
index++;
173177
}
174-
out.write(static_cast<char*>(chunkTTYP), chunkSize(chunkTTYP)); // <----2 TTYP
178+
location.write(static_cast<char*>(chunkTTYP), chunkSize(chunkTTYP)); // <----2 TTYP
175179
free(chunkTTYP);
176-
out.write(static_cast<char*>(chunkTELM), chunkSize(chunkTELM)); // <----3 TELM
180+
location.write(static_cast<char*>(chunkTELM), chunkSize(chunkTELM)); // <----3 TELM
177181
free(chunkTELM);
178-
out.write(static_cast<char*>(chunkCELM), chunkSize(chunkCELM)); // <----3 CELM
182+
location.write(static_cast<char*>(chunkCELM), chunkSize(chunkCELM)); // <----3 CELM
179183
free(chunkCELM);
180-
out.write(static_cast<char*>(chunkTGID), chunkSize(chunkTGID)); // <----3 GIND
184+
location.write(static_cast<char*>(chunkTGID), chunkSize(chunkTGID)); // <----3 GIND
181185
free(chunkTGID);
182-
out.write(static_cast<char*>(chunkTPID), chunkSize(chunkTPID)); // <----3 TPID (tracks pid)
186+
location.write(static_cast<char*>(chunkTPID), chunkSize(chunkTPID)); // <----3 TPID (tracks pid)
183187
free(chunkTPID);
184188
}
185189

@@ -230,17 +234,17 @@ void VisualisationEventOpenGLSerializer::toFile(const VisualisationEvent& event,
230234
cxyz[cidx++] = track.getClustersSpan()[i].Z();
231235
}
232236
}
233-
out.write(static_cast<char*>(chunkTXYZ), chunkSize(chunkTXYZ)); // <----4 TXYZ
237+
location.write(static_cast<char*>(chunkTXYZ), chunkSize(chunkTXYZ)); // <----4 TXYZ
234238
free(chunkTXYZ);
235-
out.write(static_cast<char*>(chunkCXYZ), chunkSize(chunkCXYZ)); // <----4 CXYZ
239+
location.write(static_cast<char*>(chunkCXYZ), chunkSize(chunkCXYZ)); // <----4 CXYZ
236240
free(chunkCXYZ);
237-
out.write(static_cast<char*>(chunkTIME), chunkSize(chunkTIME)); // <----4 TIME
241+
location.write(static_cast<char*>(chunkTIME), chunkSize(chunkTIME)); // <----4 TIME
238242
free(chunkTIME);
239-
out.write(static_cast<char*>(chunkSXYZ), chunkSize(chunkSXYZ)); // <----4 SXYZ
243+
location.write(static_cast<char*>(chunkSXYZ), chunkSize(chunkSXYZ)); // <----4 SXYZ
240244
free(chunkSXYZ);
241-
out.write(static_cast<char*>(chunkCRGE), chunkSize(chunkCRGE)); // <----4 CRGE
245+
location.write(static_cast<char*>(chunkCRGE), chunkSize(chunkCRGE)); // <----4 CRGE
242246
free(chunkCRGE);
243-
out.write(static_cast<char*>(chunkATPE), chunkSize(chunkATPE)); // <----4 CRGE
247+
location.write(static_cast<char*>(chunkATPE), chunkSize(chunkATPE)); // <----4 CRGE
244248
free(chunkATPE);
245249
}
246250

@@ -260,11 +264,11 @@ void VisualisationEventOpenGLSerializer::toFile(const VisualisationEvent& event,
260264
uxyz[idx++] = c.Y();
261265
uxyz[idx++] = c.Z();
262266
}
263-
out.write(static_cast<char*>(chunkUGID), chunkSize(chunkUGID)); //
267+
location.write(static_cast<char*>(chunkUGID), chunkSize(chunkUGID)); //
264268
free(chunkUGID);
265-
out.write(static_cast<char*>(chunkUTIM), chunkSize(chunkUTIM)); //
269+
location.write(static_cast<char*>(chunkUTIM), chunkSize(chunkUTIM)); //
266270
free(chunkUTIM);
267-
out.write(static_cast<char*>(chunkUXYZ), chunkSize(chunkUXYZ)); //
271+
location.write(static_cast<char*>(chunkUXYZ), chunkSize(chunkUXYZ)); //
268272
free(chunkUXYZ);
269273
}
270274

@@ -300,22 +304,22 @@ void VisualisationEventOpenGLSerializer::toFile(const VisualisationEvent& event,
300304
}
301305
}
302306

303-
out.write((char*)chunkCALO, chunkSize(chunkCALO)); //
307+
location.write((char*)chunkCALO, chunkSize(chunkCALO)); //
304308
free(chunkCALO);
305-
out.write((char*)chunkCALP, chunkSize(chunkCALP)); //
309+
location.write((char*)chunkCALP, chunkSize(chunkCALP)); //
306310
free(chunkCALP);
307-
out.write((char*)chunkCALG, chunkSize(chunkCALG)); //
311+
location.write((char*)chunkCALG, chunkSize(chunkCALG)); //
308312
free(chunkCALG);
309-
out.write((char*)chunkCALT, chunkSize(chunkCALT)); //
313+
location.write((char*)chunkCALT, chunkSize(chunkCALT)); //
310314
free(chunkCALT);
311315
}
312316

313317
{
314318
const auto chunkFINE = createChunk(FINE, 0);
315-
out.write(static_cast<char*>(chunkFINE), chunkSize(chunkFINE)); // <----5 FINE
319+
location.write(static_cast<char*>(chunkFINE), chunkSize(chunkFINE)); // <----5 FINE
316320
free(chunkFINE);
317321
}
318-
out.close();
322+
location.close();
319323
}
320324

321325
void* VisualisationEventOpenGLSerializer::createChunk(const char* lbl, unsigned size)

0 commit comments

Comments
 (0)