Subversion Repositories HomeAutomation

Rev

Blame | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  *
  3.  *  Copyright (C) 2012  Mattias Runge
  4.  *
  5.  *  This program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation; either version 2 of the License, or
  8.  *  (at your option) any later version.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *  GNU General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU General Public License along
  16.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  17.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18.  *
  19.  */
  20.  
  21. #include "GetFile.h"
  22. #include "Manager.h"
  23.  
  24. #include <time.h>
  25.  
  26. #include <iostream>
  27. #include <fstream>
  28.  
  29. #include <boost/bind.hpp>
  30. #include <boost/lexical_cast.hpp>
  31.  
  32. #include "common/log.h"
  33. #include "config.h"
  34.  
  35. namespace atom {
  36. namespace net {
  37.  
  38. static const std::string log_module_ = "net::getfile";
  39.  
  40. GetFile::GetFile()
  41. {
  42.   LOG_DEBUG_ENTER;
  43.  
  44.  
  45.   this->filename_         = "/tmp/atomd_download_" + boost::lexical_cast<std::string>(time(NULL));
  46.   this->header_received_  = false;
  47.   this->status_           = false;
  48.   this->socket_id_        = 0;
  49.  
  50.   LOG_DEBUG_EXIT;
  51. }
  52.  
  53. GetFile::~GetFile()
  54. {
  55.   LOG_DEBUG_ENTER;
  56.  
  57.   net::Manager::Instance()->Disconnect(this->socket_id_);
  58.  
  59.   LOG_DEBUG_EXIT;
  60. }
  61.  
  62. void GetFile::Start(Url url, const SignalOnComplete::slot_type& slot_on_complete)
  63. {
  64.   LOG_DEBUG_ENTER;
  65.  
  66.   this->url_              = url;
  67.   this->filename_         = "/tmp/atomd_download_" + boost::lexical_cast<std::string>(time(NULL));
  68.   this->header_received_  = false;
  69.   this->status_           = false;
  70.   this->socket_id_        = 0;
  71.  
  72.   this->signal_on_complete_.connect(slot_on_complete);
  73.  
  74.   this->socket_id_ = net::Manager::Instance()->Connect(net::TRANSPORT_PROTOCOL_TCP, this->url_.GetHostname(), this->url_.GetPort());
  75.  
  76.   std::string request = "GET " + this->url_.GetPath() + " HTTP/1.1\n";
  77.   request += "Host:svn.arune.se\n";
  78.   request += "Pragma:no-cache\n";
  79.   request += "Referer:http://svn.arune.se/svn/HomeAutomation/trunk/Configuration/\n";
  80.   request += "User-Agent:atomd/" + std::string(VERSION) + "\n\n";
  81.  
  82.   log::Debug(log_module_, request);
  83.  
  84.   net::Manager::Instance()->SendTo(this->socket_id_, common::Byteset(request.begin(), request.end()));
  85.  
  86.   LOG_DEBUG_EXIT;
  87. }
  88.  
  89. void GetFile::SlotOnNewClientHandler(SocketId id, SocketId server_id)
  90. {
  91.   LOG_DEBUG_ENTER;
  92.   LOG_DEBUG_EXIT;
  93. }
  94.  
  95. void GetFile::SlotOnNewDataHandler(SocketId id, common::Byteset data)
  96. {
  97.   LOG_DEBUG_ENTER;
  98.  
  99.   if (id != this->socket_id_)
  100.   {
  101.     return;
  102.   }
  103.  
  104.   std::string buffer(data.begin(), data.end());
  105.  
  106.   if (!this->header_received_)
  107.   {
  108.     std::string::size_type position = 0;
  109.  
  110.     if (!this->status_)
  111.     {
  112.       position = buffer.find("200 OK");
  113.    
  114.       if (std::string::npos == position)
  115.       {
  116.         log::Debug(log_module_, "No 200 found!");
  117.         net::Manager::Instance()->Disconnect(this->socket_id_);
  118.       }
  119.       else
  120.       {
  121.         log::Debug(log_module_, "200 found!");
  122.         this->status_ = true;
  123.       }
  124.     }
  125.    
  126.     if (this->status_)
  127.     {
  128.       position = buffer.find("\r\n\r\n");
  129.  
  130.       if (std::string::npos != position)
  131.       {
  132.         buffer = buffer.substr(position + 2);
  133.         log::Debug(log_module_, "\"%s\"", buffer.data());
  134.       }
  135.     }
  136.   }
  137.  
  138.   if (buffer.size() > 0)
  139.   {
  140.     std::ofstream file(this->filename_.data(), std::ios::app);
  141.    
  142.     file << buffer;
  143.    
  144.     file.close();
  145.   }
  146.  
  147.   LOG_DEBUG_EXIT;
  148. }
  149.  
  150. void GetFile::SlotOnNewStateHandler(SocketId id, ClientState client_state)
  151. {
  152.   LOG_DEBUG_ENTER;
  153.  
  154.   if (id != this->socket_id_)
  155.   {
  156.     return;
  157.   }
  158.  
  159.   if (CLIENT_STATE_DISCONNECTED == client_state)
  160.   {
  161.     this->socket_id_ = 0;
  162.     this->signal_on_complete_(this->status_, this->url_, this->filename_);
  163.   }
  164.  
  165.   LOG_DEBUG_EXIT;
  166. }
  167.  
  168.  
  169. }; // namespace net
  170. }; // namespace atom
  171.