1 #include "otsdaq-utilities/Chat/ChatSupervisor.h"
2 #include "otsdaq-core/MessageFacility/MessageFacility.h"
3 #include "otsdaq-core/Macros/CoutMacros.h"
4 #include "otsdaq-core/CgiDataUtilities/CgiDataUtilities.h"
5 #include "otsdaq-core/XmlUtilities/HttpXmlDocument.h"
7 #include <xdaq/NamespaceURI.h>
14 #define __MF_SUBJECT__ "Chat"
21 : CoreSupervisorBase (stub)
23 INIT_MF(
"ChatSupervisor");
25 ChatLastUpdateIndex = 1;
29 ChatSupervisor::~ChatSupervisor(
void)
35 void ChatSupervisor::destroy(
void)
42 void ChatSupervisor::defaultPage(xgi::Input * cgiIn, xgi::Output * out )
44 *out <<
"<!DOCTYPE HTML><html lang='en'><frameset col='100%' row='100%'><frame src='/WebPath/html/Chat.html?urn=" <<
45 this->getApplicationDescriptor()->getLocalId() <<
"'></frameset></html>";
51 void ChatSupervisor::forceSupervisorPropertyValues()
53 CorePropertySupervisorBase::setSupervisorProperty(CorePropertySupervisorBase::SUPERVISOR_PROPERTIES.AutomatedRequestTypes,
61 void ChatSupervisor::request(
const std::string& requestType, cgicc::Cgicc& cgiIn,
62 HttpXmlDocument& xmlOut,
const WebUsers::RequestUserInfo& userInfo)
71 cleanupExpiredChats();
73 if(requestType ==
"RefreshChat")
75 std::string lastUpdateIndexString = CgiDataUtilities::postData(cgiIn,
"lastUpdateIndex");
76 std::string user = CgiDataUtilities::postData(cgiIn,
"user");
77 uint64_t lastUpdateIndex;
78 sscanf(lastUpdateIndexString.c_str(),
"%lu",&lastUpdateIndex);
80 insertChatRefresh(&xmlOut,lastUpdateIndex,user);
82 else if(requestType ==
"RefreshUsers")
84 insertActiveUsers(&xmlOut);
86 else if(requestType ==
"SendChat")
88 std::string chat = CgiDataUtilities::postData(cgiIn,
"chat");
89 std::string user = CgiDataUtilities::postData(cgiIn,
"user");
95 else if(requestType ==
"PageUser")
97 std::string topage = CgiDataUtilities::postData(cgiIn,
"topage");
98 std::string user = CgiDataUtilities::postData(cgiIn,
"user");
100 __COUT__ <<
"Paging = " << topage.substr(0,10) <<
"... from user = " << user.substr(0,10) << std::endl;
102 theRemoteWebUsers_.sendSystemMessage(allSupervisorInfo_.getGatewayDescriptor(),
103 topage, user +
" is paging you to come chat.");
106 __COUT__ <<
"requestType request not recognized." << std::endl;
116 void ChatSupervisor::escapeChat(std::string &chat)
127 void ChatSupervisor::insertActiveUsers(HttpXmlDocument *xmlOut)
129 xmlOut->addTextElementToData(
"active_users",
130 theRemoteWebUsers_.getActiveUserList(allSupervisorInfo_.getGatewayDescriptor()));
140 void ChatSupervisor::insertChatRefresh(HttpXmlDocument *xmlOut, uint64_t lastUpdateIndex, std::string user)
144 if(!isLastUpdateIndexStale(lastUpdateIndex))
return;
149 sprintf(tempStr,
"%lu",ChatLastUpdateIndex);
150 xmlOut->addTextElementToData(
"last_update_index",tempStr);
153 xmlOut->addTextElementToData(
"chat_users",
"");
154 for(uint64_t i=0;i<ChatUsers_.size();++i)
155 xmlOut->addTextElementToParent(
"chat_user",ChatUsers_[i],
"chat_users");
158 lastUpdateIndex = ChatHistoryIndex_[ChatHistoryIndex_.size()-1]-1;
161 xmlOut->addTextElementToData(
"chat_history",
"");
162 for(uint64_t i=0;i<ChatHistoryEntry_.size();++i)
164 if(isChatOld(ChatHistoryIndex_[i],lastUpdateIndex))
continue;
166 xmlOut->addTextElementToParent(
"chat_entry",ChatHistoryEntry_[i],
"chat_history");
167 xmlOut->addTextElementToParent(
"chat_author",ChatHistoryAuthor_[i],
"chat_history");
168 sprintf(tempStr,
"%lu",ChatHistoryTime_[i]);
169 xmlOut->addTextElementToParent(
"chat_time",tempStr,
"chat_history");
176 void ChatSupervisor::newUser(std::string user)
178 for(uint64_t i=0;i<ChatUsers_.size();++i)
179 if(ChatUsers_[i] == user)
181 ChatUsersTime_[i] = time(0);
185 __COUT__ <<
"New user: " << user << std::endl;
187 ChatUsers_.push_back(user);
188 ChatUsersTime_.push_back(time(0));
189 newChat(user +
" joined the chat.",
"ots");
195 void ChatSupervisor::newChat(std::string chat, std::string user)
197 ChatHistoryEntry_.push_back(chat);
198 ChatHistoryAuthor_.push_back(user);
199 ChatHistoryTime_.push_back(time(0));
200 ChatHistoryIndex_.push_back(incrementAndGetLastUpdate());
206 bool ChatSupervisor::isChatOld(uint64_t chatIndex, uint64_t last)
208 return (last - chatIndex < (uint64_t(1) << 62));
213 bool ChatSupervisor::isLastUpdateIndexStale(uint64_t last)
215 return ChatLastUpdateIndex != last;
220 uint64_t ChatSupervisor::incrementAndGetLastUpdate()
222 if(!++ChatLastUpdateIndex) ++ChatLastUpdateIndex;
223 return ChatLastUpdateIndex;
229 void ChatSupervisor::cleanupExpiredChats()
231 for(uint64_t i=0;i<ChatHistoryEntry_.size();++i)
232 if(i >= CHAT_HISTORY_MAX_ENTRIES ||
233 ChatHistoryTime_[i] + CHAT_HISTORY_EXPIRATION_TIME < time(0))
235 removeChatHistoryEntry(i);
241 for(uint64_t i=0;i<ChatUsers_.size();++i)
242 if(ChatUsersTime_[i] + CHAT_HISTORY_EXPIRATION_TIME < time(0))
244 removeChatUserEntry(i);
253 void ChatSupervisor::removeChatHistoryEntry(uint64_t i)
255 ChatHistoryEntry_.erase (ChatHistoryEntry_.begin()+i);
256 ChatHistoryTime_.erase (ChatHistoryTime_.begin()+i);
257 ChatHistoryAuthor_.erase (ChatHistoryAuthor_.begin()+i);
258 ChatHistoryIndex_.erase (ChatHistoryIndex_.begin()+i);
263 void ChatSupervisor::removeChatUserEntry(uint64_t i)
265 newChat(ChatUsers_[i] +
" left the chat.",
"ots");
266 ChatUsers_.erase (ChatUsers_.begin()+i);
267 ChatUsersTime_.erase (ChatUsersTime_.begin()+i);