smf/smfservermodule/smfserver/server/smfserver.cpp
changeset 10 1d94eb8df9c2
parent 9 b85b0c039c14
equal deleted inserted replaced
9:b85b0c039c14 10:1d94eb8df9c2
     1 /**
       
     2  * Copyright (c) 2010 Sasken Communication Technologies Ltd.
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the "Eclipse Public License v1.0" 
       
     6  * which accompanies  this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html"
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Chandradeep Gandhi, Sasken Communication Technologies Ltd - Initial contribution
       
    11  *
       
    12  * Contributors:
       
    13  * Manasij Roy, Nalina Hariharan
       
    14  */
       
    15 
       
    16 #include "smfserver.h"
       
    17 #include "smfpluginmanager.h"
       
    18 #include "smftransportmanager.h"
       
    19 #include "smfclientglobal.h"
       
    20 #include <smfprovider.h>
       
    21 #include <smfpost.h>
       
    22 #include <smflocation.h>
       
    23 #include <smfcontact.h>
       
    24 #include <smfpicture.h>
       
    25 #include <smfcomment.h>
       
    26 //#ifdef NO_OTHER_MODULES
       
    27 #include <QImage>
       
    28 #include <QUrl>
       
    29 #include <smfgroup.h>
       
    30 #include <QTextStream>
       
    31 #include <QFile>
       
    32 //#endif
       
    33 #ifdef Q_OS_SYMBIAN
       
    34 #include "smfserversymbian_p.h"
       
    35 #else
       
    36 #include "smfserverqt_p.h"
       
    37 #endif
       
    38 
       
    39 SmfServer::SmfServer(QObject* parent): QObject(parent)
       
    40 	{
       
    41 	}
       
    42 bool SmfServer::startServer()
       
    43 	{
       
    44 	bool success = false;
       
    45 	//Initialize all the component handles
       
    46 	writeLog("Not doing SmfTransportManager::getInstance");
       
    47 	//m_transportManager = SmfTransportManager::getInstance();
       
    48 	writeLog("Not doing m_transportManager->initializeTransport");
       
    49 	//checking the network status, no need to proceed if not permitted
       
    50 	//SmfTransportInitializeResult networkStatus = m_transportManager->initializeTransport();
       
    51 	writeLog("Before networkStatus");
       
    52 	//TODO:-check this
       
    53 	if(1/*networkStatus == SmfTransportInitNoError*/)
       
    54 		{
       
    55 		writeLog("Before m_pluginManager construction");
       
    56 		m_pluginManager = SmfPluginManager::getInstance(this);
       
    57 		writeLog("After m_pluginManager construction");
       
    58 		//	m_dataStoreManager = new SmfDataStoreManager();
       
    59 		//Initialize private implementation
       
    60 		#ifdef Q_OS_SYMBIAN
       
    61 		TRAPD(err, m_SmfServerPrivate = SmfServerSymbian::NewL(CActive::EPriorityStandard,this));
       
    62 		QString log("SmfServer::startServer=");
       
    63 		int errorQ = err ;
       
    64 		log += errorQ;
       
    65 		 writeLog(log);
       
    66 		if( KErrNone != err )
       
    67 			{
       
    68 			return success;
       
    69 			}
       
    70 		TInt error = m_SmfServerPrivate->Start( KSmfServerName );
       
    71 		errorQ = error ;
       
    72 		log.clear();
       
    73 		log = QString("m_SmfServerPrivate->Start=");
       
    74 		log += QString::number(error);
       
    75 		 writeLog(log);
       
    76 		if( KErrNone == error ) 
       
    77 			{
       
    78 			success = true;
       
    79 			}
       
    80 		else 
       
    81 			{
       
    82 			//error
       
    83 			return success;
       
    84 			}
       
    85 		#else
       
    86 		m_SmfServerPrivate = new SmfServerQt();
       
    87 		success = m_SmfServerPrivate->start();
       
    88 		if (!success) 
       
    89 			{
       
    90 			return success;
       
    91 			}
       
    92 		#endif
       
    93 		//request CM server for auth expiry info, note:- we should resend the request everytime we get expiry
       
    94 		//notification
       
    95 		//TODO:- Do it after CM implementation
       
    96 		//CMclient->requestAuthExpiryNotify();
       
    97 		//connect(CMClient,SIGNAL(authExpiryNotify(NotificationType,SmfPluginID),this,SLOT(NotificationType,SmfPluginID));
       
    98 		}
       
    99     return success;
       
   100 	}
       
   101 //Note:- Almost all the following APIs are called by private impl via the handle
       
   102 /**
       
   103  * This called first once a session is created by the private impl
       
   104  * @param clientID some unique client process ID, SID for symbian platform
       
   105  * TODO:-How to get the pid for rest of the platforms?
       
   106  * @return true if client is already athorized, else false
       
   107  * 
       
   108  */
       
   109 bool SmfServer::isClientAuthorized(SmfClientAuthID clientID)
       
   110 	{
       
   111 	//TODO:- once CM is complete do it properly
       
   112 	Q_UNUSED(clientID);
       
   113 	return true;
       
   114 	}
       
   115 /**
       
   116  * This API is called by the private impl when isClientAuthorized returns false
       
   117  * @param clientID client Id (SID for symbian platform), provided by the private impl
       
   118  * Note:- The session(and in turn the client) will be blocked untill authorization completes.
       
   119  */
       
   120 void SmfServer::authorizeClient(SmfClientAuthID clientID)
       
   121 	{
       
   122 	//TODO:- once CM is complete, do it properly
       
   123 	//CMclient->authorizeClient(clientID)
       
   124 	//connect(CMClient,SIGNAL(clientAuthorizationFinished(bool,SmfClientAuthID)),this,SLOT(clientAuthorizationFinished(bool,SmfClientAuthID)));
       
   125 	Q_UNUSED(clientID);	
       
   126 	}
       
   127 /**
       
   128  * This API is called by the private impl when client is authorized
       
   129  * @param interfaceID Interface id, provided by the private impl (it gets it from client)
       
   130  * @param pluginIDMap Map of plugins who implement this interface and corresponding provider,
       
   131  * this is returned to the private impl
       
   132  * It calls PM to get the list. Note:- PM may return SmfProviderBase which is superset of SmfProvider.
       
   133  * TODO:- session should store this map for future ref?
       
   134  */
       
   135 void SmfServer::getPlugins(SmfInterfaceID interfaceID, QMap<SmfPluginID,SmfProvider>& pluginIDMap)
       
   136 	{
       
   137 	pluginIDMap.clear();
       
   138 	m_pluginManager->getPlugins(interfaceID,pluginIDMap);
       
   139 	}
       
   140 
       
   141 SmfPluginID SmfServer::getPlugin(SmfInterfaceID interfaceID,SmfProvider provider)
       
   142 	{
       
   143 	//TODO:- do it properly as per PM implementation
       
   144 	//return PM->getplugin(interfaceID,provider);
       
   145 	//TODO:- PM is not yet ready
       
   146 	SmfPluginID id;
       
   147 	m_pluginManager->getPluginId(interfaceID,provider, id);
       
   148 	return id;
       
   149 	//testing purpose only
       
   150 	}
       
   151 /**
       
   152  * This API is called by the private impl to get a list of authorized plugins from CM
       
   153  * @param list List of plugins to be filtered
       
   154  * @param authList List of authorised plugins filled by CM
       
   155  * this is returned to the private impl
       
   156  * It calls CMclient to get the list synchronously
       
   157  * TODO:- session should store this for future ref?
       
   158  */
       
   159 void SmfServer::getAuthorizedPlugins(QList<SmfPluginID>& list,QList<SmfPluginID>& authList)
       
   160 	{
       
   161 	//TODO:- do it properly as per CMclient implementation
       
   162 	//CMclient->getAuthorizedPlugins(list,authList);
       
   163 	//testing purpose only
       
   164 #ifdef NO_OTHER_MODULES
       
   165 	authList = list;
       
   166 #endif
       
   167 	}
       
   168 /**
       
   169  * This API is called by the private impl to request the PM to do the actual request/parsing asynchronously
       
   170  * Note:- the sever private impl (SmfServerSymbian for symbian platform) maintains a map of session ptr and some randomely
       
   171  * generated number.For the time being we are allowing only one outstanding request per session, so no need to store
       
   172  * both session and msg ptr, but in future we may need to.
       
   173  * @param requestID Request id (corresponds to the key of the mapmaintained by SmfServerSymbian).
       
   174  * @param pluginID PluginID, provided by the session
       
   175  * @param interfaceID InterfaceID provided by session
       
   176  * @requestTypeID Request Opcode, provided by session
       
   177  */
       
   178 void SmfServer::getRequestedData(int requestID,SmfPluginID pluginID,SmfInterfaceID interfaceID, SmfRequestTypeID requestTypeID,QByteArray dataForPlugin)
       
   179 	{
       
   180 		//TODO:- cross check the params with PM owner
       
   181 		//PM->getData(requestID,pluginID,interfaceID,requestTypeID);
       
   182 		m_SmfServerPrivate->writeLog("SmfServer::getRequestedData");
       
   183 		m_SmfServerPrivate->writeLog(QString::number(requestID));
       
   184 		m_SmfServerPrivate->writeLog(pluginID);
       
   185 		m_SmfServerPrivate->writeLog(interfaceID);
       
   186 		m_SmfServerPrivate->writeLog(QString::number(requestTypeID));
       
   187 		bool defaultCase = false;
       
   188 		//serializedInfo contains base provider info+ other data depending on requestTypeID
       
   189 		QDataStream reader(&dataForPlugin,QIODevice::ReadOnly);
       
   190 		QByteArray dataTobeSent;
       
   191 		switch(requestTypeID)
       
   192 			{
       
   193 			case SmfContactPost:
       
   194 				{
       
   195 				SmfProvider provider;
       
   196 				SmfPost post;
       
   197 				SmfLocation loc;
       
   198 				reader>>provider;
       
   199 				reader>>post;
       
   200 				reader>>loc;
       
   201 				//now form the serialized bytearray with only SmfPost and SmfPlace
       
   202 				QDataStream writer(&dataTobeSent,QIODevice::WriteOnly);
       
   203 				writer<<post;
       
   204 				writer<<loc;
       
   205 				}
       
   206 				break;
       
   207 			case SmfContactUpdatePost:
       
   208 				{
       
   209 				SmfProvider provider;
       
   210 				SmfPost post;
       
   211 				reader>>provider;
       
   212 				reader>>post;
       
   213 				//now form the serialized bytearray with only SmfPost
       
   214 				QDataStream writer(&dataTobeSent,QIODevice::WriteOnly);
       
   215 				writer<<post;
       
   216 				}
       
   217 				break;
       
   218 			case SmfContactPostDirected:
       
   219 				{
       
   220 				SmfProvider provider;
       
   221 				SmfPost post;
       
   222 				SmfContact contact;
       
   223 				SmfLocation place;
       
   224 				reader>>provider;
       
   225 				reader>>post;
       
   226 				reader>>contact;
       
   227 				reader>>place;
       
   228 				QDataStream writer(&dataTobeSent,QIODevice::WriteOnly);
       
   229 				writer<<post;
       
   230 				writer<<contact;
       
   231 				writer<<place;
       
   232 				}
       
   233 				break;
       
   234 			case SmfContactSharePost:
       
   235 				{
       
   236 				SmfProvider provider;
       
   237 				SmfPost post;
       
   238 				SmfContact contact;
       
   239 				bool edited;
       
   240 				reader>>provider;
       
   241 				reader>>post;
       
   242 				reader>>contact;
       
   243 				reader>>edited;
       
   244 				//now form the serialized bytearray with only SmfPost
       
   245 				QDataStream writer(&dataTobeSent,QIODevice::WriteOnly);
       
   246 				writer<<post;
       
   247 				writer<<contact;
       
   248 				writer<<edited;
       
   249 				}
       
   250 				break;
       
   251 			case SmfPictureDescription:
       
   252 			case SmfPictureUpload:
       
   253 				{
       
   254 				SmfProvider provider;
       
   255 				SmfPicture picture;
       
   256 				reader>>provider;
       
   257 				reader>>picture;
       
   258 				//now form the serialized bytearray with only SmfPicture
       
   259 				QDataStream writer(&dataTobeSent,QIODevice::WriteOnly);
       
   260 				writer<<picture;
       
   261 				}
       
   262 				break;
       
   263 			case SmfPictureMultiUpload:
       
   264 				{
       
   265 				SmfProvider provider;
       
   266 				SmfPictureList pictureList;
       
   267 				reader>>provider;
       
   268 				reader>>pictureList;
       
   269 				//now form the serialized bytearray with only SmfPicture
       
   270 				QDataStream writer(&dataTobeSent,QIODevice::WriteOnly);
       
   271 				writer<<pictureList;
       
   272 				}
       
   273 				break;
       
   274 			case SmfPicturePostComment:
       
   275 				{
       
   276 				SmfProvider provider;
       
   277 				SmfPicture picture;
       
   278 				SmfComment comment;
       
   279 				reader>>provider;
       
   280 				reader>>picture;
       
   281 				reader>>comment;
       
   282 				//now form the serialized bytearray with only SmfPicture
       
   283 				QDataStream writer(&dataTobeSent,QIODevice::WriteOnly);
       
   284 				writer<<picture;
       
   285 				writer<<comment;
       
   286 				}
       
   287 				break;
       
   288 			default:
       
   289 				defaultCase = true;
       
   290 				break;
       
   291 			}
       
   292 		//When xtra info to be sent to plugin manager
       
   293 		if(!defaultCase)
       
   294 			{
       
   295 				m_pluginManager->createRequest(requestID,pluginID,requestTypeID,dataTobeSent);
       
   296 			}
       
   297 		//when plugin manager needs to xtra info
       
   298 		else
       
   299 			{
       
   300 			QByteArray arr;
       
   301 			m_pluginManager->createRequest(requestID,pluginID,requestTypeID,arr);
       
   302 			}
       
   303 	}
       
   304 /**
       
   305  * This slot is invoked when CM finishes the authorization of the client.
       
   306  * @param authID As it contains the session ptr, sever directly invokes the session's API to notify success
       
   307  */
       
   308 void SmfServer::clientAuthorizationFinished(bool success,SmfClientAuthID authID )
       
   309 	{
       
   310 	//TODO:- implement this api in session class
       
   311 	//note:- in case success is false client completes the request with SmfErrClientAuthFailed
       
   312 	//TODO:- define set of smf wide error after consulting with other module owners
       
   313 	authID.session->clientathorizationFinished(success);
       
   314 	}
       
   315 /**
       
   316  * This API is called by PM once its done with request and parsing
       
   317  * @param requestID The request id which is completed
       
   318  * @param parsedData Serialized data(as per request type) filled by PM
       
   319  * @param error Error occured
       
   320  * TODO:- should use smf wide global errors instead
       
   321  */
       
   322 void SmfServer::resultsAvailable(int requestID,QByteArray* parsedData,SmfError error)
       
   323 	{
       
   324 	m_SmfServerPrivate->writeLog("SmfServer::resultsAvailable");
       
   325 	m_SmfServerPrivate->writeLog("requestID=");
       
   326 	m_SmfServerPrivate->writeLog(QString::number(requestID));
       
   327 	m_SmfServerPrivate->writeLog("parsedData->size()=");
       
   328 	m_SmfServerPrivate->writeLog(QString::number(parsedData->size()));
       
   329 	m_SmfServerPrivate->writeLog("Error=");
       
   330 	m_SmfServerPrivate->writeLog(QString::number(error));
       
   331 	//Serialize error followed by actual data
       
   332 	QByteArray dataWithError;
       
   333 	QDataStream writer(&dataWithError,QIODevice::WriteOnly);
       
   334 	writer<<error;
       
   335 	writer<<*(parsedData);
       
   336 	//find out the appropriate session and request id and service that
       
   337 	m_SmfServerPrivate->findAndServiceclient(requestID,&dataWithError,error);
       
   338 	}
       
   339 /**
       
   340  * Seems reduntant in symbian as client is served with RMessage2::Complete()
       
   341  */
       
   342 void SmfServer::serviceClient(QByteArray* parsedData)
       
   343 	{
       
   344 	Q_UNUSED(parsedData);
       
   345 	}
       
   346 /**
       
   347  * Used by PM to get a list of tokens
       
   348  * TODO:- cross check the params
       
   349  */
       
   350 void SmfServer::getAuthenticationKeys(int pluginID,QStringList& keys,QStringList& urls)
       
   351 	{
       
   352 	//SMF-CM communication will be via CM client
       
   353 	//TODO:- do it properly as per CM client impl
       
   354 	//CMclient->getKeys(pluginID,keys,urls);
       
   355 	Q_UNUSED(pluginID);
       
   356 	Q_UNUSED(keys);
       
   357 	Q_UNUSED(urls);
       
   358 	}
       
   359 
       
   360 /**
       
   361  * This is called when CMclient notifies client expiry.
       
   362  * @param type notification type, set of enums for future expansion
       
   363  * @param id Plugin Id for which the authentication has expired
       
   364  */
       
   365 void SmfServer::authenticationKeysExpired(NotificationType type,SmfPluginID id)
       
   366 	{
       
   367 	//resend the notify request
       
   368 	//CMclient->requestAuthExpiryNotify();
       
   369 	Q_UNUSED(type);
       
   370 	Q_UNUSED(id);
       
   371 	}
       
   372 
       
   373 void SmfServer::writeLog(QString log) const
       
   374 	{
       
   375 #ifdef WRITE_LOG
       
   376 	QFile file("c:\\data\\SmfServerLogs.txt");
       
   377     if (!file.open(QIODevice::Append | QIODevice::Text))
       
   378 	         ;
       
   379     QTextStream out(&file);
       
   380     out << log << "\n";
       
   381     file.close();
       
   382 #else
       
   383     Q_UNUSED(log);
       
   384 #endif
       
   385 	}