main.cpp 2.27 KB
Newer Older
1
/**
2 3 4
 * @file main.cpp
 * @brief Spawn single instance of streamer for each sensor port.
 * @copyright Copyright (C) 2017 Elphel Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * @author AUTHOR <EMAIL>
 *
 * @par License:
 *  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, see <http://www.gnu.org/licenses/>.
20
 */
21

22 23 24 25 26 27 28 29 30 31
#include <iostream>
#include <string>
#include <map>

#include "streamer.h"

using namespace std;

#include <unistd.h>
#include <linux/sysctl.h>
32 33 34 35 36 37 38 39
#include <elphel/c313a.h>
#include <pthread.h>

/**
 * Unconditionally cancel all threads.
 * @param   threads   an array of thread pointers
 * @return  None
 */
40 41 42
void clean_up(pthread_t *threads, size_t sz) {
	for (size_t i = 0; i < sz; i++)
		pthread_cancel(threads[i]);
43
}
44 45 46 47

int main(int argc, char *argv[]) {
	string opt;
	map<string, string> args;
48 49
	pthread_t threads[SENSOR_PORTS];
	Streamer *streamers[SENSOR_PORTS] = {NULL};
50 51 52 53

	for (int i = 1; i < argc; i++) {
		if (argv[i][0] == '-' && argv[i][1] != '\0') {
			if (opt != "")
54 55 56 57
				args[opt] = "";
			opt = &argv[i][1];
			continue;
		} else {
58
			if (opt != "") {
59 60 61 62 63
				args[opt] = argv[i];
				opt = "";
			}
		}
	}
64
	if (opt != "")
65
		args[opt] = "";
66
	for (map<string, string>::iterator it = args.begin(); it != args.end(); it++) {
67 68 69
		cerr << "|" << (*it).first << "| == |" << (*it).second << "|" << endl;
	}

70 71
	for (int i = 0; i < SENSOR_PORTS; i++) {
		pthread_attr_t attr;
72
		streamers[i] = new Streamer(args, i);
73 74 75

		pthread_attr_init(&attr);
		if (!pthread_create(&threads[i], &attr, Streamer::pthread_f, (void *) streamers[i])) {
76 77
			cerr << "Can not spawn streamer thread for port " << i << endl;
			clean_up(threads, SENSOR_PORTS);
78 79 80 81
			exit(EXIT_FAILURE);
		}
		pthread_attr_destroy(&attr);
	}
82 83
	for (size_t i = 0; i < SENSOR_PORTS; i++)
		pthread_join(threads[i], NULL);
84

85 86
	return 0;
}