| 1 | /* |
| 2 | * Legal Notice |
| 3 | * |
| 4 | * This document and associated source code (the "Work") is a part of a |
| 5 | * benchmark specification maintained by the TPC. |
| 6 | * |
| 7 | * The TPC reserves all right, title, and interest to the Work as provided |
| 8 | * under U.S. and international laws, including without limitation all patent |
| 9 | * and trademark rights therein. |
| 10 | * |
| 11 | * No Warranty |
| 12 | * |
| 13 | * 1.1 TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THE INFORMATION |
| 14 | * CONTAINED HEREIN IS PROVIDED "AS IS" AND WITH ALL FAULTS, AND THE |
| 15 | * AUTHORS AND DEVELOPERS OF THE WORK HEREBY DISCLAIM ALL OTHER |
| 16 | * WARRANTIES AND CONDITIONS, EITHER EXPRESS, IMPLIED OR STATUTORY, |
| 17 | * INCLUDING, BUT NOT LIMITED TO, ANY (IF ANY) IMPLIED WARRANTIES, |
| 18 | * DUTIES OR CONDITIONS OF MERCHANTABILITY, OF FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE, OF ACCURACY OR COMPLETENESS OF RESPONSES, OF RESULTS, OF |
| 20 | * WORKMANLIKE EFFORT, OF LACK OF VIRUSES, AND OF LACK OF NEGLIGENCE. |
| 21 | * ALSO, THERE IS NO WARRANTY OR CONDITION OF TITLE, QUIET ENJOYMENT, |
| 22 | * QUIET POSSESSION, CORRESPONDENCE TO DESCRIPTION OR NON-INFRINGEMENT |
| 23 | * WITH REGARD TO THE WORK. |
| 24 | * 1.2 IN NO EVENT WILL ANY AUTHOR OR DEVELOPER OF THE WORK BE LIABLE TO |
| 25 | * ANY OTHER PARTY FOR ANY DAMAGES, INCLUDING BUT NOT LIMITED TO THE |
| 26 | * COST OF PROCURING SUBSTITUTE GOODS OR SERVICES, LOST PROFITS, LOSS |
| 27 | * OF USE, LOSS OF DATA, OR ANY INCIDENTAL, CONSEQUENTIAL, DIRECT, |
| 28 | * INDIRECT, OR SPECIAL DAMAGES WHETHER UNDER CONTRACT, TORT, WARRANTY, |
| 29 | * OR OTHERWISE, ARISING IN ANY WAY OUT OF THIS OR ANY OTHER AGREEMENT |
| 30 | * RELATING TO THE WORK, WHETHER OR NOT SUCH AUTHOR OR DEVELOPER HAD |
| 31 | * ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. |
| 32 | * |
| 33 | * Contributors |
| 34 | * - Doug Johnson, Matt Emmerton |
| 35 | */ |
| 36 | |
| 37 | /****************************************************************************** |
| 38 | * Description: Implementation of the MEE class. |
| 39 | * See MEE.h for a description. |
| 40 | ******************************************************************************/ |
| 41 | |
| 42 | #include "main/MEE.h" |
| 43 | |
| 44 | using namespace TPCE; |
| 45 | |
| 46 | // Automatically generate unique RNG seeds. |
| 47 | // The CRandom class uses an unsigned 64-bit value for the seed. |
| 48 | // This routine automatically generates two unique seeds. One is used for |
| 49 | // the TxnInput generator RNG, and the other is for the TxnMixGenerator RNG. |
| 50 | // The 64 bits are used as follows. |
| 51 | // |
| 52 | // Bits 0 - 31 Caller provided unique unsigned 32-bit id. |
| 53 | // Bit 32 0 for TxnInputGenerator, 1 for TxnMixGenerator |
| 54 | // Bits 33 - 43 Number of days since the base time. The base time |
| 55 | // is set to be January 1 of the most recent year that is |
| 56 | // a multiple of 5. This allows enough space for the last |
| 57 | // field, and it makes the algorithm "timeless" by resetting |
| 58 | // the generated values every 5 years. |
| 59 | // Bits 44 - 63 Current time of day measured in 1/10's of a second. |
| 60 | // |
| 61 | void CMEE::AutoSetRNGSeeds(UINT32 UniqueId) { |
| 62 | CDateTime Now; |
| 63 | INT32 BaseYear; |
| 64 | INT32 Tmp1, Tmp2; |
| 65 | |
| 66 | Now.GetYMD(&BaseYear, &Tmp1, &Tmp2); |
| 67 | |
| 68 | // Set the base year to be the most recent year that was a multiple of 5. |
| 69 | BaseYear -= (BaseYear % 5); |
| 70 | CDateTime Base(BaseYear, 1, 1); // January 1st in the BaseYear |
| 71 | |
| 72 | // Initialize the seed with the current time of day measured in 1/10's of a |
| 73 | // second. This will use up to 20 bits. |
| 74 | RNGSEED Seed; |
| 75 | Seed = Now.MSec() / 100; |
| 76 | |
| 77 | // Now add in the number of days since the base time. |
| 78 | // The number of days in the 5 year period requires 11 bits. |
| 79 | // So shift up by that much to make room in the "lower" bits. |
| 80 | Seed <<= 11; |
| 81 | Seed += (RNGSEED)((INT64)Now.DayNo() - (INT64)Base.DayNo()); |
| 82 | |
| 83 | // So far, we've used up 31 bits. |
| 84 | // Save the "last" bit of the "upper" 32 for the RNG id. |
| 85 | // In addition, make room for the caller's 32-bit unique id. |
| 86 | // So shift a total of 33 bits. |
| 87 | Seed <<= 33; |
| 88 | |
| 89 | // Now the "upper" 32-bits have been set with a value for RNG 0. |
| 90 | // Add in the sponsor's unique id for the "lower" 32-bits. |
| 91 | Seed += UniqueId; |
| 92 | |
| 93 | // Set the Ticker Tape RNG to the unique seed. |
| 94 | m_TickerTape.SetRNGSeed(Seed); |
| 95 | m_DriverMEESettings.cur.TickerTapeRNGSeed = Seed; |
| 96 | |
| 97 | // Set the RNG Id to 1 for the Trading Floor. |
| 98 | Seed |= UINT64_CONST(0x0000000100000000); |
| 99 | m_TradingFloor.SetRNGSeed(Seed); |
| 100 | m_DriverMEESettings.cur.TradingFloorRNGSeed = Seed; |
| 101 | } |
| 102 | |
| 103 | // Constructor - automatic RNG seed generation |
| 104 | CMEE::CMEE(INT32 TradingTimeSoFar, CMEESUTInterface *pSUT, CBaseLogger *pLogger, const DataFileManager &dfm, |
| 105 | UINT32 UniqueId) |
| 106 | : m_DriverMEESettings(UniqueId, 0, 0, 0), m_pSUT(pSUT), m_pLogger(pLogger), |
| 107 | m_PriceBoard(TradingTimeSoFar, &m_BaseTime, &m_CurrentTime, dfm), |
| 108 | m_TickerTape(pSUT, &m_PriceBoard, &m_BaseTime, &m_CurrentTime, dfm), |
| 109 | m_TradingFloor(pSUT, &m_PriceBoard, &m_TickerTape, &m_BaseTime, &m_CurrentTime), m_MEELock() { |
| 110 | m_pLogger->SendToLogger("MEE object constructed using c'tor 1 (valid for publication: YES)." ); |
| 111 | |
| 112 | AutoSetRNGSeeds(UniqueId); |
| 113 | |
| 114 | m_pLogger->SendToLogger(m_DriverMEESettings); |
| 115 | } |
| 116 | |
| 117 | // Constructor - RNG seed provided |
| 118 | CMEE::CMEE(INT32 TradingTimeSoFar, CMEESUTInterface *pSUT, CBaseLogger *pLogger, const DataFileManager &dfm, |
| 119 | UINT32 UniqueId, RNGSEED TickerTapeRNGSeed, RNGSEED TradingFloorRNGSeed) |
| 120 | : m_DriverMEESettings(UniqueId, 0, TickerTapeRNGSeed, TradingFloorRNGSeed), m_pSUT(pSUT), m_pLogger(pLogger), |
| 121 | m_PriceBoard(TradingTimeSoFar, &m_BaseTime, &m_CurrentTime, dfm), |
| 122 | m_TickerTape(pSUT, &m_PriceBoard, &m_BaseTime, &m_CurrentTime, TickerTapeRNGSeed, dfm), |
| 123 | m_TradingFloor(pSUT, &m_PriceBoard, &m_TickerTape, &m_BaseTime, &m_CurrentTime, TradingFloorRNGSeed), |
| 124 | m_MEELock() { |
| 125 | m_pLogger->SendToLogger("MEE object constructed using c'tor 2 (valid for publication: NO)." ); |
| 126 | m_pLogger->SendToLogger(m_DriverMEESettings); |
| 127 | } |
| 128 | |
| 129 | CMEE::~CMEE(void) { |
| 130 | m_pLogger->SendToLogger("MEE object destroyed." ); |
| 131 | } |
| 132 | |
| 133 | RNGSEED CMEE::GetTickerTapeRNGSeed(void) { |
| 134 | return (m_TickerTape.GetRNGSeed()); |
| 135 | } |
| 136 | |
| 137 | RNGSEED CMEE::GetTradingFloorRNGSeed(void) { |
| 138 | return (m_TradingFloor.GetRNGSeed()); |
| 139 | } |
| 140 | |
| 141 | void CMEE::SetBaseTime(void) { |
| 142 | m_MEELock.lock(); |
| 143 | m_BaseTime.Set(); |
| 144 | m_MEELock.unlock(); |
| 145 | } |
| 146 | |
| 147 | bool CMEE::DisableTickerTape(void) { |
| 148 | bool Result; |
| 149 | m_MEELock.lock(); |
| 150 | Result = m_TickerTape.DisableTicker(); |
| 151 | m_MEELock.unlock(); |
| 152 | return (Result); |
| 153 | } |
| 154 | |
| 155 | bool CMEE::EnableTickerTape(void) { |
| 156 | bool Result; |
| 157 | m_MEELock.lock(); |
| 158 | Result = m_TickerTape.EnableTicker(); |
| 159 | m_MEELock.unlock(); |
| 160 | return (Result); |
| 161 | } |
| 162 | |
| 163 | INT32 CMEE::GenerateTradeResult(void) { |
| 164 | INT32 NextTime; |
| 165 | |
| 166 | m_MEELock.lock(); |
| 167 | m_CurrentTime.Set(); |
| 168 | NextTime = m_TradingFloor.GenerateTradeResult(); |
| 169 | m_MEELock.unlock(); |
| 170 | return (NextTime); |
| 171 | } |
| 172 | |
| 173 | INT32 CMEE::SubmitTradeRequest(PTradeRequest pTradeRequest) { |
| 174 | INT32 NextTime; |
| 175 | |
| 176 | m_MEELock.lock(); |
| 177 | m_CurrentTime.Set(); |
| 178 | NextTime = m_TradingFloor.SubmitTradeRequest(pTradeRequest); |
| 179 | m_MEELock.unlock(); |
| 180 | return (NextTime); |
| 181 | } |
| 182 | |