summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGluzskiy Alexandr <sss@sss.chaoslab.ru>2015-08-31 20:45:06 +0300
committerGluzskiy Alexandr <sss@sss.chaoslab.ru>2015-08-31 20:45:06 +0300
commit4b4002115d6c7b5c5113d1e2e6e9025565e1c9bb (patch)
tree4e4ecc6d26052bb5f50acd0c1a9260aceb5479fd
parent12eba4d75931c5e48dac70b1a7dbad7861d059df (diff)
protocol:
download struct must have module_name field server: core: implemented SERVER_DOWNLOADS_LIST_REPLY (draft) added global downloads list client: using std::string instead of QString to avoid unnecessary conversions
-rw-r--r--client-qt/udm-client-qt/udm_main.cpp5
-rw-r--r--client-qt/udm-client-qt/udm_main.h2
-rw-r--r--protocol/udm.proto13
-rw-r--r--server/include/download_internal.h31
-rw-r--r--server/include/modules_handler.h2
-rw-r--r--server/include/server.h4
-rw-r--r--server/include/server_session.h4
-rw-r--r--server/src/main.cpp6
-rw-r--r--server/src/modules_handler.cpp16
-rw-r--r--server/src/server.cpp4
-rw-r--r--server/src/server_session.cpp40
-rw-r--r--server/udm-server.cbp1
12 files changed, 114 insertions, 14 deletions
diff --git a/client-qt/udm-client-qt/udm_main.cpp b/client-qt/udm-client-qt/udm_main.cpp
index 3f94450..91cfb10 100644
--- a/client-qt/udm-client-qt/udm_main.cpp
+++ b/client-qt/udm-client-qt/udm_main.cpp
@@ -167,23 +167,26 @@ void udm_main::server_message_received(server_msg msg)
{
if(msg.auth_reply().status())
{
- auth_token = msg.auth_reply().auth_token().c_str();
+ auth_token = msg.auth_reply().auth_token();
//TODO: update client status
lbl_state->setText(tr("State") + ": " + tr("Connected") + ", " + tr("Authenticated"));
//request modules and settings here
{
client_msg msg;
msg.set_type(CLIENT_MSG_TYPE::CLIENT_CORE_INFO_REQUEST);
+ msg.set_auth_token(auth_token);
session->send_message(msg);
}
{
client_msg msg;
msg.set_type(CLIENT_MSG_TYPE::CLIENT_MODULES_REQUEST);
+ msg.set_auth_token(auth_token);
session->send_message(msg);
}
{
client_msg msg;
msg.set_type(CLIENT_MSG_TYPE::CLIENT_DOWNLOADS_LIST_REQUEST);
+ msg.set_auth_token(auth_token);
session->send_message(msg);
}
}
diff --git a/client-qt/udm-client-qt/udm_main.h b/client-qt/udm-client-qt/udm_main.h
index 9d56822..23fe887 100644
--- a/client-qt/udm-client-qt/udm_main.h
+++ b/client-qt/udm-client-qt/udm_main.h
@@ -67,7 +67,7 @@ private:
QThread *thread_client_session;
client_session *session;
boost::asio::io_service *io_service_;
- QString auth_token;
+ std::string auth_token;
QLabel *lbl_state;
QTableView *tbl_downloads;
QTreeView *tree_filters;
diff --git a/protocol/udm.proto b/protocol/udm.proto
index d15fdcf..029183f 100644
--- a/protocol/udm.proto
+++ b/protocol/udm.proto
@@ -147,12 +147,13 @@ message download_content_entry {
message download {
required int32 id = 1;
required string name = 2;
- optional download_content_entry content = 3;
- required int64 size = 4;
- required int64 downloaded = 5;
+ required string module_name = 3;
+ optional download_content_entry content = 4;
+ required int64 size = 5;
+ required int64 downloaded = 6;
}
-message server_downloads_reply {
+message server_download_reply {
required download download = 1;
}
@@ -347,6 +348,7 @@ enum SERVER_MSG_TYPE {
SERVER_SUBSCRIPTIONS_REPLY = 4;
SERVER_UNSUBSCRIPTIONS_REPLY = 5;
SERVER_DOWNLOADS_LIST_REPLY = 6;
+ SERVER_DOWNLOAD_INFO_REPLY = 7;
}
@@ -359,7 +361,8 @@ message server_msg {
optional core_info server_core_info_reply = 5;
repeated server_event_subscription_reply subscription_reply = 6;
repeated server_event_unsubscription_reply unsubscription_reply = 7;
- optional server_downloads_reply downloads = 8;
+ optional server_download_reply download = 8;
+ repeated server_download_reply downloads = 9;
}
//top level messages end
diff --git a/server/include/download_internal.h b/server/include/download_internal.h
new file mode 100644
index 0000000..3474071
--- /dev/null
+++ b/server/include/download_internal.h
@@ -0,0 +1,31 @@
+/*
+ Copyright © 2015 Gluzskiy Alexandr (sss)
+
+ This file is part of Unknown Download Manager (UDM).
+
+ UDM is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ UDM is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with UDM. If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#ifndef DOWNLOAD_INTERNAL_H
+#define DOWNLOAD_INTERNAL_H
+
+struct download_internal_s
+{
+ int core_id, module_id;
+ std::string module_name;
+};
+
+
+#endif
diff --git a/server/include/modules_handler.h b/server/include/modules_handler.h
index 5696053..7c8bc71 100644
--- a/server/include/modules_handler.h
+++ b/server/include/modules_handler.h
@@ -24,6 +24,7 @@
#include <api_module_metadata_storage.h>
#include <api_module_downloader.h>
+#include "download_internal.h"
#include <list>
class modules_handler
@@ -32,6 +33,7 @@ class modules_handler
modules_handler();
void load_modules();
void on_modules_loaded();
+ void sync_downloads(std::map<int, download_internal_s> &downloads);
std::string list_modules();
std::list<module_base*> &get_metadata_modules();
std::list<module_base*> &get_downloader_modules();
diff --git a/server/include/server.h b/server/include/server.h
index 699fb01..046628a 100644
--- a/server/include/server.h
+++ b/server/include/server.h
@@ -26,6 +26,7 @@
#include <boost/asio/ssl.hpp>
#include "config.h"
#include "client.h"
+#include "download_internal.h"
//TODO ssl
@@ -35,7 +36,7 @@ class server_session;
class server
{
public:
- server(boost::asio::io_service& io_service, runtime_config_s &config, std::map<std::string, client*> &clients, short port);
+ server(boost::asio::io_service& io_service, runtime_config_s &config, std::map<std::string, client*> &clients, std::map<int, download_internal_s> &downloads, short port);
private:
void start_accept();
@@ -46,6 +47,7 @@ private:
boost::asio::ssl::context *context_;
runtime_config_s &runtime_config;
std::map<std::string, client*> &clients;
+ std::map<int, download_internal_s> &downloads;
};
diff --git a/server/include/server_session.h b/server/include/server_session.h
index bad8f4f..01a8c4b 100644
--- a/server/include/server_session.h
+++ b/server/include/server_session.h
@@ -26,6 +26,7 @@
#include <boost/asio/ssl.hpp>
#include "config.h"
#include "client.h"
+#include "download_internal.h"
class client_msg;
class server_msg;
@@ -34,7 +35,7 @@ class socket_wraper;
class server_session
{
public:
- server_session(boost::asio::io_service &s, runtime_config_s &config, std::map<std::string, client*> &clients_, boost::asio::ssl::context *c = nullptr);
+ server_session(boost::asio::io_service &s, runtime_config_s &config, std::map<std::string, client*> &clients_, std::map<int, download_internal_s> &downloads, boost::asio::ssl::context *c = nullptr);
void run();
void send_message(server_msg *msg);
socket_wraper* socket();
@@ -55,6 +56,7 @@ class server_session
runtime_config_s &runtime_config;
std::map<std::string, client*> &clients;
client *client_;
+ std::map<int, download_internal_s> &downloads;
//TODO: store client ptr
};
diff --git a/server/src/main.cpp b/server/src/main.cpp
index b856fd4..09b0b62 100644
--- a/server/src/main.cpp
+++ b/server/src/main.cpp
@@ -32,6 +32,7 @@
#include "main.h"
#include "config.h"
#include "client.h"
+#include "download_internal.h"
core_api *module_api = nullptr;
@@ -44,6 +45,8 @@ std::map<std::string, client*> clients; //auth token used for key
runtime_config_s runtime_config;
+std::map<int, download_internal_s> downloads;
+
server *serv = nullptr;
@@ -212,6 +215,7 @@ extern "C" int main(int argc, char *argv[])
return -1;
}
modules->on_modules_loaded(); //call second initialization stage in modules
+ modules->sync_downloads(downloads);
if(daemon)
{
@@ -220,7 +224,7 @@ extern "C" int main(int argc, char *argv[])
boost::asio::io_service io_service_server;
try{
//TODO: server options ip address, interface name
- serv = new server(io_service_server, runtime_config, clients, runtime_config.config_file.get<int>("server.port", 6613));
+ serv = new server(io_service_server, runtime_config, clients, downloads, runtime_config.config_file.get<int>("server.port", 6613));
}
catch(std::exception &e)
{
diff --git a/server/src/modules_handler.cpp b/server/src/modules_handler.cpp
index 6d351bb..eb08261 100644
--- a/server/src/modules_handler.cpp
+++ b/server/src/modules_handler.cpp
@@ -190,6 +190,22 @@ void modules_handler::on_modules_loaded()
}
}
+void modules_handler::sync_downloads(std::map<int, download_internal_s> &downloads)
+{
+ int id = downloads.size();
+ for(auto i : downloader_modules)
+ {
+ module_downloader *d = static_cast<module_downloader*>(i);
+ for(auto it : d->get_downloads())
+ {
+ downloads[id].module_name = d->get_module_info().name;
+ downloads[id].core_id = id; //is it needed ?
+ downloads[id].module_id = it.id;
+ id++;
+ }
+ }
+}
+
void modules_handler::load_modules_settings()
{
for(auto i : metadata_modules)
diff --git a/server/src/server.cpp b/server/src/server.cpp
index 8c2c965..9eb44f4 100644
--- a/server/src/server.cpp
+++ b/server/src/server.cpp
@@ -31,7 +31,7 @@
-server::server(boost::asio::io_service& io_service,runtime_config_s &config, std::map<std::string, client*> &clients_, short port) : io_service_(io_service), acceptor_(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)), runtime_config(config), clients(clients_)
+server::server(boost::asio::io_service& io_service,runtime_config_s &config, std::map<std::string, client*> &clients_, std::map<int, download_internal_s> &downloads_, short port) : io_service_(io_service), acceptor_(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)), runtime_config(config), clients(clients_), downloads(downloads_)
{
if(runtime_config.config_file.get<bool>("server.enable_encryption", false))
{
@@ -48,7 +48,7 @@ server::server(boost::asio::io_service& io_service,runtime_config_s &config, std
void server::start_accept()
{
- server_session* new_session = new server_session(io_service_, runtime_config, clients);
+ server_session* new_session = new server_session(io_service_, runtime_config, clients, downloads);
if(runtime_config.config_file.get<bool>("server.enable_encryption", false))
acceptor_.async_accept(new_session->socket()->get_ssl_socket().lowest_layer(), boost::bind(&server::handle_accept, this, new_session, boost::asio::placeholders::error));
else
diff --git a/server/src/server_session.cpp b/server/src/server_session.cpp
index c9b8e91..d0ff26c 100644
--- a/server/src/server_session.cpp
+++ b/server/src/server_session.cpp
@@ -34,7 +34,7 @@ extern modules_handler *modules;
-server_session::server_session(boost::asio::io_service &s, runtime_config_s &config, std::map<std::string, client*> &clients_, boost::asio::ssl::context *c) : io_service_(s), context_(c), runtime_config(config), clients(clients_)
+server_session::server_session(boost::asio::io_service &s, runtime_config_s &config, std::map<std::string, client*> &clients_, std::map<int, download_internal_s> &downloads_, boost::asio::ssl::context *c) : io_service_(s), context_(c), runtime_config(config), clients(clients_), downloads(downloads_)
{
if(runtime_config.config_file.get<bool>("server.enable_encryption", false))
{
@@ -120,6 +120,12 @@ void server_session::handle_read(const boost::system::error_code& error, size_t
void server_session::handle_command(client_msg *msg)
{
+ if(msg->type() != CLIENT_MSG_TYPE::CLIENT_AUTH_REQUEST && clients.find(msg->auth_token()) == clients.end())
+ {
+ BOOST_LOG_TRIVIAL(error)<<"unauthorized client trying to execute command";
+ delete this; //unauthorized client, delete session
+ return;
+ }
switch(msg->type())
{
case CLIENT_MSG_TYPE::CLIENT_AUTH_REQUEST:
@@ -268,7 +274,37 @@ void server_session::handle_command(client_msg *msg)
break;
case CLIENT_MSG_TYPE::CLIENT_DOWNLOADS_LIST_REQUEST:
{
- //TODO:
+ //TODO: thread safety
+ //draft implementation, need a lot of optimizations
+ server_msg msg;
+ msg.set_type(SERVER_MSG_TYPE::SERVER_DOWNLOADS_LIST_REPLY);
+ for(auto i : downloads)
+ {
+ server_download_reply *r = msg.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);
+ }
+ send_message(&msg);
}
break;
default:
diff --git a/server/udm-server.cbp b/server/udm-server.cbp
index 0d79409..7d2a776 100644
--- a/server/udm-server.cbp
+++ b/server/udm-server.cbp
@@ -62,6 +62,7 @@
<Unit filename="include/api_module_metadata_storage.h" />
<Unit filename="include/client.h" />
<Unit filename="include/config.h" />
+ <Unit filename="include/download_internal.h" />
<Unit filename="include/main.h" />
<Unit filename="include/modules_handler.h" />
<Unit filename="include/protocol.h" />