Created
June 30, 2017 12:50
-
-
Save sehe/17cd706db4153f5321a43b0b78900339 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <boost/interprocess/managed_shared_memory.hpp> | |
#include <boost/multi_index_container.hpp> | |
#include <boost/multi_index/member.hpp> | |
#include <boost/multi_index/ordered_index.hpp> | |
//#include <boost/multi_index/composite_key.hpp> | |
#include <iostream> | |
namespace bip = boost::interprocess; | |
namespace bmi = boost::multi_index; | |
struct Order_Message { | |
long Timestamp; | |
double Order_Id; | |
int Token; | |
char Order_Type; | |
int Price; | |
int Quantity; | |
}; | |
struct MIOrder_Message /// For Data | |
{ | |
// STREAM_HEADER Global_Header; | |
// char Message_Type; | |
MIOrder_Message(Order_Message _Ord) | |
: Timestamp(_Ord.Timestamp), Order_Id(_Ord.Order_Id), Token(_Ord.Token), | |
Order_Type(_Ord.Order_Type), Price(_Ord.Price), | |
Quantity(_Ord.Quantity) { | |
// std::cout << " Insert data for Token "<< _Ord.Token<<std::endl; | |
} | |
long Timestamp; | |
double Order_Id; | |
int Token; | |
char Order_Type; | |
int Price; | |
int Quantity; | |
}; | |
/// Order_Message and MIOrder_Message are almost identical | |
typedef bip::allocator<MIOrder_Message, | |
bip::managed_shared_memory::segment_manager> | |
shared_struct_allocator; | |
enum { | |
ORDERVIEW, | |
TOKENVIEW, | |
PRICEVIEW, | |
TYPEVIEW, | |
}; | |
typedef bmi::multi_index_container< | |
MIOrder_Message, | |
bmi::indexed_by< | |
bmi::ordered_unique<bmi::tag<struct Order_Id>, BOOST_MULTI_INDEX_MEMBER( MIOrder_Message, double, MIOrder_Message::Order_Id)>, | |
bmi::ordered_non_unique<bmi::tag<struct Token>, BOOST_MULTI_INDEX_MEMBER( MIOrder_Message, int, MIOrder_Message::Token)>, | |
bmi::ordered_non_unique<bmi::tag<struct Price>, BOOST_MULTI_INDEX_MEMBER( MIOrder_Message, int, MIOrder_Message::Price), std::less<int>>, | |
bmi::ordered_non_unique<bmi::tag<struct Order_Type>, BOOST_MULTI_INDEX_MEMBER(MIOrder_Message, char, Order_Type)> >, | |
shared_struct_allocator | |
> Order_Set; | |
typedef bmi::nth_index<Order_Set, ORDERVIEW>::type Order_view; | |
typedef bmi::nth_index<Order_Set, TOKENVIEW>::type Token_View; | |
typedef bmi::nth_index<Order_Set, PRICEVIEW>::type Price_View; | |
typedef std::map<int, int> _PricePoint; | |
_PricePoint GenerateData(int _TKN, std::pair<Order_Set *, std::size_t> OrderRecord, bool IsReverse = false) { | |
_PricePoint CurrentPrice; | |
if (OrderRecord.second > 0) { | |
Token_View::const_iterator t1 = | |
OrderRecord.first->get<TOKENVIEW>().find(_TKN); | |
Price_View::const_iterator it2 = OrderRecord.first->project<PRICEVIEW>(t1); | |
int icount = 0; | |
int Price = 0; | |
while (icount < 500) | |
/// I am forcefully iterating to 500 assuming that i will | |
/// get Max value within that range. What i need to fetch is | |
/// Top 10 unique Price and sum its Qty if Price is repeated. | |
/// Ex. Price 10 Qty 5 ; Price 9 Qty 4; Price 8 Qty 7 .... n | |
/// Same is the case when I am trying to fetch top 10 Min Price with Qty. | |
{ | |
auto val = std::next(it2, icount); | |
if (val->Token == _TKN) { | |
// printf(" Bid Data Found for token %d , Price %d , Qty %d , Time %li | |
// , OrderNumber %16f , icount %d \n | |
// ",val->Token,val->Price,val->Quantity,val->Timestamp,val->Order_Id,icount); | |
CurrentPrice[val->Price] += val->Quantity; | |
Price = val->Price; | |
} | |
icount++; | |
} | |
std::cout << " Bid Price " << Price << std::endl; | |
} | |
return CurrentPrice; | |
} | |
int main() { | |
bip::managed_shared_memory segment(bip::open_or_create, "mySharedMemory", 20ull<<20); | |
Order_Set::allocator_type alloc(segment.get_segment_manager()); | |
Order_Set * BuyOrderRecord = segment.find_or_construct<Order_Set>("MCASTPORT0BUY")(alloc); | |
if (BuyOrderRecord->empty()) { | |
} | |
while (true) { | |
int _TKN = 49732; | |
_PricePoint CurrentPrice = GenerateData(_TKN, std::make_pair(BuyOrderRecord, 1), true); | |
std::cout << "=========================================================" << std::endl; | |
sleep(2); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment