ServerWebAppletHandler.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2004
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <webserver/ServerWebAppletHandler.h>
00022 #include <webserver/ServerWebServerUtil.h>
00023 #include <webserver/ServerWebServer.h>
00024 #include <server/ServerChannelManager.h>
00025 #include <server/ServerAdminCommon.h>
00026 #include <XML/XMLNode.h>
00027 
00028 bool ServerWebAppletHandler::AppletFileHandler::processRequest(
00029         ServerWebServerIRequest &request,
00030         std::string &text)
00031 {
00032         // Get file
00033         std::string file = 
00034                 S3D::getDataFile(
00035                         S3D::formatStringBuffer("data/html/server/binary/%s", request.getUrl()));
00036 
00037         // Read file contents
00038         std::string contents;
00039         FILE *in = fopen(file.c_str(), "rb");
00040         if (!in) return false;
00041         int read = 0;
00042         char buffer[256];
00043         while (read = fread(buffer, 1, 256, in))
00044         {
00045                 contents.append(buffer, read);
00046         }
00047         fclose(in);
00048 
00049         // Return file
00050         unsigned int dataSize = contents.size();
00051         text.append(S3D::formatStringBuffer(
00052                 "HTTP/1.1 200 OK\r\n"
00053                 "Server: Scorched3D\r\n"
00054                 "Content-Length: %u\r\n"
00055                 "Content-Type: application/octet-stream\r\n"
00056                 "Connection: Close\r\n"
00057                 "\r\n", dataSize));
00058         text.append(contents.data(), dataSize);
00059 
00060         return true;
00061 }
00062 
00063 bool ServerWebAppletHandler::AppletHtmlHandler::processRequest(
00064         ServerWebServerIRequest &request,
00065         std::string &text)
00066 {
00067         return ServerWebServerUtil::getHtmlTemplate(request.getSession(), "applet.html", request.getFields(), text);
00068 }
00069 
00070 bool ServerWebAppletHandler::AppletActionHandler::processRequest(
00071         ServerWebServerIRequest &request,
00072         std::string &text)
00073 {
00074         const char *action = ServerWebServerUtil::getField(request.getFields(), "action");
00075         if (action)
00076         {
00077                 // Check which action we need to perform
00078                 if (0 == strcmp(action, "chat"))
00079                 {
00080                         // Add a new chat message
00081                         const char *text = ServerWebServerUtil::getField(request.getFields(), "text");
00082                         const char *channel = ServerWebServerUtil::getField(request.getFields(), "channel");
00083                         if (text && channel && request.getSession())
00084                         {
00085                                 ServerAdminCommon::adminSay(request.getSession()->credentials, channel, text);
00086                         }
00087                 }
00088         }
00089 
00090         return true; // Return empty document
00091 }
00092 
00093 ServerWebAppletHandler::AppletAsyncHandler::AppletAsyncHandler() :
00094         lastMessage_(0),
00095         initialized_(false)
00096 {
00097 }
00098 
00099 bool ServerWebAppletHandler::AppletAsyncHandler::processRequest(
00100         ServerWebServerIRequest &request,
00101         std::string &text)
00102 {
00103         // Check if we have sent the initial data
00104         if (!initialized_)
00105         {
00106                 initialized_ = true;
00107 
00108                 // Add all of the available chat channels
00109                 {
00110                         std::list<std::string> channels =
00111                                 ServerChannelManager::instance()->getAllChannels();
00112                         std::list<std::string>::iterator itor;
00113                         for (itor = channels.begin();
00114                                 itor != channels.end();
00115                                 itor++)
00116                         {
00117                                 text.append("<addchannel>").
00118                                         append(itor->c_str()).
00119                                         append("</addchannel>\n");
00120                         }
00121                 }
00122         }
00123 
00124         // Add all of the chat message that this applet has not seen
00125         std::string chatText;
00126         std::list<ServerChannelManager::MessageEntry> &textsList = 
00127                 ServerChannelManager::instance()->getLastMessages();
00128         if (!textsList.empty())
00129         {
00130                 std::list<ServerChannelManager::MessageEntry>::reverse_iterator textsListItor;
00131                 for (textsListItor = textsList.rbegin();
00132                         textsListItor != textsList.rend();
00133                         textsListItor++)
00134                 {
00135                         ServerChannelManager::MessageEntry &entry = *textsListItor;
00136                         if (lastMessage_ >= entry.messageid) break;
00137 
00138                         std::string cleanText;
00139                         XMLNode::removeSpecialChars(entry.message, cleanText);
00140                         
00141                         chatText.insert(0, 
00142                                 S3D::formatStringBuffer("<chat>%s</chat>\n", cleanText.c_str()));
00143                 }
00144                 lastMessage_ = textsList.back().messageid;
00145                 text.append(chatText);
00146         }
00147 
00148         return true;
00149 }

Generated on Mon Feb 16 15:14:53 2009 for Scorched3D by  doxygen 1.5.3