From 413a9fcb3703bea951d0c4fb3b0a32ee7c814a94 Mon Sep 17 00:00:00 2001 From: Yann Diorcet Date: Wed, 23 May 2012 12:02:50 +0200 Subject: [PATCH] Remove strong dependency to readline: disable usage available --- daemon/commands/audio-codec-get.cc | 2 - daemon/commands/audio-codec-move.cc | 2 - daemon/commands/audio-codec-set.cc | 2 - daemon/commands/audio-stream-start.cc | 2 - daemon/commands/call-stats.cc | 2 - daemon/commands/call-status.cc | 2 - daemon/commands/call.cc | 2 - daemon/commands/ptime.cc | 2 - daemon/commands/quit.cc | 2 - daemon/commands/register.cc | 2 - daemon/daemon.cc | 124 ++++++++++++++++++-------- daemon/daemon.h | 38 ++++++-- 12 files changed, 120 insertions(+), 62 deletions(-) diff --git a/daemon/commands/audio-codec-get.cc b/daemon/commands/audio-codec-get.cc index 373913b46..283215c43 100644 --- a/daemon/commands/audio-codec-get.cc +++ b/daemon/commands/audio-codec-get.cc @@ -1,7 +1,5 @@ #include "audio-codec-get.h" -#include - using namespace std; AudioCodecGetCommand::AudioCodecGetCommand() : diff --git a/daemon/commands/audio-codec-move.cc b/daemon/commands/audio-codec-move.cc index b26452119..713c2c559 100644 --- a/daemon/commands/audio-codec-move.cc +++ b/daemon/commands/audio-codec-move.cc @@ -1,7 +1,5 @@ #include "audio-codec-move.h" -#include - using namespace std; AudioCodecMoveCommand::AudioCodecMoveCommand() : diff --git a/daemon/commands/audio-codec-set.cc b/daemon/commands/audio-codec-set.cc index 92998b86c..bbce0a68a 100644 --- a/daemon/commands/audio-codec-set.cc +++ b/daemon/commands/audio-codec-set.cc @@ -1,7 +1,5 @@ #include "audio-codec-set.h" -#include - using namespace std; AudioCodecSetCommand::AudioCodecSetCommand() : diff --git a/daemon/commands/audio-stream-start.cc b/daemon/commands/audio-stream-start.cc index 6ff0c84ac..5974f5e39 100644 --- a/daemon/commands/audio-stream-start.cc +++ b/daemon/commands/audio-stream-start.cc @@ -1,7 +1,5 @@ #include "audio-stream-start.h" -#include - using namespace std; AudioStreamStartCommand::AudioStreamStartCommand() : diff --git a/daemon/commands/call-stats.cc b/daemon/commands/call-stats.cc index 7fc9ee22b..fc0186715 100644 --- a/daemon/commands/call-stats.cc +++ b/daemon/commands/call-stats.cc @@ -1,7 +1,5 @@ #include "call-stats.h" -#include - using namespace std; CallStatsCommand::CallStatsCommand() : diff --git a/daemon/commands/call-status.cc b/daemon/commands/call-status.cc index 2db7a3c16..a42173e68 100644 --- a/daemon/commands/call-status.cc +++ b/daemon/commands/call-status.cc @@ -1,7 +1,5 @@ #include "call-status.h" -#include - using namespace std; CallStatusCommand::CallStatusCommand() : diff --git a/daemon/commands/call.cc b/daemon/commands/call.cc index dcca5780d..11a02fe00 100644 --- a/daemon/commands/call.cc +++ b/daemon/commands/call.cc @@ -1,7 +1,5 @@ #include "call.h" -#include - using namespace std; CallCommand::CallCommand() : diff --git a/daemon/commands/ptime.cc b/daemon/commands/ptime.cc index 50c1db8d2..e1456a62d 100644 --- a/daemon/commands/ptime.cc +++ b/daemon/commands/ptime.cc @@ -1,7 +1,5 @@ #include "ptime.h" -#include - using namespace std; PtimeCommand::PtimeCommand() : diff --git a/daemon/commands/quit.cc b/daemon/commands/quit.cc index 584f8495c..db07610df 100644 --- a/daemon/commands/quit.cc +++ b/daemon/commands/quit.cc @@ -1,7 +1,5 @@ #include "quit.h" -#include - using namespace std; QuitCommand::QuitCommand() : diff --git a/daemon/commands/register.cc b/daemon/commands/register.cc index 75f76478a..a08c84f32 100644 --- a/daemon/commands/register.cc +++ b/daemon/commands/register.cc @@ -1,7 +1,5 @@ #include "register.h" -#include - using namespace std; RegisterCommand::RegisterCommand() : diff --git a/daemon/daemon.cc b/daemon/daemon.cc index 6bb826ca4..59b4ec568 100644 --- a/daemon/daemon.cc +++ b/daemon/daemon.cc @@ -2,8 +2,10 @@ #include #include +#ifdef HAVE_READLINE #include #include +#endif #include @@ -30,6 +32,31 @@ using namespace std; +#ifndef WIN32 +#else +#include +void usleep(int waitTime) { + Sleep(waitTime/1000); +} +#endif + +#ifdef HAVE_READLINE +#define LICENCE_GPL +#else +#define LICENCE_COMMERCIAL +#endif + +void *Daemon::iterateThread(void *arg) { + Daemon *daemon = (Daemon *) arg; + while (daemon->mRunning) { + ms_mutex_lock(&daemon->mMutex); + daemon->iterate(); + ms_mutex_unlock(&daemon->mMutex); + usleep(20000); + } + return 0; +} + EventResponse::EventResponse(Daemon *daemon, LinphoneCall *call, LinphoneCallState state) { ostringstream ostr; char *remote = linphone_call_get_remote_address_as_string(call); @@ -151,18 +178,21 @@ bool DaemonCommand::matches(const char *name) const { return strcmp(name, mName.c_str()) == 0; } -Daemon * Daemon::sZis = NULL; -int Daemon::sCallIds = 0; -int Daemon::sProxyIds = 0; -int Daemon::sAudioStreamIds = 0; - Daemon::Daemon(const char *config_path, const char *factory_config_path, const char *log_file, const char *pipe_name, bool display_video, bool capture_video) : - mLogFile(NULL) { - sZis = this; + mLogFile(NULL), mCallIds(0), mProxyIds(0), mAudioStreamIds(0) { + ms_mutex_init(&mMutex, NULL); mServerFd = -1; mChildFd = -1; if (pipe_name == NULL) { - initReadline(); +#ifdef HAVE_READLINE + const char *homedir = getenv("HOME"); + rl_readline_name = "daemon"; + if (homedir == NULL) + homedir = "."; + mHistfile = string(homedir) + string("/.linphone_history"); + read_history(mHistfile.c_str()); + setlinebuf(stdout); +#endif } else { mServerFd = ortp_server_pipe_create(pipe_name); listen(mServerFd, 2); @@ -198,8 +228,8 @@ LinphoneCore *Daemon::getCore() { int Daemon::updateCallId(LinphoneCall *call) { int val = (int) (long) linphone_call_get_user_pointer(call); if (val == 0) { - linphone_call_set_user_pointer(call, (void*) (long) ++sCallIds); - return sCallIds; + linphone_call_set_user_pointer(call, (void*) (long) ++mCallIds); + return mCallIds; } return val; } @@ -217,8 +247,8 @@ LinphoneCall *Daemon::findCall(int id) { int Daemon::updateProxyId(LinphoneProxyConfig *cfg) { int val = (int) (long) linphone_proxy_config_get_user_data(cfg); if (val == 0) { - linphone_proxy_config_set_user_data(cfg, (void*) (long) ++sProxyIds); - return sProxyIds; + linphone_proxy_config_set_user_data(cfg, (void*) (long) ++mProxyIds); + return mProxyIds; } return val; } @@ -239,9 +269,9 @@ int Daemon::updateAudioStreamId(AudioStream *audio_stream) { return it->first; } - ++sProxyIds; - mAudioStreams.insert(std::pair(sProxyIds, audio_stream)); - return sProxyIds; + ++mProxyIds; + mAudioStreams.insert(std::pair(mProxyIds, audio_stream)); + return mProxyIds; } AudioStream *Daemon::findAudioStream(int id) { @@ -346,32 +376,15 @@ void Daemon::iterate() { } } -int Daemon::readlineHook() { - sZis->iterate(); - return 0; -} - -void Daemon::initReadline() { - const char *homedir = getenv("HOME"); - rl_readline_name = "daemon"; - - rl_set_keyboard_input_timeout(20000); - rl_event_hook = readlineHook; - - if (homedir == NULL) - homedir = "."; - mHistfile = string(homedir) + string("/.linphone_history"); - read_history(mHistfile.c_str()); - setlinebuf(stdout); -} - void Daemon::execCommand(const char *cl) { char args[sLineSize] = { 0 }; char name[sLineSize] = { 0 }; sscanf(cl, "%511s %511[^\n]", name, args); //Read the rest of line in args list::iterator it = find_if(mCommands.begin(), mCommands.end(), bind2nd(mem_fun(&DaemonCommand::matches), name)); if (it != mCommands.end()) { + ms_mutex_lock(&mMutex); (*it)->exec(this, args); + ms_mutex_unlock(&mMutex); } else { sendResponse(Response("Unknown command.")); } @@ -441,6 +454,17 @@ char *Daemon::readPipe(char *buffer, int buflen) { static void printHelp() { fprintf(stdout, "daemon-linphone []\n" +#if defined(LICENCE_GPL) || defined(LICENCE_COMMERCIAL) + "Licence: " +#ifdef LICENCE_GPL + "GPL" +#endif +#ifdef LICENCE_COMMERCIAL + "Commercial" +#endif + "\n" +#endif + "where options are :\n" "\t--help\t\t\tPrint this notice.\n" "\t--pipe \tCreate an unix server socket to receive commands.\n" @@ -451,15 +475,33 @@ static void printHelp() { "\t-D\t\t\tenable video display.\n"); } +void Daemon::startThread() { + ms_thread_create(&this->mThread, NULL, Daemon::iterateThread, this); +} + +char *Daemon::readLine(const char *prompt) { +#ifdef HAVE_READLINE + return readline(prompt); +#else + cout << prompt; + char *buff = (char *) malloc(sLineSize); + cin.getline(buff, sLineSize); + return buff; +#endif +} + int Daemon::run() { char line[sLineSize] = "daemon-linphone>"; char *ret; mRunning = true; + startThread(); while (mRunning) { if (mServerFd == -1) { - ret = readline(line); + ret = readLine(line); if (ret && ret[0] != '\0') { +#ifdef HAVE_READLINE add_history(ret); +#endif } } else { ret = readPipe(line, sLineSize); @@ -467,13 +509,19 @@ int Daemon::run() { if (ret && ret[0] != '\0') { execCommand(ret); } - if (mServerFd == -1) { + if (mServerFd == -1 && ret != NULL) { free(ret); } } + stopThread(); return 0; } +void Daemon::stopThread() { + void *ret; + ms_thread_join(mThread, &ret); +} + void Daemon::quit() { mRunning = false; } @@ -497,8 +545,12 @@ Daemon::~Daemon() { fclose(mLogFile); } + ms_mutex_destroy(&mMutex); + +#ifdef HAVE_READLINE stifle_history(30); write_history(mHistfile.c_str()); +#endif } int main(int argc, char *argv[]) { diff --git a/daemon/daemon.h b/daemon/daemon.h index 0288fbe74..8405c58fe 100644 --- a/daemon/daemon.h +++ b/daemon/daemon.h @@ -3,11 +3,34 @@ #include #include +#include #include #include #include #include +#include + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_READLINE_H +#include +#define HAVE_READLINE +#else +#ifdef HAVE_READLINE_READLINE_H +#include +#define HAVE_READLINE +#endif +#endif +#ifdef HAVE_HISTORY_H +#include +#else +#ifdef HAVE_READLINE_HISTORY_H +#include +#endif +#endif class Daemon; @@ -121,17 +144,19 @@ public: int updateProxyId(LinphoneProxyConfig *proxy); int updateAudioStreamId(AudioStream *audio_stream); private: + static void* iterateThread(void *arg); static void callStateChanged(LinphoneCore *lc, LinphoneCall *call, LinphoneCallState state, const char *msg); static void callStatsUpdated(LinphoneCore *lc, LinphoneCall *call, const LinphoneCallStats *stats); static void dtmfReceived(LinphoneCore *lc, LinphoneCall *call, int dtmf); - static int readlineHook(); void callStateChanged(LinphoneCall *call, LinphoneCallState state, const char *msg); void callStatsUpdated(LinphoneCall *call, const LinphoneCallStats *stats); void dtmfReceived(LinphoneCall *call, int dtmf); void execCommand(const char *cl); - void initReadline(); + char *readLine(const char *); char *readPipe(char *buffer, int buflen); void iterate(); + void startThread(); + void stopThread(); void initCommands(); void uninitCommands(); LinphoneCore *mLc; @@ -142,10 +167,11 @@ private: std::string mHistfile; bool mRunning; FILE *mLogFile; - static Daemon *sZis; - static int sCallIds; - static int sProxyIds; - static int sAudioStreamIds; + int mCallIds; + int mProxyIds; + int mAudioStreamIds; + ms_thread_t mThread; + ms_mutex_t mMutex; static const int sLineSize = 512; std::map mAudioStreams; };