summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/include/server_session.h2
-rw-r--r--server/src/server_session.cpp140
2 files changed, 113 insertions, 29 deletions
diff --git a/server/include/server_session.h b/server/include/server_session.h
index 436c4ee..a4c570b 100644
--- a/server/include/server_session.h
+++ b/server/include/server_session.h
@@ -53,6 +53,8 @@ private:
void handle_write_no_read(const boost::system::error_code& error);
void handle_handshake(const boost::system::error_code& error);
bool handle_command(client_msg* msg);
+
+ void send_download_list();
char* recv_data_ = nullptr;
socket_wraper* socket_ = nullptr;
diff --git a/server/src/server_session.cpp b/server/src/server_session.cpp
index 868370a..43d8837 100644
--- a/server/src/server_session.cpp
+++ b/server/src/server_session.cpp
@@ -421,40 +421,28 @@ bool server_session::handle_command(client_msg *msg)
}
break;
case CLIENT_MSG_TYPE::CLIENT_DOWNLOADS_LIST_REQUEST:
+ send_download_list();
+ break;
+ case CLIENT_MSG_TYPE::CLIENT_DOWNLOAD_INFO_REQUEST:
{
- //TODO: thread safety
- //draft implementation, need a lot of optimizations
- server_msg m;
- m.set_type(SERVER_MSG_TYPE::SERVER_DOWNLOADS_LIST_REPLY);
- for(auto i : downloads)
+ for(auto i : modules->get_downloader_modules())
{
- server_download_reply *r = m.add_downloads();
- download *d = r->mutable_download();
- d->set_id(i.first); //set core_id for later access
- d->set_module_name(i.second.module_name);
- module_base *module = nullptr;
- for(auto dm : modules->get_downloader_modules())
- {
- if(dm->get_module_info().name == i.second.module_name)
- {
- module = dm;
- break;
- }
- }
- if(!module)
+ if(i->get_module_info().name == downloads[msg->download_info_request().download_id()].module_name)
{
- //downloader module not found, this should be a fatal error
- continue;
+ auto dm = static_cast<module_downloader *>(i);
+ server_msg m;
+ m.set_type(SERVER_MSG_TYPE::SERVER_DOWNLOAD_INFO_REPLY);
+ download *d = m.mutable_download()->mutable_download();
+ d->set_id(msg->download_info_request().download_id());
+ auto dl = dm->get_download(downloads[msg->download_info_request().download_id()].module_id);
+ d->set_name(dl.name);
+ d->set_size(dl.size);
+ d->set_state(dl.state);
+ d->set_module_name(msg->download_add_request().module_name());
+ d->set_downloaded(dl.downloaded);
+ send_message(&m);
}
- module_downloader *dm = static_cast<module_downloader*>(module);
- download_s ds = dm->get_download(i.second.module_id);
- d->set_downloaded(ds.downloaded);
- d->set_size(ds.size);
- d->set_name(ds.name);
- d->set_state(ds.state);
}
- send_message(&m);
- return true;
}
break;
case CLIENT_MSG_TYPE::CLIENT_DOWNLOAD_ADD:
@@ -482,6 +470,64 @@ bool server_session::handle_command(client_msg *msg)
d->set_module_name(msg->download_add_request().module_name());
d->set_downloaded(dl.downloaded);
send_message(&m);
+ break;
+ }
+ }
+ }
+ break;
+ case CLIENT_MSG_TYPE::CLIENT_DOWNLOAD_START:
+ {
+ for(auto i : msg->download_start_request())
+ {
+ for(auto m : modules->get_downloader_modules())
+ {
+ if(m->get_module_info().name == downloads[i.download_id()].module_name)
+ {
+ auto dm = static_cast<module_downloader *>(m);
+ if(!dm->start_download(i.download_id()))
+ {
+ //TODO: handle error
+ }
+ break;
+ }
+ }
+ }
+ }
+ break;
+ case CLIENT_MSG_TYPE::CLIENT_DOWNLOAD_STOP:
+ {
+ for(auto i : msg->download_stop_request())
+ {
+ for(auto m : modules->get_downloader_modules())
+ {
+ if(m->get_module_info().name == downloads[i.download_id()].module_name)
+ {
+ auto dm = static_cast<module_downloader *>(m);
+ if(!dm->stop_download(i.download_id()))
+ {
+ //TODO: handle error
+ }
+ break;
+ }
+ }
+ }
+ }
+ break;
+ case CLIENT_MSG_TYPE::CLIENT_DOWNLOAD_DELETE:
+ {
+ for(auto i : msg->download_delete_request())
+ {
+ for(auto m : modules->get_downloader_modules())
+ {
+ if(m->get_module_info().name == downloads[i.download_id()].module_name)
+ {
+ auto dm = static_cast<module_downloader *>(m);
+ if(!dm->delete_download(i.download_id(), i.with_data()));
+ {
+ //TODO: handle error
+ }
+ break;
+ }
}
}
}
@@ -493,6 +539,42 @@ bool server_session::handle_command(client_msg *msg)
return true;
}
+void server_session::send_download_list()
+{
+ //TODO: thread safety
+ //draft implementation, need a lot of optimizations
+ server_msg m;
+ m.set_type(SERVER_MSG_TYPE::SERVER_DOWNLOADS_LIST_REPLY);
+ for(auto i : downloads)
+ {
+ server_download_reply *r = m.add_downloads();
+ download *d = r->mutable_download();
+ d->set_id(i.first); //set core_id for later access
+ d->set_module_name(i.second.module_name);
+ module_base *module = nullptr;
+ for(auto dm : modules->get_downloader_modules())
+ {
+ if(dm->get_module_info().name == i.second.module_name)
+ {
+ module = dm;
+ break;
+ }
+ }
+ if(!module)
+ {
+ //downloader module not found, this should be a fatal error
+ continue;
+ }
+ module_downloader *dm = static_cast<module_downloader*>(module);
+ download_s ds = dm->get_download(i.second.module_id);
+ d->set_downloaded(ds.downloaded);
+ d->set_size(ds.size);
+ d->set_name(ds.name);
+ d->set_state(ds.state);
+ }
+ send_message(&m);
+}
+
void server_session::send_message(server_msg *msg)
{
int size = 0;