Browse Source

MWutils class

master
Pedro Berrocal 5 years ago
parent
commit
2246acae96
3 changed files with 115 additions and 10 deletions
  1. +8
    -8
      src/MWSessionManagerI.cpp
  2. +105
    -0
      src/MWUtils.cpp
  3. +2
    -2
      src/Makefile

+ 8
- 8
src/MWSessionManagerI.cpp View File

@ -1,19 +1,19 @@
#include <ChatSessionManagerI.h>
#include <ChatSessionI.h>
#include <ChatUtils.h>
#include <MWSessionManagerI.h>
#include <MWSessionI.h>
#include <MWUtils.h>
using namespace std;
ChatSessionManagerI::ChatSessionManagerI(const shared_ptr<ChatRoom>& chatRoom, bool trace,
MWSessionManagerI::MWSessionManagerI(const shared_ptr<MWMap>& mwmap, bool trace,
const shared_ptr<Ice::Logger>& logger) :
_chatRoom(chatRoom),
_MWMap(mwmap),
_trace(trace),
_logger(logger)
{
}
shared_ptr<Glacier2::SessionPrx>
ChatSessionManagerI::create(string name,
MWSessionManagerI::create(string name,
shared_ptr<Glacier2::SessionControlPrx> sessionControl,
const Ice::Current& current)
{
@ -21,7 +21,7 @@ ChatSessionManagerI::create(string name,
try
{
vname = validateName(name);
_chatRoom->reserve(vname);
_MWMap->reserve(vname);
}
catch(const exception& ex)
{
@ -36,7 +36,7 @@ ChatSessionManagerI::create(string name,
shared_ptr<Glacier2::SessionPrx> proxy;
try
{
auto session = make_shared<ChatSessionI>(_chatRoom, vname, _trace, _logger);
auto session = make_shared<MWSessionI>(_MWMap, vname, _trace, _logger);
proxy = Ice::uncheckedCast<Glacier2::SessionPrx>(current.adapter->addWithUUID(session));
Ice::IdentitySeq ids;


+ 105
- 0
src/MWUtils.cpp View File

@ -0,0 +1,105 @@
#include <MWUtils.h>
#include <MW.h>
#include <sstream>
#include <algorithm>
using namespace std;
static const unsigned int maxNameSize = 12;
static const unsigned int minNameSize = 3;
static const string nameRange = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static const string isEmptyTokens = "\t\r\n\f\v ";
static const unsigned int maxMessageSize = 1024;
string
validateName(const string& in)
{
if(in.size() > maxNameSize || in.size() < minNameSize)
{
ostringstream msg;
msg << "Your name must be between " << minNameSize << " and " << maxNameSize << " characters in length.";
throw invalid_argument(msg.str());
}
if(in.find_last_not_of(nameRange) != string::npos)
{
throw invalid_argument("Invalid character in name. Valid characters are letter and digits.");
}
string out = in;
transform(out.begin(), out.end(), out.begin(), ::tolower);
if(out.begin() != out.end())
{
transform(out.begin(), out.begin() + 1, out.begin(), ::toupper);
}
return out;
}
string
validateMessage(const string& in)
{
if(in.size() > maxMessageSize)
{
ostringstream os;
os << "Message length exceeded, maximum length is " << maxMessageSize << " characters.";
throw MW::InvalidMessageException(os.str());
}
if(in.find_last_not_of(isEmptyTokens) == string::npos)
{
throw invalid_argument("Your message is empty and was ignored.");
}
// Strip html codes in the message
string out;
for(char c : in)
{
switch(c)
{
case '&':
{
out.append("&amp;");
break;
}
case '"':
{
out.append("&quot;");
break;
}
case '\'':
{
out.append("&#39;");
break;
}
case '<':
{
out.append("&lt;");
break;
}
case '>':
{
out.append("&gt;");
break;
}
case '\r':
case '\n':
case '\v':
case '\f':
case '\t':
{
out.append(" ");
break;
}
default:
{
out.push_back(c);
break;
}
}
}
return out;
}

+ 2
- 2
src/Makefile View File

@ -1,6 +1,6 @@
CC=c++
IDIR =../include
CFLAGS=-I. -DICE_CPP11_MAPPING -I $(IDIR) -std=c++11
CFLAGS=-I. -DICE_CPP11_MAPPING -I $(IDIR) -std=c++11 -pthread
ODIR=obj
LDIR =../lib
@ -10,7 +10,7 @@ LIBS=-lm -lIce++11
_DEPS = MW.h MWMap.h MWSession.h MWSessionI.h MWSessionManagerI.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
_OBJ = MW.o MWServer.o MWMap.o MWSession.o MWSesionManagerI.o MWUtils.o
_OBJ = MW.o MWServer.o MWMap.o MWSession.o MWSessionManagerI.o MWUtils.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
$(ODIR)/%.o: %.cpp $(DEPS)


Loading…
Cancel
Save