Skip to content

Instantly share code, notes, and snippets.

@malefs
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save malefs/8976015 to your computer and use it in GitHub Desktop.

Select an option

Save malefs/8976015 to your computer and use it in GitHub Desktop.
Boost thread ctrl-c abbort thrift stop
#include "StanzeCam.h"
#include <signal.h>
//#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
#include <iostream>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
using namespace std;
using boost::shared_ptr;
TSimpleServer* camServer = NULL;
static void sig_handler(int signum)
{
int c=0;
printf("get Signal %d\n",signum);
if(signum != SIGINT)
return;
else
{
signal(SIGINT,SIG_IGN);
printf("\nBeenden (j/n)? : ");
c=getchar();
if(c == 'j')
{
camServer->stop();//exit (EXIT_FAILURE);
return;
}
else
{
signal(SIGINT,sig_handler);
return;
}
}
}
void *start_server(void *pArgs)
{
printf("Camserver start\n");
while(1)
{
sleep(1);
printf(".");
fflush(0);
}
}
void imagegrabber_func()
{
boost::posix_time::seconds workTime(5);
std::cout<<"start Thread imgrabbber"<<std::endl;
boost::this_thread::sleep(workTime);
std::cout<<"finish"<< std::endl;
}
int main(int argc, char *argv[])
{
//pthread_t thread_id;
int port = 19091;
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
signal(SIGKILL, sig_handler);
signal(SIGUSR1, sig_handler);
//pthread_create(&thread_id, NULL, start_server, NULL);
boost::thread bt_imaggrabber(imagegrabber_func);
bt_imaggrabber.join();
std::cout<<"thread ende"<< std::endl;
shared_ptr<CamserverHandler> handler(new CamserverHandler());
shared_ptr<TProcessor> processor(new CamserverProcessor(handler));
shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
//TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
camServer = new TSimpleServer(processor, serverTransport,transportFactory, protocolFactory);
//handler->addShutdownHandler(shutdownHandler);
std::cout<<"Camserver running"<<std::endl;
camServer->serve();
cout<<"finish"<<endl;
return 0;
//QCoreApplication a(argc, argv);
//return a.exec();
}
//#include <QtCore/QCoreApplication>
//#include <QDebug>
#include "StanzeCam.h"
#include <signal.h>
//#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
#include <iostream>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
using namespace std;
using boost::shared_ptr;
TSimpleServer* camServer = NULL;
static void sig_handler(int signum)
{
cout <<"get Signal:"<<signum<<endl;
if(signum == SIGINT || signum ==SIGTERM)
{
//signal(SIGINT,SIG_IGN);
cout <<"Stop Server"<<endl;
camServer->stop();//exit (EXIT_FAILURE);
return;
}
}
void waitms(int millis)
{
boost::this_thread::sleep(boost::posix_time::milliseconds(millis));
}
//Boost Thread Test
void imagegrabber_func(int millis)
{
try{
cout<<"start Thread Imggrabber"<<endl;
while(true)
{
waitms(millis);
cout<<"."<<flush;
}
}
catch (boost::thread_interrupted&){
cout<<"thread interrupted"<< endl;
}
}
int main()//int argc, char *argv[])
{
int port = 19091;
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
signal(SIGKILL, sig_handler);
signal(SIGUSR1, sig_handler);
boost::thread bt_imaggrabber(imagegrabber_func,1000);
shared_ptr<CamserverHandler> handler(new CamserverHandler());
shared_ptr<TProcessor> processor(new CamserverProcessor(handler));
shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
camServer = new TSimpleServer(processor, serverTransport,transportFactory, protocolFactory);
cout<<"Camserver running"<<endl;
camServer->serve();
cout<<"server finish"<<endl;
bt_imaggrabber.interrupt();
bt_imaggrabber.join();
cout<<"thread finish"<< endl;
exit (EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment