| 1 |
#include "EventController.h" |
|---|
| 2 |
|
|---|
| 3 |
EventController::EventController(void) |
|---|
| 4 |
{ |
|---|
| 5 |
pCurrentElement = NULL; |
|---|
| 6 |
} |
|---|
| 7 |
|
|---|
| 8 |
EventController::~EventController(void) |
|---|
| 9 |
{ |
|---|
| 10 |
instanceCreated = false; |
|---|
| 11 |
if(pCurrentElement != NULL) |
|---|
| 12 |
{ |
|---|
| 13 |
if(pCurrentElement->data != NULL) |
|---|
| 14 |
{ |
|---|
| 15 |
free(pCurrentElement->data); |
|---|
| 16 |
} |
|---|
| 17 |
delete pCurrentElement; |
|---|
| 18 |
pCurrentElement = NULL; |
|---|
| 19 |
} |
|---|
| 20 |
} |
|---|
| 21 |
|
|---|
| 22 |
EventController* |
|---|
| 23 |
EventController::getInstance() |
|---|
| 24 |
{ |
|---|
| 25 |
if(!instanceCreated) |
|---|
| 26 |
{ |
|---|
| 27 |
pEventController = new EventController(); |
|---|
| 28 |
instanceCreated = true; |
|---|
| 29 |
} |
|---|
| 30 |
return pEventController; |
|---|
| 31 |
} |
|---|
| 32 |
|
|---|
| 33 |
boost::signals::connection |
|---|
| 34 |
EventController::connect_onServerEvent(wstring eventType, |
|---|
| 35 |
const signal_serverEvent::slot_type& s) |
|---|
| 36 |
{ |
|---|
| 37 |
stdext::hash_map<wstring, signal_serverEvent*>::iterator it; |
|---|
| 38 |
if((it = onServerEventMap.find(eventType)) != onServerEventMap.end()) |
|---|
| 39 |
{ |
|---|
| 40 |
signal_serverEvent* signal_onServerEvent = it->second; |
|---|
| 41 |
return signal_onServerEvent->connect(s); |
|---|
| 42 |
} else { |
|---|
| 43 |
signal_serverEvent* signal_onServerEvent = new signal_serverEvent(); |
|---|
| 44 |
boost::signals::connection connection = signal_onServerEvent->connect(s); |
|---|
| 45 |
onServerEventMap.insert(OnServerEventPair(eventType, signal_onServerEvent)); |
|---|
| 46 |
return connection; |
|---|
| 47 |
} |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
void |
|---|
| 51 |
EventController::notifyListeners() |
|---|
| 52 |
{ |
|---|
| 53 |
if(pCurrentElement != NULL) |
|---|
| 54 |
{ |
|---|
| 55 |
stdext::hash_map<wstring, signal_serverEvent*>::iterator it; |
|---|
| 56 |
it = onServerEventMap.find(pCurrentElement->name); |
|---|
| 57 |
if(it != onServerEventMap.end()) |
|---|
| 58 |
{ |
|---|
| 59 |
signal_serverEvent* signal_onServerEvent = it->second; |
|---|
| 60 |
(*signal_onServerEvent)(pCurrentElement); |
|---|
| 61 |
} |
|---|
| 62 |
if(pCurrentElement->data != NULL) |
|---|
| 63 |
{ |
|---|
| 64 |
free(pCurrentElement->data); |
|---|
| 65 |
} |
|---|
| 66 |
delete pCurrentElement; |
|---|
| 67 |
pCurrentElement = NULL; |
|---|
| 68 |
} |
|---|
| 69 |
} |
|---|
| 70 |
|
|---|
| 71 |
void |
|---|
| 72 |
EventController::receiveServerEvent(const char* xmlDocument) |
|---|
| 73 |
{ |
|---|
| 74 |
this->parseString(xmlDocument); |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
void |
|---|
| 78 |
EventController::startElement(const char* name, const char** atts) |
|---|
| 79 |
{ |
|---|
| 80 |
size_t nameLength = strlen(name) + 1; |
|---|
| 81 |
wchar_t* wszElementName = (wchar_t*)malloc((strlen(name) + 1)*sizeof(wchar_t)); |
|---|
| 82 |
size_t convertedChars = 0; |
|---|
| 83 |
mbstowcs_s(&convertedChars, wszElementName, nameLength, name, _TRUNCATE); |
|---|
| 84 |
wstring elementName = wszElementName; |
|---|
| 85 |
free(wszElementName); |
|---|
| 86 |
|
|---|
| 87 |
if(pCurrentElement != NULL) |
|---|
| 88 |
{ |
|---|
| 89 |
notifyListeners(); |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
pCurrentElement = new Element(); |
|---|
| 93 |
pCurrentElement->name = elementName; |
|---|
| 94 |
pCurrentElement->data = NULL; |
|---|
| 95 |
|
|---|
| 96 |
if (atts) |
|---|
| 97 |
{ |
|---|
| 98 |
for(int i = 0; atts[i]; i+=2) |
|---|
| 99 |
{ |
|---|
| 100 |
if(atts[i+1] != NULL) |
|---|
| 101 |
{ |
|---|
| 102 |
Attribute att; |
|---|
| 103 |
|
|---|
| 104 |
size_t attributeNameLength = strlen(atts[i]) + 1; |
|---|
| 105 |
size_t attributeValueLength = strlen(atts[i+1]) + 1; |
|---|
| 106 |
wchar_t* wszAttributeName = (wchar_t*)malloc(attributeNameLength*sizeof(wchar_t)); |
|---|
| 107 |
wchar_t* wszAttributeValue = (wchar_t*)malloc(attributeValueLength*sizeof(wchar_t)); |
|---|
| 108 |
|
|---|
| 109 |
mbstowcs_s(&convertedChars, wszAttributeName, attributeNameLength, atts[i], _TRUNCATE); |
|---|
| 110 |
mbstowcs_s(&convertedChars, wszAttributeValue, attributeValueLength, atts[i+1], _TRUNCATE); |
|---|
| 111 |
|
|---|
| 112 |
att.name = wszAttributeName; |
|---|
| 113 |
att.value = wszAttributeValue; |
|---|
| 114 |
|
|---|
| 115 |
pCurrentElement->attributes.push_back(att); |
|---|
| 116 |
|
|---|
| 117 |
free(wszAttributeName); |
|---|
| 118 |
free(wszAttributeValue); |
|---|
| 119 |
|
|---|
| 120 |
} else { |
|---|
| 121 |
printf("EventController: ERROR - Malformed XML received\n"); |
|---|
| 122 |
} |
|---|
| 123 |
} |
|---|
| 124 |
} |
|---|
| 125 |
} |
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
void |
|---|
| 129 |
EventController::endElement(const XML_Char* name) |
|---|
| 130 |
{ |
|---|
| 131 |
notifyListeners(); |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
void |
|---|
| 136 |
EventController::charData(const XML_Char *s, int len) |
|---|
| 137 |
{ |
|---|
| 138 |
char* data = (char*)malloc(len+1); |
|---|
| 139 |
memcpy(data, &s[0], len); |
|---|
| 140 |
data[len] = '\0'; |
|---|
| 141 |
pCurrentElement->data = data; |
|---|
| 142 |
pCurrentElement->dataLength = len; |
|---|
| 143 |
} |
|---|