~ivantis/armagetronad/sty+ct+ivantis

« back to all changes in this revision

Viewing changes to src/network/master.cpp

  • Committer: ivantis
  • Date: 2008-09-09 21:33:18 UTC
  • Revision ID: ivantis@ivantis.net-20080909213318-k43y6yuq0zd6wbsa
first commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
*************************************************************************
 
4
 
 
5
ArmageTron -- Just another Tron Lightcycle Game in 3D.
 
6
Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
 
7
 
 
8
**************************************************************************
 
9
 
 
10
This program is free software; you can redistribute it and/or
 
11
modify it under the terms of the GNU General Public License
 
12
as published by the Free Software Foundation; either version 2
 
13
of the License, or (at your option) any later version.
 
14
 
 
15
This program is distributed in the hope that it will be useful,
 
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
GNU General Public License for more details.
 
19
 
 
20
You should have received a copy of the GNU General Public License
 
21
along with this program; if not, write to the Free Software
 
22
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
23
  
 
24
***************************************************************************
 
25
 
 
26
*/
 
27
 
 
28
#include "tConfiguration.h"
 
29
#include "tDirectories.h"
 
30
#include "nNetwork.h"
 
31
#include "nServerInfo.h"
 
32
#include "tSysTime.h"
 
33
#include "tLocale.h"
 
34
#include "tCommandLine.h"
 
35
#include "nSocket.h"
 
36
#include  <time.h>
 
37
 
 
38
REAL save_interval = 300.0f;
 
39
static tSettingItem< REAL > si( "MASTER_SAVE_INTERVAL", save_interval );
 
40
 
 
41
REAL query_interval = 10.0f;
 
42
static tSettingItem< REAL > qi( "MASTER_QUERY_INTERVAL", query_interval );
 
43
 
 
44
int master_port = 4533;
 
45
static tSettingItem< int > mp( "MASTER_PORT", master_port );
 
46
 
 
47
REAL master_idle = 2;
 
48
static tSettingItem< REAL > mi( "MASTER_IDLE", master_idle );
 
49
 
 
50
// console with filter for better machine readable log format
 
51
class nConsoleDateFilter:public tConsoleFilter{
 
52
private:
 
53
    virtual void DoFilterLine( tString &line )
 
54
    {
 
55
        char szTemp[128];
 
56
        time_t     now;
 
57
        struct tm *pTime;
 
58
        now = time(NULL);
 
59
        pTime = localtime(&now);
 
60
        strftime(szTemp,sizeof(szTemp),"[%Y/%m/%d %H:%M:%S] ",pTime);
 
61
 
 
62
        tString orig = line;
 
63
        line = "";
 
64
        line << szTemp  << orig;
 
65
    }
 
66
 
 
67
    virtual int DoGetPriority() const{ return 1; }
 
68
};
 
69
 
 
70
static nConsoleDateFilter sn_consoleFilter;
 
71
 
 
72
int main(int argc, char** argv)
 
73
{
 
74
    tCommandLineData commandLine;
 
75
    commandLine.programVersion_ = &sn_programVersion;
 
76
    commandLine.Analyse(argc, argv);
 
77
    tLocale::Load("languages.txt");
 
78
    atexit(tLocale::Clear);
 
79
 
 
80
    st_LoadConfig();
 
81
 
 
82
    nServerInfo::GetMasters();
 
83
 
 
84
 
 
85
#ifdef DEBUG
 
86
    // std::istringstream s("SERVER_IP ANY");
 
87
    // tConfItemBase::LoadAll( s );
 
88
#endif
 
89
 
 
90
    nServerInfo::Load( tDirectories::Var(), "master_list.srv" );
 
91
 
 
92
    nServerInfo::StartQueryAll();
 
93
    //  st_LoadConfig();
 
94
 
 
95
    sn_serverPort = master_port;
 
96
    sn_SetNetState(nSERVER);
 
97
 
 
98
    nTimeAbsolute savetimeout  = tSysTimeFloat();
 
99
    nTimeAbsolute querytimeout = tSysTimeFloat();
 
100
    nTimeAbsolute quitTimeout  = tSysTimeFloat() + master_idle * 3600;
 
101
 
 
102
    bool goon = true;
 
103
    while ( goon )
 
104
    {
 
105
        nServerInfo::RunMaster();
 
106
 
 
107
        sn_BasicNetworkSystem.Select( .1f );
 
108
        tAdvanceFrame();
 
109
        nTimeAbsolute time = tSysTimeFloat();
 
110
 
 
111
        sn_Receive();
 
112
        sn_ReceiveFromControlSocket();
 
113
        sn_SendPlanned();
 
114
 
 
115
        static bool queryGoesOn = true;
 
116
        if (queryGoesOn && time > querytimeout)
 
117
        {
 
118
            queryGoesOn = nServerInfo::DoQueryAll(1);
 
119
            querytimeout = time + query_interval;
 
120
        }
 
121
 
 
122
        if (time > savetimeout)
 
123
        {
 
124
            nServerInfo::Save( tDirectories::Var(), "master_list.srv" );
 
125
            if (!queryGoesOn)
 
126
            {
 
127
                nServerInfo::StartQueryAll();
 
128
                queryGoesOn = true;
 
129
            }
 
130
            savetimeout = time + save_interval;
 
131
 
 
132
            goon = time < quitTimeout;
 
133
        }
 
134
    }
 
135
 
 
136
    nServerInfo::DeleteAll();
 
137
 
 
138
    return(0);
 
139
}
 
140