// Adds an event listener to the list for that event type. Returns false if listener is not added, true if added bool EventManager::addListener(EventListenerPtr const & listener, EventType const & type) { if (!validateType(type)) return false; EventTypeSet::iterator it = m_eventTypes.find(type); // Looks to see if the event is a type seen before and adds the event type if not. if (it == m_eventTypes.end()) { EventTypeSetRet itEvent = m_eventTypes.insert(type); if (itEvent.second == false) return false; if (itEvent.first == m_eventTypes.end()) return false; it = itEvent.first; } EventListenerMap::iterator itMap = m_listenerMap.find(type.getId()); // Finds the list of listeners for the event type and creates one if it doesn't exist. if (itMap == m_listenerMap.end()) { EventListenerMapRet itListMap = m_listenerMap.insert(EventListenerMapEntry(type.getId(), EventListenerList())); if (itListMap.second == false) return false; if (itListMap.first == m_listenerMap.end()) return false; itMap = itListMap.first; } EventListenerList & theList= (*itMap).second; // Checks to see if the listener is already in the map. for (EventListenerList::iterator i = theList.begin(); i != theList.end(); i++) { if ((*i) == listener) return false; } theList.push_back(listener); return true; }
// Checks if type is valid. Does this by finding the hash value of the name // and then checking if the names match if that hash number is already taken. bool EventManager::validateType(EventType const & type) { if (type.getName() == NULL) return false; if (type.getId() == 0) return false; EventTypeSet::iterator it = m_eventTypes.find(type); if (it != m_eventTypes.end()) { EventType known = (*it); char * const oldName = known.getName(); char * const newName = type.getName(); int check = strcmp(oldName, newName); assert(check==0 && "Event types hashed wrong!"); if (check!=0) return false; } return true; }
// Goes through the event queue and processes events for the given time. bool EventManager::tick(unsigned int maxMS) { unsigned int curTick = GetTickCount(); unsigned int maxTime = maxMS + curTick; bool processed = false; while (m_eventQueue.size() > 0) { EventPtr event = m_eventQueue.front(); m_eventQueue.pop_front(); EventType type = event->getType(); EventListenerMap::iterator itMap = m_listenerMap.find(type.getId()); if (itMap == m_listenerMap.end()) continue; EventListenerList theList = (*itMap).second; for (EventListenerList::iterator i = theList.begin(); i != theList.end(); i++) { if ((*i)->HandleEvent(*event)) break; } curTick = GetTickCount(); if (curTick >= maxTime) break; } return processed; }
// Adds event to event queue. Returns true if added. bool EventManager::queueEvent(EventPtr const & event) { EventType type = event->getType(); if (!validateType(type)) return false; EventListenerMap::iterator itMap = m_listenerMap.find(type.getId()); if (itMap == m_listenerMap.end()) return false; m_eventQueue.push_back(event); return true; }
int main(int argc, char* argv[]) { if (argc < 4) { std::cerr << "Usage: " << argv[0] << " <application-key> <username> <password>" << std::endl; return 1; } ExchangeApi exchangeApi(argv[1]); if (exchangeApi.login(argv[2], argv[3])) { MarketFilter filter; ListEventTypesRequest req(filter); if (req.isValid()) { ListEventTypesResponse resp = exchangeApi.listEventTypes(req); if (resp.isSuccess()) { std::vector<EventTypeResult> eventTypeResults = resp.getEventTypeResults(); for (unsigned i = 0; i < eventTypeResults.size(); ++i) { EventTypeResult etr = eventTypeResults[i]; EventType et = etr.getEventType(); std::cout << et.getId() << " " << et.getName() << std::endl; } } else { std::cout << "request failed: " << resp.getFaultCode() << " " << resp.getFaultString() << std::endl; } } else { std::cout << "invalid request: " << req.toString() << std::endl; } } else { std::cout << "failed to log in" << std::endl; } }
// Instantly triggers an event. Returns true if event is processed bool EventManager::triggerEvent(Event const & event) { EventType type = event.getType(); if (!validateType(type)) return false; EventListenerMap::iterator itMap = m_listenerMap.find(type.getId()); if (itMap == m_listenerMap.end()) return false; EventListenerList theList = (*itMap).second; bool processed = false; for (EventListenerList::iterator i = theList.begin(); i != theList.end(); i++) { if ((*i)->HandleEvent(event)) processed = true; } return processed; }