WebExpositorBundle.cpp

Go to the documentation of this file.
00001 /** @file    WebExpositorBundle.cpp
00002  *  @author  Alessandro Polo
00003  *  @version $Id: WebExpositorBundle.cpp 3775 2011-01-01 16:38:17Z alex $
00004  *  @brief
00005  * File containing methods for the wosh::bundles::WebExpositorBundle class.
00006  * The header for this class can be found in WebExpositorBundle.h, check that file
00007  * for class description.
00008  ****************************************************************************/
00009 /* Copyright (c) 2007-2011, WOSH - Wide Open Smart Home 
00010  * by Alessandro Polo - OpenSmartHome.com
00011  * All rights reserved.
00012  *
00013  * Redistribution and use in source and binary forms, with or without
00014  * modification, are permitted provided that the following conditions are met:
00015  *     * Redistributions of source code must retain the above copyright
00016  *       notice, this list of conditions and the following disclaimer.
00017  *     * Redistributions in binary form must reproduce the above copyright
00018  *       notice, this list of conditions and the following disclaimer in the
00019  *       documentation and/or other materials provided with the distribution.
00020  *     * Neither the name of the OpenSmartHome.com WOSH nor the
00021  *       names of its contributors may be used to endorse or promote products
00022  *       derived from this software without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
00025  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00026  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027  * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
00028  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00029  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00031  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00033  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  ****************************************************************************/
00035 
00036  #include "WebExpositorBundle.h"
00037  #include <core/ObjectFactory.h>
00038  #include <core/PersistenceManager.h>
00039  #include <core/BusManager.h>
00040  #include <core/FileSystem.h>
00041  #include <core/WoshKernel.h>
00042  #include <core/MethodsCommon.h>
00043  #include <core/DataSet.h>
00044  #include <core/BusRing.h>
00045  #include <core/SystemInfo.h>
00046  #include <cstdio>
00047 
00048 
00049 using namespace std;
00050 namespace wosh {
00051  namespace bundles {
00052 
00053  WOSH_REGISTER(wosh::bundles::WebExpositorBundle, wosh::interfaces::network::WebExpositor, _WebExpositor_VERSION, bundles_WebExpositorBundle1)
00054  WOSH_REGISTER(wosh::bundles::WebExpositorBundle, wosh::BundleGeneric, _WebExpositor_VERSION, bundles_WebExpositorBundle2)
00055 
00056 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
00057 //////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS
00058 
00059 WebExpositorBundle::WebExpositorBundle()
00060     : BundleGeneric(), webServer(NULL) {
00061     BundleGeneric::setName( _WebExpositor_NAME, false );
00062     Properties.update( _KEY_Version, _WebExpositor_VERSION );
00063     Requirements.add<wosh::network::WebServer>();
00064 
00065 Log.setLevel(LOG_DEBUG);
00066 
00067     Log(LOG_DEBUG, " Setting default properties and permissions.." );
00068 
00069 
00070 //  SHARED PROPERTIES
00071 
00072     Log(LOG_DEBUG, " Registering methods.." );
00073 
00074     Log(LOG_DEBUG, " Setting up WebServer.." );
00075     this->webServer = ObjectFactory::createTypeOf<wosh::network::WebServer>(false);
00076     if ( this->webServer != NULL ) {
00077         this->webServer->setListener(this);
00078         this->webServer->setParentObject(this);
00079      }
00080 
00081     setBundleState(Bundle::STATE_CREATED, false);
00082  }
00083 
00084 WebExpositorBundle::~WebExpositorBundle() {
00085     Log(LOG_INFO, " Destroying.." );
00086     if ( isBundleRunning() ) {
00087         Log(LOG_WARNING, "~WebExpositorBundle() : Destroying while RUNNING! Trying to stop.." );
00088         bundleStop();
00089      }
00090     Log(LOG_DEBUG, ":~WebExpositorBundle() : Destroying Scheduler.." );
00091     WRESULT destroyed = ObjectFactory::destroy(this->webServer);
00092     this->webServer = NULL;
00093     if ( WFAILED(destroyed) ) {
00094      }
00095     Log(LOG_VERBOSE, ":~WebExpositorBundle() : Destroyed." );
00096  }
00097 
00098 //////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS
00099 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
00100 ////////////////////////////////////////////////////////////////////////////////////////////// BUNDLE CONTROL
00101 
00102 WRESULT WebExpositorBundle::bundleStart() {
00103     if ( !BundleGeneric::bundleValidate_StartStop(Bundle::STATE_STARTING) ) return WRET_ERR_WRONG_STATE;
00104     setBundleState( Bundle::STATE_STARTING );
00105 
00106     WRESULT ret = WRET_OK;
00107     if ( this->webServer == NULL ) {
00108         ret = WRET_ERR_INTERNAL;
00109         SystemInfo::raise(this, &Log, SystemInfo::TYPE_ERROR, SystemInfo::MODE_PERMANENT, SystemInfo::SCOPE_SYSTEM, SystemInfo::PRIORITY_FAILURE,
00110                             "Internal Error",
00111                             "bundleStart() FAILED Creating WebServer.." );
00112      }
00113     else {
00114         ret = this->webServer->startServer();
00115         
00116      }
00117 
00118     if ( WSUCCEEDED(ret) )
00119         setBundleState( Bundle::STATE_STARTED );
00120     return ret;
00121  }
00122 
00123 WRESULT WebExpositorBundle::bundleStop() {
00124     if ( !BundleGeneric::bundleValidate_StartStop(Bundle::STATE_STOPPING) ) return WRET_ERR_WRONG_STATE;
00125     setBundleState( Bundle::STATE_STOPPING );
00126 
00127     WRESULT ret = WRET_ERR_INTERNAL;
00128     if ( this->webServer != NULL ) {
00129         ret = this->webServer->stopServer();
00130      }
00131 
00132     setBundleState( Bundle::STATE_STOPPED );
00133     return ret;
00134  }
00135 
00136 ////////////////////////////////////////////////////////////////////////////////////////////// BUNDLE CONTROL
00137 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
00138 
00139 void WebExpositorBundle::busMessage( const Message& message, const Bus* source ) {
00140     if ( message.isEmpty() ) return;
00141     if ( !MessageFilter::filterReplica(message, &BusCore) ) return;
00142     BundleGeneric::busMessage(message, source);
00143  }
00144 
00145 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
00146 ////////////////////////////////////////////////////////////////////////////////////////////// PROPERTY EVENT
00147 
00148 bool WebExpositorBundle::updatingProperty( bool& do_update, const Variant& value_proposed, Property& property_current, const PropertiesProvider* source ) {
00149     return BundleGeneric::updatingProperty(do_update, value_proposed, property_current, source);
00150  }
00151 
00152 ////////////////////////////////////////////////////////////////////////////////////////////// PROPERTY EVENT
00153 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
00154 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
00155 
00156 bool WebExpositorBundle::incomingConnection( const std::string& address_port, WebServer* ) {
00157     // check greenlist/blacklist
00158     return true;
00159  }
00160 
00161 bool WebExpositorBundle::webrequest_evaluate( WebServerRequest* request, WebServer* ) {
00162     SecurityToken* session_token = NULL; // TODO
00163     Request* msg_request = new Request();
00164     URI target;
00165 //const char* page = WebServer::getDefaultPage();
00166 //request->replyResponse(page);
00167     vector<string> vpath;
00168     String::split(vpath, request->url, "/", _String_SPLIT_OPTION_NOT_EMPTY | _String_SPLIT_OPTION_TRIM);
00169     target.setKernelName(vpath.front());
00170     if ( !vpath.empty() )
00171         vpath.erase(vpath.begin());
00172     target.setPath(vpath);
00173     target.setName(vpath.back());
00174     if ( !target.getPath().empty() ) 
00175         target.getPath().pop_back();
00176     Log(LOG_DEBUG, ":webrequest_evaluate() Parsed url '%s' as URI '%s'", request->url, target.toString().c_str()  );
00177 
00178     msg_request->setMethod(_METHOD_ListProperties);
00179     Log(LOG_DEBUG, ":webrequest_evaluate() Parsed method '%s'", msg_request->getMethod().c_str()  );
00180 
00181     //------------------------------------
00182     // synch MODE:
00183     unsigned long timeout = 15 * 1000L;
00184     Message* message = new Message(msg_request);
00185     int64 id = message->getID();
00186     message->setSource(this);
00187     message->setDestinationBus(_Bus_ANY);
00188     message->setDestination(target);
00189     signMessage(message, session_token);
00190     Log(LOG_VERBOSE, ":webrequest_evaluate() Posting Message#%"PRId64" (waiting reply %ul)", id, timeout );
00191     Message* reply = BusCore.postMessageWaitReply(message, timeout);
00192     if ( reply == NULL ) {
00193         Log(LOG_WARNING, ":webrequest_evaluate() Timeout Message#%"PRId64"", id );
00194         return false;
00195      }
00196     if ( reply->isEmpty() || !reply->getContent()->isResponse() || !reply->getContent()->asResponse()->hasData() ) {
00197         const char* page = WebServer::getDefaultPage();
00198         request->replyResponse(page);
00199         return true;
00200      }
00201 //  char* buffer = new char[16000];
00202 //  memset(buffer, 0, 10);
00203 //  int64 size = PersistenceManager::serializeTo(reply->getContent()->asResponse()->getData(), "HTML", buffer, 16000, EncoderOptions() );
00204 //printf( "\n-------------\n%s\n----------------\n", buffer);
00205 //  if ( size < 15089LL )
00206 //      memset(buffer+(int)size, 0, 10);
00207 //  request->replyResponse( buffer );
00208 //  delete [] buffer;
00209 
00210 //   WebServerPage* response = new WebServerPage();
00211 //  response->allocateBuffer(16000);
00212 //  response->setBuffer(NULL);
00213 //  delete response;
00214     return true;
00215  }
00216 
00217 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
00218 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
00219 
00220 
00221 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
00222 ///////////////////////////////////////////////////////////////////////////////////////////////////// METHODS
00223 
00224 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
00225 
00226  }; // namespace bundles
00227 }; // namespace wosh

Generated on Tue Feb 8 2011 09:33:12 for WOSH system 0.8.888 [wolf] by Alessandro Polo, using DoxyGen 1.7.2 hosted by WOSH Framework