-
Notifications
You must be signed in to change notification settings - Fork 0
/
IO.cpp
91 lines (73 loc) · 2.05 KB
/
IO.cpp
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
//
// Created by archie on 3/18/17.
//
#include "IO.h"
IO::IO() {
inFileName = "";
outFilename = "";
msgHeader = "$XMARUS05MSGSTARTSIZE:";
msgFooter = "XMARUS05MSGEND$";
}
void IO::createPipes(string write, string read, bool writeFirst) {
inFileName = read;
outFilename = write;
createOutput(outFilename);
openPipes(writeFirst);
}
void IO::createOutput(string out){
mknod(out.c_str(), S_IFIFO | 0666, 0);
};
void IO::openPipes(bool writeFirst){
if(writeFirst){
openForWrite();
openForRead();
}
else {
openForRead();
openForWrite();
}
}
void IO::openForRead() {
cerr << "log.debug: " <<"waiting for writers..." << endl;
while(!inputStream.is_open()) {
inputStream.open(inFileName, ifstream::in);
}
cerr << "log.debug: " << "got a writer" << endl;
}
void IO::openForWrite() {
cerr << "log.debug: " << "waiting for readers..." << endl;
outputStream.open(outFilename, ifstream::out);
cerr << "log.debug: " << "got a reader" << endl;
}
void IO::sendMessage(string msg) {
outputStream << msgHeader << " ";
outputStream << msg.size();
outputStream << " ";
outputStream << msg;
outputStream << msgFooter;
outputStream << endl;
cerr << "log.debug: " << "Sent message size:'" << msg.size() <<"' content: " << msg << endl;
}
string IO::readMessage() {
string msg("");
cerr << "log.debug: " << "Waiting for message" << endl;
int msgSize = 0;
char c;
while (msg.length() == 0)
{
string msgStart("");
inputStream >> msgStart;
if(msgStart == msgHeader){
inputStream >> msgSize;
cerr << "log.debug: message size = " << msgSize << endl;
inputStream.get(c); //odstran medzeru zo zaciatku
for(int i = 0; i <= (msgSize + msgFooter.length()); i++) {
inputStream.get(c);
msg += c;
}
}
}
msg = msg.substr(0, msg.size()-16);
cerr << "log.debug: " << "Got message:" << msg << endl;
return msg;
}