Client.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <LibProtocol/Client.h>
  2. #include <LibProtocol/Download.h>
  3. #include <SharedBuffer.h>
  4. namespace LibProtocol {
  5. Client::Client()
  6. : ConnectionNG(*this, "/tmp/portal/protocol")
  7. {
  8. handshake();
  9. }
  10. void Client::handshake()
  11. {
  12. auto response = send_sync<ProtocolServer::Greet>(getpid());
  13. set_server_pid(response->server_pid());
  14. set_my_client_id(response->client_id());
  15. }
  16. bool Client::is_supported_protocol(const String& protocol)
  17. {
  18. return send_sync<ProtocolServer::IsSupportedProtocol>(protocol)->supported();
  19. }
  20. RefPtr<Download> Client::start_download(const String& url)
  21. {
  22. i32 download_id = send_sync<ProtocolServer::StartDownload>(url)->download_id();
  23. auto download = Download::create_from_id({}, *this, download_id);
  24. m_downloads.set(download_id, download);
  25. return download;
  26. }
  27. bool Client::stop_download(Badge<Download>, Download& download)
  28. {
  29. if (!m_downloads.contains(download.id()))
  30. return false;
  31. return send_sync<ProtocolServer::StopDownload>(download.id())->success();
  32. }
  33. void Client::handle(const ProtocolClient::DownloadFinished& message)
  34. {
  35. RefPtr<Download> download;
  36. if ((download = m_downloads.get(message.download_id()).value_or(nullptr))) {
  37. download->did_finish({}, message.success(), message.total_size(), message.shared_buffer_id());
  38. }
  39. send_sync<ProtocolServer::DisownSharedBuffer>(message.shared_buffer_id());
  40. m_downloads.remove(message.download_id());
  41. }
  42. void Client::handle(const ProtocolClient::DownloadProgress& message)
  43. {
  44. if (auto download = m_downloads.get(message.download_id()).value_or(nullptr)) {
  45. download->did_progress({}, message.total_size(), message.downloaded_size());
  46. }
  47. }
  48. }