bool FileMgr::add_page( const Page & page , IndexEntry & idx) { MY_LOG_DEBUG("FileMgr::add_page() %ld", page.m_id); MutexGuard guard(m_mutex); m_fileStream[m_lastFileNum].seekp(0, std::ios::end); size_t off1 = m_fileStream[m_lastFileNum].tellp(); m_fileStream[m_lastFileNum] << page; size_t off2 = m_fileStream[m_lastFileNum].tellp(); idx.m_offset = off1; idx.m_length = off2 - off1; std::string hash = page.get_hash(); memcpy( (void *) idx.m_contenthash, (const void*)hash.c_str(), hash.size()); idx.m_filenum = m_lastFileNum; if( !m_fileStream[m_lastFileNum]) { MY_LOG_ERROR("fstream error file num: %d ", m_lastFileNum); return false; } MY_LOG_DEBUG("FileMgr::add_page() pageid=%ld hash=%s", page.m_id, idx.hash_to_hex_string().c_str()); return true; }
void MYPositionDataManager::InitPosition( const std::string &code, char dir, VolumeType volume,int today_buy_volume, int today_sell_volume,int yestoday_long_pos,char ex) { MY_LOG_DEBUG("MYPositionDataManager::InitPosition: %s, dir=%c, volume=%ld", code.c_str(), dir, volume); if (volume != 0){ std::lock_guard < std::mutex > lock(mutex_pos_); PositionDataOfCode::iterator it = pos_datas_.find(code); if (it == pos_datas_.end()){ it = pos_datas_.insert(std::make_pair(code, PositionInfoInEX())).first; } MY_LOG_DEBUG("before InitPosition (%s): long=%d, short=%d", code.c_str(), it->second.long_position, it->second.short_position); it->second.exchange = ex; if (dir == BUSSINESS_DEF::ENTRUST_BUY){ it->second.long_position += volume; it->second.today_buy_volume += today_buy_volume; it->second.today_sell_volume += today_sell_volume; it->second.long_pos_yd += yestoday_long_pos; }else if (dir == BUSSINESS_DEF::ENTRUST_SELL){ it->second.short_position += volume; }else{ MY_LOG_ERROR("MYPositionDataManager::InitPosition goto wrong branch"); } MY_LOG_DEBUG("after InitPosition, pos(%s): l=%d,s=%d", code.c_str(),it->second.long_position, it->second.short_position); } }
void MYPositionDataManager::UpdateOutterPosition(const std::string &code, char dir, char open_close, VolumeType volume,char ex) { MY_LOG_DEBUG("UpdateOutterPosition: %s, dir=%c, oc=%c, volume=%ld", code.c_str(), dir, open_close,volume); if (volume > 0){ std::lock_guard < std::mutex > lock(mutex_pos_); PositionDataOfCode::iterator it = pos_datas_.find(code); if (it == pos_datas_.end()){ it = pos_datas_.insert(std::make_pair(code, PositionInfoInEX())).first; } it->second.exchange = ex; if (dir == BUSSINESS_DEF::ENTRUST_BUY){ it->second.long_position += volume; it->second.today_buy_volume += volume; }else if (dir == BUSSINESS_DEF::ENTRUST_SELL){ it->second.today_sell_volume += volume; it->second.long_position -= volume; } MY_LOG_DEBUG("after UpdateOutterPosition, pos(%s): l=%d,s=%d", code.c_str(),it->second.long_position, it->second.short_position); } }
void Thread::start() { MY_LOG_DEBUG("Thread::start() "); if( !m_starting) { pthread_create( & m_threadid, NULL, run_in_thread, this); m_starting = true; } MY_LOG_DEBUG("thread id = %lu ", m_threadid); }
void PageDB::save_to_file() { MY_LOG_DEBUG("PageDB::save_to_file()"); if( m_update) { m_fileMgr.save_to_file(); m_indexer->save(); m_update =false; MY_LOG_DEBUG("PageDB::save_to_file() sucess!"); } MY_LOG_DEBUG("PageDB::save_to_file() donot need to save!"); }
void PageDB::init_instance() { MY_LOG_DEBUG("PageDB::init()"); MutexGuard guard(m_mutex); if( m_instance == NULL ) { Conf cf = Conf::get_instance(); m_instance = new PageDB( cf["pagedb_file"], cf["pagedbidx_use_file"] ); } MY_LOG_DEBUG("PageDB::init() complete!"); }
bool IndexDB::get_index( IndexEntry & idx) { MY_LOG_DEBUG("IndexDB::get_index()"); MutexGuard guard(m_mutex); char cmd_buf[512]; int cmd_len = sprintf( cmd_buf, "select * from %s where id=%lu ", m_table.c_str(), idx.m_id ); int qret = mysql_real_query(m_conn, cmd_buf, cmd_len ); if( qret ) { MY_LOG_INFO("IndexDB::get_index() mysql_real_query() failed %s", mysql_error(m_conn) ); return false; } else { MYSQL_RES * result = mysql_store_result(m_conn); MY_LOG_DEBUG("IndexDB::get_index() mysql_store_result() result = %p ", result); if( NULL == result) { MY_LOG_INFO("IndexDB::get_index() mysql_store_result() failed %s", mysql_error(m_conn) ); mysql_free_result(result); return false; } else { MY_LOG_DEBUG("IndexDB::get_index() result is not empty"); MYSQL_ROW row; if( (row = mysql_fetch_row(result))) { size_t * row_len = mysql_fetch_lengths(result); idx.m_offset = strtoul(row[1], NULL, 0); idx.m_length = strtoul(row[2], NULL, 0); memcpy((void *)idx.m_contenthash, (const void *)row[3], row_len[3] ); idx.m_filenum = atoi(row[4]); } mysql_free_result(result); } } MY_LOG_DEBUG("IndexDB::get_index() idx.m_id=%lu, idx.m_offset=%lu, idx.m_length=%lu, idx.m_filenum=%d, hash=%s", idx.m_id, idx.m_offset, idx.m_length, idx.m_filenum, idx.hash_to_hex_string().c_str()); return true; }
bool IndexDB::add_index( const IndexEntry & idx) { MY_LOG_DEBUG("IndexDB::add_index() idx.m_id=%lu, idx.m_offset=%lu, idx.m_length=%lu, idx.m_filenum=%d, hash=%s", idx.m_id, idx.m_offset, idx.m_length, idx.m_filenum, idx.hash_to_hex_string().c_str()); MutexGuard guard(m_mutex); const int len = sizeof(IndexEntry) + sizeof(IndexEntry) +2; char cmd_buf[512]; char escape_buf[len]; mysql_real_escape_string(m_conn, escape_buf, (const char *)idx.m_contenthash, sizeof(idx.m_contenthash) ); int cmd_len = sprintf( cmd_buf, "insert into %s values( %lu, %lu, %lu, '%s', %d)", m_table.c_str(), idx.m_id, idx.m_offset, idx.m_length, escape_buf, idx.m_filenum ); int qret = mysql_real_query( m_conn, cmd_buf, cmd_len); if( qret) { MY_LOG_INFO("IndexDB::add_index() insert failed %s", mysql_error(m_conn) ); return false; } else { return true; } }
void MYExchange::SendOrderReturn(const T_OrderReturn * order_rtn, VolumeType matched_volume) { MYExchangeLogUtil::OnOrderReturn(order_rtn, investorid); // update position of model if (matched_volume > 0) { my_exchange_inner_imp_->UpdatePositionForOrder(order_rtn->serial_no, order_rtn->stock_code, order_rtn->direction, matched_volume); } // rollback remain volume if (OrderUtil::IsTerminated(order_rtn->entrust_status)) { my_exchange_inner_imp_->RollBackPendingVolumeForOrder(order_rtn->serial_no, order_rtn->stock_code, order_rtn->direction, order_rtn->volume_remain); } // get position of model T_PositionData pos = my_exchange_inner_imp_->GetPosition(order_rtn->serial_no, order_rtn->stock_code); pos.update_flag = (matched_volume > 0); // send to client if (order_pos_return_handler_) { order_pos_return_handler_(order_rtn, &pos); } MY_LOG_DEBUG("MYExchange::SendOrderReturn, serial_no=%ld, long=%d, short=%d, uflag=%d", order_rtn->serial_no, pos.long_position, pos.short_position, pos.update_flag); }
IndexDB::IndexDB():m_mutex() { MY_LOG_DEBUG("IndexDB::IndexDB()"); const Conf & cf = Conf::get_instance(); m_conn = mysql_init(NULL); if( m_conn == NULL) { MY_LOG_ERROR("IndexDB() mysql_init() %s", mysql_error(m_conn)); } unsigned int port = (unsigned int)atoi( cf["sql_port"].c_str()); m_db = cf["sql_db"]; m_table = cf["sql_table"]; if( mysql_real_connect( m_conn, cf["sql_host"].c_str(), cf["sql_usr"].c_str(), cf["sql_passwd"].c_str(), m_db.c_str(), port, NULL, 0 ) == NULL) { MY_LOG_ERROR("IndexDB() mysql_real_connect() %s ", mysql_error(m_conn)); } std::string qstr = " select max(id) from "; qstr += m_table; if( mysql_real_query( m_conn, qstr.c_str(), qstr.size() ) ) { MY_LOG_ERROR("IndexDB() mysql_real_query() %s : %s ", qstr.c_str(), mysql_error(m_conn)); } MYSQL_RES * result = mysql_store_result( m_conn); if( NULL == result ) { MY_LOG_ERROR("IndexDB() mysql_store_result() %s ", mysql_error(m_conn)); } else { MYSQL_ROW row; while( (row = mysql_fetch_row( result))) { if( row[0] == NULL) { m_count =0; } else { m_count = strtoul( row[0] , NULL, 0) ; } } } mysql_free_result(result); }
void PageDB::shut_down() { MY_LOG_DEBUG("PageDB::shut_down()"); if( m_instance != NULL ) { delete m_instance; m_instance = NULL; } }
void FileMgr::save_to_file() { MY_LOG_DEBUG("FileMgr()::save_to_file()"); MutexGuard guard(m_mutex); for( int i=0; i <= m_lastFileNum ; i++) { m_fileStream[i].flush(); } }
PageDB & PageDB::get_instance() { MY_LOG_DEBUG("PageDB::get_instance()"); if( m_instance == NULL) { init_instance() ; } return *m_instance; }
InetAddress::InetAddress( const std::string &ip, unsigned short port) { MY_LOG_DEBUG("InetAddress::InetAddress() ip: %s, port= %d " ,ip.c_str(), port); memset(&m_addr, 0, sizeof( m_addr)); m_addr.sin_family = AF_INET; m_addr.sin_port = htons(port); if( inet_aton( ip.c_str(), & m_addr.sin_addr )==0) { MY_LOG_ERROR(" ip invalid "); } }
void MYExchange::SendTradeReturn(const T_TradeReturn * trade_rtn) { MYExchangeLogUtil::OnTradeReturn(trade_rtn, investorid); // get position of model T_PositionData pos = my_exchange_inner_imp_->GetPosition(trade_rtn->serial_no, trade_rtn->stock_code); if (trade_pos_return_handler_) trade_pos_return_handler_(trade_rtn, &pos); MY_LOG_DEBUG("MYExchange::SendTradeReturn, serial_no=%ld, long=%d, short=%d, uflag=%d", trade_rtn->serial_no, pos.long_position, pos.short_position, pos.update_flag); }
FileMgr::FileMgr( const std::string & file ) : m_mutex() { MY_LOG_DEBUG("FileMgr() %s", file.c_str()); m_lastFileNum = 0; m_fileStream[ m_lastFileNum ].open( file , std::ios::in | std::ios::out | std::ios::binary ); if( !m_fileStream) { MY_LOG_ERROR("FileMgr::FileMgr() m_fileStream error"); } }
bool FileMgr::get_page( const IndexEntry & idx, Page & page ) { MY_LOG_DEBUG("FileMgr::get_page() %ld", idx.m_id); MutexGuard guard(m_mutex); int num = idx.m_filenum; if( num <= m_lastFileNum && num >=0 ) { std::unique_ptr<char> p( new char [ idx.m_length] ); if( !p ) { MY_LOG_ERROR("unique_ptr"); } m_fileStream[num].clear(); m_fileStream[num].seekg( idx.m_offset , std::ios::beg); m_fileStream[num].read( p.get(), idx.m_length ); if( m_fileStream[num]) { std::string tmp( p.get(), idx.m_length ); Page pg(tmp); std::string hash = pg.get_hash(); if( 0 == memcmp( idx.m_contenthash, hash.c_str(), hash.size() )) { page = pg; return true; } IndexEntry idx2; memcpy((void *)idx.m_contenthash, (const void *)hash.c_str(), hash.size()); std::string hashstr1 = idx.hash_to_hex_string(); std::string hashstr2 = idx2.hash_to_hex_string(); MY_LOG_INFO("FileMgr::get_page() %ld hash not match {hash1:%s hash2:%s}, tmp={%s}\npage.m_content={%s}", idx.m_id, hashstr1.c_str(), hashstr2.c_str(), tmp.c_str(), page.m_content.c_str()); } } MY_LOG_DEBUG("FileMgr::get_page() %lu failed!", idx.m_id); return false; }
bool PageDB::get_page( size_t id , Page & page ) { MY_LOG_DEBUG("PageDB::get_page() id=%lu", id); IndexEntry idx; idx.m_id = id ; if( m_indexer->get_index( idx )) { if( m_fileMgr.get_page( idx, page )) { return true; } } return false; }
bool PageDB::add_page( const Page & page) { MY_LOG_DEBUG("PageDB::add_page() id=%lu", page.m_id); IndexEntry idx; idx.m_id = page.m_id; if( m_fileMgr.add_page( page , idx )) { if( m_indexer->add_index( idx)) { m_update = true; return true; } } return false; }
void MYExchange::SendOrderRespond(const T_OrderRespond * order_rsp, const char *contract, char dir, VolumeType volume_remain) { MYExchangeLogUtil::OnOrderRespond(order_rsp, investorid); if (OrderUtil::IsTerminated(order_rsp->entrust_status)) { my_exchange_inner_imp_->RollBackPendingVolumeForOrder(order_rsp->serial_no, contract, dir, volume_remain); } T_PositionData pos = my_exchange_inner_imp_->GetPosition(order_rsp->serial_no, contract); if (order_pos_respond_handler_) { order_pos_respond_handler_(order_rsp, &pos); } MY_LOG_DEBUG("MYExchange::SendOrderRespond, serial_no=%ld, long=%d, short=%d, uflag=%d", order_rsp->serial_no, pos.long_position, pos.short_position, pos.update_flag); }
/* * @author liujun05([email protected]) * @brief 根据key获得value */ char* my_conf_get_conf_by_key(char* key,MY_CONF_INS* conf) { if(conf->size() == 0) { return NULL; } MY_CONF_INS::iterator iter; for(iter = conf->begin(); iter != conf->end(); iter++) { //如果找到这个key if( strcmp(key, (*iter)->key) == 0 ) { MY_LOG_DEBUG("key is [%s], value is [%s]", key, (*iter)->value); return (*iter)->value; } } return NULL; }
//计算TF,并按从大到小的顺序排序 void Iterm::calculate_tf( double max_tf ) { MY_LOG_DEBUG("Iterm::calculate_tf(): max TF = %lf ", max_tf); for( auto & v : m_itermVec ) { v.second = 0.4 + 0.6*v.second/max_tf; // MY_LOG_DEBUG("Iterm::calculate_tf(): TF %s = %lf ", v.first.c_str(), v.second); } std::sort( m_itermVec.begin(), m_itermVec.end(), []( const std::pair< std::string , double > &lhs, const std::pair< std::string , double > &rhs ) -> bool { return lhs.second > rhs.second; } ); }
void QuoteInterface_TDF::InitOpenSetting(TDF_OPEN_SETTING *settings, const ConfigData &cfg) { if (cfg.Logon_config().quote_provider_addrs.empty()) { MY_LOG_ERROR("TDF - quote provider's address is empty"); return; } // tcp://192.168.60.23:7120 const std::string &addr_cfg = cfg.Logon_config().quote_provider_addrs.front(); std::string ip_port = addr_cfg.substr(6); std::size_t split_pos = ip_port.find(":"); if ((split_pos == std::string::npos) || (split_pos + 1 >= ip_port.length())) { MY_LOG_ERROR("TDF - parse quote provider's address error: %s", addr_cfg.c_str()); } MY_LOG_DEBUG("ip_port: %s", ip_port.c_str()); std::string addr_ip = ip_port.substr(0, split_pos); std::string addr_port = ip_port.substr(split_pos + 1); //MY_LOG_DEBUG("ip: %s, port: %s, user: %s, pw: %s", addr_ip.c_str(), addr_port.c_str(), cfg.Logon_config().account.c_str(), cfg.Logon_config().password.c_str()); //MY_LOG_DEBUG("subscriptions: %s", DeepCopy(FormatSubscribString(cfg.Subscribe_datas()).c_str())); memset(settings, 0, sizeof(TDF_OPEN_SETTING)); strncpy(settings->szIp, addr_ip.c_str(), sizeof(settings->szIp) - 1); strncpy(settings->szPort, addr_port.c_str(), sizeof(settings->szPort) - 1); strncpy(settings->szUser, cfg.Logon_config().account.c_str(), sizeof(settings->szUser) - 1); strncpy(settings->szPwd, cfg.Logon_config().password.c_str(), sizeof(settings->szPwd) - 1); settings->pfnMsgHandler = &QuoteInterface_TDF::QuoteDataHandler; settings->pfnSysMsgNotify = &QuoteInterface_TDF::SystemMsgHandler; settings->szMarkets = DeepCopy("SZ-2;SH-2;"); // subscibe two market settings->szSubScriptions = DeepCopy(FormatSubscribString(cfg.Subscribe_datas()).c_str()); settings->nTime = 0; settings->nTypeFlags = DATA_TYPE_TRANSACTION | DATA_TYPE_ORDER | DATA_TYPE_ORDERQUEUE; }
PageDB::PageDB( const std::string & datafile, const std::string & indexfile): m_update(false), m_fileMgr( datafile) { MY_LOG_DEBUG("PageDB::PageDB()"); const Conf & cf = Conf::get_instance(); if( cf["pagedbidx_use_db"] == "yes") { m_indexer.reset(new IndexDB()); if(!m_indexer) { MY_LOG_ERROR( "PageDB::PageDB() new IndexDB() error "); } } else { m_indexer.reset(new IndexFile( indexfile )); if( !m_indexer) { MY_LOG_ERROR( "PageDB::PageDB() new IndexFile() error "); } } }
size_t IndexDB::generate_id() { MY_LOG_DEBUG("IndexDB::generate_id() id=%lu", m_count); return m_count++; }
size_t PageDB::generate_id() { MY_LOG_DEBUG("PageDB::generate_id()"); return m_indexer->generate_id(); }
QuoteInterface_TDF::~QuoteInterface_TDF() { MY_LOG_DEBUG("TDF_Close"); TDF_Close(g_hTDF); }