Emeric Vigier | 2f62582 | 2012-08-06 11:09:52 -0400 | [diff] [blame] | 1 | // Copyright (C) 2000-2005 Open Source Telecom Corporation. |
| 2 | // Copyright (C) 2006-2008 David Sugar, Tycho Softworks. |
| 3 | // |
| 4 | // This program is free software; you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation; either version 2 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program; if not, write to the Free Software |
| 16 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | |
| 18 | #include <cc++/slog.h> |
| 19 | #include <cc++/process.h> |
| 20 | #include "server.h" |
| 21 | #include <iostream> |
| 22 | #include <fstream> |
| 23 | |
| 24 | using namespace std; |
| 25 | |
| 26 | #ifdef CCXX_NAMESPACES |
| 27 | namespace ost { |
| 28 | using namespace std; |
| 29 | #endif |
| 30 | |
| 31 | void server(void) |
| 32 | { |
| 33 | const char *reason = "exiting"; |
| 34 | char *cp, *ep; |
| 35 | std::fstream fifo; |
| 36 | new RTPAudio; |
| 37 | |
| 38 | ::signal(SIGPIPE, SIG_IGN); |
| 39 | |
| 40 | int fd; |
| 41 | char buf[256]; |
| 42 | |
| 43 | ::remove(".phonepid"); |
| 44 | |
| 45 | if(daemon) |
| 46 | { |
| 47 | close(0); |
| 48 | close(1); |
| 49 | close(2); |
| 50 | Process::detach(); |
| 51 | open("/dev/null", O_RDWR); |
| 52 | open("/dev/null", O_RDWR); |
| 53 | open("/dev/null", O_RDWR); |
| 54 | slog.open("phone", Slog::classDaemon); |
| 55 | slog.level(Slog::levelNotice); |
| 56 | slog(Slog::levelNotice) << "daemon mode started" << std::endl; |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | slog.open("phone", Slog::classDaemon); |
| 61 | slog.level(Slog::levelDebug); |
| 62 | slog(Slog::levelNotice) << "server starting..." << std::endl; |
| 63 | } |
| 64 | snprintf(buf, 11, "%d", getpid()); |
| 65 | fd = ::creat(".phonepid", 0660); |
| 66 | if(fd > -1) |
| 67 | { |
| 68 | ::write(fd, buf, 10); |
| 69 | ::close(fd); |
| 70 | } |
| 71 | fifo.open(".phonectrl", std::ios::in | std::ios::out); |
| 72 | if(!fifo.is_open()) |
| 73 | { |
| 74 | slog(Slog::levelError) << "fifo failed; exiting" << std::endl; |
| 75 | exit(-1); |
| 76 | } |
| 77 | |
| 78 | rtp->startRunning(); // we assume it's always running |
| 79 | while(!fifo.eof()) |
| 80 | { |
| 81 | fifo.getline(buf, 256); |
| 82 | cp = buf; |
| 83 | while(isspace(*cp)) |
| 84 | ++cp; |
| 85 | ep = strrchr(cp, '\n'); |
| 86 | if(ep) |
| 87 | *ep = 0; |
| 88 | if(!*cp) |
| 89 | continue; |
| 90 | slog(Slog::levelDebug) << "fifo: " << cp << std::endl; |
| 91 | if(!strnicmp(cp, "exit", 4)) |
| 92 | break; |
| 93 | |
| 94 | } |
| 95 | rtp->exit(reason); |
| 96 | fifo.close(); |
| 97 | slog(Slog::levelWarning) << "server exiting..." << std::endl; |
| 98 | exit(0); |
| 99 | } |
| 100 | |
| 101 | #ifdef CCXX_NAMESPACES |
| 102 | } |
| 103 | #endif |