Planeshift
serverconsole.h
Go to the documentation of this file.
1 /*
2  * serverconsole.h - author: Matze Braun <matze@braunis.de>
3  *
4  * Copyright (C) 2001 Atomic Blue (info@planeshift.it, http://www.atomicblue.org)
5  *
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation (version 2 of the License)
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */
19 #ifndef __SERVERCONSOLE_H__
20 #define __SERVERCONSOLE_H__
21 
22 #include <csutil/threading/thread.h>
23 #include "util/consoleout.h"
24 #include "util/gameevent.h"
25 
26 #ifdef USE_ANSI_ESCAPE
27 #define COL_NORMAL "\033[m\017"
28 #define COL_RED "\033[31m"
29 #define COL_BLUE "\033[34m"
30 #define COL_YELLOW "\033[33m"
31 #define COL_CYAN "\033[36m"
32 #define COL_GREEN "\033[32m"
33 #else
34 #define COL_NORMAL ""
35 #define COL_RED ""
36 #define COL_BLUE ""
37 #define COL_CYAN ""
38 #define COL_GREEN ""
39 #define COL_YELLOW ""
40 #endif
41 
42 struct COMMAND;
43 
44 const COMMAND *find_command(const char *name);
45 int execute_line(const char *line,csString *buffer);
46 
47 struct iObjectRegistry;
48 
58 {
59 public:
60  virtual void CatchCommand(const char *cmd) = 0;
61  virtual ~iCommandCatcher() {}
62 };
63 
67 class ServerConsole : public ConsoleOut, public CS::Threading::Runnable
68 {
69 public:
70  ServerConsole(iObjectRegistry *oreg, const char *appname, const char *prompt);
71  ~ServerConsole();
72 
81  static void ExecuteScript(const char* script);
82 
87  void Run();
88 
95  void MainLoop();
96 
98  {
99  cmdcatcher = cmdcatch;
100  }
101 
103  static const char *prompt;
104 
105 protected:
107  const char *appname;
108 
110  csRef<CS::Threading::Thread> thread;
111 
113  bool stop;
114 
117 
118  iObjectRegistry *objreg;
119 };
120 
121 // Allows the console commands to be thread-safe by inserting them into the main event queue
123 {
124  csString command;
125 
126 public:
127  // 0 offset for highest priority in the game event queue
128  psServerConsoleCommand(const char* command) : psGameEvent(0, 0, "psServerStatusRunEvent"), command(command) {};
129  void Trigger()
130  {
131  execute_line(command,NULL);
133  };
134  virtual csString ToString() const
135  {
136  return command;
137  }
138 };
139 
142 #endif
143 
void Trigger()
Abstract event processing function.
virtual csString ToString() const
Return a string with information about the event.
int execute_line(const char *line, csString *buffer)
#define COL_BLUE
Definition: serverconsole.h:36
static const char * prompt
Holds the prompt.
This defines an interface for intercepting commands instead of handling them locally in the server co...
Definition: serverconsole.h:57
csRef< CS::Threading::Thread > thread
The server console runs in its own thread.
bool stop
If true, the server is shutting down, and the main loop should stop.
iCommandCatcher * cmdcatcher
CommandCatcher intercepts typed commands without processing them here.
virtual void CatchCommand(const char *cmd)=0
const char * name
Definition: command.h:40
#define COL_NORMAL
Definition: serverconsole.h:34
This is a little class to store an array of commands and functions to call with each command...
Definition: command.h:39
void SetCommandCatcher(iCommandCatcher *cmdcatch)
Definition: serverconsole.h:97
Simple static class for controlled user output.
Definition: consoleout.h:50
const COMMAND * find_command(const char *name)
This class is implements the user input and output console for the server.
Definition: serverconsole.h:67
virtual ~iCommandCatcher()
Definition: serverconsole.h:61
#define CPrintf
Allows other classes to print to the server console easily.
Definition: consoleout.h:130
iObjectRegistry * objreg
const char * appname
The name of this application. Only used for printing & readline.
psServerConsoleCommand(const char *command)
All scheduled events must inherit from this class.
Definition: gameevent.h:36