make it work
This commit is contained in:
parent
dba185c6aa
commit
19d75eb917
@ -16,20 +16,20 @@ int main(int argc, char * argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
nntpchan::Mainloop loop;
|
nntpchan::Mainloop loop;
|
||||||
|
|
||||||
nntpchan::NNTPServer nntp(loop);
|
nntpchan::NNTPServer nntp(loop);
|
||||||
|
|
||||||
std::string fname(argv[1]);
|
std::string fname(argv[1]);
|
||||||
|
|
||||||
std::ifstream i(fname);
|
std::ifstream i(fname);
|
||||||
|
|
||||||
if(i.is_open()) {
|
if(i.is_open()) {
|
||||||
INI::Parser conf(i);
|
INI::Parser conf(i);
|
||||||
|
|
||||||
std::vector<std::string> requiredSections = {"nntp", "storage"};
|
std::vector<std::string> requiredSections = {"nntp", "storage"};
|
||||||
|
|
||||||
auto & level = conf.top();
|
auto & level = conf.top();
|
||||||
|
|
||||||
for ( const auto & section : requiredSections ) {
|
for ( const auto & section : requiredSections ) {
|
||||||
if(level.sections.find(section) == level.sections.end()) {
|
if(level.sections.find(section) == level.sections.end()) {
|
||||||
std::cerr << "config file " << fname << " does not have required section: ";
|
std::cerr << "config file " << fname << " does not have required section: ";
|
||||||
@ -46,7 +46,7 @@ int main(int argc, char * argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
nntp.SetStoragePath(storeconf["path"]);
|
nntp.SetStoragePath(storeconf["path"]);
|
||||||
|
|
||||||
auto & nntpconf = level.sections["nntp"].values;
|
auto & nntpconf = level.sections["nntp"].values;
|
||||||
|
|
||||||
if (nntpconf.find("bind") == nntpconf.end()) {
|
if (nntpconf.find("bind") == nntpconf.end()) {
|
||||||
@ -75,29 +75,26 @@ int main(int argc, char * argv[]) {
|
|||||||
} else {
|
} else {
|
||||||
std::cerr << "unknown frontend type '" << ftype << "'" << std::endl;
|
std::cerr << "unknown frontend type '" << ftype << "'" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto & a = nntpconf["bind"];
|
auto & a = nntpconf["bind"];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
nntp.Bind(a);
|
nntp.Bind(a);
|
||||||
} catch ( std::exception & ex ) {
|
} catch ( std::exception & ex ) {
|
||||||
std::cerr << "failed to bind: " << ex.what() << std::endl;
|
std::cerr << "failed to bind: " << ex.what() << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
std::cerr << "run mainloop" << std::endl;
|
std::cerr << "run mainloop" << std::endl;
|
||||||
loop.Run();
|
loop.Run();
|
||||||
} catch ( std::exception & ex ) {
|
std::cerr << "stopped mainloop" << std::endl;
|
||||||
std::cerr << "exception in mainloop: " << ex.what() << std::endl;
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
std::cerr << "failed to open " << fname << std::endl;
|
std::cerr << "failed to open " << fname << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ namespace nntpchan
|
|||||||
|
|
||||||
void Mainloop::Run(uv_run_mode mode)
|
void Mainloop::Run(uv_run_mode mode)
|
||||||
{
|
{
|
||||||
uv_run(m_loop, mode);
|
assert(uv_run(m_loop, mode) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ namespace nntpchan {
|
|||||||
|
|
||||||
LineReader::LineReader(size_t limit) : m_close(false), lineLimit(limit) {}
|
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;
|
if(l <= 0) return;
|
||||||
// process leftovers
|
// process leftovers
|
||||||
@ -32,6 +32,8 @@ namespace nntpchan {
|
|||||||
// leftovers
|
// leftovers
|
||||||
m_leftovers = std::string(data, begin-idx);
|
m_leftovers = std::string(data, begin-idx);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
m_leftovers = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
void LineReader::OnLine(const char *d, const size_t l)
|
void LineReader::OnLine(const char *d, const size_t l)
|
||||||
|
@ -13,7 +13,7 @@ namespace nntpchan
|
|||||||
LineReader(size_t lineLimit);
|
LineReader(size_t lineLimit);
|
||||||
|
|
||||||
/** @brief queue inbound data from connection */
|
/** @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 */
|
/** implements IConnHandler */
|
||||||
virtual bool ShouldClose();
|
virtual bool ShouldClose();
|
||||||
|
@ -21,7 +21,7 @@ namespace nntpchan
|
|||||||
char * buff = new char[l];
|
char * buff = new char[l];
|
||||||
// read file
|
// read file
|
||||||
m_instream->read(buff, l);
|
m_instream->read(buff, l);
|
||||||
OnData(buff, l);
|
Data(buff, l);
|
||||||
delete [] buff;
|
delete [] buff;
|
||||||
return m_found;
|
return m_found;
|
||||||
}
|
}
|
||||||
|
@ -21,11 +21,6 @@ namespace nntpchan
|
|||||||
if(m_auth) delete m_auth;
|
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)
|
void NNTPServerHandler::HandleLine(const std::string &line)
|
||||||
{
|
{
|
||||||
if(m_state == eStateReadCommand) {
|
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)
|
void NNTPServerHandler::HandleCommand(const std::deque<std::string> & command)
|
||||||
{
|
{
|
||||||
auto cmd = command[0];
|
auto cmd = command[0];
|
||||||
|
@ -16,10 +16,10 @@ namespace nntpchan
|
|||||||
|
|
||||||
virtual bool ShouldClose();
|
virtual bool ShouldClose();
|
||||||
|
|
||||||
virtual void OnData(const char * data, ssize_t sz);
|
|
||||||
|
|
||||||
void SetAuth(NNTPCredentialDB * creds);
|
void SetAuth(NNTPCredentialDB * creds);
|
||||||
|
|
||||||
|
virtual void OnData(const char *, ssize_t);
|
||||||
|
|
||||||
void Greet();
|
void Greet();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -58,7 +58,7 @@ namespace nntpchan
|
|||||||
void NNTPServerConn::SendNextReply()
|
void NNTPServerConn::SendNextReply()
|
||||||
{
|
{
|
||||||
IConnHandler * handler = GetHandler();
|
IConnHandler * handler = GetHandler();
|
||||||
if(handler->HasNextLine()) {
|
while(handler->HasNextLine()) {
|
||||||
auto line = handler->GetNextLine();
|
auto line = handler->GetNextLine();
|
||||||
SendString(line + "\n");
|
SendString(line + "\n");
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@ namespace nntpchan
|
|||||||
|
|
||||||
void Server::Close()
|
void Server::Close()
|
||||||
{
|
{
|
||||||
|
std::cout << "Close server" << std::endl;
|
||||||
uv_close((uv_handle_t*)&m_server, [](uv_handle_t * s) {
|
uv_close((uv_handle_t*)&m_server, [](uv_handle_t * s) {
|
||||||
Server * self = (Server*)s->data;
|
Server * self = (Server*)s->data;
|
||||||
if (self) delete self;
|
if (self) delete self;
|
||||||
@ -39,8 +40,10 @@ namespace nntpchan
|
|||||||
OnAcceptError(status);
|
OnAcceptError(status);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
std::cout << "new conn" << std::endl;
|
||||||
IServerConn * conn = CreateConn(s);
|
IServerConn * conn = CreateConn(s);
|
||||||
assert(conn);
|
assert(conn);
|
||||||
|
m_conns.push_back(conn);
|
||||||
conn->Greet();
|
conn->Greet();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,15 +76,14 @@ namespace nntpchan
|
|||||||
return line;
|
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_loop = l;
|
||||||
m_stream = s;
|
|
||||||
m_parent = parent;
|
m_parent = parent;
|
||||||
m_handler = h;
|
m_handler = h;
|
||||||
uv_tcp_init(l, &m_conn);
|
uv_tcp_init(l, &m_conn);
|
||||||
m_conn.data = this;
|
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) {
|
uv_read_start((uv_stream_t*) &m_conn, [] (uv_handle_t * h, size_t s, uv_buf_t * b) {
|
||||||
IServerConn * self = (IServerConn*) h->data;
|
IServerConn * self = (IServerConn*) h->data;
|
||||||
if(self == nullptr) return;
|
if(self == nullptr) return;
|
||||||
@ -94,6 +96,7 @@ namespace nntpchan
|
|||||||
IServerConn * self = (IServerConn*) s->data;
|
IServerConn * self = (IServerConn*) s->data;
|
||||||
if(self == nullptr) return;
|
if(self == nullptr) return;
|
||||||
if(nread > 0) {
|
if(nread > 0) {
|
||||||
|
std::cout << "read " << nread << std::endl;
|
||||||
self->m_handler->OnData(b->base, nread);
|
self->m_handler->OnData(b->base, nread);
|
||||||
self->SendNextReply();
|
self->SendNextReply();
|
||||||
if(self->m_handler->ShouldClose())
|
if(self->m_handler->ShouldClose())
|
||||||
@ -118,7 +121,7 @@ namespace nntpchan
|
|||||||
void IServerConn::SendString(const std::string & str)
|
void IServerConn::SendString(const std::string & str)
|
||||||
{
|
{
|
||||||
WriteBuffer * b = new WriteBuffer(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;
|
(void) status;
|
||||||
WriteBuffer * wb = (WriteBuffer *) w->data;
|
WriteBuffer * wb = (WriteBuffer *) w->data;
|
||||||
if(wb)
|
if(wb)
|
||||||
|
@ -50,11 +50,9 @@ namespace nntpchan
|
|||||||
void SendString(const std::string & str);
|
void SendString(const std::string & str);
|
||||||
Server * Parent() { return m_parent; };
|
Server * Parent() { return m_parent; };
|
||||||
IConnHandler * GetHandler() { return m_handler; };
|
IConnHandler * GetHandler() { return m_handler; };
|
||||||
operator uv_stream_t * () { return m_stream; };
|
|
||||||
uv_loop_t * GetLoop() { return m_loop; };
|
uv_loop_t * GetLoop() { return m_loop; };
|
||||||
private:
|
private:
|
||||||
uv_tcp_t m_conn;
|
uv_tcp_t m_conn;
|
||||||
uv_stream_t * m_stream;
|
|
||||||
uv_loop_t * m_loop;
|
uv_loop_t * m_loop;
|
||||||
Server * m_parent;
|
Server * m_parent;
|
||||||
IConnHandler * m_handler;
|
IConnHandler * m_handler;
|
||||||
|
Reference in New Issue
Block a user