network_file.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "json_parser.h"
  24. #include "container_types.h"
  25. #include "log.h"
  26. #include "math_utils.h"
  27. #include "network_file.h"
  28. #include "network_filesystem.h"
  29. #include "string_stream.h"
  30. #include "string_utils.h"
  31. #include "temp_allocator.h"
  32. #include "types.h"
  33. #include "dynamic_string.h"
  34. namespace crown
  35. {
  36. NetworkFile::NetworkFile(const NetAddress& addr, uint16_t port, const char* filename)
  37. : File(FOM_READ)
  38. , m_address(addr)
  39. , m_port(port)
  40. , m_position(0)
  41. {
  42. string::strncpy(m_filename, filename, MAX_PATH_LENGTH);
  43. m_socket.connect(addr, port);
  44. }
  45. NetworkFile::~NetworkFile()
  46. {
  47. m_socket.close();
  48. }
  49. void NetworkFile::seek(size_t position)
  50. {
  51. m_position = position;
  52. }
  53. void NetworkFile::seek_to_end()
  54. {
  55. m_position = size();
  56. }
  57. void NetworkFile::skip(size_t bytes)
  58. {
  59. m_position += bytes;
  60. }
  61. void NetworkFile::read(void* buffer, size_t size)
  62. {
  63. using namespace string_stream;
  64. TempAllocator1024 alloc;
  65. StringStream command(alloc);
  66. // Request the file
  67. command << "{\"type\":\"filesystem\",\"filesystem\":\"read\",";
  68. command << "\"file\":\"" << m_filename << "\",";
  69. command << "\"position\":" << m_position << ",";
  70. command << "\"size\":" << size << "}";
  71. network_filesystem::send(m_socket, c_str(command));
  72. // Wait for response
  73. Array<char> response(default_allocator());
  74. network_filesystem::read_response(m_socket, response);
  75. // Parse the response
  76. JSONParser json(array::begin(response));
  77. JSONElement root = json.root();
  78. // DynamicString data_base64;
  79. // root.key("data").to_string(data_base64);
  80. // size_t out_len = 0;
  81. // unsigned char* data = math::base64_decode(data_base64.c_str(), data_base64.length(), &out_len);
  82. // memcpy(buffer, data, sizeof(unsigned char) * out_len);
  83. // default_allocator().deallocate(data);
  84. }
  85. void NetworkFile::write(const void* /*buffer*/, size_t /*size*/)
  86. {
  87. CE_FATAL("Cannot write to a network file");
  88. }
  89. bool NetworkFile::copy_to(File& file, size_t size)
  90. {
  91. return true;
  92. }
  93. bool NetworkFile::end_of_file()
  94. {
  95. return position() == size();
  96. }
  97. bool NetworkFile::is_valid()
  98. {
  99. return true;
  100. }
  101. void NetworkFile::flush()
  102. {
  103. // Do nothing
  104. }
  105. size_t NetworkFile::position()
  106. {
  107. return m_position;
  108. }
  109. size_t NetworkFile::size()
  110. {
  111. using namespace string_stream;
  112. TempAllocator1024 alloc;
  113. StringStream command(alloc);
  114. // Request the file
  115. command << "{\"type\":\"filesystem\",\"filesystem\":\"size\",";
  116. command << "\"file\":\"" << m_filename << "\"}";
  117. network_filesystem::send(m_socket, c_str(command));
  118. // Wait for response
  119. Array<char> response(default_allocator());
  120. network_filesystem::read_response(m_socket, response);
  121. JSONParser parser(array::begin(response));
  122. JSONElement root = parser.root();
  123. return (size_t) root.key("size").to_int();
  124. }
  125. bool NetworkFile::can_read() const
  126. {
  127. return true;
  128. }
  129. bool NetworkFile::can_write() const
  130. {
  131. return false;
  132. }
  133. bool NetworkFile::can_seek() const
  134. {
  135. return true;
  136. }
  137. } // namespace crown