|
solarpowerlog trunk
|
00001 /* ---------------------------------------------------------------------------- 00002 solarpowerlog 00003 Copyright (C) 2009 Tobias Frost 00004 00005 This file is part of solarpowerlog. 00006 00007 Solarpowerlog is free software; However, it is dual-licenced 00008 as described in the file "COPYING". 00009 00010 For this file (ConnectionTCP.h), the license terms are: 00011 00012 You can redistribute it and/or modify it under the terms of the GNU Lesser 00013 General Public License (LGPL) as published by the Free Software Foundation; 00014 either version 3 of the License, or (at your option) any later version. 00015 00016 This program is distributed in the hope that it will be useful, but 00017 WITHOUT ANY WARRANTY; without even the implied warranty of 00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00019 Lesser General Public License for more details. 00020 00021 You should have received a copy of the GNU Library General Public 00022 License along with this proramm; if not, see 00023 <http://www.gnu.org/licenses/>. 00024 ---------------------------------------------------------------------------- 00025 */ 00026 00033 #ifndef CONNECTIONTCPASIO_H_ 00034 #define CONNECTIONTCPASIO_H_ 00035 00036 #ifdef HAVE_CONFIG_H 00037 #include "config.h" 00038 #include "porting.h" 00039 #endif 00040 00041 #ifdef HAVE_COMMS_ASIOTCPIO 00042 00043 #include <boost/asio/ip/tcp.hpp> 00044 #include <boost/asio/streambuf.hpp> 00045 #include <semaphore.h> 00046 00047 #include "interfaces/IConnect.h" 00048 #include "interfaces/CWorkScheduler.h" 00049 #include "configuration/Registry.h" 00050 #include "patterns/ICommand.h" 00051 #include "Connections/CAsyncCommand.h" 00052 00054 #define TCP_ASIO_DEFAULT_TIMEOUT (3000UL) 00055 00056 using namespace std; 00057 00065 class CConnectTCPAsio: public IConnect 00066 { 00067 protected: 00068 friend class IConnectFactory; 00069 CConnectTCPAsio( const string & configurationname ); 00070 00071 public: 00072 virtual ~CConnectTCPAsio(); 00073 00074 virtual bool Connect( ICommand *callback ); 00075 00076 virtual bool Disconnect( ICommand *callback ); 00077 00078 virtual void SetupLogger( const string& parentlogger, const string & = "" ) 00079 { 00080 IConnect::SetupLogger(parentlogger, "Comms_TCP_ASIO"); 00081 } 00082 00083 virtual bool Send( const char *tosend, unsigned int len, 00084 ICommand *callback = NULL ); 00085 00086 virtual bool Send( const string& tosend, ICommand *callback = NULL ); 00087 00088 virtual bool Receive( ICommand *callback ); 00089 00090 virtual bool CheckConfig( void ); 00091 00092 virtual bool IsConnected( void ); 00093 00094 private: 00095 boost::asio::io_service *ioservice; 00096 boost::asio::ip::tcp::socket *sockt; 00097 boost::asio::streambuf *data; 00098 00099 time_t timer; 00100 00101 // async patch 00102 virtual void _main( void ); 00103 00114 bool PushWork( CAsyncCommand *cmd ); 00115 00117 //void CancelWork( void ); 00118 00128 bool HandleConnect( CAsyncCommand *cmd ); 00129 00136 bool HandleDisConnect( CAsyncCommand *cmd ); 00137 00138 bool HandleReceive( CAsyncCommand *cmd ); 00139 00140 bool HandleSend( CAsyncCommand *cmd ); 00141 00142 list<CAsyncCommand*> cmds; 00143 sem_t cmdsemaphore; 00144 00145 }; 00146 00147 #endif /* HAVE_COMMS_ASIOTCPIO */ 00148 #endif /* CONNECTIONTCP_H_ */