git-svn-id: svn://elaine/murooma/trunk@187 d8a302eb-03bc-478d-80e4-98257eca68ef
This commit is contained in:
(no author) 2014-08-13 06:12:25 +00:00
parent 6855687f96
commit b0add66146
4 changed files with 118 additions and 42 deletions

View file

@ -1,7 +1,7 @@
VERSION = 0.01 VERSION = 0.01
CC = /usr/bin/g++ CC = /usr/bin/g++
CFLAGS = -std=gnu++0x -Wall -Wno-unused-function -O3 -D_REENTRANT -DVERSION=\"$(VERSION)\" -I.. CFLAGS = -std=gnu++0x -Wall -Wno-unused-function -O3 -D_REENTRANT -DVERSION=\"$(VERSION)\" -I..
LDFLAGS = -lrt -lpthread -lportaudio -lboost_system LDFLAGS = -lrt -lpthread -lportaudio -lboost_system -lboost_program_options
OBJ = snapClient.o stream.o ../common/chunk.o OBJ = snapClient.o stream.o ../common/chunk.o
BIN = snapclient BIN = snapclient

View file

@ -15,6 +15,8 @@
#include <thread> #include <thread>
#include <portaudio.h> #include <portaudio.h>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/program_options.hpp>
#include "common/chunk.h" #include "common/chunk.h"
#include "common/utils.h" #include "common/utils.h"
@ -22,21 +24,21 @@
using boost::asio::ip::tcp; using boost::asio::ip::tcp;
using namespace std; using namespace std;
namespace po = boost::program_options;
int bufferMs;
int deviceIdx; int deviceIdx;
Stream* stream; Stream* stream;
void player() void player(const std::string& ip, int port)
{ {
try try
{ {
boost::asio::io_service io_service; boost::asio::io_service io_service;
tcp::resolver resolver(io_service); tcp::resolver resolver(io_service);
tcp::resolver::query query(tcp::v4(), "192.168.0.2", "98765"); tcp::resolver::query query(tcp::v4(), ip, boost::lexical_cast<string>(port));
tcp::resolver::iterator iterator = resolver.resolve(query); tcp::resolver::iterator iterator = resolver.resolve(query);
while (true) while (true)
@ -178,12 +180,27 @@ error:
int main (int argc, char *argv[]) int main (int argc, char *argv[])
{ {
deviceIdx = -1; string ip;
bufferMs = 300; int bufferMs;
if (argc > 1) size_t port;
bufferMs = atoi(argv[1]); po::options_description desc("Allowed options");
if (argc > 2) desc.add_options()
deviceIdx = atoi(argv[2]); ("help,h", "produce help message")
("port,p", po::value<size_t>(&port)->default_value(98765), "port where the server listens on")
("ip,i", po::value<string>(&ip)->default_value("192.168.0.2"), "server IP")
("device,d", po::value<int>(&deviceIdx)->default_value(-1), "index of the soundcard")
("buffer,b", po::value<int>(&bufferMs)->default_value(300), "buffer size [ms]")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help"))
{
cout << desc << "\n";
return 1;
}
stream = new Stream(); stream = new Stream();
stream->setBufferLen(bufferMs); stream->setBufferLen(bufferMs);
@ -191,10 +208,10 @@ int main (int argc, char *argv[])
PaStream* paStream = initAudio(paError); PaStream* paStream = initAudio(paError);
stream->setLatency(1000*Pa_GetStreamInfo(paStream)->outputLatency); stream->setLatency(1000*Pa_GetStreamInfo(paStream)->outputLatency);
std::thread playerThread(player); std::thread playerThread(player, ip, port);
std::string cmd; std::string cmd;
while (true && (argc > 3)) /* while (true && (argc > 3))
{ {
std::cout << "> "; std::cout << "> ";
std::getline(std::cin, cmd); std::getline(std::cin, cmd);
@ -210,7 +227,7 @@ int main (int argc, char *argv[])
stream->setBufferLen(atoi(cmd.c_str())); stream->setBufferLen(atoi(cmd.c_str()));
} }
} }
*/
playerThread.join(); playerThread.join();
return 0; return 0;

27
common/signalHandler.h Normal file
View file

@ -0,0 +1,27 @@
#ifndef SIGNAL_HANDLER_H
#define SIGNAL_HANDLER_H
#include <signal.h>
#include <syslog.h>
extern bool g_terminated;
void signal_handler(int sig) {
switch(sig) {
case SIGHUP:
syslog(LOG_WARNING, "Received SIGHUP signal.");
break;
case SIGTERM:
syslog(LOG_WARNING, "Received SIGTERM signal.");
g_terminated = true;
break;
default:
syslog(LOG_WARNING, "Unhandled signal ");
break;
}
}
#endif

View file

@ -23,6 +23,8 @@
#include "common/chunk.h" #include "common/chunk.h"
#include "common/timeUtils.h" #include "common/timeUtils.h"
#include "common/queue.h" #include "common/queue.h"
#include "common/signalHandler.h"
#include <syslog.h>
using boost::asio::ip::tcp; using boost::asio::ip::tcp;
@ -35,6 +37,9 @@ using namespace std;
using namespace std::chrono; using namespace std::chrono;
bool g_terminated = false;
std::string return_current_time_and_date() std::string return_current_time_and_date()
{ {
auto now = system_clock::now(); auto now = system_clock::now();
@ -149,7 +154,7 @@ public:
void stop() void stop()
{ {
acceptThread->join(); // acceptThread->join();
} }
private: private:
@ -160,17 +165,62 @@ private:
}; };
void daemonize()
{
/* Our process ID and Session ID */
pid_t pid, sid;
/* Fork off the parent process */
pid = fork();
if (pid < 0)
exit(EXIT_FAILURE);
/* If we got a good PID, then
we can exit the parent process. */
if (pid > 0)
exit(EXIT_SUCCESS);
/* Change the file mode mask */
umask(0);
/* Open any logs here */
/* Create a new SID for the child process */
sid = setsid();
if (sid < 0)
{
/* Log the failure */
exit(EXIT_FAILURE);
}
/* Change the current working directory */
if ((chdir("/")) < 0)
{
/* Log the failure */
exit(EXIT_FAILURE);
}
/* Close out the standard file descriptors */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
try try
{ {
size_t port; size_t port;
string fifoName; string fifoName;
bool runAsDaemon;
po::options_description desc("Allowed options"); po::options_description desc("Allowed options");
desc.add_options() desc.add_options()
("help,h", "produce help message") ("help,h", "produce help message")
("port,p", po::value<size_t>(&port)->default_value(98765), "port to listen on") ("port,p", po::value<size_t>(&port)->default_value(98765), "port to listen on")
("fifo,f", po::value<string>(&fifoName)->default_value("/tmp/snapfifo"), "name of fifo file") ("fifo,f", po::value<string>(&fifoName)->default_value("/tmp/snapfifo"), "name of fifo file")
("daemon,d", po::bool_switch(&runAsDaemon)->default_value(false), "daemonize")
; ;
po::variables_map vm; po::variables_map vm;
@ -198,6 +248,11 @@ int main(int argc, char* argv[])
} }
*/ */
if (runAsDaemon)
daemonize();
openlog ("firstdaemon", LOG_PID, LOG_DAEMON);
using namespace std; // For atoi. using namespace std; // For atoi.
Server* server = new Server(port); Server* server = new Server(port);
server->start(); server->start();
@ -206,34 +261,11 @@ int main(int argc, char* argv[])
gettimeofday(&tvChunk, NULL); gettimeofday(&tvChunk, NULL);
long nextTick = getTickCount(); long nextTick = getTickCount();
/* pid_t pid, sid;
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
if (pid > 0) {
exit(EXIT_SUCCESS);
}
umask(0);
sid = setsid();
if (sid < 0) {
exit(EXIT_FAILURE);
}
if ((chdir("/")) < 0) {
exit(EXIT_FAILURE);
}
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
*/
mkfifo(fifoName.c_str(), 0777); mkfifo(fifoName.c_str(), 0777);
while (true)
while (!g_terminated)
{ {
syslog (LOG_NOTICE, "First daemon started.");
int fd = open(fifoName.c_str(), O_RDONLY); int fd = open(fifoName.c_str(), O_RDONLY);
try try
{ {
@ -281,7 +313,6 @@ int main(int argc, char* argv[])
close(fd); close(fd);
} }
return 0;
server->stop(); server->stop();
} }
catch (std::exception& e) catch (std::exception& e)
@ -289,7 +320,8 @@ int main(int argc, char* argv[])
std::cerr << "Exception: " << e.what() << "\n"; std::cerr << "Exception: " << e.what() << "\n";
} }
return 0; syslog (LOG_NOTICE, "First daemon terminated.");
closelog();
} }