test_shm.cpp 3.98 KB
Newer Older
1
/*
2
 *  Copyright (C) 2012-2013 Savoir-Faire Linux Inc.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 *  Author: Tristan Matthews <tristan.matthews@savoirfairelinux.com>
 *
 *  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 3 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
17
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
18
19
20
21
22
23
24
25
26
27
28
29
30
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
 */

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "shm_sink.h"
#include "shm_src.h"
#include <thread>
#include <signal.h>
#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
#include <atomic>
#include <cstring>
#include <cassert>

namespace {

std::atomic<bool> done(false);

void signal_handler(int /*sig*/)
{
    done = true;
}

const char test_data[] = "abcdefghijklmnopqrstuvwxyz";

void sink_thread()
{
55
    sfl_video::SHMSink sink("bob");;
56
57
    if (!sink.start())
        return;
58
59
    std::vector<unsigned char> test_vec(test_data, test_data + sizeof(test_data) / sizeof(test_data[0]));

60
    while (!done) {
61
        sink.render(test_vec);
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
        usleep(1000);
    }
    sink.stop();
    std::cerr << std::endl;
    std::cerr << "Exitting sink thread" << std::endl;
}

void run_client()
{
    SHMSrc src("bob");;
    bool started = false;
    while (not done and not started) {
        sleep(1);
        if (src.start())
            started = true;
    }
    // we get here if the above loop was interupted by our signal handler
    if (!started)
        return;

    // initialize destination string to 0's
83
84
    std::vector<char> dest(sizeof(test_data), 0);
    const std::vector<char> test_data_str(test_data, test_data + sizeof(test_data));
85
86
87
    assert(test_data_str.size() == 27);
    assert(dest.size() == test_data_str.size());
    while (not done and dest != test_data_str) {
88
        src.render(dest.data(), dest.size());
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
        usleep(1000);
    }
    src.stop();
    std::cerr << "Got characters, exitting client process" << std::endl;
}

void run_daemon()
{
    std::thread bob(sink_thread);
    /* Wait for child process. */
    int status;
    int pid;
    if ((pid = wait(&status)) == -1) {
        perror("wait error");
    } else {
        // Check status.
        if (WIFSIGNALED(status) != 0)
            std::cout << "Child process ended because of signal " <<
                    WTERMSIG(status) << std::endl;
        else if (WIFEXITED(status) != 0)
            std::cout << "Child process ended normally; status = " <<
                    WEXITSTATUS(status) << std::endl;
        else
            std::cout << "Child process did not end normally" << std::endl;
    }
    std::cout << "Finished waiting for child" << std::endl;
    done = true;
    // wait for thread
    bob.join();
}

} // end anonymous namespace

int main()
{
    signal(SIGINT, signal_handler);
    pid_t pid = fork();
    if (pid < 0) {
        std::cerr << "Failed to fork" << std::endl;
        return 1;
    } else if (pid == 0) {
        // child code only
        run_client();
    } else {
        // parent code only
        run_daemon();
    }
    return 0;
}