bool SessionManager::StartSession(
		ILiveChatClient* livechat,
		IRequest* request,
		const string& identify
		) {
	bool bFlag = true;

	LogManager::GetLogManager()->Log(
			LOG_MSG,
			"SessionManager::StartSession( "
			"[会话交互(Session), 创建客户端到LiveChat会话], "
			"livechat : %p, "
			"request : %p "
			")",
			livechat,
			request
			);

	if( !livechat || !request ) {
		return false;
	}

	bool bRecord = false;
	// 是否需要等待返回的请求
	bool bNeedReturn = request->IsNeedReturn();
	if( bNeedReturn ) {
		mLiveChat2SessionMap.Lock();
		Session* session = NULL;
		LiveChat2SessionMap::iterator itr = mLiveChat2SessionMap.Find(livechat);
		if( itr != mLiveChat2SessionMap.End() ) {
			session = itr->second;
			LogManager::GetLogManager()->Log(
					LOG_MSG,
					"SessionManager::StartSession( "
					"[会话交互(Session), Client -> LiveChat, 继续会话], "
					"livechat : %p, "
					"session : %p, "
					"request : %p, "
					"identify : %s "
					")",
					livechat,
					session,
					request,
					identify.c_str()
					);

		} else {
			session = new Session(livechat);
			session->timeout = 1000 * miTimeout;
			mLiveChat2SessionMap.Insert(livechat, session);

			LogManager::GetLogManager()->Log(
					LOG_MSG,
					"SessionManager::StartSession( "
					"[会话交互(Session), Client -> LiveChat, 开始新会话] "
					"livechat : %p, "
					"session : %p, "
					"request : %p, "
					"identify : %s "
					")",
					livechat,
					session,
					request,
					identify.c_str()
					);
		}

		LogManager::GetLogManager()->Log(
				LOG_MSG,
				"SessionManager::StartSession( "
				"[会话交互(Session), Client -> LiveChat, 插入任务到会话], "
				"livechat : %p, "
				"session : %p, "
				"request : %p, "
				"identify : %s "
				")",
				livechat,
				session,
				request,
				identify.c_str()
				);

		// 增加到等待返回的列表
		if( session->AddRequest(identify, request) ) {
			bRecord = true;

		} else {
			LogManager::GetLogManager()->Log(
					LOG_WARNING,
					"SessionManager::StartSession( "
					"[会话交互(Session), Client -> LiveChat, 插入任务到会话, 任务已经存在, 丢弃], "
					"livechat : %p, "
					"session : %p, "
					"request : %p, "
					"identify : %s "
					")",
					livechat,
					session,
					request,
					identify.c_str()
					);
		}


		mLiveChat2SessionMap.Unlock();
	}

	// 判断请求是否需要返回,且是否重复请求
	if ( bNeedReturn && !bRecord ) {
		// 若是,则返回true,且删除请求object
		bFlag = true;
		delete request;
	}
	else {
		// 否则开始请求
		bFlag = request->StartRequest();
	}

	/**
	 * 清除任务
	 * 启动失败(包括请求不用返回, 或需要返回且不是重复)
	 */
	if( !bFlag ) {
		if( bRecord ) {
			// 需要返回且不是重复
			mLiveChat2SessionMap.Lock();
			Session* session = NULL;
			LiveChat2SessionMap::iterator itr = mLiveChat2SessionMap.Find(livechat);
			if( itr != mLiveChat2SessionMap.End() ) {
				session = itr->second;
				IRequest* theRequest = session->EraseRequest(identify);
				delete theRequest;
			}
			mLiveChat2SessionMap.Unlock();
		}
		else {
			// 请求不用返回,删除请求object
			delete request;
		}
	}

	return bFlag;
}
IRequest* SessionManager::FinishSession(
		ILiveChatClient* livechat,
		const string& identify
		) {
	IRequest* request = NULL;

	LogManager::GetLogManager()->Log(
			LOG_MSG,
			"SessionManager::FinishSession( "
			"[会话交互(Session), LiveChat -> Client, 返回响应到客户端], "
			"livechat : %p, "
			"identify : %s "
			")",
			livechat,
			identify.c_str()
			);

	Session* session = NULL;
	mLiveChat2SessionMap.Lock();
	LiveChat2SessionMap::iterator itr = mLiveChat2SessionMap.Find(livechat);
	if( itr != mLiveChat2SessionMap.End() ) {
		LogManager::GetLogManager()->Log(
				LOG_MSG,
				"SessionManager::FinishSession( "
				"[会话交互(Session), LiveChat -> Client, 返回响应到客户端, 找到对应会话], "
				"livechat : %p, "
				"identify : %s "
				")",
				livechat,
				identify.c_str()
				);

		session = itr->second;
		if( session != NULL ) {
			request = session->EraseRequest(identify);
			if( request != NULL ) {
				LogManager::GetLogManager()->Log(
						LOG_MSG,
						"SessionManager::FinishSession( "
						"[会话交互(Session), LiveChat -> Client, 返回响应到客户端, 找到对应请求], "
						"livechat : %p, "
						"identify : %s, "
						"request : %p "
						")",
						livechat,
						identify.c_str(),
						request
						);

				// 标记完成任务, 执行成功
				if( request ) {
					request->FinisRequest(true);
				}

			} else {
				LogManager::GetLogManager()->Log(
						LOG_MSG,
						"SessionManager::FinishSession( "
						"[会话交互(Session), LiveChat -> Client, 返回响应到客户端, 找不到对应请求], "
						"livechat : %p, "
						"identify : %s "
						")",
						livechat,
						identify.c_str()
						);
			}

			// 不关闭会话, 等待livechat断开连接才关闭
		}

	}
	mLiveChat2SessionMap.Unlock();

	return request;
}