Client.cpp 1.6 KB

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