2006-12-18 15:39:33 +01:00
|
|
|
/******************************************************************************\
|
2009-02-22 12:07:18 +01:00
|
|
|
* Copyright (c) 2004-2009
|
2006-12-18 15:39:33 +01:00
|
|
|
*
|
|
|
|
* Author(s):
|
|
|
|
* Volker Fischer
|
|
|
|
*
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation; either version 2 of the License, or (at your option) any later
|
|
|
|
* version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
\******************************************************************************/
|
|
|
|
|
|
|
|
#include <qapplication.h>
|
2008-07-13 20:43:40 +02:00
|
|
|
#include <qmessagebox.h>
|
2007-09-09 11:50:22 +02:00
|
|
|
#include <iostream>
|
2006-12-18 15:39:33 +01:00
|
|
|
#include "global.h"
|
|
|
|
#include "llconclientdlg.h"
|
2006-01-28 12:29:22 +01:00
|
|
|
#include "llconserverdlg.h"
|
2006-12-18 15:39:33 +01:00
|
|
|
#include "settings.h"
|
|
|
|
|
|
|
|
|
2008-12-02 19:02:40 +01:00
|
|
|
// Implementation **************************************************************
|
2008-01-22 20:58:53 +01:00
|
|
|
// these pointers are only used for the post-event routine
|
|
|
|
QApplication* pApp = NULL;
|
|
|
|
QDialog* pMainWindow = NULL;
|
2006-12-18 15:39:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
int main ( int argc, char** argv )
|
2006-02-17 20:07:10 +01:00
|
|
|
{
|
2009-02-22 12:07:18 +01:00
|
|
|
std::string strArgument;
|
2008-07-22 17:17:19 +02:00
|
|
|
double rDbleArgument;
|
2006-02-17 20:07:10 +01:00
|
|
|
|
2007-09-09 11:50:22 +02:00
|
|
|
/* check if server or client application shall be started */
|
2008-08-08 20:51:25 +02:00
|
|
|
bool bIsClient = true;
|
|
|
|
bool bUseGUI = true;
|
2008-08-19 21:59:59 +02:00
|
|
|
bool bForceLowUploadRate = false;
|
2009-03-01 12:17:35 +01:00
|
|
|
quint16 iPortNumber = LLCON_DFAULT_PORT_NUMBER;
|
2009-02-22 12:07:18 +01:00
|
|
|
std::string strIniFileName = "";
|
2008-08-08 20:51:25 +02:00
|
|
|
std::string strHTMLStatusFileName = "";
|
2008-08-09 09:55:27 +02:00
|
|
|
std::string strServerName = "";
|
2008-12-02 19:02:40 +01:00
|
|
|
std::string strLoggingFileName = "";
|
2007-05-06 14:27:41 +02:00
|
|
|
|
2009-02-22 12:07:18 +01:00
|
|
|
/* QT docu: argv()[0] is the program name, argv()[1] is the first
|
|
|
|
argument and argv()[argc()-1] is the last argument.
|
|
|
|
Start with first argument, therefore "i = 1" */
|
|
|
|
for ( int i = 1; i < argc; i++ )
|
|
|
|
{
|
|
|
|
// server mode flag ----------------------------------------------------------
|
|
|
|
if ( GetFlagArgument ( argc, argv, i, "-s", "--server" ) )
|
|
|
|
{
|
|
|
|
bIsClient = false;
|
2009-03-01 12:17:35 +01:00
|
|
|
cout << "server mode chosen" << std::endl;
|
2009-02-22 12:07:18 +01:00
|
|
|
continue;
|
|
|
|
}
|
2008-01-15 23:54:04 +01:00
|
|
|
|
2009-02-22 12:07:18 +01:00
|
|
|
// use GUI flag --------------------------------------------------------------
|
|
|
|
if ( GetFlagArgument ( argc, argv, i, "-n", "--nogui" ) )
|
|
|
|
{
|
|
|
|
bUseGUI = false;
|
2009-03-01 12:17:35 +01:00
|
|
|
cout << "no GUI mode chosen" << std::endl;
|
2009-02-22 12:07:18 +01:00
|
|
|
continue;
|
|
|
|
}
|
2008-01-15 23:54:04 +01:00
|
|
|
|
2009-02-22 12:07:18 +01:00
|
|
|
// use logging ---------------------------------------------------------------
|
|
|
|
if ( GetStringArgument ( argc, argv, i, "-l", "--log", strArgument ) )
|
|
|
|
{
|
|
|
|
strLoggingFileName = strArgument;
|
2009-03-01 12:17:35 +01:00
|
|
|
cout << "logging file name: " << strLoggingFileName << std::endl;
|
2009-02-22 12:07:18 +01:00
|
|
|
continue;
|
|
|
|
}
|
2008-01-15 23:54:04 +01:00
|
|
|
|
2009-02-22 12:07:18 +01:00
|
|
|
// force low upload data rate flag -------------------------------------------
|
2008-08-19 21:59:59 +02:00
|
|
|
if ( GetFlagArgument ( argc, argv, i, "-u", "--lowuploadrate" ) )
|
|
|
|
{
|
2009-02-22 12:07:18 +01:00
|
|
|
bForceLowUploadRate = true;
|
2009-03-01 12:17:35 +01:00
|
|
|
cout << "force low upload rate" << std::endl;
|
2009-02-22 12:07:18 +01:00
|
|
|
continue;
|
2008-08-19 21:59:59 +02:00
|
|
|
}
|
|
|
|
|
2008-12-02 19:02:40 +01:00
|
|
|
// port number ---------------------------------------------------------------
|
2009-02-22 12:07:18 +01:00
|
|
|
if ( GetNumericArgument ( argc, argv, i, "-p", "--port",
|
2008-07-22 17:17:19 +02:00
|
|
|
0, 65535, rDbleArgument ) )
|
2009-02-22 12:07:18 +01:00
|
|
|
{
|
|
|
|
iPortNumber = static_cast<quint16> ( rDbleArgument );
|
2009-03-01 12:17:35 +01:00
|
|
|
cout << "selected port number: " << iPortNumber << std::endl;
|
2009-02-22 12:07:18 +01:00
|
|
|
continue;
|
|
|
|
}
|
2008-01-15 23:54:04 +01:00
|
|
|
|
2009-02-22 12:07:18 +01:00
|
|
|
// HTML status file ----------------------------------------------------------
|
|
|
|
if ( GetStringArgument ( argc, argv, i, "-m", "--htmlstatus", strArgument ) )
|
|
|
|
{
|
|
|
|
strHTMLStatusFileName = strArgument;
|
2009-03-01 12:17:35 +01:00
|
|
|
cout << "HTML status file name: " << strHTMLStatusFileName << std::endl;
|
2009-02-22 12:07:18 +01:00
|
|
|
continue;
|
|
|
|
}
|
2008-08-08 20:51:25 +02:00
|
|
|
|
2009-02-22 12:07:18 +01:00
|
|
|
if ( GetStringArgument ( argc, argv, i, "-a", "--servername", strArgument ) )
|
|
|
|
{
|
|
|
|
strServerName = strArgument;
|
2009-03-01 12:17:35 +01:00
|
|
|
cout << "server name for HTML status file: " << strServerName << std::endl;
|
2009-02-22 12:07:18 +01:00
|
|
|
continue;
|
|
|
|
}
|
2008-08-09 09:55:27 +02:00
|
|
|
|
2009-02-22 12:07:18 +01:00
|
|
|
// initialization file -------------------------------------------------------
|
|
|
|
if ( GetStringArgument ( argc, argv, i, "-i", "--inifile", strArgument ) )
|
|
|
|
{
|
|
|
|
strIniFileName = strArgument;
|
2009-03-01 12:17:35 +01:00
|
|
|
cout << "initialization file name: " << strIniFileName << std::endl;
|
2009-02-22 12:07:18 +01:00
|
|
|
continue;
|
|
|
|
}
|
2008-01-15 23:54:04 +01:00
|
|
|
|
2009-02-22 12:07:18 +01:00
|
|
|
// help (usage) flag ---------------------------------------------------------
|
|
|
|
if ( ( !strcmp ( argv[i], "--help" ) ) ||
|
|
|
|
( !strcmp ( argv[i], "-h" ) ) || ( !strcmp ( argv[i], "-?" ) ) )
|
|
|
|
{
|
2008-01-15 23:54:04 +01:00
|
|
|
const std::string strHelp = UsageArguments(argv);
|
2009-03-01 12:17:35 +01:00
|
|
|
cout << strHelp;
|
2009-02-22 12:07:18 +01:00
|
|
|
exit ( 1 );
|
|
|
|
}
|
2008-01-15 23:54:04 +01:00
|
|
|
|
2009-02-22 12:07:18 +01:00
|
|
|
// unknown option ------------------------------------------------------------
|
|
|
|
cerr << argv[0] << ": ";
|
|
|
|
cerr << "Unknown option '" << argv[i] << "' -- use '--help' for help"
|
|
|
|
<< endl;
|
2008-01-15 23:54:04 +01:00
|
|
|
|
2009-02-22 12:07:18 +01:00
|
|
|
exit ( 1 );
|
|
|
|
}
|
2006-02-17 20:07:10 +01:00
|
|
|
|
2008-10-31 21:27:55 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
// Set application priority class -> high priority
|
|
|
|
SetPriorityClass ( GetCurrentProcess(), HIGH_PRIORITY_CLASS );
|
|
|
|
#endif
|
|
|
|
|
2008-01-15 23:54:04 +01:00
|
|
|
// Application object
|
2006-11-25 15:46:57 +01:00
|
|
|
QApplication app ( argc, argv, bUseGUI );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2008-01-20 19:07:13 +01:00
|
|
|
// init resources
|
|
|
|
extern int qInitResources();
|
|
|
|
qInitResources();
|
|
|
|
|
2008-07-13 20:43:40 +02:00
|
|
|
try
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2008-07-13 20:43:40 +02:00
|
|
|
if ( bIsClient )
|
|
|
|
{
|
|
|
|
// client
|
|
|
|
// actual client object
|
2008-08-13 21:08:45 +02:00
|
|
|
CClient Client ( iPortNumber );
|
2006-01-28 12:29:22 +01:00
|
|
|
|
2008-07-13 20:43:40 +02:00
|
|
|
// load settings from init-file
|
|
|
|
CSettings Settings ( &Client );
|
2008-01-26 11:38:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
// TODO use QString
|
|
|
|
|
2008-07-13 20:43:40 +02:00
|
|
|
Settings.Load ( strIniFileName.c_str() );
|
2006-01-28 12:29:22 +01:00
|
|
|
|
2008-07-13 20:43:40 +02:00
|
|
|
// GUI object
|
2008-08-13 18:45:35 +02:00
|
|
|
CLlconClientDlg ClientDlg ( &Client, 0, Qt::WindowMinMaxButtonsHint );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2008-07-13 20:43:40 +02:00
|
|
|
// set main window
|
|
|
|
pMainWindow = &ClientDlg;
|
|
|
|
pApp = &app; // Needed for post-event routine
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2008-07-13 20:43:40 +02:00
|
|
|
// show dialog
|
|
|
|
ClientDlg.show();
|
|
|
|
app.exec();
|
2006-01-28 12:29:22 +01:00
|
|
|
|
2008-07-13 20:43:40 +02:00
|
|
|
// save settings to init-file
|
2008-01-26 11:38:18 +01:00
|
|
|
|
|
|
|
// TODO use QString
|
|
|
|
|
2008-07-13 20:43:40 +02:00
|
|
|
Settings.Save ( strIniFileName.c_str() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// server
|
|
|
|
// actual server object
|
2008-08-08 20:51:25 +02:00
|
|
|
|
|
|
|
// TODO use QString
|
|
|
|
|
2008-12-02 19:02:40 +01:00
|
|
|
CServer Server ( strLoggingFileName.c_str(),
|
|
|
|
iPortNumber,
|
2008-08-09 09:55:27 +02:00
|
|
|
strHTMLStatusFileName.c_str(),
|
2008-08-19 21:59:59 +02:00
|
|
|
strServerName.c_str(),
|
|
|
|
bForceLowUploadRate );
|
2008-07-13 20:43:40 +02:00
|
|
|
|
|
|
|
if ( bUseGUI )
|
|
|
|
{
|
|
|
|
// GUI object for the server
|
|
|
|
CLlconServerDlg ServerDlg ( &Server, 0 );
|
|
|
|
|
|
|
|
// set main window
|
|
|
|
pMainWindow = &ServerDlg;
|
|
|
|
pApp = &app; // needed for post-event routine
|
|
|
|
|
|
|
|
// show dialog
|
|
|
|
ServerDlg.show();
|
|
|
|
app.exec();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// only start application without using the GUI
|
2009-03-08 08:26:01 +01:00
|
|
|
cout << CAboutDlg::GetVersionAndNameStr ( false ).toStdString() << std::endl;
|
2008-07-13 20:43:40 +02:00
|
|
|
app.exec();
|
|
|
|
}
|
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
2006-02-17 20:07:10 +01:00
|
|
|
|
2008-07-13 20:43:40 +02:00
|
|
|
catch ( CGenErr generr )
|
|
|
|
{
|
|
|
|
// show generic error
|
2006-11-25 15:46:57 +01:00
|
|
|
if ( bUseGUI )
|
|
|
|
{
|
2008-07-22 17:17:19 +02:00
|
|
|
QMessageBox::critical ( 0, APP_NAME, generr.GetErrorText(), "Quit", 0 );
|
2006-11-25 15:46:57 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-07-22 17:17:19 +02:00
|
|
|
qDebug() << generr.GetErrorText();
|
2006-11-25 15:46:57 +01:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-09-09 11:50:22 +02:00
|
|
|
|
|
|
|
/******************************************************************************\
|
|
|
|
* Command Line Argument Parsing *
|
|
|
|
\******************************************************************************/
|
2008-01-15 23:54:04 +01:00
|
|
|
std::string UsageArguments ( char **argv )
|
|
|
|
{
|
2009-02-22 12:07:18 +01:00
|
|
|
return
|
|
|
|
"Usage: " + std::string ( argv[0] ) + " [option] [argument]\n"
|
|
|
|
"Recognized options:\n"
|
|
|
|
" -s, --server start server\n"
|
|
|
|
" -n, --nogui disable GUI (only avaiable for server)\n"
|
|
|
|
" -l, --log enable logging, set file name\n"
|
|
|
|
" -i, --inifile initialization file name (only available for client)\n"
|
|
|
|
" -p, --port local port number (only avaiable for server)\n"
|
|
|
|
" -m, --htmlstatus enable HTML status file, set file name (only avaiable for server)\n"
|
|
|
|
" -u, --lowuploadrate force low upload rate (only avaiable for server)\n"
|
|
|
|
" -h, -?, --help this help text\n"
|
|
|
|
"Example: " + std::string ( argv[0] ) + " -l -inifile myinifile.ini\n";
|
2008-01-15 23:54:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GetFlagArgument ( int, char **argv, int &i,
|
|
|
|
std::string strShortOpt, std::string strLongOpt )
|
|
|
|
{
|
2009-02-22 12:07:18 +01:00
|
|
|
if ( ( !strShortOpt.compare ( argv[i] ) ) || ( !strLongOpt.compare ( argv[i] ) ) )
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2008-01-15 23:54:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GetStringArgument ( int argc, char **argv, int &i,
|
|
|
|
std::string strShortOpt, std::string strLongOpt,
|
|
|
|
std::string & strArg )
|
|
|
|
{
|
2009-02-22 12:07:18 +01:00
|
|
|
if ( ( !strShortOpt.compare ( argv[i] ) ) || ( !strLongOpt.compare ( argv[i] ) ) )
|
|
|
|
{
|
|
|
|
if ( ++i >= argc )
|
|
|
|
{
|
|
|
|
cerr << argv[0] << ": ";
|
|
|
|
cerr << "'" << strLongOpt << "' needs a string argument" << endl;
|
|
|
|
exit ( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
strArg = argv[i];
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2008-01-15 23:54:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GetNumericArgument ( int argc, char **argv, int &i,
|
|
|
|
std::string strShortOpt, std::string strLongOpt,
|
|
|
|
double rRangeStart, double rRangeStop,
|
|
|
|
double & rValue)
|
|
|
|
{
|
2009-02-22 12:07:18 +01:00
|
|
|
if ( ( !strShortOpt.compare ( argv[i] ) ) || ( !strLongOpt.compare ( argv[i] ) ) )
|
|
|
|
{
|
|
|
|
if ( ++i >= argc )
|
|
|
|
{
|
|
|
|
cerr << argv[0] << ": ";
|
|
|
|
cerr << "'" << strLongOpt << "' needs a numeric argument between "
|
|
|
|
<< rRangeStart << " and " << rRangeStop << endl;
|
|
|
|
exit ( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
char *p;
|
|
|
|
rValue = strtod ( argv[i], &p );
|
|
|
|
if ( *p || rValue < rRangeStart || rValue > rRangeStop )
|
|
|
|
{
|
|
|
|
cerr << argv[0] << ": ";
|
|
|
|
cerr << "'" << strLongOpt << "' needs a numeric argument between "
|
|
|
|
<< rRangeStart << " and " << rRangeStop << endl;
|
|
|
|
exit ( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2007-09-09 11:50:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************\
|
|
|
|
* Window Message System *
|
|
|
|
\******************************************************************************/
|
2006-12-18 15:39:33 +01:00
|
|
|
void PostWinMessage ( const _MESSAGE_IDENT MessID, const int iMessageParam,
|
|
|
|
const int iChanNum )
|
|
|
|
{
|
2007-09-09 11:50:22 +02:00
|
|
|
// first check if application is initialized
|
2006-12-18 15:39:33 +01:00
|
|
|
if ( pApp != NULL )
|
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
CLlconEvent* LlconEv =
|
2006-12-18 15:39:33 +01:00
|
|
|
new CLlconEvent ( MessID, iMessageParam, iChanNum );
|
|
|
|
|
2007-09-09 11:50:22 +02:00
|
|
|
// Qt will delete the event object when done
|
2008-01-22 20:58:53 +01:00
|
|
|
QCoreApplication::postEvent ( pMainWindow, LlconEv );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
}
|