summaryrefslogtreecommitdiff
path: root/orkaudio/OrkAudio.cpp
blob: 45a14342c1042f982c336013cd67ea1b7cd637b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Oreka -- A media capture and retrieval platform
 * 
 * Copyright (C) 2005, orecx LLC
 *
 * http://www.orecx.com
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License.
 * Please refer to http://www.gnu.org/copyleft/gpl.html
 *
 */


#include "stdio.h"

#include "MultiThreadedServer.h"
#include "ace/Thread_Manager.h"
#include "OrkAudio.h"
#include "Utils.h"
#include "messages/TapeMsg.h"
#include "messages/PingMsg.h"
#include "messages/DeleteTapeMsg.h"
#include "messages/CaptureMsg.h"
#include "messages/TestMsg.h"
#include "Config.h"
#include "LogManager.h"
#include "ImmediateProcessing.h"
#include "BatchProcessing.h"
#include "Reporting.h"
#include "ConfigManager.h"
#include "Daemon.h"
#include "ObjectFactory.h"
#include "CapturePluginProxy.h"
#include "ace/OS_NS_arpa_inet.h"


static volatile bool serviceStop = false;

void StopHandler()
{
	serviceStop = true;
}

void MainThread()
{
	LogManagerSingleton::instance()->Initialize();
	LOG4CXX_INFO(LOG.rootLog, CStdString("\n\nOrkAudio service starting\n"));

	// Initialize object factory and register existing objects
	ObjectFactorySingleton::instance()->Initialize();

	ObjectRef objRef;
	objRef.reset(new PingMsg);
	ObjectFactorySingleton::instance()->RegisterObject(objRef);
	objRef.reset(new TapeMsg);
	ObjectFactorySingleton::instance()->RegisterObject(objRef);
	objRef.reset(new SimpleResponseMsg);
	ObjectFactorySingleton::instance()->RegisterObject(objRef);
	objRef.reset(new DeleteTapeMsg);
	ObjectFactorySingleton::instance()->RegisterObject(objRef);
	objRef.reset(new CaptureMsg);
	ObjectFactorySingleton::instance()->RegisterObject(objRef);
	objRef.reset(new TestMsg);
	ObjectFactorySingleton::instance()->RegisterObject(objRef);

	ConfigManagerSingleton::instance()->Initialize();

	if (!ACE_Thread_Manager::instance()->spawn(ACE_THR_FUNC(ImmediateProcessing::ThreadHandler)))
	{
		LOG4CXX_INFO(LOG.rootLog, CStdString("Failed to create immediate processing thread"));
	}
	if(CONFIG.m_storageAudioFormat != AudioTape::FfNative)
	{
		// storage format is not native, which means we need batch workers to compress to wanted format 
		if (!ACE_Thread_Manager::instance()->spawn_n(CONFIG.m_numBatchThreads, ACE_THR_FUNC(BatchProcessing::ThreadHandler)))
		{
			LOG4CXX_INFO(LOG.rootLog, CStdString("Failed to create batch processing thread"));
		}
	}
	if (!ACE_Thread_Manager::instance()->spawn(ACE_THR_FUNC(Reporting::ThreadHandler)))
	{
		LOG4CXX_INFO(LOG.rootLog, CStdString("Failed to create reporting thread"));
	}
	// Create command line server on port 10000
	if (!ACE_Thread_Manager::instance()->spawn(ACE_THR_FUNC(CommandLineServer::run), (void *)10000))
	{
		LOG4CXX_INFO(LOG.rootLog, CStdString("Failed to create command line server"));
	}

	// Create Http server on port 20000
	if (!ACE_Thread_Manager::instance()->spawn(ACE_THR_FUNC(HttpServer::run), (void *)20000))
	{
		LOG4CXX_INFO(LOG.rootLog, CStdString("Failed to create Http server"));
	}

	if(CapturePluginProxySingleton::instance()->Initialize())
	{
		CapturePluginProxySingleton::instance()->Run();
	}

	//ACE_Thread_Manager::instance ()->wait ();
	while(serviceStop == false)
	{
		ACE_OS::sleep(1);
	}
	LOG4CXX_INFO(LOG.rootLog, CStdString("Stopping service"));
}


int main(int argc, char* argv[])
{
	// figure out service name
	CStdString program(argv[0]);
	CStdString serviceNameWithExtension = BaseName(program);
	CStdString serviceName = StripFileExtension(serviceNameWithExtension);
	if (serviceName.IsEmpty())
	{
		return -1;
	}

	DaemonSingleton::instance()->Initialize(serviceName, MainThread, StopHandler);
	CStdString argument = argv[1];

	if (argc>1)
	{
		if (argument.CompareNoCase("debug") == 0)
		{
			MainThread();
		}
		else if (argument.CompareNoCase("install") == 0)
		{
			DaemonSingleton::instance()->Install();
		}
		else if  (argument.CompareNoCase("uninstall") == 0)
		{
			DaemonSingleton::instance()->Uninstall();
		}
		else
		{
			printf("Argument incorrect. Possibilies are:\n\tdebug\n\tinstall\n\tuninstall\n");
		}
	}
	else
	{
		// No arguments, launch the daemon
		DaemonSingleton::instance()->Start();
	}
	return 0;
}