text:
// Copyright (c) 2019 Graphcore Ltd. All rights reserved.

#ifdef __POPC__

#include <poplar/Vertex.hpp>

using namespace poplar;

class Increment : public Vertex {
public:
  Input<Vector<float>> x;
  Output<Vector<float>> y;

  bool compute() {
    if (x->size() != y->size())
      return false;

    for (unsigned i = 0; i < x->size(); i++) {
      y[i] = x[i] + 1;
    }
    return true;
  }
};

#else

#include <poplar/DeviceManager.hpp>
#include <poplar/Engine.hpp>
#include <poplar/Tensor.hpp>
#include <poplar/exceptions.hpp>

#include <boost/optional.hpp>

#include <algorithm>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <vector>

#include <cassert>
#include <cstdlib>

using namespace poplar;
using namespace poplar::program;

// Callback that fills the buffer with a value
class FillCallback final : public poplar::StreamCallback {
public:
  using Result = poplar::StreamCallback::Result;

  FillCallback(float value, size_t elements) : value(value), size(elements) {}

  Result prefetch(void *__restrict p) noexcept override {
    std::printf("%s (value: %g)\n", __func__, value);
    std::fill_n(static_cast<float *>(p), size, value);
    return Result::Success;
  }

  void fetch(void *__restrict p) noexcept override {
    std::printf("%s (value: %g)\n", __func__, value);
    std::fill_n(static_cast<float *>(p), size, value);
  }

  void complete() noexcept override {
    std::printf("%s (value: %g)\n", __func__, value);
  }

private:
  float value;
  size_t size;
};

std::ostream &operator<<(std::ostream &os, const std::vector<float> &values) {
  const char *sep = "";
  os << '[';
  for (auto &v : values) {
    os << sep << v;
    sep = ", ";
  }
  os << ']';
  return os;
}

int main(int argc, char *argv[]) {
  const unsigned elements = 8;
  const unsigned repeat = 5;
  const unsigned numIpus = 1;

  // Obtain list of available devices
  auto manager = DeviceManager::createDeviceManager();
  auto devices = manager.getDevices(poplar::TargetType::IPU, numIpus);
  if (devices.empty()) {
    throw poplar::poplar_error("No devices found");
  }

  // Connect to first available device
  auto it = std::find_if(devices.begin(), devices.end(), [](Device &d){ return d.attach(); });
  if (it == devices.end()) {
    throw poplar::poplar_error("Could not attach to any device");
  }

  auto device = std::move(*it);

  // Graph elements
  Graph graph(device);
  Tensor t = graph.addVariable(FLOAT, {elements}, "data");
  graph.setTileMapping(t, 0);

  graph.addCodelets(__FILE__);

  ComputeSet cs = graph.addComputeSet();
  auto v1 = graph.addVertex(cs, "Increment");
  graph.setTileMapping(v1, 0);

  graph.connect(v1["x"], t);
  graph.connect(v1["y"], t);

  // Streams
  auto inStream = graph.addHostToDeviceFIFO("in", FLOAT, t.numElements());
  auto outStream = graph.addDeviceToHostFIFO("out", FLOAT, t.numElements());

  // Program
  auto prog = Repeat(
      repeat, Sequence({Copy(inStream, t), Execute(cs), Copy(t, outStream)}));

  // Compile program
  OptionFlags options{{"exchange.streamBufferOverlap", "none"},
                      {"exchange.enablePrefetch", "true"}};
  Engine eng(graph, prog, options);
  eng.load(device);

  // Connect output
  std::vector<float> hOut(t.numElements());
  eng.connectStream("out", hOut.data(),
                    std::next(hOut.data(), hOut.size()));

  // Run the program multiple times.
  // Replace input stream callback when value changes.
  boost::optional<float> previous;
  for (float in : {1.0f, 3.0f, 3.0f, 9.0f, 9.0f}) {
    if (previous != in) {
      std::cout << "Set input callback\n";
      std::unique_ptr<FillCallback> cb{new FillCallback(in, t.numElements())};
      eng.connectStreamToCallback("in", std::move(cb));
    }

    std::cout << "Running\n";
    eng.run(0);

    // Values in output result should be 'in' plus 1
    float expected = in + 1;
    auto match_expected =
        std::bind(std::equal_to<float>(), std::placeholders::_1, expected);
    bool success = std::all_of(hOut.begin(), hOut.end(), match_expected);
    if (!success) {
      std::stringstream ss("Unexpected result. ");
      ss << "Expected: " << expected << ";\n"
         << "Actual : " << hOut << "\n";
      throw std::runtime_error(ss.str());
    }

    previous = in;
  }

  return 0;
}

#endif

ext: cpp

max_stars_repo_name: LRVerkin/tutorials

max_issues_repo_name: LRVerkin/tutorials

max_forks_repo_name: LRVerkin/tutorials


text:
#include "BoundaryConditions.h"

// to avoid compiler confusion, python.hpp must be include before Halide headers
#include <boost/python.hpp>

#include "../../src/Lambda.h" // needed by BoundaryConditions.h
#include "../../src/BoundaryConditions.h"
#include "../../src/Func.h"

#include <string>

namespace h = Halide;
namespace hb = Halide::BoundaryConditions;
namespace p = boost::python;

namespace {

template<typename T>
h::Func constant_exterior0(T func_like, h::Expr value)
{
    return hb::constant_exterior(func_like, value);
}

// C++ fun, variadic template recursive function !
template<typename T=void, typename ...Types>
void def_constant_exterior_for_image()
{
    p::def("constant_exterior", &constant_exterior0<h::Image<T>>, p::args("source", "value"));
    def_constant_exterior_for_image<Types...>(); // recursive call
    return;
}

template<>
void def_constant_exterior_for_image<void>()
{ // end of recursion
    return;
}

} // end of anonymous namespace

namespace {

template<typename T>
h::Func repeat_edge0(T func_like)
{
    return hb::repeat_edge(func_like);
}

// C++ fun, variadic template recursive function !
template<typename T=void, typename ...Types>
void def_repeat_edge_for_image()
{
    p::def("repeat_edge", &repeat_edge0<h::Image<T>>, p::args("source"));
    def_repeat_edge_for_image<Types...>(); // recursive call
    return;
}

template<>
void def_repeat_edge_for_image<void>()
{ // end of recursion
    return;
}

} // end of anonymous namespace

namespace {

template<typename T>
h::Func repeat_image0(T func_like)
{
    return hb::repeat_image(func_like);
}

// C++ fun, variadic template recursive function !
template<typename T=void, typename ...Types>
void def_repeat_image_for_image()
{
    p::def("repeat_image", &repeat_image0<h::Image<T>>, p::args("source"));
    def_repeat_image_for_image<Types...>(); // recursive call
    return;
}

template<>
void def_repeat_image_for_image<void>()
{ // end of recursion
    return;
}

} // end of anonymous namespace

namespace {

template<typename T>
h::Func mirror_image0(T func_like)
{
    return hb::mirror_image(func_like);
}

// C++ fun, variadic template recursive function !
template<typename T=void, typename ...Types>
void def_mirror_image_for_image()
{
    p::def("mirror_image", &mirror_image0<h::Image<T>>, p::args("source"));
    def_mirror_image_for_image<Types...>(); // recursive call
    return;
}

template<>
void def_mirror_image_for_image<void>()
{ // end of recursion
    return;
}

} // end of anonymous namespace

namespace {

template<typename T>
h::Func mirror_interior0(T func_like)
{
    return hb::mirror_interior(func_like);
}

// C++ fun, variadic template recursive function !
template<typename T=void, typename ...Types>
void def_mirror_interior_for_image()
{
    p::def("mirror_interior", &mirror_interior0<h::Image<T>>, p::args("source"));
    def_mirror_interior_for_image<Types...>(); // recursive call
    return;
}

template<>
void def_mirror_interior_for_image<void>()
{ // end of recursion
    return;
}

} // end of anonymous namespace

void defineBoundaryConditions()
{

    // Other variants of constant_exterior exist, but not yet implemented

    p::def("constant_exterior", &constant_exterior0<h::ImageParam>, p::args("source", "value"),
           "Impose a boundary condition such that a given expression is returned "
           "everywhere outside the boundary. Generally the expression will be a "
           "constant, though the code currently allows accessing the arguments  "
           "of source.\n\n"
           "An ImageParam, Image<T>, or similar can be passed instead of a Func. If this  "
           "is done and no bounds are given, the boundaries will be taken from the  "
           "min and extent methods of the passed object.\n\n"
           "(This is similar to setting GL_TEXTURE_WRAP_* to GL_CLAMP_TO_BORDER  "
           " and putting value in the border of the texture.) ");

    def_constant_exterior_for_image<
            boost::uint8_t, boost::uint16_t, boost::uint32_t,
            boost::int8_t, boost::int16_t, boost::int32_t,
            float, double >();

    // The following variant is not yet implemented
    //    EXPORT Func constant_exterior(const Func &source, Expr value,
    //                                  const std::vector<std::pair<Expr, Expr>> &bounds);

    p::def("repeat_edge", &repeat_edge0<h::ImageParam>, p::args("source"),
           "Impose a boundary condition such that the nearest edge sample is returned "
           "everywhere outside the given region.\n\n"
           "An ImageParam, Image<T>, or similar can be passed instead of a Func. If this "
           "is done and no bounds are given, the boundaries will be taken from the "
           "min and extent methods of the passed object.\n\n"
           "(This is similar to setting GL_TEXTURE_WRAP_* to GL_CLAMP_TO_EDGE.)");

    def_repeat_edge_for_image<
            boost::uint8_t, boost::uint16_t, boost::uint32_t,
            boost::int8_t, boost::int16_t, boost::int32_t,
            float, double >();

    // The following variant is not yet implemented
    //    EXPORT Func repeat_edge(const Func &source,
    //                            const std::vector<std::pair<Expr, Expr>> &bounds);

    p::def("repeat_image", &repeat_image0<h::ImageParam>, p::args("source"),
           "Impose a boundary condition such that the entire coordinate space is "
           "tiled with copies of the image abutted against each other.\n\n"
           "An ImageParam, Image<T>, or similar can be passed instead of a Func. If this "
           "is done and no bounds are given, the boundaries will be taken from the "
           "min and extent methods of the passed object.\n\n"
           "(This is similar to setting GL_TEXTURE_WRAP_* to GL_REPEAT.)");

    def_repeat_image_for_image<
            boost::uint8_t, boost::uint16_t, boost::uint32_t,
            boost::int8_t, boost::int16_t, boost::int32_t,
            float, double >();

    //The following variant is not yet implemented
    //        EXPORT Func repeat_image(const Func &source,
    //                                 const std::vector<std::pair<Expr, Expr>> &bounds);

    p::def("mirror_image", &mirror_image0<h::ImageParam>, p::args("source"),
           "Impose a boundary condition such that the entire coordinate space is "
           "tiled with copies of the image abutted against each other, but mirror "
           "them such that adjacent edges are the same.\n\n"
           "An ImageParam, Image<T>, or similar can be passed instead of a Func. If this "
           "is done and no bounds are given, the boundaries will be taken from the "
           "min and extent methods of the passed object.\n\n"
           "(This is similar to setting GL_TEXTURE_WRAP_* to GL_MIRRORED_REPEAT.)");

    def_mirror_image_for_image<
            boost::uint8_t, boost::uint16_t, boost::uint32_t,
            boost::int8_t, boost::int16_t, boost::int32_t,
            float, double >();

    // The following variant is not yet implemented
    //        EXPORT Func mirror_image(const Func &source,
    //                                 const std::vector<std::pair<Expr, Expr>> &bounds);

    p::def("mirror_interior", &mirror_interior0<h::ImageParam>, p::args("source"),
           "Impose a boundary condition such that the entire coordinate space is "
           "tiled with copies of the image abutted against each other, but mirror "
           "them such that adjacent edges are the same and then overlap the edges.\n\n"
           "This produces an error if any extent is 1 or less. (TODO: check this.)\n\n"
           "An ImageParam, Image<T>, or similar can be passed instead of a Func. If this "
           "is done and no bounds are given, the boundaries will be taken from the "
           "min and extent methods of the passed object. "
           "(I do not believe there is a direct GL_TEXTURE_WRAP_* equivalent for this.)");

    def_mirror_interior_for_image<
            boost::uint8_t, boost::uint16_t, boost::uint32_t,
            boost::int8_t, boost::int16_t, boost::int32_t,
            float, double >();

    // The following variant is not yet implemented
    //    EXPORT Func mirror_interior(const Func &source,
    //                                const std::vector<std::pair<Expr, Expr>> &bounds);

    return;
}

max_stars_repo_name: jrayzero/TylerDistHalide

max_issues_repo_name: jrayzero/TylerDistHalide

max_forks_repo_name: jrayzero/TylerDistHalide


text:
/* Copyright (c) 2017, United States Government, as represented by the
 * Administrator of the National Aeronautics and Space Administration.
 *
 * All rights reserved.
 *
 * The Astrobee platform is licensed under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

#include <Eigen/Dense>

#include <iostream>

using namespace Eigen;

void matrix_multiply(float* m1, int m1_rows, int m1_cols, float* m2, int m2_rows, int m2_cols, float* m_out) {
  Map<MatrixXf> A(m1, m1_rows, m1_cols);
  Map<MatrixXf> B(m2, m2_rows, m2_cols);
  Map<MatrixXf> out(m_out, m1_rows, m2_cols);

  out.noalias() = A * B;
}

max_stars_repo_name: PeterWofford/astrobee

max_issues_repo_name: PeterWofford/astrobee

max_forks_repo_name: PeterWofford/astrobee


text:
#include <iostream>

#include <boost/functional.hpp>

#include "../action.hpp"
#include "../../util/io.hpp"

using namespace po;

namespace associative
{

	class QueryDBAction : public Action
	{
		COMMANDLINE_DECL;

	private:
		static void performQuery(const std::string& query, Connection& conn)
		{
			auto result = conn.executeQuery(query);
			std::cout << collToString(result.columnNames, QueryResult::tableRowFormat()) << std::endl << std::endl;
			std::cout << collToString(result.rows, QueryResult::tableFormat()) << std::endl;
			std::cout << result.rows.size() << " row(s) fetched" << std::endl;
		}

		static void performStatement(const std::string& statement, Connection& conn)
		{
			std::cout << "Statement execution returned with status " << conn.executeStatement(statement) << std::endl;
		}

	protected:
		virtual options_description* desc()
		{
			auto desc = new options_description("query-db options");
			desc->add_options()
				("query", "query database and print results")
				("statement", "execute a statement");
			return desc;
		}

	public:
		QueryDBAction()
		: Action("query-db")
		{
		}

		virtual int perform(const variables_map& vm, const std::vector<std::string>& parameters, Environment& env)
		{
			bool query = vm.count("query");
			bool statement = vm.count("statement");
			if (query == statement)
				return 1;

			if (query)
				doREPL(parameters, boost::bind2nd(boost::ptr_fun(&performQuery), env.getConnection()), "query> ");
			else
				doREPL(parameters, boost::bind2nd(boost::ptr_fun(&performStatement), env.getConnection()), "statement> ");

			return 0;
		}

	};

}

COMMANDLINE_DEF(QueryDB);

max_stars_repo_name: larsrh/associative

max_issues_repo_name: larsrh/associative

max_forks_repo_name: larsrh/associative


text:
#include "SessionManager.h"
#include "BusinessLogHelper.h"
#include "IAsyncLogModule.h"
#include "SapMessage.h"
#include "SapTLVBody.h"
#include "HpsResponseMsg.h"
#include "XmlConfigParser.h"
#include <algorithm>
#include "HpsStack.h"
#include <boost/algorithm/string.hpp>
#include "UrlCode.h"
#include "HpsCommonFunc.h"
#include "SapMessage.h"
#include "HpsRequestMsg.h"
#include "HttpRecVirtualClient.h"
#include "Cipher.h"
#include "AsyncVirtualClientLog.h"
#include "ResourceAgent.h"

#ifndef WIN32
#include <arpa/inet.h>
#endif

using namespace sdo::hps;

const unsigned int LSC_SERVICE_ID = 55101;
const unsigned int RSC_SERVICE_ID = 55201;

CHpsSessionManager::CHpsSessionManager(CHttpRecVirtualClient *pVirtualClient, int nThreadId)
    : m_nThreadId(nThreadId),
      m_pHpsContainer(NULL),
      m_pTransfer(NULL),
      m_dwSeq(0),
      m_isAlive(false),
      m_tmTimeOut(m_oIoService),
      m_nLen(0),
      m_dwSosContainerCount(0),
      m_tmSelfCheck(this, SELF_CHECK_INTERVAL, boost::bind(&CHpsSessionManager::DoSelfCheck, this), CThreadTimer::TIMER_CIRCLE),
      m_timerSocSelfCheck(this, SOC_SELFCHECK_INTERVAL ,boost::bind(&CHpsSessionManager::DoReportSosStatusCheckData, this), CThreadTimer::TIMER_CIRCLE),
      //m_timerRequest(this, REQUEST_TIME_OUT, boost::bind(&CHpsSessionManager::DetectRequestTimerList, this), CThreadTimer::TIMER_CIRCLE),
      m_pHttpClientContainer(NULL),
      m_pVirtualClient(pVirtualClient)
{
	m_pHpsContainer = new CHpsSessionContainer(this);
	m_pTransfer = new CAvenueHttpTransfer(this);
	m_pHttpClientContainer = new CHttpClientContainer(this);
	m_osapManager.SetSessionManager(this);
    m_pResourceAgent = new CResourceAgent(this);
	memset(m_szTransferData.arrTransferData, 0, MAXBUFLEN);
}

CHpsSessionManager::~CHpsSessionManager()
{
	if(m_pHpsContainer != NULL)
	{
		delete m_pHpsContainer;
	}
	if(m_pTransfer != NULL)
	{
		delete m_pTransfer;
	}
	if(m_pHttpClientContainer != NULL)
	{
		delete m_pHttpClientContainer;
	}

    m_mapSosSessionContainerByServiceId.clear();
    //OnClearCache();
}

void CHpsSessionManager::Start(unsigned int affinity)
{
    BS_XLOG(XLOG_DEBUG, "CHpsSessionManager::%s, affinity[%d]\n", __FUNCTION__, affinity);
    m_oWork = new boost::asio::io_service::work(m_oIoService);
    m_thread = boost::thread(boost::bind(&boost::asio::io_service::run, &m_oIoService));
    m_oIoService.post(boost::bind(&CHpsSessionManager::StartInThread, this));
	m_nTimeCount = 0;

    m_isAlive=true;
	m_tmTimeOut.expires_from_now(boost::posix_time::microseconds(300));
	m_tmTimeOut.async_wait(
        MakeSapAllocHandler(m_allocTimeOut,boost::bind(&CHpsSessionManager::DoTimer, this)));
}

void CHpsSessionManager::StartInThread()
{
    BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s...\n",__FUNCTION__);
    Reset();
    /* start the timers */
	m_tmSelfCheck.Start();
	m_timerSocSelfCheck.Start();
	//m_timerRequest.Start();
	m_pHpsContainer->StartInThread();
	m_pTransfer->StartInThread();
	m_pHttpClientContainer->StartInThread();
}

void CHpsSessionManager::DoTimer()
{
    //BS_XLOG(XLOG_TRACE,"CHpsSessionManager::%s\n",__FUNCTION__);
	if (m_nTimeCount++ == 6)
	{
		DetectTimerList();
		m_nTimeCount = 0;
	}
	DetectRequestTimerList();
    m_isAlive = true;
    if (m_isAlive)
    {
        m_tmTimeOut.expires_from_now(boost::posix_time::microseconds(300));
        m_tmTimeOut.async_wait(
                MakeSapAllocHandler(m_allocTimeOut,boost::bind(&CHpsSessionManager::DoTimer, this)));
    }
}

void CHpsSessionManager::StopInThread()
{
    //BS_XLOG(XLOG_TRACE,"CHpsSessionManager::%s\n",__FUNCTION__);
    m_tmSelfCheck.Stop();
    m_timerSocSelfCheck.Stop();
    m_pHpsContainer->StopInThread();
    m_pTransfer->StopInThread();
    m_pHttpClientContainer->StopInThread();
    m_oIoService.stop();
}

void CHpsSessionManager::Stop()
{
    BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s\n",__FUNCTION__);
    m_isAlive=false;
    delete m_oWork;
    m_oIoService.post(boost::bind(&CHpsSessionManager::StopInThread, this));
    m_thread.join();
    m_oIoService.reset();
}
void CHpsSessionManager::AddConnection(HpsConnection_ptr ptrConn)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s..\n", __FUNCTION__);
	m_oIoService.post(boost::bind(&CHpsSessionManager::AddConnectionInner,this,ptrConn));
}

void CHpsSessionManager::AddConnectionInner(HpsConnection_ptr ptrConn)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s..\n", __FUNCTION__);
	m_pHpsContainer->AddConnection(ptrConn);
}

void CHpsSessionManager::OnLoadTransferObjs(const string &strH2ADir, const string &strH2AFilter, const string &strA2HDir, const string &strA2HFilter)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s, strH2ADir[%s], strH2AFilter[%s], strA2HDir[%s], strA2HFilter[%s]\n",
		__FUNCTION__,strH2ADir.c_str(), strH2AFilter.c_str(),strA2HDir.c_str(), strA2HFilter.c_str());
	m_oIoService.post(boost::bind(&CHpsSessionManager::DoLoadTransferObjs,this,strH2ADir,strH2AFilter,strA2HDir,strA2HFilter));
}

void CHpsSessionManager::OnLoadSosList(const vector<string> & vecServer)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s..\n", __FUNCTION__);
	m_oIoService.post(boost::bind(&CHpsSessionManager::DoLoadSosList,this,vecServer));
}

void CHpsSessionManager::OnUnLoadSharedLibs(const string &strDirPath,const vector<string> &vecSo)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s..\n", __FUNCTION__);
	m_oIoService.post(boost::bind(&CHpsSessionManager::DoUnLoadSharedLibs,this,strDirPath,vecSo));
}

void CHpsSessionManager::OnReLoadSharedLibs(const string & strDirPath,const vector<string> &vecSo)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s..\n", __FUNCTION__);
	m_oIoService.post(boost::bind(&CHpsSessionManager::DoReLoadSharedLibs,this,strDirPath,vecSo));
}

void CHpsSessionManager::OnLoadRouteConfig(const vector<string> &vecRouteBalance)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s\n", __FUNCTION__);
	m_oIoService.post(boost::bind(&CHpsSessionManager::DoLoadRouteConfig,this,vecRouteBalance));
}

void CHpsSessionManager::DoLoadRouteConfig(const vector<string> &vecRouteBalance)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s\n", __FUNCTION__);
	map<unsigned int, map<int, string> > mapSosPriAddrsByServiceId;
	map<unsigned int, CSosSessionContainer*>::iterator itr;
	for(itr=m_mapSosSessionContainerByServiceId.begin(); itr!=m_mapSosSessionContainerByServiceId.end(); ++itr)
	{
		unsigned int dwServiceId = itr->first;
		CSosSessionContainer *pContainer = itr->second;
		map<int, string> mapTemp;
		pContainer->GetMapPriSosList(mapTemp);
		mapSosPriAddrsByServiceId.insert(make_pair(dwServiceId, mapTemp));
	}

	m_oHttpReqConfigManager.LoadConfig(vecRouteBalance,mapSosPriAddrsByServiceId);
}

void CHpsSessionManager::OnUpdateRouteConfig(unsigned int dwServiceId, unsigned int dwMsgId, int nErrCode)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s, serviceId[%u], msgId[%u], nErrCode[%d]\n", __FUNCTION__,dwServiceId,dwMsgId,nErrCode);

	m_oIoService.post(boost::bind(&CHpsSessionManager::DoUpdateRouteConfig,this,dwServiceId,dwMsgId,nErrCode));
}

void CHpsSessionManager::DoUpdateRouteConfig(unsigned int dwServiceId, unsigned int dwMsgId, int nErrCode)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s, serviceId[%u], msgId[%u], nErrCode[%d]\n", __FUNCTION__,dwServiceId,dwMsgId,nErrCode);

	m_oHttpReqConfigManager.UpdateRouteConfig(dwServiceId,dwMsgId,nErrCode);
}

void CHpsSessionManager::OnReLoadCommonTransfer(const vector<string>& vecServer, const string& strTransferName)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s vecsize[%d]\n", __FUNCTION__,vecServer.size());
	m_oIoService.post(boost::bind(&CHpsSessionManager::DoReLoadCommonTransfer,this,vecServer,strTransferName));
}

void CHpsSessionManager::OnResourceRequestCompleted(SRequestInfo &sReq)
{
    BS_XLOG(XLOG_DEBUG, "CHpsSessionManager::%s\n", __FUNCTION__);
    SendHttpResponse(sReq);
    RecordeHttpRequest(sReq);
}

SUrlInfo CHpsSessionManager::GetSoNameByUrl(const string& strUrl)
{
	if (m_pTransfer != NULL)
	{
		return m_pTransfer->GetSoNameByUrl(strUrl);
	}
	return SUrlInfo();
}

int CHpsSessionManager::FindUrlMapping(const string& strUrl, unsigned int& dwServiceID, unsigned int& dwMsgID)
{
    if (NULL != m_pHpsContainer)
    {
        return m_pHpsContainer->FindUrlMapping(strUrl, dwServiceID, dwMsgID);
    }

    return -1;
}

/*****************************************************/

void CHpsSessionManager::DoLoadTransferObjs(const string &strH2ADir, const string &strH2AFilter, const string &strA2HDir, const string &strA2HFilter)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s, strH2ADir[%s], strH2AFilter[%s], strA2HDir[%s], strA2HFilter[%s]\n",
		__FUNCTION__,strH2ADir.c_str(), strH2AFilter.c_str(),strA2HDir.c_str(), strA2HFilter.c_str());
	if(0 == m_pTransfer->DoLoadTransferObj(strH2ADir, strH2AFilter, strA2HDir, strA2HFilter))
	{
		vector<string> vecUrlMapping;
		m_pTransfer->GetUriMapping(vecUrlMapping);
		m_pHpsContainer ->LoadMethodMapping(vecUrlMapping);
		m_osapManager.LoadConfig();

		m_vecUrlMappingForCheck = vecUrlMapping;
	}
}

void CHpsSessionManager::DoUnLoadSharedLibs(const string &strDirPath,const vector<string> &vecSo)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s\n",	__FUNCTION__);
	m_pTransfer->UnLoadTransferObj(strDirPath,vecSo);

	vector<string> vecUrlMapping;
	m_pTransfer->GetUriMapping(vecUrlMapping);
	m_pHpsContainer ->LoadMethodMapping(vecUrlMapping);
}

void CHpsSessionManager::DoReLoadSharedLibs(const string & strDirPath,const vector<string> &vecSo)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s\n",	__FUNCTION__);
	if(0 == m_pTransfer->ReLoadTransferObj(strDirPath,vecSo))
	{
		vector<string> vecUrlMapping;
		m_pTransfer->GetUriMapping(vecUrlMapping);
		m_pHpsContainer ->LoadMethodMapping(vecUrlMapping);
		m_osapManager.LoadConfig();
	}
}

void CHpsSessionManager::DoLoadSosList(const vector<string> & vecServer)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s.,vecServer.size[%d]\n", __FUNCTION__,vecServer.size());

	vector<SServiceSosAddr> vecServiceIdSosAdds;

	vector<unsigned int> vecNewServiceId;
	vector<string>::const_iterator iter;
	for(iter = vecServer.begin(); iter != vecServer.end(); ++iter)
	{
		SServiceSosAddr sSrvSosAddr;

		string strSosInfo = *iter;
		CXmlConfigParser oConfig;
		oConfig.ParseDetailBuffer(strSosInfo.c_str());
		string strServiceIds = oConfig.GetParameter("ServiceId","0");
		sSrvSosAddr.vecSosAddr = oConfig.GetParameters("ServerAddr");
		vector<unsigned int> vecIntServiceId;
		vector<string> vecStrServiceId;
		boost::algorithm::split( vecStrServiceId, strServiceIds, boost::algorithm::is_any_of(","),boost::algorithm::token_compress_on);
		vector<string>::iterator iterVec;
		for(iterVec=vecStrServiceId.begin(); iterVec!=vecStrServiceId.end(); ++iterVec)
		{
			unsigned int dwServiceId = atoi((*iterVec).c_str());
			sSrvSosAddr.vecServiceId.push_back(dwServiceId);
			vecNewServiceId.push_back(dwServiceId);
		}
		vecServiceIdSosAdds.push_back(sSrvSosAddr);
	}

	sort(vecNewServiceId.begin(), vecNewServiceId.end());

	/*step1, travers the old m_mapSosSessionContainerByServiceId, delete the deleted service*/
	map<unsigned int, CSosSessionContainer*>::iterator iterOldMap = m_mapSosSessionContainerByServiceId.begin();;
	while(iterOldMap != m_mapSosSessionContainerByServiceId.end())
	{
		map<unsigned int, CSosSessionContainer*>::iterator itrTmp = iterOldMap++;
		unsigned int dwOldServiceId = itrTmp->first;
		if(!binary_search(vecNewServiceId.begin(), vecNewServiceId.end(),dwOldServiceId ))
		{
			BS_XLOG(XLOG_TRACE,"CHpsSessionManager::%s, erase map-serviceId[%u]\n", __FUNCTION__,dwOldServiceId);
			m_mapSosSessionContainerByServiceId.erase(itrTmp);
		}
	}

	/* set the sosSessionContainer's status to false*/
	map<unsigned int, CSosSessionContainer*>::iterator itrContainer = m_mapSosSessionContainerByContainerId.begin();
	for(;itrContainer != m_mapSosSessionContainerByContainerId.end(); ++itrContainer)
	{
		CSosSessionContainer* pContainer = itrContainer->second;
		pContainer->SetUsableStatus(false);
	}

	/*step2, tranvers the new mapNewSosList, add the load the services' sossession*/
	vector<SServiceSosAddr>::iterator itrVec = vecServiceIdSosAdds.begin();
	for(; itrVec != vecServiceIdSosAdds.end(); ++itrVec)
	{
		vector<unsigned int> &vecServiceId = itrVec->vecServiceId;
		vector<string> &vecSosAddr = itrVec->vecSosAddr;

		CSosSessionContainer* pContainer = GetContainerPtrBySosAddr(vecSosAddr);
		if(pContainer == NULL)
		{
			BS_XLOG(XLOG_TRACE,"CHpsSessionManager::%s, no pContainer\n", __FUNCTION__);
			pContainer = new CSosSessionContainer(++m_dwSosContainerCount,this,vecServiceId);
			pContainer->LoadSosList(vecSosAddr, this);
			m_mapSosSessionContainerByContainerId.insert(make_pair(pContainer->GetContainerId(),pContainer));
		}
		else
		{
			BS_XLOG(XLOG_TRACE,"CHpsSessionManager::%s, pContainer has exist\n", __FUNCTION__);
			pContainer->LoadSosList(vecSosAddr, this);
		}

		pContainer->SetServiceIds(vecServiceId);
		pContainer->SetUsableStatus(true);

		vector<unsigned int>::iterator iterSrvIdVec;
		for(iterSrvIdVec = vecServiceId.begin(); iterSrvIdVec != vecServiceId.end(); ++iterSrvIdVec)
		{

			unsigned int dwServiceId = *iterSrvIdVec;
			//m_mapSosSessionContainerByServiceId.insert(make_pair(dwServiceId, pContainer));
			m_mapSosSessionContainerByServiceId[dwServiceId] = pContainer;

			BS_XLOG(XLOG_TRACE,"CHpsSessionManager::%s, dwServiceId[%u], pContainerId[%u]\n", __FUNCTION__,dwServiceId,pContainer->GetContainerId());
		}
	}

	/* delete the sosSessionContainer*/
	itrContainer = m_mapSosSessionContainerByContainerId.begin();
	while(itrContainer != m_mapSosSessionContainerByContainerId.end())
	{
		map<unsigned int, CSosSessionContainer*>::iterator itrContainerIdTmp = itrContainer++;
		CSosSessionContainer* pContainerTmp = itrContainerIdTmp->second;
		if(pContainerTmp->GetUsableStatus() == false)
		{
			m_mapSosSessionContainerByContainerId.erase(itrContainerIdTmp);
			delete pContainerTmp;
		}
	}
}

CSosSessionContainer* CHpsSessionManager::GetContainerPtrBySosAddr(const vector<string> &vecSosAddr)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s.  vecSosAddr.size[%d]\n", __FUNCTION__,vecSosAddr.size());

	map<unsigned int, CSosSessionContainer*>::iterator itrContainer = m_mapSosSessionContainerByContainerId.begin();
	for(;itrContainer != m_mapSosSessionContainerByContainerId.end(); ++itrContainer)
	{
		CSosSessionContainer* pContainer = itrContainer->second;
		if(true == pContainer->ContainSosAddr(vecSosAddr))
		{
			return pContainer;
		}
	}
	return NULL;
}

void CHpsSessionManager::DoReLoadCommonTransfer(const vector<string>& vecServer, const string& strTransferName)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s\n",	__FUNCTION__);
	DoLoadSosList(vecServer);
	if(0 == m_pTransfer->ReLoadCommonTransfer(strTransferName))
	{
		vector<string> vecUrlMapping;
		m_pTransfer->GetUriMapping(vecUrlMapping);
		m_pHpsContainer->LoadMethodMapping(vecUrlMapping);

		m_vecUrlMappingForCheck = vecUrlMapping;
	}
}

void CHpsSessionManager::OnHttpConnected(unsigned int dwId, const string &strRemoteIp,unsigned int dwRemotePort)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s, dwId[%u], strRemoteIp[%s], dwRemotePort[%d]\n",
		__FUNCTION__,dwId,strRemoteIp.c_str(),dwRemotePort);
	m_oIoService.post(boost::bind(&CHpsSessionManager::DoHttpConnected,this,dwId, strRemoteIp, dwRemotePort));
}

void CHpsSessionManager::DoHttpConnected(unsigned int dwId, const string &strRemoteIp,unsigned int dwRemotePort)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s, dwId[%u], strRemoteIp[%s], dwRemotePort[%d]\n",
		__FUNCTION__,dwId, strRemoteIp.c_str(),dwRemotePort);

	SHttpConnectInfo connInfo(E_Http_Connect,dwId,strRemoteIp,dwRemotePort);
	GetAsyncLogModule()->OnHttpConnectInfo(connInfo);

	m_pHpsContainer->OnHttpConnected(dwId);
}

void CHpsSessionManager::DoReceiveHttpRequest(SRequestInfo &sReq, const string &strRemoteIp,unsigned int dwRemotePort)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s, dwId[%u], nServiceId[%d], nMsgId[%d], strUriCommand[%s], strUriAttribute[%s], sReq.strRemoteIp[%s], sReq.dwRemotePort[%u], strRemoteIp[%s], dwRemotePort[%d]\n",
		__FUNCTION__, sReq.dwId,sReq.nServiceId, sReq.nMsgId, sReq.strUriCommand.c_str(),  sReq.strUriAttribute.c_str(), sReq.strRemoteIp.c_str(),sReq.dwRemotePort,strRemoteIp.c_str(),dwRemotePort);

	GetAsyncLogModule()->OnReceiveHttpRequest(sReq.nServiceId,sReq.nMsgId);

	sReq.dwSequence = ++m_dwSeq;

	if(0 == m_ipLimitCheckor.CheckIpLimit(sReq.strUrlIdentify,sReq.strRemoteIp))
	{
		sReq.inWhiteList = true;
	}

	OsapUserInfo* pUserInfo = NULL;
	int nCode = m_osapManager.GetUserInfo(sReq, &pUserInfo);
	if (nCode > 0)
	{
		return; // waiting for osap response
	}

        if (!sReq.isNomalBpeRequest)
        {
            DoHandleResourceRequest(sReq);
        }
        else
        {
            DoSendAvenueRequest(sReq, nCode, pUserInfo);
        }
}

int CHpsSessionManager::DoHandleResourceRequest(SRequestInfo &sReq)
{
    BS_XLOG(XLOG_DEBUG, "CHpsSessionManager::%s\n", __FUNCTION__);
    bool isFind = false, isLoad = false;
    string strContentType;
    m_pResourceAgent->OnResourceRequest(sReq, isFind, isLoad, strContentType);
    if (!isFind)
    {
        BS_XLOG(XLOG_WARNING, "CHpsSessionManager::%s the resource [%s] is not found\n", __FUNCTION__, sReq.strUrlIdentify.c_str());
        EErrorCode errCode = ERROR_URL_NOT_SUPPORT;
        if (sReq.requestType == E_JsonRpc_Request)
        {
            errCode = ERROR_JSONRPC_METHOD_NOT_FOUND;
        }

        char szErrorCodeNeg[16] = {0};
        snprintf(szErrorCodeNeg, 15, "%d", errCode);
        sReq.strResponse = string("{\"return_code\":") + szErrorCodeNeg
                              + ",\"return_message\":\"" +  CErrorCode::GetInstance()->GetErrorMessage(errCode)
                               + "\",\"data\":{}}";
        sReq.isVaild = false;
        sReq.nCode = errCode;
        sReq.nHttpCode = 404;
        SendHttpResponse(sReq);
        RecordeHttpRequest(sReq);
    }
    else if (isLoad)
    {
        BS_XLOG(XLOG_DEBUG, "CHpsSessionManager::%s the resource [%s] is static and loaded\n", __FUNCTION__, sReq.strUrlIdentify.c_str());
        sReq.nCode = 0;
        sReq.strContentType = strContentType;
        SendHttpResponse(sReq);
        RecordeHttpRequest(sReq);
    }
    else
    {
        BS_XLOG(XLOG_DEBUG, "CHpsSessionManager::%s the resource [%s] is dynamic and loaded\n", __FUNCTION__, sReq.strUrlIdentify.c_str());
        //m_mapSeqReqInfo[sReq.dwSequence] = sReq;
    }
    return 0;
}

int CHpsSessionManager::DoSendAvenueRequest(SRequestInfo &sReq, int nCode, OsapUserInfo* pUserInfo)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s username[%s]\n", __FUNCTION__,sReq.strUserName.c_str());

	CSapTLVBodyEncoder exHeaderEncoder;
	if (nCode == 0 || sReq.nServiceId == RSC_SERVICE_ID)
	{
		if (nCode == 0)
		{
			nCode = m_osapManager.CheckAuthenAndSignature(sReq, pUserInfo);
		}
		char szAddr[16] = {0};
#if defined(_WIN32)||defined(_WIN64)
        char szPeerAddr[4] = { 0 };
        unsigned int segment[4];
        sscanf(sReq.strRemoteIp.c_str(), "%u.%u.%u.%u", &segment[0], &segment[1], &segment[2], &segment[3]);
        szPeerAddr[0] = segment[0];
        szPeerAddr[1] = segment[1];
        szPeerAddr[2] = segment[2];
        szPeerAddr[3] = segment[3];
#else
        in_addr_t peerAddr = inet_addr(sReq.strRemoteIp.c_str());
        memcpy(szAddr, &peerAddr, 4);
#endif
        int netPort = htonl(sReq.dwRemotePort);
        memcpy(szAddr + 4, &netPort, 4);
		exHeaderEncoder.SetValue(SAP_HEADEX_CODE_USER, sReq.strUserName);
		exHeaderEncoder.SetValue(SAP_HEADEX_CODE_ADDR, szAddr, 8);
		if (!m_osapManager.IsUrlInNoSaagList(sReq.strUrlIdentify))
		{
			exHeaderEncoder.SetValue(SAP_HEADEX_CODE_APPID, sReq.nAppid);
			exHeaderEncoder.SetValue(SAP_HEADEX_CODE_AREAID, sReq.nAreaId);
			exHeaderEncoder.SetValue(SAP_HEADEX_CODE_GROUPID, sReq.nGroupId);
			exHeaderEncoder.SetValue(SAP_HEADEX_CODE_HOSTID, sReq.nHostid);
			exHeaderEncoder.SetValue(SAP_HEADEX_CODE_SPID, sReq.nSpid);
		}
		if (sReq.nServiceId == RSC_SERVICE_ID)
		{
			sReq.strGuid = "RSC" + sReq.strGuid;
		}
		exHeaderEncoder.SetValue(SAP_HEADEX_CODE_GUID, sReq.strGuid);
		exHeaderEncoder.SetValue(SAP_HEADEX_CODE_COMMENT, sReq.strComment);

		memcpy(m_szTransferData.arrTransferData, exHeaderEncoder.GetBuffer(), exHeaderEncoder.GetLen());
		m_nLen = exHeaderEncoder.GetLen();

		if (sReq.nServiceId == RSC_SERVICE_ID)
		{
			if (nCode == 0)
			{
				unsigned osapcheck = 1;
				int RSC_SIG_FLAG = 100;
				CSapTLVBodyEncoder::SetValue(RSC_SIG_FLAG,
					&osapcheck, 4, m_szTransferData.arrTransferData+m_nLen, (unsigned&)m_nLen);
			}
			else
			{
				nCode = 0; // "RegistTransfer" will check authen
			}
		}
	}

	if (nCode == 0)
	{
		if (!sReq.strCookie.empty())
		{
			CSapTLVBodyEncoder::SetValue(TRANSEX_CODE_COOKIE,
				sReq.strCookie.c_str(), sReq.strCookie.length(),
				m_szTransferData.arrTransferData+m_nLen, (unsigned&)m_nLen);
		}

                m_szTransferData.pReq = &sReq;
		nCode = m_pTransfer->DoTransferRequestMsg(sReq.strUrlIdentify, sReq.nServiceId, sReq.nMsgId,
			sReq.dwSequence, sReq.strUriCommand.c_str(), sReq.strUriAttribute.c_str(),
			sReq.strRemoteIp.c_str(),sReq.dwRemotePort, (void*)&m_szTransferData, &m_nLen);
		sReq.headers.clear();
	}

	if (nCode == 0)
	{
        void *pBuffer = m_szTransferData.arrTransferData;
		SSapMsgHeader* pHeader = (SSapMsgHeader*)pBuffer;
		sReq.nServiceId = htonl(pHeader->dwServiceId);
		sReq.nMsgId = htonl(pHeader->dwMsgId);

        const void* pExHeader = exHeaderEncoder.GetBuffer();
        int nExLen = exHeaderEncoder.GetLen();
        void *wrBuffer = NULL;
        int nFactLen = m_nLen;
        if (nExLen>0)
        {
            nFactLen += nExLen;
            wrBuffer = malloc(nFactLen);
            memcpy(wrBuffer, pBuffer, pHeader->byHeadLen);
            memcpy((char *)wrBuffer + pHeader->byHeadLen, pExHeader, nExLen);
            memcpy((char *)wrBuffer + pHeader->byHeadLen + nExLen, (char *)pBuffer + pHeader->byHeadLen, m_nLen - pHeader->byHeadLen);

            pHeader = (SSapMsgHeader *)wrBuffer;
            pHeader->byHeadLen += nExLen;
            pHeader->dwPacketLen = htonl(ntohl(pHeader->dwPacketLen) + nExLen);
        }
        else
        {
            wrBuffer = malloc(nFactLen);
            memcpy(wrBuffer, pBuffer, m_nLen);
        }

        string strSosSignatueKey = m_oHttpReqConfigManager.GetSignatureKey(sReq.nServiceId, sReq.nMsgId, sReq.strUrlIdentify);
        if(!strSosSignatueKey.empty())
        {
            int nDigestLen = strSosSignatueKey.length();
            int nMin = (nDigestLen<16? nDigestLen:16);
            memcpy(pHeader->bySignature, strSosSignatueKey.c_str(), nMin);
            CCipher::Md5((unsigned char *)wrBuffer, nFactLen, pHeader->bySignature);
        }

        BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s SendRequest\n", __FUNCTION__);
        m_pVirtualClient->SendRequest(this, wrBuffer, nFactLen);
        BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s AddHistoryRequest\n", __FUNCTION__);
        AddHistoryRequest(sReq);
        BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s insert into map\n", __FUNCTION__);
        m_mapSeqReqInfo[sReq.dwSequence] = sReq;

        free(wrBuffer);
	}

	if(nCode != 0)  //error happened, response and record the request info
	{
		BS_XLOG(XLOG_TRACE,"CHpsSessionManager::%s::%d, nCode[%d]\n",__FUNCTION__,__LINE__,nCode);
		sReq.nCode = nCode;
		sReq.strSosIp = "";
		sReq.dwSosPort = 0;
		sReq.headers.clear();
		if(sReq.nCode == ERROR_BAD_REQUEST_INNER)
		{
			sReq.strResponse = string(m_szTransferData.arrTransferData,m_nLen);
		}
		SendHttpResponse(sReq);
		//m_pHpsContainer->DoSendResponse(sReq);
		RecordeHttpRequest(sReq);
	}
	return nCode;
}

void CHpsSessionManager::DoHttpPeerClose(unsigned int dwId, const string &strRemoteIp,const unsigned int dwRemotePort)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s, dwId[%u], strRemoteIp[%s], dwRemotePort[%d]\n",
		__FUNCTION__,dwId,strRemoteIp.c_str(),dwRemotePort);

	/*erase the m_mapSeqReqInfo and m_mapQueue according to the pIdentifier*/
	//pair<multimap<unsigned int, unsigned int>::iterator, multimap<unsigned int, unsigned int>::iterator> ii;
	//ii = m_multimapIdSeq.equal_range(dwId);
	//multimap<unsigned int, unsigned int>::iterator itermmap;
	//for( itermmap = ii.first; itermmap != ii.second; ++itermmap )
	//{
	//	map<unsigned int, SRequestInfo>::iterator iter = m_mapSeqReqInfo.find(itermmap->second);
	//	if(iter != m_mapSeqReqInfo.end())
	//	{
	//		SRequestInfo &sReq = iter->second;
	//		sReq.nCode = ERROR_CLIENT_CANCEL;
	//		RecordeHttpRequest(sReq);
	//		m_mapSeqReqInfo.erase(iter);
	//		RemoveHistory(sReq);
	//	}
	//}
	//m_multimapIdSeq.erase(dwId);

	SHttpConnectInfo connInfo(E_Http_Close,dwId,strRemoteIp,dwRemotePort);
	GetAsyncLogModule()->OnHttpConnectInfo(connInfo);
}

void CHpsSessionManager::OnConnected(unsigned int dwSosContainerId, unsigned int dwSosSessionId, unsigned int dwConnId)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s, containerId[%u], sosSessionId[%u], sosConnId[%u]\n",__FUNCTION__,
		dwSosContainerId, dwSosSessionId, dwConnId);
}

/* 以响应包中的serviceId为准*/
void CHpsSessionManager::OnReceiveSosAvenueResponse(unsigned int dwId,const void *pBuffer, int nLen,const string &strRemoteIp,unsigned int dwRemotePort)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s, dwId[%u], strRemoteAddr[%s:%u], bufferLen[%d]\n",
		__FUNCTION__, dwId, strRemoteIp.c_str(), dwRemotePort, nLen);

	CSapDecoder sapDecoderObj(pBuffer, nLen);
	unsigned int dwServiceId = sapDecoderObj.GetServiceId();
	unsigned int dwMsgId = sapDecoderObj.GetMsgId();
	unsigned int nSeq = sapDecoderObj.GetSequence();
	unsigned int dwCode = sapDecoderObj.GetCode();

	if (sapDecoderObj.GetServiceId() == SAP_SMS_SERVICE_ID)
	{
            BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s, SAP_SMS_SERVICE_ID\n", __FUNCTION__);
		m_osapManager.OnOsapResponse(pBuffer, nLen);
		return;
	}

	if(dwCode >= 100 && dwCode < 200)
	{
		BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s, template data packet\n",__FUNCTION__);
		return;
	}

	if(dwServiceId==SAP_PLATFORM_SERVICE_ID)
	{
		BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s.  Register Sls succ. remoteIp[%s], port[%u]\n", __FUNCTION__,strRemoteIp.c_str(), dwRemotePort);
		return;
	}

        BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s, Code[%d]\n", __FUNCTION__, dwCode);
	if(dwCode != 0)
	{
		CHpsStack::Instance()->OnUpdateRouteConfig(dwServiceId,dwMsgId,(int)dwCode);
	}

	/* judge here can avoid call the transfer method when the request has been cancled before reveiving response
	The method call metho which in shared libs is inefficient */
	map<unsigned int, SRequestInfo>::iterator itr = m_mapSeqReqInfo.find(nSeq);
	if(itr != m_mapSeqReqInfo.end())
	{
        BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s, find record\n", __FUNCTION__);
		SRequestInfo sReq = itr->second;
        m_szTransferData.pReq = &sReq;
		sReq.nCode = m_pTransfer->DoTransferResponseMsg(sReq.strUrlIdentify, dwServiceId, dwMsgId, pBuffer,nLen, (void*)m_szTransferData.arrTransferData, &m_nLen);
		if(0 == sReq.nCode)//transfer the packet sucessfully
		{
            BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s, transfer the packet sucessfully\n", __FUNCTION__);
			if (VER2SO_PRECODE == *(unsigned*)m_szTransferData.arrTransferData)
			{
                            BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s, VER2SO_PRECODE\n", __FUNCTION__);
				unsigned tlvLen = *(unsigned*)(m_szTransferData.arrTransferData+4);
				sReq.strResponse.assign((char*)m_szTransferData.arrTransferData+8+tlvLen, m_nLen-8-tlvLen);
				CSapTLVBodyDecoder tlvDecoder(m_szTransferData.arrTransferData+8, tlvLen);
				tlvDecoder.GetValue(TRANSEX_CODE_HTTPCODE, (unsigned*)&sReq.nHttpCode);
				tlvDecoder.GetValue(TRANSEX_CODE_LOCATION, sReq.strLocationAddr);
				tlvDecoder.GetValues(TRANSEX_CODE_COOKIE, sReq.vecSetCookie);
				tlvDecoder.GetValue(TRANSEX_CODE_CONTTYPE, sReq.strContentType);
			}
			else
			{
				sReq.strResponse.assign((char*)m_szTransferData.arrTransferData, m_nLen);
			}
		}
		else if(0 < sReq.nCode)
		{
                    BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s, fail to transfer\n", __FUNCTION__);
			sReq.nHttpCode = sReq.nCode;
			sReq.nCode = 0;

			char *szSplit = strstr(m_szTransferData.arrTransferData, "\r\n\r\n");
			if(szSplit!=NULL && szSplit>m_szTransferData.arrTransferData && szSplit<m_szTransferData.arrTransferData+m_nLen)
			{
				sReq.strHeaders.assign((char*)m_szTransferData.arrTransferData,szSplit+2-m_szTransferData.arrTransferData);
				sReq.strResponse.assign((char*)(szSplit+4),m_szTransferData.arrTransferData+m_nLen-szSplit-4);
			}
			else
			{
				sReq.strResponse.assign((char*)m_szTransferData.arrTransferData, m_nLen);
			}

		}

		sReq.strSosIp = strRemoteIp;
		sReq.dwSosPort = dwRemotePort;
		RemoveHistory(sReq);

		m_mapSeqReqInfo.erase(itr);
		/*must erase the itr before DoSendResponse because the OnHttpPeerClose will be called if the
		http connection is non-persistant and it will erase the map in DoHttpClose function*/
                SendHttpResponse(sReq);
		RecordeHttpRequest(sReq);
		//m_pHpsContainer->DoSendResponse(sReq);
	}
	else
	{
		/*the client close the connect before receive response
			do nothing*/
		/*nCode = ERROR_CLIENT_CLOSE_BEFORE_RESPONSE;
			remove the request info in DoHttpPeerClose function*/
	}
}

void CHpsSessionManager::OnPeerClose(unsigned int dwId)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s, dwId[%u]\n",__FUNCTION__, dwId);
	map<unsigned int, SRequestInfo>::iterator itr=m_mapSeqReqInfo.begin();
	while(itr!=m_mapSeqReqInfo.end())
	{
		SRequestInfo sReq = itr->second;
		if(sReq.dwSosId==dwId)
		{
			sReq.nCode=ERROR_SOS_DISCONNECTED;
			RemoveHistory(sReq);
			m_mapSeqReqInfo.erase(itr++);
			SendHttpResponse(sReq);
			RecordeHttpRequest(sReq);
		}
		else
		{
			itr++;
		}

	}
}

int CHpsSessionManager::SendHttpResponse(SRequestInfo &sReq)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s\n",__FUNCTION__);
	if(sReq.nCode != 0 && sReq.nCode != ERROR_BAD_REQUEST_INNER)
	{
		sReq.strResponse = m_oHttpReqConfigManager.GetResponseString(sReq.nServiceId,sReq.nMsgId,sReq.nCode,sReq.strRemoteIp,sReq.strUrlIdentify);
	}

	if (!sReq.strFnCallback.empty())
	{
		sReq.strResponse = sReq.strFnCallback + "(" + sReq.strResponse + ")";
		sReq.strContentType = "application/javascript";
	}

	string strContentType;
	m_oHttpReqConfigManager.GetResponseOption(sReq.nServiceId,sReq.nMsgId,sReq.strUrlIdentify,
		 sReq.strResCharSet, strContentType);
	if (sReq.strContentType.empty())
		sReq.strContentType = strContentType;
	m_pHpsContainer->DoSendResponse(sReq);

	return 0;
}

int CHpsSessionManager::SendOsapRequest(const void* pBuffer, int nLength)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s...\n",__FUNCTION__);
	CSosSessionContainer* pSosContainer;
	map<unsigned int, CSosSessionContainer*>::iterator itermap = m_mapSosSessionContainerByServiceId.find(SAP_SMS_SERVICE_ID);
	if(itermap != m_mapSosSessionContainerByServiceId.end())
	{
		int nFactPri = 0;
		pSosContainer = itermap->second;
		if(0 != pSosContainer->DoSendMessage(pBuffer, nLength, NULL,0,0,&nFactPri))
		{
			return ERROR_SOS_CAN_NOT_REACH;
		}
	}
	else
	{
		return ERROR_NO_SOS_AVAILABLE;
	}
	return 0;
}

void CHpsSessionManager::OnUpdataUserInfo(const string& strUserName, OsapUserInfo* pUserInfo)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s..\n", __FUNCTION__);
	m_oIoService.post(boost::bind(&CHpsSessionManager::DoUpdataUserInfo,this,strUserName,pUserInfo));
}

void CHpsSessionManager::DoUpdataUserInfo(const string& strUserName, OsapUserInfo* pUserInfo)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s..\n", __FUNCTION__);
	m_osapManager.UpdataUserInfo(strUserName, pUserInfo);
}

void CHpsSessionManager::OnUpdataOsapUserTime(const string& strUserName, const timeval_a& sTime)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s..\n", __FUNCTION__);
	m_oIoService.post(boost::bind(&CHpsSessionManager::DoUpdataOsapUserTime,this,strUserName,sTime));
}

void CHpsSessionManager::DoUpdataOsapUserTime(const string& strUserName, const timeval_a& sTime)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s..\n", __FUNCTION__);
	m_osapManager.UpdataUserReqeustTime(strUserName, sTime);
}

void CHpsSessionManager::OnClearOsapCatch(const string& strMerchant)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s..\n", __FUNCTION__);
	m_oIoService.post(boost::bind(&CHpsSessionManager::DoClearOsapCatch,this,strMerchant));
}
void CHpsSessionManager::DoClearOsapCatch(const string& strMerchant)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s..\n", __FUNCTION__);
	m_osapManager.ClearCatch(strMerchant);
}

void CHpsSessionManager::OnUpdateIpLimit(const map<string, string> & mapIpLimits)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s..\n", __FUNCTION__);
	m_oIoService.post(boost::bind(&CHpsSessionManager::DoUpdateIpLimit,this,mapIpLimits));
}
void CHpsSessionManager::DoUpdateIpLimit(const map<string, string> & mapIpLimits)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s..\n", __FUNCTION__);
	m_ipLimitCheckor.UpdateIps(mapIpLimits);
}

void CHpsSessionManager::OnReceiveAvenueRequest(unsigned dwSosContainerId,
					unsigned int dwSosSessionId, unsigned int dwConnId,
					const void *pBuffer, int nLen,
					const string &strRemoteIp,unsigned int dwRemotePort)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s, containerId[%u], sosSessionId[%u], connId[%u], remoteIp[%s], remotePort[%u], bufferLen[%d]\n",__FUNCTION__,dwSosContainerId,dwSosSessionId, dwConnId,strRemoteIp.c_str(),dwRemotePort,nLen);

	SAvenueRequestInfo sAvenueReqInfo;

	gettimeofday_a(&(sAvenueReqInfo.tStart),0);

	sAvenueReqInfo.dwSosSessionContainerId = dwSosContainerId;
	sAvenueReqInfo.dwSosSessionId = dwSosSessionId;
	sAvenueReqInfo.dwSosConnectionId = dwConnId;
	sAvenueReqInfo.strSosIp = strRemoteIp;
	sAvenueReqInfo.dwSosPort = dwRemotePort;

	CSapDecoder oSapDecoder(pBuffer, nLen);
	sAvenueReqInfo.dwServiceId = oSapDecoder.GetServiceId();
	sAvenueReqInfo.dwMsgId = oSapDecoder.GetMsgId();
	sAvenueReqInfo.dwAvenueSequence= oSapDecoder.GetSequence();

	if (oSapDecoder.GetServiceId() != SAP_SLS_SERVICE_ID)
	{
		CSapEncoder oSapEncoder(SAP_PACKET_RESPONSE, oSapDecoder.GetServiceId(), oSapDecoder.GetMsgId(), ERROR_SOS_REQUEST_NOT_SUPPORT);
		oSapEncoder.SetSequence(oSapDecoder.GetSequence());
		SendAvenueResponse(sAvenueReqInfo,oSapEncoder.GetBuffer(),oSapEncoder.GetLen());
		return;
	}

	GetAsyncLogModule()->OnReceiveAvenueRequest(sAvenueReqInfo.dwServiceId,sAvenueReqInfo.dwMsgId);

	char *exheader;
	int exheader_len;
	oSapDecoder.GetExtHeader((void**)&exheader, &exheader_len);
	CSapTLVBodyDecoder exDecoder(exheader,exheader_len);
	exDecoder.GetValue(SAP_HEADEX_CODE_USER, sAvenueReqInfo.strUsername);

	OsapUserInfo* pUserInfo = NULL;
	if (!sAvenueReqInfo.strUsername.empty())
	{
		int nCode = m_osapManager.GetUserInfo(sAvenueReqInfo, pBuffer, nLen, &pUserInfo);
		if (nCode > 0)
		{
			return; //waiting for osap response
		}
	}

	DoSendHttpRequest(sAvenueReqInfo, pBuffer, nLen, pUserInfo);

}

void CHpsSessionManager::DoSendHttpRequest( SAvenueRequestInfo &sAvenueReqInfo,
						const void * pBuffer, int nLen, OsapUserInfo* pUserInfo)
{
	CSapTLVBodyEncoder exEncoder;
	exEncoder.SetValue(HTTPTRAN_CODE_USERNAME, sAvenueReqInfo.strUsername);
	if (pUserInfo != NULL)
	{
		timeval_a now;
		gettimeofday_a(&now,NULL);
		for (vector<OsapPassword>::const_iterator iter = pUserInfo->vecPassword.begin();
			iter != pUserInfo->vecPassword.end(); ++iter)
		{
			if (now.tv_sec >= iter->nBeginTime && now.tv_sec <= iter->nEndTime)
			{
				exEncoder.SetValue(HTTPTRAN_CODE_USERKEY, iter->strKey);
				break;
			}
		}
		for (vector<OsapKeyValue>::iterator iter = pUserInfo->vecKeyValue.begin();
			iter != pUserInfo->vecKeyValue.end(); ++iter)
		{
			exEncoder.SetValue(iter->nKey, iter->strValue);
		}
	}
	sAvenueReqInfo.nAvenueRetCode = m_pTransfer->DoTransferRequestAvenue2Http(sAvenueReqInfo.dwServiceId,
		sAvenueReqInfo.dwMsgId, pBuffer, nLen,sAvenueReqInfo.strSosIp.c_str(),sAvenueReqInfo.dwSosPort,sAvenueReqInfo.strHttpUrl,
		(void*)m_szTransferData.arrTransferData, &m_nLen, sAvenueReqInfo.strHttpMethod,exEncoder.GetBuffer(), exEncoder.GetLen());
	if(0 != sAvenueReqInfo.nAvenueRetCode)
	{
		AvenueRequestFail(sAvenueReqInfo);
	}
	else
	{
		sAvenueReqInfo.strHttpRequestBody.assign((char*)m_szTransferData.arrTransferData, m_nLen);
		BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s, containerId[%u], sosSessionId[%u], connId[%u], remoteIp[%s], remotePort[%u], url[%s], body[%s]\n",
			__FUNCTION__,sAvenueReqInfo.dwSosSessionContainerId,sAvenueReqInfo.dwSosSessionId,
			sAvenueReqInfo.dwSosConnectionId,sAvenueReqInfo.strSosIp.c_str(),sAvenueReqInfo.dwSosPort,sAvenueReqInfo.strHttpUrl.c_str(),sAvenueReqInfo.strHttpRequestBody.c_str());

		m_pHttpClientContainer->SendHttpRequest(sAvenueReqInfo);
	}
}

void CHpsSessionManager::OnReceiveHttpResponse(SAvenueRequestInfo &sAvenueReqInfo)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s, serviceId[%u], msgId[%u], avenueCode[%d], httpCode[%d], httpResponse[%s]\n",__FUNCTION__,
			sAvenueReqInfo.dwServiceId, sAvenueReqInfo.dwMsgId, sAvenueReqInfo.nAvenueRetCode,sAvenueReqInfo.nHttpCode,sAvenueReqInfo.strHttpResponse.c_str());
	if(0 != sAvenueReqInfo.nAvenueRetCode)
	{
		AvenueRequestFail(sAvenueReqInfo);
		return;
	}

	sAvenueReqInfo.nAvenueRetCode = m_pTransfer->DoTransferResponseHttp2Avenue(sAvenueReqInfo.dwServiceId,
		sAvenueReqInfo.dwMsgId,sAvenueReqInfo.dwAvenueSequence,sAvenueReqInfo.nAvenueRetCode,sAvenueReqInfo.nHttpCode,sAvenueReqInfo.strHttpResponse,
    	(void*)m_szTransferData.arrTransferData, &m_nLen);

	if(0 != sAvenueReqInfo.nAvenueRetCode)
	{
		AvenueRequestFail(sAvenueReqInfo);
		return;
	}

	SendAvenueResponse(sAvenueReqInfo,m_szTransferData.arrTransferData,m_nLen);
}

void CHpsSessionManager::OnReceiveServiceResponse(const void *pBuffer, int nLen)
{
    BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s, pBuffer[%p], nLen[%d]\n", __FUNCTION__, pBuffer, nLen);
    void *pBufferCopy = new char[nLen];
    memcpy(pBufferCopy, pBuffer, nLen);
    m_oIoService.post(boost::bind(&CHpsSessionManager::DoReceiveServiceResponse, this, pBufferCopy, nLen));
}

void CHpsSessionManager::DoReceiveServiceResponse(const void *pBuffer, int nLen)
{
    BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s, pBuffer[%p], nLen[%d]\n", __FUNCTION__, pBuffer, nLen);
    OnReceiveSosAvenueResponse(0, pBuffer, nLen, "127.0.0.1", 0);
    delete [](const char *)pBuffer;
}

void CHpsSessionManager::AvenueRequestFail(SAvenueRequestInfo &sAvenueReqInfo)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s, serviceId[%u], msgId[%u], avenueCode[%d]\n",__FUNCTION__,sAvenueReqInfo.dwServiceId, sAvenueReqInfo.dwMsgId, sAvenueReqInfo.nAvenueRetCode);
	CSapEncoder oSapEncoder(SAP_PACKET_RESPONSE, sAvenueReqInfo.dwServiceId, sAvenueReqInfo.dwMsgId, sAvenueReqInfo.nAvenueRetCode);
	oSapEncoder.SetSequence(sAvenueReqInfo.dwAvenueSequence);
	SendAvenueResponse(sAvenueReqInfo,oSapEncoder.GetBuffer(),oSapEncoder.GetLen());
}

int CHpsSessionManager::SendAvenueResponse(SAvenueRequestInfo &sAvenueReqInfo, const void *pBuffer, int nLen)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s, serviceId[%u], msgId[%u], avenueCode[%d]\n",__FUNCTION__,sAvenueReqInfo.dwServiceId, sAvenueReqInfo.dwMsgId, sAvenueReqInfo.nAvenueRetCode);

	RecordAvenueRequest(sAvenueReqInfo);

	map<unsigned int, CSosSessionContainer*>::iterator itr = m_mapSosSessionContainerByContainerId.find(sAvenueReqInfo.dwSosSessionContainerId);
	if(itr != m_mapSosSessionContainerByContainerId.end())
	{
		CSosSessionContainer* pContainer = itr->second;
		sAvenueReqInfo.nAvenueRetCode = pContainer->DoSendAvenueResponse(sAvenueReqInfo.dwSosSessionId,sAvenueReqInfo.dwSosConnectionId,pBuffer,nLen);
		if(sAvenueReqInfo.nAvenueRetCode != 0)
		{
			BS_XLOG(XLOG_ERROR,"CHpsSessionManager::%s, DoSendAvenueResponse fail. containerId[%u], sosSessionId[%u], connId[%u], remoteIp[%s], remotePort[%u], bufferLen[%d], ret[%d]\n",	__FUNCTION__,
				sAvenueReqInfo.dwSosSessionContainerId,sAvenueReqInfo.dwSosSessionId,sAvenueReqInfo.dwSosConnectionId,sAvenueReqInfo.strSosIp.c_str(),sAvenueReqInfo.dwSosPort,nLen,sAvenueReqInfo.nAvenueRetCode);
		}
	}
	else
	{
		BS_XLOG(XLOG_ERROR,"CHpsSessionManager::%s, DoSendAvenueResponse fail. no containerId. containerId[%u], sosSessionId[%u], connId[%u], remoteIp[%s], remotePort[%u], bufferLen[%d]\n", __FUNCTION__,
			sAvenueReqInfo.dwSosSessionContainerId,sAvenueReqInfo.dwSosSessionId,sAvenueReqInfo.dwSosConnectionId,sAvenueReqInfo.strSosIp.c_str(),sAvenueReqInfo.dwSosPort,nLen);
	}

	return 0;
}

void CHpsSessionManager::AddHistoryRequest(SRequestInfo &sReq)
{
	string strTimeoutParam;
	unsigned nTimeout = REQUEST_TIME_INTERVAL;
	if (m_oHttpReqConfigManager.FindTimeoutConfig(sReq.strUrlIdentify, strTimeoutParam, nTimeout))
	{
		string strTimeOutType = sdo::hps::CHpsRequestMsg::GetValueByKey(sReq.strUriAttribute, strTimeoutParam);
		if (strTimeoutParam == "1")
		{
			nTimeout = 500;
		}
		else if (strTimeoutParam == "2")
		{
			nTimeout = 3000;
		}
	}
	struct timeval_a interval;
	interval.tv_sec = nTimeout / 1000;
	interval.tv_usec = (nTimeout % 1000) * 1000;

    timeradd(&(sReq.tStart),&interval,&(sReq.tEnd));

    m_mapEndtimeReqInfo.insert(MMHistoryTimers::value_type(sReq.tEnd,sReq));
}

void CHpsSessionManager::RemoveHistory(SRequestInfo &sReq)
{
	std::pair<MMHistoryTimers::iterator, MMHistoryTimers::iterator> itr_pair = m_mapEndtimeReqInfo.equal_range(sReq.tEnd);
    MMHistoryTimers::iterator itr;
    for(itr=itr_pair.first; itr!=itr_pair.second; ++itr)
    {
        SRequestInfo & oQueryRequest = itr->second;
        if(oQueryRequest.dwSequence == sReq.dwSequence)
        {
            m_mapEndtimeReqInfo.erase(itr);
            break;
        }
    }
}

void CHpsSessionManager::DetectRequestTimerList()
{
    struct timeval_a now;
	gettimeofday_a(&now,0);
    while(!m_mapEndtimeReqInfo.empty())
    {
        if((m_mapEndtimeReqInfo.begin()->first)<now)
        {
            SRequestInfo & oRequest = m_mapEndtimeReqInfo.begin()->second;
            oRequest.nCode = ERROR_SOS_RESPONSE_TIMEOUT;
			oRequest.strSosIp = "";
			oRequest.dwSosPort = 0;
			SendHttpResponse(oRequest);
			//m_pHpsContainer->DoSendResponse(oRequest);
			RecordeHttpRequest(oRequest);

	    	m_mapSeqReqInfo.erase(oRequest.dwSequence);
            m_mapEndtimeReqInfo.erase(m_mapEndtimeReqInfo.begin());
        }
        else
        {
            break;
        }
    }
	m_osapManager.DetectOsapRequestTimout(now);
}

void CHpsSessionManager::GetSelfCheck(bool &isRun)
{
	isRun = m_isAlive;
	m_isAlive = false;
}

void CHpsSessionManager::DoSelfCheck()
{
	m_isAlive = true;
}

void CHpsSessionManager::DoReportSosStatusCheckData()
{
	if(m_isAlive)
	{
		SSosStatusData sData;
		map<unsigned int, CSosSessionContainer*>::iterator iter;
		for(iter = m_mapSosSessionContainerByContainerId.begin(); iter != m_mapSosSessionContainerByContainerId.end(); ++iter)
		{
			CSosSessionContainer* pContainer = iter->second;
			SSosStatusData sTemp;
			pContainer->GetSosStatusData(sTemp);
			sData = sData + sTemp;
		}

		GetAsyncLogModule()->OnReportSosStatus(sData);

		unsigned int dwNum = m_pHttpClientContainer->GetCurrentRequestNum();
		GetAsyncLogModule()->OnReportNoHttpResNum(m_nThreadId, dwNum);
	}
}

void CHpsSessionManager::RecordeHttpRequest(SRequestInfo & sReq)
{
	struct timeval_a now,interval;
	gettimeofday_a(&now,0);
	timersub(&now,&(sReq.tStart),&interval);

	sReq.fSpentTime = (static_cast<float>(interval.tv_sec))*1000 + (static_cast<float>(interval.tv_usec))/1000;

	GetAsyncLogModule()->OnDetailLog(sReq);
}

void CHpsSessionManager::RecordAvenueRequest(SAvenueRequestInfo &sAvenueReqInfo)
{
	BS_XLOG(XLOG_DEBUG,"CHpsSessionManager::%s, serviceId[%u], msgId[%u], avenueCode[%d]\n",__FUNCTION__,sAvenueReqInfo.dwServiceId, sAvenueReqInfo.dwMsgId, sAvenueReqInfo.nAvenueRetCode);
	struct timeval_a now,interval;
	gettimeofday_a(&now,0);
	timersub(&now,&(sAvenueReqInfo.tStart),&interval);

	sAvenueReqInfo.fAllSpentTime = (static_cast<float>(interval.tv_sec))*1000 + (static_cast<float>(interval.tv_usec))/1000;

	GetAsyncLogModule()->OnDetailAvenueRequestLog(sAvenueReqInfo);
}

string CHpsSessionManager::GetPluginSoInfo()
{
	if(m_pTransfer)
		return m_pTransfer->GetPluginSoInfo();
	else
		return "";
}

void CHpsSessionManager::GetUrlMapping(vector<string> &vecUrlMappin)
{
	vecUrlMappin = m_vecUrlMappingForCheck;
}

void CHpsSessionManager::Dump()
{
	BS_XLOG(XLOG_NOTICE,"===========CHpsSessionManager::%s======sm_dwSeq[%u]======\n",__FUNCTION__,m_dwSeq);

	BS_XLOG(XLOG_NOTICE,"\n");
	BS_XLOG(XLOG_NOTICE,"   m_mapSeqReqInfo.size()[ %u ], <dwSequence, addr>:\n", m_mapSeqReqInfo.size());
	map<unsigned int, SRequestInfo>::iterator iter2;
	for(iter2 = m_mapSeqReqInfo.begin(); iter2 != m_mapSeqReqInfo.end(); ++iter2)
	{
		BS_XLOG(XLOG_NOTICE,"	   ---<%u, %s:%u>\n", iter2->first, iter2->second.strRemoteIp.c_str(), iter2->second.dwRemotePort);
	}

	BS_XLOG(XLOG_NOTICE,"\n");
	//BS_XLOG(XLOG_NOTICE,"   m_multimapIdSeq.size()[ %d ], <id, dwSequence>:\n", m_multimapIdSeq.size());
	//multimap<unsigned int, unsigned int >::const_iterator iter3;
	//for(iter3 = m_multimapIdSeq.begin(); iter3 != m_multimapIdSeq.end(); ++iter3)
	//{
	//	BS_XLOG(XLOG_NOTICE,"	  ---id[%u], seq[ %u ]\n", iter3->first, iter3->second);
	//}

	BS_XLOG(XLOG_NOTICE,"\n");
	BS_XLOG(XLOG_NOTICE,"   m_mapEndtimeReqInfo.size()[ %d ], <time, dwSequence>:\n", m_mapEndtimeReqInfo.size());
	MMHistoryTimers::iterator itrTime;
	for(itrTime = m_mapEndtimeReqInfo.begin(); itrTime != m_mapEndtimeReqInfo.end(); ++itrTime)
	{
		BS_XLOG(XLOG_NOTICE,"	  ---time[ %u.%u ], seq[ %u ]\n", itrTime->first.tv_sec,
			itrTime->first.tv_usec, itrTime->second.dwSequence);
	}

	BS_XLOG(XLOG_NOTICE,"\n");
	BS_XLOG(XLOG_NOTICE,"   m_mapSosSessionContainerByServiceId.size()[ %d ],<serviceId, sosContainerId>:\n", m_mapSosSessionContainerByServiceId.size());
	map<unsigned int, CSosSessionContainer*>::iterator iter4;
	for(iter4 = m_mapSosSessionContainerByServiceId.begin(); iter4 != m_mapSosSessionContainerByServiceId.end(); ++iter4)
	{
		BS_XLOG(XLOG_NOTICE,"      ---serviceId[%u],sosContainerId[%d]\n", iter4->first,iter4->second->GetContainerId());
	}

	BS_XLOG(XLOG_NOTICE,"\n");
	BS_XLOG(XLOG_NOTICE,"   +m_mapSosSessionContainerByContainerId.size[ %d ]\n", m_mapSosSessionContainerByContainerId.size());
	map<unsigned int, CSosSessionContainer*>::iterator iter5 ;
	for(iter5 = m_mapSosSessionContainerByContainerId.begin(); iter5 != m_mapSosSessionContainerByContainerId.end(); ++iter5)
	{
		BS_XLOG(XLOG_NOTICE,"     -m_mapSosSessionContainerByContainerId[%u]\n", iter5->first);
		(iter5->second)->Dump();
		BS_XLOG(XLOG_NOTICE,"\n");
	}

	BS_XLOG(XLOG_NOTICE,"\n");
	m_pHpsContainer->Dump();

	m_pTransfer->Dump();

	m_oHttpReqConfigManager.Dump();

	m_pHttpClientContainer->Dump();
}

max_stars_repo_name: shrewdlin/BPE

max_issues_repo_name: shrewdlin/BPE

max_forks_repo_name: shrewdlin/BPE


text:
//==================================================================================================
/*!
  @file

  @copyright 2016 NumScale SAS

  Distributed under the Boost Software License, Version 1.0.
  (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)

**/
//==================================================================================================
#ifndef BOOST_SIMD_REDUCTION_HPP_INCLUDED
#define BOOST_SIMD_REDUCTION_HPP_INCLUDED

namespace boost {
namespace simd {
/*!
  @ingroup group-functions
  @defgroup group-reduction Reduction Functions

  These functions provide algorithms for in-register reduction and prefix-scan operations.

  They are usually only defined for **SIMD** types but may, in some
  cases such as @ref sum or  @ref prod, have a valid scalar semantic.

    <center>
|                      |                         |                    |                            |
|:--------------------:|:-----------------------:|:------------------:|:--------------------------:|
| @ref all             | @ref any                | @ref compare_equal | @ref compare_greater_equal |
| @ref compare_greater | @ref compare_less_equal | @ref compare_less  | @ref compare_not_equal     |
| @ref cummax          | @ref cummin             | @ref cumprod       | @ref cumsum                |
| @ref dot             | @ref hmsb               | @ref isincluded    | @ref isincluded_c          |
| @ref maximum         | @ref minimum            | @ref nbtrue        | @ref none                  |
| @ref prod            | @ref sum                |                    |                            |
     </center>
**/
}
}

#include <boost/simd/function/all.hpp>
#include <boost/simd/function/any.hpp>
#include <boost/simd/function/compare_equal.hpp>
#include <boost/simd/function/compare_greater.hpp>
#include <boost/simd/function/compare_greater_equal.hpp>
#include <boost/simd/function/compare_less.hpp>
#include <boost/simd/function/compare_less_equal.hpp>
#include <boost/simd/function/compare_not_equal.hpp>
#include <boost/simd/function/cummax.hpp>
#include <boost/simd/function/cummin.hpp>
#include <boost/simd/function/cumprod.hpp>
#include <boost/simd/function/cumsum.hpp>
#include <boost/simd/function/dot.hpp>
#include <boost/simd/function/hmsb.hpp>
#include <boost/simd/function/isincluded.hpp>
#include <boost/simd/function/isincluded_c.hpp>
#include <boost/simd/function/maximum.hpp>
#include <boost/simd/function/minimum.hpp>
#include <boost/simd/function/nbtrue.hpp>
#include <boost/simd/function/none.hpp>
#include <boost/simd/function/prod.hpp>
#include <boost/simd/function/sum.hpp>

#endif

ext: hpp

max_stars_repo_name: SylvainCorlay/pythran

max_issues_repo_name: SylvainCorlay/pythran

max_forks_repo_name: SylvainCorlay/pythran


text:
#ifndef TUBE_SEGMENTATION
#define TUBE_SEGMENTATION

#define CL_USE_DEPRECATED_OPENCL_1_1_APIS

#include "OpenCLManager.hpp"
#include <iostream>
#include <string>
#include <set>
#ifdef CPP11
#include <unordered_map>
using std::unordered_map;
#else
#include <boost/unordered_map.hpp>
using boost::unordered_map;
#endif
#include "parameters.hpp"
#include "SIPL/Exceptions.hpp"
#include "inputOutput.hpp"

#include "SIPL/Types.hpp"

typedef struct TubeSegmentation {
    float *Fx, *Fy, *Fz; // The GVF vector field
    float *FxSmall, *FySmall, *FzSmall; // The GVF vector field
    float *TDF; // The TDF response
    float *radius;
    char *centerline;
    char *segmentation;
    float *intensity;
} TubeSegmentation;

/*
 * For debugging.
 */
void print(paramList parameters);

cl::Image3D readDatasetAndTransfer(OpenCL &ocl, std::string, paramList &parameters, SIPL::int3 *, TSFOutput *);

void runCircleFittingAndRidgeTraversal(OpenCL *, cl::Image3D *dataset, SIPL::int3 * size, paramList &parameters, TSFOutput *);

void runCircleFittingAndNewCenterlineAlg(OpenCL *, cl::Image3D *dataset, SIPL::int3 * size, paramList &parameters, TSFOutput *, std::string kernel_dir, std::string oul_dir);

void runCircleFittingAndTest(OpenCL *, cl::Image3D *dataset, SIPL::int3 * size, paramList &parameters, TSFOutput *);

TSFOutput * run(std::string filename, paramList &parameters, std::string kernel_dir, std::string oul_dir);

#endif


text:
#pragma once

#pragma warning(push)
#pragma warning(disable: 4819)
#include <boost/geometry/core/access.hpp>

#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/geometries/polygon.hpp>

#include <boost/geometry/geometries/multi_point.hpp>
#include <boost/geometry/geometries/multi_polygon.hpp>

#include <boost/geometry/strategies/strategies.hpp>
#pragma warning(pop)

namespace ompu { namespace geo {

namespace bg = boost::geometry;

using Point = bg::model::d2::point_xy<double>;
using MultiPoint = bg::model::multi_point<Point>;

using Polygon = bg::model::polygon<Point>;
using MultiPolygon = bg::model::multi_polygon<Polygon>;
using Box = bg::model::box<Point>;

struct Size
{
    unsigned w, h;
};

struct CircleSize
{
    bg::strategy::buffer::distance_symmetric<double>
    radius;
};

}} // ompu

max_stars_repo_name: ompu/ompu

max_issues_repo_name: ompu/ompu

max_forks_repo_name: ompu/ompu


text:
/**
 * Copyright Soramitsu Co., Ltd. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

#include "ametsuchi/impl/postgres_indexer.hpp"

#include <soci/soci.h>
#include <boost/format.hpp>
#include "cryptography/hash.hpp"

using namespace iroha::ametsuchi;
using namespace shared_model::interface::types;

PostgresIndexer::PostgresIndexer(soci::session &sql) : sql_(sql) {}

void PostgresIndexer::txHashPosition(const HashType &hash,
                                     TxPosition position) {
  tx_hash_position_.hash.emplace_back(hash.hex());
  tx_hash_position_.height.emplace_back(position.height);
  tx_hash_position_.index.emplace_back(position.index);
}

void PostgresIndexer::txHashStatus(const HashType &tx_hash, bool is_committed) {
  tx_hash_status_.hash.emplace_back(tx_hash.hex());
  tx_hash_status_.status.emplace_back(is_committed ? "TRUE" : "FALSE");
}

void PostgresIndexer::committedTxHash(const HashType &committed_tx_hash) {
  txHashStatus(committed_tx_hash, true);
}

void PostgresIndexer::rejectedTxHash(const HashType &rejected_tx_hash) {
  txHashStatus(rejected_tx_hash, false);
}

void PostgresIndexer::txPositionByCreator(const AccountIdType creator,
                                          TxPosition position) {
  tx_position_by_creator_.creator.emplace_back(creator);
  tx_position_by_creator_.height.emplace_back(position.height);
  tx_position_by_creator_.index.emplace_back(position.index);
}

void PostgresIndexer::accountAssetTxPosition(const AccountIdType &account_id,
                                             const AssetIdType &asset_id,
                                             TxPosition position) {
  account_asset_tx_position_.account_id.emplace_back(account_id);
  account_asset_tx_position_.asset_id.emplace_back(asset_id);
  account_asset_tx_position_.height.emplace_back(position.height);
  account_asset_tx_position_.index.emplace_back(position.index);
}

iroha::expected::Result<void, std::string> PostgresIndexer::flush() {
  try {
    assert(tx_hash_position_.hash.size() == tx_hash_position_.height.size());
    assert(tx_hash_position_.hash.size() == tx_hash_position_.index.size());
    if (not tx_hash_position_.hash.empty()) {
      sql_ << "INSERT INTO position_by_hash"
              "(hash, height, index) VALUES "
              "(:hash, :height, :index);",
          soci::use(tx_hash_position_.hash),
          soci::use(tx_hash_position_.height),
          soci::use(tx_hash_position_.index);

      tx_hash_position_.hash.clear();
      tx_hash_position_.height.clear();
      tx_hash_position_.index.clear();
    }

    assert(tx_hash_status_.hash.size() == tx_hash_status_.status.size());
    if (not tx_hash_status_.hash.empty()) {
      sql_ << "INSERT INTO tx_status_by_hash"
              "(hash, status) VALUES "
              "(:hash, :status);",
          soci::use(tx_hash_status_.hash), soci::use(tx_hash_status_.status);

      tx_hash_status_.hash.clear();
      tx_hash_status_.status.clear();
    }

    assert(tx_position_by_creator_.creator.size()
           == tx_position_by_creator_.height.size());
    assert(tx_position_by_creator_.creator.size()
           == tx_position_by_creator_.index.size());
    if (not tx_position_by_creator_.creator.empty()) {
      sql_ << "INSERT INTO tx_position_by_creator"
              "(creator_id, height, index) VALUES "
              "(:creator_id, :height, :index);",
          soci::use(tx_position_by_creator_.creator),
          soci::use(tx_position_by_creator_.height),
          soci::use(tx_position_by_creator_.index);

      tx_position_by_creator_.creator.clear();
      tx_position_by_creator_.height.clear();
      tx_position_by_creator_.index.clear();
    }

    assert(account_asset_tx_position_.account_id.size()
           == account_asset_tx_position_.asset_id.size());
    assert(account_asset_tx_position_.account_id.size()
           == account_asset_tx_position_.height.size());
    assert(account_asset_tx_position_.account_id.size()
           == account_asset_tx_position_.index.size());
    if (not account_asset_tx_position_.account_id.empty()) {
      sql_ << "INSERT INTO position_by_account_asset"
              "(account_id, asset_id, height, index) VALUES "
              "(:account_id, :asset_id, :height, :index);",
          soci::use(account_asset_tx_position_.account_id),
          soci::use(account_asset_tx_position_.asset_id),
          soci::use(account_asset_tx_position_.height),
          soci::use(account_asset_tx_position_.index);

      account_asset_tx_position_.account_id.clear();
      account_asset_tx_position_.asset_id.clear();
      account_asset_tx_position_.height.clear();
      account_asset_tx_position_.index.clear();
    }
  } catch (const std::exception &e) {
    return e.what();
  }
  return {};
}

max_stars_repo_name: akshatkarani/iroha

max_issues_repo_name: akshatkarani/iroha

max_forks_repo_name: akshatkarani/iroha