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