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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
153
154
155
156
157
158
159
160
161
|
/*
* File: MetaScanTest.cc
* Author: django
*
* Created on 27.07.2012, 10:03
*/
#include <File.h>
#include <FileReader.h>
#include <LineReader.h>
#include <ConfigReader.h>
#include <CommandReader.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <wait.h>
#include <tr1/tuple>
#include <util.h>
static void testPipe(const char *chk = NULL);
static void parseConfig(const char *FileName)
{
cConfigReader *cr = new cConfigReader(new cLineReader(new cFileReader(new cFile(FileName))));
cConfigReader::ConfigEntry *ce;
while ((ce = cr->ReadValue())) {
std::cout << "config entry [" << std::get<0>(*ce) << "] => " << std::get<1>(*ce) << std::endl;
delete ce;
}
cr->Close();
delete cr;
}
static void testMediaFiles(const char *FileName)
{
cLineReader *lr = new cLineReader(new cFileReader(new cFile(FileName)));
const char *line;
while ((line = lr->ReadLine())) {
std::cout << "media-test-file: " << line << std::endl;
testPipe(line);
}
lr->Close();
delete lr;
}
static const short BufferSize = 100;
enum PipeFileDescriptors {
READ_FD = 0,
WRITE_FD = 1
};
static void testCommandReader()
{
cCommandReader *cr = new cCommandReader("/bin/ls");
cLineReader *lr = new cLineReader(cr);
const char *line;
cr->AddCommandParameter("-al");
cr->AddCommandParameter("--color");
while ((line = lr->ReadLine())) {
std::cout << "from command: " << line << std::endl;
}
delete lr;
};
static void testPipe(const char *chk)
{
int parent2Child[2];
int child2Parent[2];
pid_t pid;
std::string dataReadFromChild;
char buffer[BufferSize + 1];
ssize_t readResult;
bool run = true;
int status;
ASSERT_IS(0, pipe(parent2Child));
ASSERT_IS(0, pipe(child2Parent));
switch (pid = fork()) {
case -1:
FAIL("fork failed");
exit(-1);
case 0: /* child */
ASSERT_NOT(-1, dup2(parent2Child[READ_FD], STDIN_FILENO));
ASSERT_NOT(-1, dup2(child2Parent[WRITE_FD], STDOUT_FILENO));
ASSERT_NOT(-1, dup2(child2Parent[WRITE_FD], STDERR_FILENO));
ASSERT_IS(0, close(parent2Child[WRITE_FD]));
ASSERT_IS(0, close(child2Parent[READ_FD]));
if (chk) {
execlp("mediainfo", "mediainfo", chk, NULL);
}
else execlp("ls", "ls", "-al", "--color", NULL);
FAIL("this line should never be reached!");
exit(-1);
default: /* parent */
std::cout << "child " << pid << " process running ..." << std::endl;
ASSERT_IS(0, close(parent2Child[READ_FD]));
ASSERT_IS(0, close(child2Parent[WRITE_FD]));
while (run) {
switch (readResult = read(child2Parent[READ_FD], buffer, BufferSize)) {
case 0: /* end of file, or non-blocking read. */
std::cout << "End of File reached ..." << std::endl
<< "Data received was ("
<< dataReadFromChild.size() << "):" << std::endl
<< dataReadFromChild << std::endl;
ASSERT_IS(pid, waitpid(pid, &status, 0));
std::cout << std::endl
<< "child exit status is: " << WEXITSTATUS(status) << std::endl << std::endl;
// exit(0);
run = false;
break;
case -1:
if ((errno == EINTR) || (errno == EAGAIN)) {
errno = 0;
}
else {
FAIL("read() failed");
run = false;
} break;
default:
dataReadFromChild.append(buffer, readResult);
break;
}
}
}
}
// mediainfo ${media} | grep "Scan type"
// ffmpeg -i ${media}
int main()
{
parseConfig("srclient.conf");
std::cout << std::endl << "===========================================" << std::endl << std::endl;
parseConfig("srserver.conf");
std::cout << std::endl << "===========================================" << std::endl << std::endl;
testCommandReader();
// std::cout << std::endl << "===========================================" << std::endl << std::endl;
// testMediaFiles("testMedia.files");
cFile::Cleanup();
return 0;
}
|