author | František Kučera <franta-hg@frantovo.cz> |
Sun, 05 May 2019 11:51:45 +0200 | |
branch | v_0 |
changeset 3 | e086ae6a19c3 |
parent 2 | 580ccb511301 |
child 4 | 050ec7c1f2e7 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation, either version 3 of the License, or |
|
8 |
* (at your option) any later version. |
|
9 |
* |
|
10 |
* This program is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
* GNU General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License |
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
#pragma once |
|
19 |
||
20 |
#include <memory> |
|
21 |
#include <string> |
|
22 |
#include <vector> |
|
23 |
#include <iostream> |
|
24 |
#include <sstream> |
|
25 |
#include <locale> |
|
26 |
#include <codecvt> |
|
27 |
#include <regex> |
|
28 |
||
1 | 29 |
#include <unistd.h> |
30 |
#include <wait.h> |
|
31 |
#include <ext/stdio_filebuf.h> |
|
32 |
||
0 | 33 |
#include <relpipe/reader/typedefs.h> |
34 |
#include <relpipe/reader/TypeId.h> |
|
35 |
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h> |
|
36 |
#include <relpipe/reader/handlers/AttributeMetadata.h> |
|
37 |
||
38 |
#include <relpipe/writer/Factory.h> |
|
39 |
||
40 |
#include <relpipe/cli/RelpipeCLIException.h> |
|
41 |
||
42 |
#include "Configuration.h" |
|
43 |
||
44 |
namespace relpipe { |
|
45 |
namespace tr { |
|
46 |
namespace awk { |
|
47 |
||
48 |
using namespace std; |
|
49 |
using namespace relpipe; |
|
50 |
using namespace relpipe::reader; |
|
51 |
using namespace relpipe::reader::handlers; |
|
52 |
||
53 |
class AwkHandler : public RelationalReaderStringHandler { |
|
54 |
private: |
|
55 |
Configuration configuration; |
|
56 |
writer::RelationalWriter* relationalWriter; |
|
57 |
||
2 | 58 |
int awkInputWriterFD = -1; |
59 |
||
1 | 60 |
void createPipe(int& readerFD, int& writerFD) { |
61 |
int fds[2]; |
|
62 |
int result = pipe(fds); |
|
63 |
readerFD = fds[0]; |
|
64 |
writerFD = fds[1]; |
|
65 |
if (result < 0) throw cli::RelpipeCLIException(L"Unable to create a pipe.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions? |
|
66 |
} |
|
67 |
||
68 |
void redirectFD(int oldfd, int newfd) { |
|
69 |
int result = dup2(oldfd, newfd); |
|
70 |
if (result < 0) throw cli::RelpipeCLIException(L"Unable redirect FD.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions? |
|
71 |
} |
|
72 |
||
73 |
void closeOrThrow(int fd) { |
|
74 |
int error = close(fd); |
|
2 | 75 |
if (error) throw cli::RelpipeCLIException(L"Unable to close FD: " + to_wstring(fd) + L" from PID: " + to_wstring(getpid()), cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions? |
76 |
} |
|
77 |
||
78 |
void cleanUp() { |
|
79 |
if (awkInputWriterFD >= 0) { |
|
80 |
closeOrThrow(awkInputWriterFD); |
|
81 |
fwprintf(stderr, L"writing done and closed\n"); |
|
82 |
__pid_t waitResult1 = wait(NULL); |
|
83 |
fwprintf(stderr, L"wait 1 done: %d\n", waitResult1); |
|
84 |
__pid_t waitResult2 = wait(NULL); |
|
85 |
fwprintf(stderr, L"wait 2 done: %d\n", waitResult2); |
|
86 |
awkInputWriterFD = -1; |
|
87 |
} |
|
1 | 88 |
} |
89 |
||
0 | 90 |
public: |
91 |
||
92 |
AwkHandler(writer::RelationalWriter* relationalWriter, Configuration& configuration) : relationalWriter(relationalWriter), configuration(configuration) { |
|
93 |
} |
|
94 |
||
95 |
void startRelation(string_t name, vector<AttributeMetadata> attributes) override { |
|
2 | 96 |
cleanUp(); |
0 | 97 |
|
1 | 98 |
|
99 |
int awkInputReaderFD; |
|
100 |
int awkOutputReaderFD; |
|
101 |
int awkOutputWriterFD; |
|
102 |
||
103 |
createPipe(awkInputReaderFD, awkInputWriterFD); |
|
104 |
createPipe(awkOutputReaderFD, awkOutputWriterFD); |
|
105 |
||
106 |
__pid_t awkPid = fork(); |
|
107 |
||
108 |
if (awkPid < 0) { |
|
109 |
throw cli::RelpipeCLIException(L"Unable to fork AWK process.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions? |
|
110 |
} else if (awkPid == 0) { |
|
111 |
// AWK child process |
|
2 | 112 |
closeOrThrow(awkInputWriterFD); |
113 |
closeOrThrow(awkOutputReaderFD); |
|
1 | 114 |
|
2 | 115 |
redirectFD(awkInputReaderFD, STDIN_FILENO); |
116 |
redirectFD(awkOutputWriterFD, STDOUT_FILENO); |
|
1 | 117 |
|
118 |
||
119 |
fwprintf(stderr, L"I am child AWK with PID: %d\n", getpid()); |
|
120 |
execlp("awk", "awk", "{print \"AWK says: line \" NR \" = \" $0;}", nullptr); |
|
121 |
||
122 |
} else { |
|
123 |
// Parent process |
|
2 | 124 |
closeOrThrow(awkInputReaderFD); |
125 |
closeOrThrow(awkOutputWriterFD); |
|
1 | 126 |
fwprintf(stderr, L"Forked AWK has PID: %d\n", awkPid); |
127 |
||
128 |
__pid_t writerPid = fork(); |
|
129 |
||
130 |
if (writerPid < 0) { |
|
131 |
throw cli::RelpipeCLIException(L"Unable to fork Writer process.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions? |
|
132 |
} else if (writerPid == 0) { |
|
133 |
// Writer child process |
|
2 | 134 |
closeOrThrow(awkInputWriterFD); |
3
e086ae6a19c3
generate (some) relational output
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
135 |
fwprintf(stderr, L"I am child Writer with PID: %d Writer: %p\n", getpid(), relationalWriter); |
1 | 136 |
|
3
e086ae6a19c3
generate (some) relational output
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
137 |
__gnu_cxx::stdio_filebuf<wchar_t> awkOutputReaderBuffer(awkOutputReaderFD, std::ios::in); |
e086ae6a19c3
generate (some) relational output
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
138 |
std::wistream awkOutputReader(&awkOutputReaderBuffer); |
1 | 139 |
|
3
e086ae6a19c3
generate (some) relational output
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
140 |
relationalWriter->startRelation(L"writer_debug",{ |
e086ae6a19c3
generate (some) relational output
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
141 |
{L"message", writer::TypeId::STRING}, |
e086ae6a19c3
generate (some) relational output
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
142 |
}, true); |
e086ae6a19c3
generate (some) relational output
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
143 |
|
e086ae6a19c3
generate (some) relational output
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
144 |
for (wchar_t ch = awkOutputReader.get(); awkOutputReader.good(); ch = awkOutputReader.get()) { |
e086ae6a19c3
generate (some) relational output
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
145 |
relationalWriter->writeAttribute(string_t(1, ch)); |
1 | 146 |
} |
147 |
||
2 | 148 |
closeOrThrow(awkOutputReaderFD); |
149 |
exit(0); |
|
1 | 150 |
} else { |
151 |
// Parent process |
|
2 | 152 |
closeOrThrow(awkOutputReaderFD); |
1 | 153 |
fwprintf(stderr, L"Forked Writer has PID: %d\n", writerPid); |
154 |
} |
|
155 |
} |
|
156 |
||
0 | 157 |
} |
158 |
||
159 |
void attribute(const string_t& value) override { |
|
2 | 160 |
dprintf(awkInputWriterFD, "attribute!\n"); |
0 | 161 |
} |
162 |
||
163 |
void endOfPipe() { |
|
2 | 164 |
cleanUp(); |
0 | 165 |
} |
166 |
||
167 |
}; |
|
168 |
||
169 |
} |
|
170 |
} |
|
171 |
} |