-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.cpp
More file actions
221 lines (200 loc) · 7.24 KB
/
Copy pathservice.cpp
File metadata and controls
221 lines (200 loc) · 7.24 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "service.h"
extern const std::string version;
Service::Service(int argc, char **argv) :
QtService<Application>(argc, argv, "Multimedia Controller"),
mSettings(NULL),
mDaemon(NULL)
{
registerTypes();
setServiceDescription("Multimedia Controller");
setServiceFlags(NeedsStopOnShutdown);
}
Service::State Service::state()
{
Service::State out = Service::Undefined;
/**/ if (mParamList.value("isHelp" ).toBool())
out = Service::Help;
else if (mParamList.value("isVers" ).toBool())
out = Service::Vers;
else if (mParamList.value("isStart").toBool()
&& mParamList.value("port" ).toInt() != 0)
out = Service::Start;
else if (mParamList.value("isStop" ).toBool()
&& mParamList.value("port" ).toInt() != 0)
out = Service::Stop;
return out;
}
int Service::help()
{
std::cout << version << std::endl;
std::cout << std::endl;
std::cout << " Syntax: evalnetd [options]" << std::endl;
std::cout << std::endl;
std::cout << " opions:" << std::endl;
std::cout << " --start start server" << std::endl;
std::cout << " --stop stop server" << std::endl;
std::cout << std::endl;
std::cout << " -s, --server server neme (OPTIONAL for --stop)" << std::endl;
std::cout << " -p, --port server port (REQUIRED)" << std::endl;
std::cout << std::endl;
std::cout << " -a, --path multimedia content path list" << std::endl;
std::cout << std::endl;
std::cout << " -h, --help list of command line parameters" << std::endl;
std::cout << " -v, --version display version information" << std::endl;
std::cout << std::endl;
std::cout << "NOTE: - if no parameter is set, default configuration is loaded" << std::endl;
std::cout << " - --path is a comma or semicolon separated list usable multiple times" << std::endl;
return 0;
}
int Service::vers()
{
std::cout << version << std::endl;
return 0;
}
int Service::terminate()
{
return remoteStop(mParamList.value("server").toString(),
mParamList.value("port" ).toInt());
}
int Service::execute()
{
// get application
QCoreApplication *app = application();
// get variables
int port = mParamList.value("port").toInt();
QStringList path = mParamList.value("path").toStringList();
// store settings -> only if not custom config
if (mSettings != NULL)
{
mSettings->setValue("port",port);
mSettings->setValue("path",path);
}
// create object
mDaemon = new Server(new Executer,new ResourceContainer(path));
mDaemon->listen(QHostAddress::Any,port);
// check if created and listening
if (!mDaemon || !mDaemon->isListening())
app->exit(2);
// return
return 0;
}
int Service::undefined()
{
std::cout << "Command line parameter error" << std::endl;
std::cout << "For help type: \"mc -h\"" << std::endl;
return 1;
}
void Service::start()
{
// get application and data
Application *app = application();
mSettings = new QSettings(app->applicationDirPath()+"/../mc.conf",QSettings::IniFormat);
// load parameters
loadParam();
// execute
switch (state())
{
case Service::Help:
app->exit(help());
case Service::Vers:
app->exit(vers());
case Service::Start:
execute();
break;
case Service::Stop:
app->exit(terminate());
default:
app->exit(undefined());
}
}
void Service::stop()
{
if (mDaemon != NULL)
mDaemon->stop();
}
void Service::loadParam()
{
// convert arguments
Application *app = application();
int argc = app->argc();
char **argv = app->argv();
// init
mParamList["isHelp" ] = QVariant(false );
mParamList["isVers" ] = QVariant(false );
mParamList["isStart"] = QVariant(false );
mParamList["isStop" ] = QVariant(false );
mParamList["server" ] = QVariant(QString() );
mParamList["port" ] = QVariant(0 );
mParamList["path" ] = QVariant(QStringList());
// load config file
/**/ if (argc == 1 && mSettings != NULL)
{
mParamList["isStart"] = QVariant(true);
mParamList["port" ] = mSettings->value("port",mParamList.value("port"));
mParamList["path" ] = mSettings->value("path",mParamList.value("path"));
}
// parse input arguments
else if (argc > 1)
{
// create parameter list
static struct option longOpt[] = {{"help", 0,0,'h'},
{"version",0,0,'v'},
{"server", 1,0,'s'},
{"port", 1,0,'p'},
{"path", 1,0,'a'},
{"start", 0,0, 0 },
{"stop", 0,0, 0 },
{0, 0,0, 0 }};
// parameter processing
int c;
int optIdx = 0;
while ((c = getopt_long(argc,argv,"hvs:p:a:0",longOpt,&optIdx)) != -1)
{
switch (c) {
case 0:
if (strcmp(longOpt[optIdx].name,"start" ) == 0)
mParamList["isStart" ] = true;
if (strcmp(longOpt[optIdx].name,"stop" ) == 0)
mParamList["isStop" ] = true;
break;
case 'h':
mParamList["isHelp" ] = true;
break;
case 'v':
mParamList["isVers" ] = true;
break;
case 's':
mParamList["server" ] = optarg;
break;
case 'p':
mParamList["port" ] = atoi(optarg);
break;
case 'a':
{
QStringList path = mParamList.value("path").toStringList() << QString(optarg).split(QRegExp(",|;"));
path.removeDuplicates();
mParamList["path" ] = path;
break;
}
case '?':
break;
default:
printf ("?? getopt returned character code 0%o ??\n", c);
}
}
// not identified arguments
if (optind < argc) {
printf ("non-option ARGV-elements: ");
while (optind < argc)
printf ("%s ", argv[optind++]);
printf ("\n");
}
}
}
void Service::registerTypes()
{
qRegisterMetaType<StatusContainer>();
qRegisterMetaType<ImageContainer>();
qRegisterMetaType<CommandContainer>();
qRegisterMetaType<ListContainer>();
}