2 * @file Stock_Database.h
3 * @author Shanshan Jiang <shanshan.jiang@vanderbilt.edu>
4 * @author William R. Otte <wotte@dre.vanderbilt.edu>
5 * @author Douglas C. Schmidt <schmidt@dre.vanderbilt.edu>
8 #ifndef STOCK_DATABASE_H_
9 #define STOCK_DATABASE_H_
15 // #include "tao/orbconf.h"
22 * @class Stock_Database
23 * @brief This class is used to install, update and publish the information of
24 * all the stocks. It uses the singleton design pattern.
25 * The parameter type may be any type that has a method called "updated_stocks" and
26 * accepts a std::vector of std::strings as an argument.
28 template <typename CALLBACK
>
29 class Stock_Database
: public ACE_Task_Base
32 /// Default constructor
33 /// Initializes the stock database with MSFT, IBM, and INTEL.
34 /// @param the rate at which to perform updates
35 Stock_Database (u_int rate
= 1);
38 /// @param file The name of a file to read initial stocks and values from.
39 Stock_Database (const char *file
, u_int rate
= 1);
41 typedef std::map
<std::string
,
42 unsigned int> Init_Map
;
45 /// @param stockmap A map containing stocks and initial values. An initial value
46 /// of 0 will be assigned a random start value.
47 Stock_Database (const Init_Map
&stockmap
, u_int rate
= 0);
49 typedef std::string Cookie
;
52 * Register a callback object with the database. The callback object must have
53 * the () operator defined accepting a std::vector of strings as the argument.
54 * @returns A cookie to identify the registration
56 Cookie
register_callback (CALLBACK
&obj
);
59 * Removes a callback from the notification queue.
60 * @returns false if the provided cookie is not found.
62 bool remove_callback (const Cookie
&);
64 /// Raised if an invalid stock name is requested.
65 class Invalid_Stock
{};
70 : name_(""), high_ (0), low_ (0), last_ (0)
73 StockInfo (const std::string name
)
74 : name_ (name
), high_ (0), low_(0), last_(0)
84 * Create a StockInfo object stored in the database with the given name.
86 * @param name The name of the stock.
87 * @return A StockInfo object.
89 StockInfo
get_stock_info (const char *name
);
92 * This function is used to calculate the new high, low and last values
93 * for each stock in the stock database randomly.
95 virtual int svc (void);
97 /// Change the rate at which database updates are made
98 void update_rate (u_int rate
);
100 /// Launch the active object
103 /// Stop the active object
106 typedef std::map
<std::string
, StockInfo
> Stock_Map
;
108 /// This method is not intended to be called by clients of this class,
109 /// it is public only by necessity.
110 virtual int handle_signal (int signum
,
114 typedef std::map
<Cookie
, CALLBACK
*> Callback_Map
;
117 /// The filname initialized from, if any.
118 const std::string filename_
;
120 /// Keep track of the stock names and information about them.
121 Stock_Map stock_map_
;
123 /// Lock to protect concurrent access to the <stock_map_>.
124 ACE_RW_Thread_Mutex lock_
;
126 /// Rate at which updates are made.
129 Callback_Map callbacks_
;
134 #include "Stock_Database.tpp"
136 //typedef ACE_Singleton<Stock_Database, TAO_SYNCH_MUTEX> Stock_Database_Singleton;
137 //#define STOCK_DATABASE Stock_Database_Singleton::instance()
139 #endif // !defined STOCK_DATABASE_H_