00001
00002
00003
00004
00005
00006
00007
00008 #include "ChatRoom.h"
00009
00010
00011 ChatRoom::ChatRoom(void) {}
00012
00013
00014
00015
00016
00017 ChatRoom::ChatRoom(Player *firstPlayer) {
00018 addPlayer(firstPlayer);
00019 }
00020
00021
00022 ChatRoom::~ChatRoom(void) {exit();}
00023
00024
00025
00026
00027 void ChatRoom::init(void) {}
00028
00029
00030
00031
00032
00033
00034 void ChatRoom::run(void) {
00035 string message;
00036 string completeMessage;
00037
00038 list<Player *>::iterator pli;
00039 char time_buffer[64];
00040 int last;
00041 time_t t;
00042 tm *tbp;
00043 string now;
00044
00045 while (true) {
00046 if ( playerList.size() == 0 ) {
00047 cout << "<ChatRoom msg> ChatRoom Empty: Suspended." << endl;
00048 suspend();
00049 cout << "<ChatRoom msg> ChatRoom Resumed." << endl;
00050 }
00051 pli = playerList.begin();
00052
00053 while ( pli != playerList.end() ) {
00054
00055
00056 message = (*pli)->hear(500);
00057
00058
00059 t = std::time(NULL);
00060 tbp = std::localtime(&t);
00061 last = std::strftime(time_buffer, 64, "%Y-%m-%d %H:%M:%S", tbp);
00062 time_buffer[last] = '\0';
00063 now = time_buffer;
00064
00065
00066 completeMessage = "[" + now + "] ";
00067 completeMessage += (*pli)->getName();
00068 completeMessage += " >>> " + message + "\n";
00069
00070 if ( !message.empty() ) {
00071 if ( !strcmp(message.data(), "\\quit") ) {
00072 delPlayer(*pli);
00073 break;
00074 }
00075 else {
00076
00077
00078 sayToAll(completeMessage, *pli);
00079 }
00080 }
00081 pli++;
00082
00083
00084 message.clear();
00085 }
00086
00087
00088 sleep(200);
00089 }
00090
00091 }
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 void ChatRoom::final(void) {
00102 if ( playerList.size() > 0 ) {
00103 list<Player *>::iterator pli;
00104 Player *temp;
00105 mutex.enterMutex();
00106
00107 while ( pli != playerList.end() ) {
00108
00109
00110
00111
00112 (*pli)->say("<ChatRoom msg> ChatRoom Terminated. Now exit.\n");
00113 (*pli)->resumeConnHandler();
00114 temp = *pli;
00115 pli++;
00116 playerList.remove(temp);
00117 }
00118
00119 mutex.leaveMutex();
00120 }
00121 }
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131 void ChatRoom::addPlayer (Player *newPlayer) {
00132 mutex.enterMutex();
00133
00134 playerList.push_back(newPlayer);
00135
00136
00137 newPlayer->say("\n<----------------------------------------->\n");
00138 newPlayer->say(">>>>> Hello at MGS's ChatRoom <<<<<\n");
00139 newPlayer->say("<----------------------------------------->\n\n");
00140 newPlayer->say("[Users On-Line: ");
00141 newPlayer->say((int)(playerList.size() -1));
00142 newPlayer->say("]\n");
00143
00144
00145 sayToAll("<ChatRoom msg> " + newPlayer->getName() + " has joined at this ChatRoom\n",
00146 newPlayer);
00147 cout << "<ChatRoom msg> " << newPlayer->getName() << " has joined at this ChatRoom\n";
00148
00149
00150
00151 if ( playerList.size() == 1 ) {
00152 if ( isRunning() )
00153 resume();
00154 else
00155 start();
00156 }
00157
00158 mutex.leaveMutex();
00159 }
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169 bool ChatRoom::delPlayer (const string &toDelete) {
00170 list<Player *>::iterator pli;
00171 mutex.enterMutex();
00172
00173 pli = playerList.begin();
00174 while ( pli != playerList.end() ) {
00175 if ( (*pli)->getName() == toDelete) {
00176
00177
00178
00179
00180 (*pli)->say("<ChatRoom msg> GoodBye. ;-)\n");
00181 sayToAll("<ChatRoom msg> " + (*pli)->getName() + " has leaved this ChatRoom\n", *pli);
00182 (*pli)->resumeConnHandler();
00183 playerList.remove(*pli);
00184 mutex.leaveMutex();
00185 return true;
00186 }
00187 pli++;
00188 }
00189
00190 mutex.leaveMutex();
00191 return false;
00192 }
00193
00194
00195
00196
00197
00198
00199
00200 void ChatRoom::delPlayer (Player *toDelete) {
00201 mutex.enterMutex();
00202
00203 toDelete->say("<ChatRoom msg> GoodBye. ;-)\n");
00204 sayToAll("<ChatRoom msg> " + toDelete->getName() + " has leaved this ChatRoom\n", toDelete);
00205 toDelete->resumeConnHandler();
00206 playerList.remove(toDelete);
00207
00208 mutex.leaveMutex();
00209 }
00210
00211
00212
00213
00214 int ChatRoom::getNumPlayers (void) const {
00215 return playerList.size();
00216 }
00217
00218
00219 void ChatRoom::printPlayersList(iostream *streamWherePrint) {
00220 list<Player *>::iterator pli;
00221 mutex.enterMutex();
00222
00223 pli = playerList.begin();
00224 while ( pli != playerList.end() ) {
00225 *streamWherePrint << "\t* " << (*pli)->getName() << endl;
00226 pli++;
00227 }
00228
00229 mutex.leaveMutex();
00230 }
00231
00232 string ChatRoom::getName(void) const {return "ChatRoom - Simple Chat for MGS";}
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243 void ChatRoom::sayToAll(const string &message, Player *notSay) {
00244 list<Player *>::iterator pli;
00245
00246 pli = playerList.begin();
00247 while ( pli != playerList.end() ) {
00248 if ( notSay == NULL || (notSay != NULL && *pli != notSay) )
00249 (*pli)->say(message);
00250 pli++;
00251 }
00252 }
00253
00254
00255
00256
00257
00258 extern "C"
00259 ChatRoom *buildObject(void) { return new ChatRoom; }