Archived
1
0

make it work

This commit is contained in:
Jeff Becker 2017-05-03 09:44:42 -04:00
parent dba185c6aa
commit 19d75eb917
10 changed files with 38 additions and 38 deletions

View File

@ -16,20 +16,20 @@ int main(int argc, char * argv[]) {
}
nntpchan::Mainloop loop;
nntpchan::NNTPServer nntp(loop);
std::string fname(argv[1]);
std::ifstream i(fname);
if(i.is_open()) {
INI::Parser conf(i);
std::vector<std::string> requiredSections = {"nntp", "storage"};
auto & level = conf.top();
for ( const auto & section : requiredSections ) {
if(level.sections.find(section) == level.sections.end()) {
std::cerr << "config file " << fname << " does not have required section: ";
@ -46,7 +46,7 @@ int main(int argc, char * argv[]) {
}
nntp.SetStoragePath(storeconf["path"]);
auto & nntpconf = level.sections["nntp"].values;
if (nntpconf.find("bind") == nntpconf.end()) {
@ -75,29 +75,26 @@ int main(int argc, char * argv[]) {
} else {
std::cerr << "unknown frontend type '" << ftype << "'" << std::endl;
}
}
auto & a = nntpconf["bind"];
try {
nntp.Bind(a);
nntp.Bind(a);
} catch ( std::exception & ex ) {
std::cerr << "failed to bind: " << ex.what() << std::endl;
return 1;
}
try {
std::cerr << "run mainloop" << std::endl;
loop.Run();
} catch ( std::exception & ex ) {
std::cerr << "exception in mainloop: " << ex.what() << std::endl;
return 2;
}
std::cerr << "run mainloop" << std::endl;
loop.Run();
std::cerr << "stopped mainloop" << std::endl;
} else {
std::cerr << "failed to open " << fname << std::endl;
return 1;
}
}

View File

@ -21,7 +21,7 @@ namespace nntpchan
void Mainloop::Run(uv_run_mode mode)
{
uv_run(m_loop, mode);
assert(uv_run(m_loop, mode) == 0);
}
}

View File

@ -5,7 +5,7 @@ namespace nntpchan {
LineReader::LineReader(size_t limit) : m_close(false), lineLimit(limit) {}
void LineReader::OnData(const char * d, ssize_t l)
void LineReader::Data(const char * d, ssize_t l)
{
if(l <= 0) return;
// process leftovers
@ -32,6 +32,8 @@ namespace nntpchan {
// leftovers
m_leftovers = std::string(data, begin-idx);
}
else
m_leftovers = "";
}
void LineReader::OnLine(const char *d, const size_t l)

View File

@ -13,7 +13,7 @@ namespace nntpchan
LineReader(size_t lineLimit);
/** @brief queue inbound data from connection */
virtual void OnData(const char * data, ssize_t s);
void Data(const char * data, ssize_t s);
/** implements IConnHandler */
virtual bool ShouldClose();

View File

@ -21,7 +21,7 @@ namespace nntpchan
char * buff = new char[l];
// read file
m_instream->read(buff, l);
OnData(buff, l);
Data(buff, l);
delete [] buff;
return m_found;
}

View File

@ -21,11 +21,6 @@ namespace nntpchan
if(m_auth) delete m_auth;
}
void NNTPServerHandler::OnData(const char * data, ssize_t sz)
{
LineReader::OnData(data, sz);
}
void NNTPServerHandler::HandleLine(const std::string &line)
{
if(m_state == eStateReadCommand) {
@ -42,6 +37,11 @@ namespace nntpchan
}
}
void NNTPServerHandler::OnData(const char * data, ssize_t l)
{
Data(data, l);
}
void NNTPServerHandler::HandleCommand(const std::deque<std::string> & command)
{
auto cmd = command[0];

View File

@ -16,10 +16,10 @@ namespace nntpchan
virtual bool ShouldClose();
virtual void OnData(const char * data, ssize_t sz);
void SetAuth(NNTPCredentialDB * creds);
virtual void OnData(const char *, ssize_t);
void Greet();
protected:

View File

@ -58,7 +58,7 @@ namespace nntpchan
void NNTPServerConn::SendNextReply()
{
IConnHandler * handler = GetHandler();
if(handler->HasNextLine()) {
while(handler->HasNextLine()) {
auto line = handler->GetNextLine();
SendString(line + "\n");
}

View File

@ -15,6 +15,7 @@ namespace nntpchan
void Server::Close()
{
std::cout << "Close server" << std::endl;
uv_close((uv_handle_t*)&m_server, [](uv_handle_t * s) {
Server * self = (Server*)s->data;
if (self) delete self;
@ -39,8 +40,10 @@ namespace nntpchan
OnAcceptError(status);
return;
}
std::cout << "new conn" << std::endl;
IServerConn * conn = CreateConn(s);
assert(conn);
m_conns.push_back(conn);
conn->Greet();
}
@ -73,15 +76,14 @@ namespace nntpchan
return line;
}
IServerConn::IServerConn(uv_loop_t * l, uv_stream_t * s, Server * parent, IConnHandler * h)
IServerConn::IServerConn(uv_loop_t * l, uv_stream_t * st, Server * parent, IConnHandler * h)
{
m_loop = l;
m_stream = s;
m_parent = parent;
m_handler = h;
uv_tcp_init(l, &m_conn);
m_conn.data = this;
uv_accept(s, (uv_stream_t*) &m_conn);
uv_accept(st, (uv_stream_t*) &m_conn);
uv_read_start((uv_stream_t*) &m_conn, [] (uv_handle_t * h, size_t s, uv_buf_t * b) {
IServerConn * self = (IServerConn*) h->data;
if(self == nullptr) return;
@ -94,6 +96,7 @@ namespace nntpchan
IServerConn * self = (IServerConn*) s->data;
if(self == nullptr) return;
if(nread > 0) {
std::cout << "read " << nread << std::endl;
self->m_handler->OnData(b->base, nread);
self->SendNextReply();
if(self->m_handler->ShouldClose())
@ -118,7 +121,7 @@ namespace nntpchan
void IServerConn::SendString(const std::string & str)
{
WriteBuffer * b = new WriteBuffer(str);
uv_write(&b->w, *this, &b->b, 1, [](uv_write_t * w, int status) {
uv_write(&b->w, (uv_stream_t*)&m_conn, &b->b, 1, [](uv_write_t * w, int status) {
(void) status;
WriteBuffer * wb = (WriteBuffer *) w->data;
if(wb)

View File

@ -50,11 +50,9 @@ namespace nntpchan
void SendString(const std::string & str);
Server * Parent() { return m_parent; };
IConnHandler * GetHandler() { return m_handler; };
operator uv_stream_t * () { return m_stream; };
uv_loop_t * GetLoop() { return m_loop; };
private:
uv_tcp_t m_conn;
uv_stream_t * m_stream;
uv_loop_t * m_loop;
Server * m_parent;
IConnHandler * m_handler;