Removed files accidentally left over from merge.

This commit is contained in:
Mark Côté 2012-08-20 16:54:12 -04:00
Родитель 5ddc48865c
Коммит d7526ed72c
13 изменённых файлов: 0 добавлений и 704 удалений

Просмотреть файл

@ -1,131 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "BufferedSocket.h"
#include <iostream>
#include <prerror.h>
#include <string.h>
std::streamsize
SearchableBuffer::find(char c)
{
std::streamsize count = 0;
char* p = gptr();
for (char* p = gptr(); p != egptr(); p++)
{
if (*p == c)
return count;
count++;
}
return -1;
}
BufferedSocket::BufferedSocket(PRFileDesc* socket)
: mSocket(socket), mReadBufferSize(0), tmpBuf(new char[1024])
{
}
BufferedSocket::~BufferedSocket()
{
delete[] tmpBuf;
}
void
BufferedSocket::close()
{
if (mSocket && !closed())
{
// FIXME: log errors
PR_Shutdown(mSocket, PR_SHUTDOWN_BOTH);
PR_Close(mSocket);
mSocket = NULL;
}
}
PRUint32
BufferedSocket::read(SearchableBuffer& buf, PRUint32 size)
{
PRUint32 numRead = 0;
while (readIntoBuffer(1024))
continue;
if (mReadBufferSize)
numRead = copyBuf(buf, mReadBuffer, mReadBufferSize, size);
return numRead;
}
PRUint32
BufferedSocket::readUntil(SearchableBuffer& buf, char c)
{
while (readIntoBuffer(256) != 0)
continue;
PRUint32 numRead = 0;
std::streamsize loc = mReadBuffer.find(c);
if (c != -1)
numRead = copyBuf(buf, mReadBuffer, mReadBufferSize, c);
return numRead;
}
void
BufferedSocket::write(const char* buf, PRUint32 size)
{
PRInt32 numSent = PR_Send(mSocket, buf, size, 0, PR_INTERVAL_NO_WAIT);
}
PRUint32
BufferedSocket::readIntoBuffer(PRUint32 size)
{
if (!size)
return 0;
PRInt32 numRead = 0;
PRInt32 status = PR_Recv(mSocket, tmpBuf, size, 0, PR_INTERVAL_NO_WAIT);
if (status == 0)
close();
else if (status == -1)
{
PRErrorCode err = PR_GetError();
if (err != PR_WOULD_BLOCK_ERROR)
close();
}
else if (status > 0)
{
std::streamsize bytesPut = mReadBuffer.sputn(tmpBuf, status);
mReadBufferSize += status;
numRead = status;
}
return numRead;
}
PRUint32
BufferedSocket::copyBuf(SearchableBuffer& dest, SearchableBuffer& src,
PRUint32& bufferSize, PRUint32 size)
{
PRUint32 sizeWritten = 0;
while (bufferSize && sizeWritten < size)
{
PRUint32 toWrite = (size - sizeWritten) % 1024;
if (toWrite > bufferSize)
toWrite = bufferSize;
src.sgetn(tmpBuf, toWrite);
dest.sputn(tmpBuf, toWrite);
sizeWritten += toWrite;
bufferSize -= toWrite;
}
return sizeWritten;
}

Просмотреть файл

@ -1,62 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef negatus_buffered_socket_h
#define negatus_buffered_socket_h
#include <prio.h>
#include <prtypes.h>
#include <sstream>
#include <vector>
class SearchableBuffer: public std::stringbuf
{
public:
std::streamsize find(char c);
};
class BufferedSocket
{
public:
BufferedSocket(PRFileDesc* socket);
virtual ~BufferedSocket();
void close();
bool closed() { return !mSocket; }
PRFileDesc* fd() { return mSocket; }
/* Attempts to read 'size' bytes into 'buf'. If the whole
amount cannot be read, it is buffered and nothing is
written to 'buf'. Returns the number of bytes written to
'buf'. 'buf' should be empty; anything in it may be
overwritten. */
PRUint32 read(SearchableBuffer& buf, PRUint32 size);
PRUint32 readUntil(SearchableBuffer& buf, char c);
PRUint32 readUntilNewline(SearchableBuffer& buf)
{ return readUntil(buf, '\n'); }
void write(const char* buf, PRUint32 size);
void flush();
bool writeBufferEmpty();
private:
PRFileDesc* mSocket;
SearchableBuffer mReadBuffer;
//char* mReadBuffer;
PRUint32 mReadBufferSize;
SearchableBuffer mWriteBuffer;
//char* mWriteBuffer;
//PRUint32 mWriteBufferSize;
char* tmpBuf;
PRUint32 readIntoBuffer(PRUint32 size);
PRUint32 copyBuf(SearchableBuffer& dest, SearchableBuffer& src,
PRUint32& bufferSize, PRUint32 size);
};
#endif

Просмотреть файл

@ -1,70 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "CommandEventHandler.h"
#include "BufferedSocket.h"
#include <iostream>
CommandEventHandler::CommandEventHandler(BufferedSocket& mBufSocket,
SessionEventHandler& session)
: mBufSocket(mBufSocket), mSession(session), mPrompt("$>")
{
sendPrompt();
}
void
CommandEventHandler::getPollDescs(std::vector<PRPollDesc>& descs)
{
if (!mBufSocket.closed())
{
PRPollDesc desc;
desc.fd = mBufSocket.fd();
desc.in_flags = PR_POLL_READ;
descs.push_back(desc);
}
}
void
CommandEventHandler::handleEvent(PRPollDesc desc)
{
if (desc.fd != mBufSocket.fd())
return;
if (!(desc.out_flags & PR_POLL_READ))
return;
SearchableBuffer buf;
while (mBufSocket.readUntilNewline(buf))
{
std::string line(buf.str());
for (std::string::reverse_iterator i = line.rbegin(); i != line.rend(); )
{
char c = *i++;
if (c == '\n' || c == '\r' || c == ' ' || c == '\t')
line.erase(line.size()-1);
else
break;
}
handleLine(line);
sendPrompt();
}
if (mBufSocket.closed())
close();
}
void
CommandEventHandler::handleLine(std::string line)
{
std::cout << "got line: " << line << std::endl;
}
void
CommandEventHandler::sendPrompt()
{
mBufSocket.write(mPrompt.c_str(), mPrompt.size());
}

Просмотреть файл

@ -1,34 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef negatus_command_handler_h
#define negatus_command_handler_h
#include <prio.h>
#include "EventHandler.h"
class BufferedSocket;
class SessionEventHandler;
class CommandEventHandler: public EventHandler
{
public:
CommandEventHandler(BufferedSocket& mBufSocket,
SessionEventHandler& session);
virtual void getPollDescs(std::vector<PRPollDesc>& desc);
virtual void handleEvent(PRPollDesc handle);
virtual std::string name() { return "CommandEventHandler"; }
void handleLine(std::string line);
private:
BufferedSocket& mBufSocket;
SessionEventHandler& mSession;
std::string mPrompt;
void sendPrompt();
};
#endif

Просмотреть файл

@ -1,36 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef negatus_event_handler_h
#define negatus_event_handler_h
#include <prio.h>
#include <prtime.h>
#include <string>
#include <vector>
class EventHandler {
public:
EventHandler() : mClosed(false) {}
virtual void close() { mClosed = true; }
bool closed() { return mClosed; }
virtual void getPollDescs(std::vector<PRPollDesc>& descs) = 0;
PRTime getTimeout() { return mTimeout; }
virtual void handleEvent(PRPollDesc handle) {}
virtual void handleTimeout() {}
virtual std::string name() { return "EventHandler"; }
protected:
void setTimeout(PRTime timeout) { mTimeout = timeout; }
void clearTimeout() { mTimeout = 0; }
PRTime mTimeout;
private:
bool mClosed;
};
#endif

Просмотреть файл

@ -1,21 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "Logging.h"
#include <prnetdb.h>
#include <sstream>
#include <iostream>
std::string
addrStr(PRNetAddr addr)
{
std::stringstream ss;
char s[32];
PR_NetAddrToString(&addr, s, 32);
ss << s;
if (addr.inet.port)
ss << ":" << PR_ntohs(addr.inet.port);
return ss.str();
}

Просмотреть файл

@ -1,13 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef negatus_logging_h
#define negatus_logging_h
#include <string>
#include <prio.h>
std::string addrStr(PRNetAddr addr);
#endif

Просмотреть файл

@ -1,121 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "Reactor.h"
#include "EventHandler.h"
#include <map>
Reactor* Reactor::mInstance = NULL;
Reactor::Reactor()
{
}
Reactor*
Reactor::instance()
{
if (!mInstance)
mInstance = new Reactor();
return mInstance;
}
void
Reactor::registerHandler(EventHandler* evtHandler)
{
mEvtHandlers.push_back(evtHandler);
}
void
Reactor::removeHandler(EventHandler* evtHandler)
{
for (std::vector<EventHandler*>::iterator i = mEvtHandlers.begin();
i != mEvtHandlers.end(); i++)
{
if (*i == evtHandler)
{
mEvtHandlers.erase(i);
return;
}
}
}
void
Reactor::run()
{
std::map<PRFileDesc*, EventHandler*> handleMap;
std::vector<PRPollDesc> descs;
for (std::vector<EventHandler*>::iterator i = mEvtHandlers.begin();
i != mEvtHandlers.end(); i++)
{
std::vector<PRPollDesc> handlerDescs;
(*i)->getPollDescs(handlerDescs);
for (std::vector<PRPollDesc>::iterator j = handlerDescs.begin();
j != handlerDescs.end(); j++)
{
descs.push_back(*j);
handleMap[(*j).fd] = *i;
}
}
PRIntn npds = descs.size();
PRPollDesc* pds = new PRPollDesc[npds];
int count = 0;
for (std::vector<PRPollDesc>::iterator i = descs.begin();
i != descs.end(); i++)
{
pds[count] = *i;
count++;
}
PRInt32 npdsReady = PR_Poll(pds, npds, PR_MillisecondsToInterval(500));
// FIXME: log errors
if (npdsReady > 0)
{
for (int i = 0; i < npds; i++)
{
if (pds[i].out_flags)
{
EventHandler* evtHandler = handleMap[pds[i].fd];
evtHandler->handleEvent(pds[i]);
}
}
}
delete[] pds;
deleteClosed();
}
void
Reactor::stop()
{
for (std::vector<EventHandler*>::iterator i = mEvtHandlers.begin();
i != mEvtHandlers.end(); )
{
(*i)->close();
delete (*i);
mEvtHandlers.erase(i);
}
}
void
Reactor::deleteClosed()
{
for (std::vector<EventHandler*>::iterator i = mEvtHandlers.begin();
i != mEvtHandlers.end(); i++)
{
if ((*i)->closed())
{
EventHandler* hdlr = *i;
mEvtHandlers.erase(i);
i = mEvtHandlers.begin();
delete hdlr;
}
}
}

Просмотреть файл

@ -1,31 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef negatus_reactor_h
#define negatus_reactor_h
#include <prio.h>
#include <prtime.h>
#include <vector>
class EventHandler;
class Reactor {
public:
Reactor();
void registerHandler(EventHandler* evtHandler);
void removeHandler(EventHandler* evtHandler);
void run();
void stop();
static Reactor* instance();
private:
std::vector<EventHandler*> mEvtHandlers;
static Reactor* mInstance;
void deleteClosed();
};
#endif

Просмотреть файл

@ -1,45 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "SessionEventHandler.h"
#include "CommandEventHandler.h"
#include "Reactor.h"
SessionEventHandler::SessionEventHandler(PRFileDesc* socket)
: mBufSocket(socket)
{
EventHandler* cmdEventHandler = new CommandEventHandler(mBufSocket, *this);
mEvtHandlerStack.push_back(cmdEventHandler);
Reactor::instance()->registerHandler(this);
}
void
SessionEventHandler::close()
{
mBufSocket.close();
EventHandler::close();
}
void
SessionEventHandler::getPollDescs(std::vector<PRPollDesc>& descs)
{
(*mEvtHandlerStack.back()).getPollDescs(descs);
}
void
SessionEventHandler::handleEvent(PRPollDesc desc)
{
EventHandler* evtHandler = mEvtHandlerStack.back();
evtHandler->handleEvent(desc);
if (evtHandler->closed())
{
mEvtHandlerStack.pop_back();
delete evtHandler;
}
if (mEvtHandlerStack.empty())
close();
}

Просмотреть файл

@ -1,30 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef negatus_session_handler_h
#define negatus_session_handler_h
#include <prio.h>
#include <vector>
#include "BufferedSocket.h"
#include "EventHandler.h"
class SessionEventHandler: public EventHandler
{
public:
SessionEventHandler(PRFileDesc* socket);
virtual void close();
virtual void getPollDescs(std::vector<PRPollDesc>& desc);
virtual void handleEvent(PRPollDesc handle);
virtual std::string name() { return "SessionEventHandler"; }
private:
std::vector<EventHandler*> mEvtHandlerStack;
BufferedSocket mBufSocket;
};
#endif

Просмотреть файл

@ -1,83 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "SocketAcceptor.h"
#include "Logging.h"
#include "Reactor.h"
#include "SessionEventHandler.h"
SocketAcceptor::SocketAcceptor()
: mSocket(NULL)
{
}
void
SocketAcceptor::close()
{
if (mSocket)
{
// FIXME: log errors
PR_Shutdown(mSocket, PR_SHUTDOWN_BOTH);
PR_Close(mSocket);
mSocket = NULL;
}
EventHandler::close();
}
void
SocketAcceptor::listen(PRNetAddr addr)
{
// FIXME: log errors
if (mSocket)
return;
mSocket = PR_OpenTCPSocket(PR_AF_INET);
PRSocketOptionData sockOpt;
sockOpt.option = PR_SockOpt_Nonblocking;
sockOpt.value.non_blocking = PR_TRUE;
PR_SetSocketOption(mSocket, &sockOpt);
sockOpt.option = PR_SockOpt_Reuseaddr;
sockOpt.value.reuse_addr = PR_TRUE;
PR_SetSocketOption(mSocket, &sockOpt);
PR_Bind(mSocket, &addr);
PR_Listen(mSocket, 128);
Reactor::instance()->registerHandler(this);
}
void
SocketAcceptor::getPollDescs(std::vector<PRPollDesc>& descs)
{
if (mSocket)
{
PRPollDesc desc;
desc.fd = mSocket;
desc.in_flags = PR_POLL_READ;
descs.push_back(desc);
}
}
void
SocketAcceptor::handleEvent(PRPollDesc desc)
{
if (desc.fd != mSocket)
return;
if (!(desc.out_flags & PR_POLL_READ))
return;
PRNetAddr remoteAddr;
PRFileDesc* newSocket = PR_Accept(mSocket, &remoteAddr, PR_INTERVAL_NO_WAIT);
PRSocketOptionData sockOpt;
sockOpt.option = PR_SockOpt_Nonblocking;
sockOpt.value.non_blocking = PR_TRUE;
PR_SetSocketOption(newSocket, &sockOpt);
SessionEventHandler* session = new SessionEventHandler(newSocket);
}

Просмотреть файл

@ -1,27 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef negatus_socket_acceptor_h
#define negatus_socket_acceptor_h
#include "EventHandler.h"
#include <prio.h>
class SocketAcceptor: public EventHandler {
public:
SocketAcceptor();
virtual void close();
void listen(PRNetAddr addr);
virtual void getPollDescs(std::vector<PRPollDesc>& desc);
virtual void handleEvent(PRPollDesc handle);
virtual std::string name() { return "SocketAcceptor"; }
private:
PRFileDesc* mSocket;
};
#endif