~ubuntu-branches/ubuntu/precise/triplea/precise

« back to all changes in this revision

Viewing changes to src/games/strategy/engine/lobby/server/headless/HeadlessLobbyConsole.java

  • Committer: Package Import Robot
  • Author(s): Scott Howard
  • Date: 2011-11-11 21:40:11 UTC
  • Revision ID: package-import@ubuntu.com-20111111214011-sehf2rwat36o2xqf
Tags: upstream-1.3.2.2
ImportĀ upstreamĀ versionĀ 1.3.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This program is free software; you can redistribute it and/or modify
 
3
 * it under the terms of the GNU General Public License as published by
 
4
 * the Free Software Foundation; either version 2 of the License, or
 
5
 * (at your option) any later version.
 
6
 * This program is distributed in the hope that it will be useful,
 
7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
8
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
9
 * GNU General Public License for more details.
 
10
 * You should have received a copy of the GNU General Public License
 
11
 * along with this program; if not, write to the Free Software
 
12
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
13
 */
 
14
 
 
15
package games.strategy.engine.lobby.server.headless;
 
16
 
 
17
import games.strategy.debug.Console;
 
18
import games.strategy.engine.lobby.server.LobbyServer;
 
19
import games.strategy.engine.lobby.server.ui.DBExplorerPanel;
 
20
import games.strategy.engine.lobby.server.userDB.Database;
 
21
import games.strategy.net.IConnectionChangeListener;
 
22
import games.strategy.net.INode;
 
23
 
 
24
import java.io.BufferedReader;
 
25
import java.io.IOException;
 
26
import java.io.InputStream;
 
27
import java.io.InputStreamReader;
 
28
import java.io.PrintStream;
 
29
import java.sql.Connection;
 
30
import java.sql.ResultSet;
 
31
import java.sql.SQLException;
 
32
import java.sql.Statement;
 
33
import java.util.Date;
 
34
import java.util.Formatter;
 
35
import java.util.concurrent.atomic.AtomicInteger;
 
36
 
 
37
/**
 
38
 * Headless admin console for the lobby.
 
39
 * 
 
40
 * Reads commands from stdin, and writes responses to stdout.
 
41
 * 
 
42
 * @author Sean Bridges
 
43
 *
 
44
 */
 
45
 
 
46
public class HeadlessLobbyConsole
 
47
{
 
48
    private final LobbyServer server;
 
49
    private final PrintStream out;
 
50
    private final BufferedReader in;
 
51
 
 
52
    @SuppressWarnings("deprecation")
 
53
    private final String startDate = new Date().toGMTString();
 
54
    private final AtomicInteger totalLogins = new AtomicInteger();
 
55
    private final AtomicInteger currentConnections = new AtomicInteger();
 
56
    private volatile int maxConcurrentLogins = 0;
 
57
    
 
58
    
 
59
    public HeadlessLobbyConsole(LobbyServer server, InputStream in, PrintStream out)
 
60
    {
 
61
        this.out = out;
 
62
        this.in = new BufferedReader(new InputStreamReader(in));
 
63
        this.server = server;
 
64
        
 
65
        server.getMessenger().addConnectionChangeListener(new IConnectionChangeListener()
 
66
        {
 
67
        
 
68
            public void connectionAdded(INode to)
 
69
            {
 
70
                currentConnections.incrementAndGet();
 
71
                totalLogins.incrementAndGet();
 
72
                
 
73
                //not strictly thread safe, but good enough
 
74
                maxConcurrentLogins = Math.max(maxConcurrentLogins, currentConnections.get());
 
75
            }
 
76
        
 
77
            public void connectionRemoved(INode to)
 
78
            {
 
79
                currentConnections.decrementAndGet();
 
80
            }
 
81
        
 
82
        });
 
83
    }
 
84
 
 
85
    public void start() 
 
86
    {
 
87
        Thread t = new Thread(new Runnable()
 
88
        {
 
89
        
 
90
            public void run()
 
91
            {
 
92
                printEvalLoop();
 
93
        
 
94
            }
 
95
        
 
96
        }, "Headless console eval print loop");
 
97
        t.start();
 
98
    }
 
99
    
 
100
    private void printEvalLoop()
 
101
    {
 
102
       out.println();
 
103
        
 
104
        while(true) 
 
105
        {
 
106
            out.print(">>>>");
 
107
            out.flush();
 
108
            try
 
109
            {
 
110
                String command = in.readLine();
 
111
                process(command.trim());
 
112
            } 
 
113
            catch(Throwable t)
 
114
            {
 
115
                t.printStackTrace();
 
116
                t.printStackTrace(out);
 
117
            }
 
118
        }
 
119
        
 
120
        
 
121
        
 
122
    }
 
123
 
 
124
    private void process(String command)
 
125
    {
 
126
        if(command.equals("")) {        
 
127
            return;
 
128
        }
 
129
        final String noun = command.split("\\s")[0];
 
130
        if(noun.equals("help")) { 
 
131
            showHelp();
 
132
        } else if(noun.equals("status")) { 
 
133
            showStatus();
 
134
        } else if(noun.equals("sql")) {
 
135
            executeSql(command.substring("sql".length(), command.length()).trim());
 
136
        } else if(noun.equals("quit")) {
 
137
            quit();
 
138
        } else if(noun.equals("backup")) {
 
139
            backup();
 
140
        } else if(noun.equals("memory")) {
 
141
            memory();
 
142
        } else if(noun.equals("threads")) {
 
143
            threads();
 
144
        }
 
145
        else {
 
146
            out.println("unrecognized command:" + command);
 
147
            showHelp();
 
148
        }
 
149
            
 
150
        
 
151
    }
 
152
 
 
153
    private void threads()
 
154
    {
 
155
        out.println(Console.getThreadDumps());
 
156
        
 
157
    }
 
158
 
 
159
    private void memory()
 
160
    {
 
161
        out.println(Console.getMemory());        
 
162
    }
 
163
 
 
164
    private void backup()
 
165
    {
 
166
        Database.backup();        
 
167
    }
 
168
 
 
169
    private void quit()
 
170
    {
 
171
        out.println("Are you sure? (y/n)");
 
172
        try
 
173
        {
 
174
            if(in.readLine().toLowerCase().startsWith("y")) {
 
175
                System.exit(0);
 
176
            }
 
177
        } catch (IOException e)
 
178
        {
 
179
            e.printStackTrace();
 
180
        }
 
181
    }
 
182
 
 
183
    private void executeSql(String sql)
 
184
    {
 
185
        Connection con = Database.getConnection();
 
186
        try
 
187
        {
 
188
            
 
189
            Statement ps = con.createStatement();
 
190
            
 
191
            if(DBExplorerPanel.isNotQuery(sql ))
 
192
            {
 
193
                int rs = ps.executeUpdate(sql);
 
194
                out.println("Update count:" + rs);
 
195
            }
 
196
            else
 
197
            {
 
198
                ResultSet rs = ps.executeQuery(sql);
 
199
                print(rs);
 
200
                rs.close();
 
201
            }
 
202
            
 
203
        }
 
204
        catch(SQLException sqle)
 
205
        {
 
206
            sqle.printStackTrace();
 
207
            out.println(sqle.getMessage());
 
208
        }
 
209
        finally
 
210
        {
 
211
            try
 
212
            {
 
213
                con.close();
 
214
            } catch (SQLException e)
 
215
            {
 
216
                // TODO Auto-generated catch block
 
217
                e.printStackTrace();
 
218
            }
 
219
        }
 
220
    }
 
221
 
 
222
    private void print(ResultSet rs)
 
223
    {
 
224
        try
 
225
        {
 
226
            Formatter f = new Formatter(out);
 
227
            String itemFormat = "%20s ";
 
228
            f.format(itemFormat, "Count");
 
229
            
 
230
            int count = rs.getMetaData().getColumnCount();
 
231
            
 
232
            for(int i =1; i <= count; i++)
 
233
            {
 
234
                String columnName = rs.getMetaData().getColumnName(i);
 
235
                f.format(itemFormat, columnName);     
 
236
            }
 
237
            
 
238
            f.format("\n");
 
239
            for(int i =0; i < count; i++) {
 
240
                f.format(itemFormat, "-----------");
 
241
            }
 
242
            f.format("\n");
 
243
            
 
244
            int row = 1;
 
245
            while(rs.next())
 
246
            {               
 
247
                f.format(itemFormat, row++);
 
248
                for(int i =1; i <= count; i++)
 
249
                {
 
250
                    f.format(itemFormat, rs.getString(i));
 
251
                }
 
252
                f.format("\n");
 
253
                f.flush();
 
254
            }
 
255
        } catch(SQLException e) {
 
256
            e.printStackTrace(out);
 
257
        }
 
258
    }
 
259
 
 
260
    private void showStatus()
 
261
    {
 
262
        
 
263
        int port = server.getMessenger().getServerNode().getPort();
 
264
        
 
265
        out.print(
 
266
                String.format(
 
267
                        "port:%s\n" +
 
268
                        "up since:%s\n" +
 
269
                                "total logins:%s\n" + 
 
270
                                "current connections:%s\n" +
 
271
                                "max concurrent connections:%s\n",
 
272
                        port,
 
273
                        startDate,
 
274
                        totalLogins.get(),
 
275
                        currentConnections.get(),
 
276
                        maxConcurrentLogins
 
277
                        )
 
278
         );
 
279
        
 
280
    }
 
281
 
 
282
    private void showHelp()
 
283
    {
 
284
        out.println("available commands:\n" +
 
285
                "  backup - backup the database \n" +
 
286
                        "  help - show this message\n" +
 
287
                        "  memory - show memory usage\n" +
 
288
                        "  status - show status information\n" +
 
289
                        "  sql {sql} - execute a sql command and print the results\n" +
 
290
                        "  threads - get thread dumps\n" +
 
291
                        "  quit - quit\n" 
 
292
                        );
 
293
    }
 
294
    
 
295
}