source: trunk/FACT++/src/ChatClient.h@ 19536

Last change on this file since 19536 was 10505, checked in by tbretz, 14 years ago
File size: 3.2 KB
Line 
1#ifndef FACT_ChatClient
2#define FACT_ChatClient
3
4// **************************************************************************
5/** @class ChatClientImp
6
7@brief The base implementation for a chat client
8
9**/
10// **************************************************************************
11#include "Time.h"
12#include "MessageDim.h"
13#include "DimErrorRedirecter.h"
14
15using namespace std;
16
17class ChatClientImp : public MessageImp, public DimErrorRedirecter, public MessageDimRX
18{
19protected:
20 std::ostream &lout; /// Output stream for local synchrounous output
21
22 int Write(const Time &t, const string &txt, int)
23 {
24 Out() << t << ": " << txt.substr(6) << endl;
25 return 0;
26 }
27
28protected:
29 // Redirect asynchronous output to the output window
30 ChatClientImp(std::ostream &out, std::ostream &in) :
31 MessageImp(out),
32 DimErrorRedirecter(static_cast<MessageImp&>(*this)),
33 MessageDimRX("CHAT", static_cast<MessageImp&>(*this)),
34 lout(in)
35 {
36 Out() << Time::fmt("%H:%M:%S");
37 }
38};
39
40// **************************************************************************
41/** @class ChatClient
42
43@brief Implements a remote control based on a Readline class for the Chat client
44
45@tparam T
46 The base class for ChatClient. Either Readline or a class
47 deriving from it. This is usually either Console or Shell.
48
49**/
50// **************************************************************************
51#include "WindowLog.h"
52#include "ReadlineColor.h"
53#include "tools.h"
54
55template <class T>
56class ChatClient : public T, public ChatClientImp
57{
58public:
59 // Redirect asynchronous output to the output window
60 ChatClient(const char *name) : T(name),
61 ChatClientImp(T::GetStreamOut(), T::GetStreamIn())
62 {
63 }
64
65 // returns whether a command should be put into the history
66 bool Process(const std::string &str)
67 {
68 if (ReadlineColor::Process(lout, str))
69 return true;
70
71 if (T::Process(str))
72 return true;
73
74 const int rc = DimClient::sendCommand("CHAT/MSG", str.c_str());
75 if (!rc)
76 lout << kRed << "ERROR - Sending message failed." << endl;
77
78 return false;
79 }
80
81 std::string GetUpdatePrompt() const
82 {
83 // If we have not cd'ed to a server show only the line start
84 return T::GetLinePrompt() + " " + (IsConnected() ? "" : "dis") + "connected> ";
85 }
86};
87
88
89
90// **************************************************************************
91/** @class ChatConsole
92
93@brief Derives the ChatClient from Control and adds a proper prompt
94
95*/
96// **************************************************************************
97#include "Console.h"
98
99class ChatConsole : public ChatClient<Console>
100{
101public:
102 ChatConsole(const char *name, bool continous=false) :
103 ChatClient<Console>(name)
104 {
105 SetContinous(continous);
106 }
107};
108
109// **************************************************************************
110/** @class ChatShell
111
112@brief Derives the ChatClient from Shell and adds colored prompt
113
114 */
115// **************************************************************************
116#include "Shell.h"
117
118class ChatShell : public ChatClient<Shell>
119{
120public:
121 ChatShell(const char *name, bool = false) :
122 ChatClient<Shell>(name)
123 {
124 }
125};
126
127#endif
Note: See TracBrowser for help on using the repository browser.