-
Notifications
You must be signed in to change notification settings - Fork 442
/
DataFeed.cpp
200 lines (192 loc) · 6.71 KB
/
DataFeed.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "gb-include.h"
#include "DataFeed.h"
DataFeed::DataFeed() : MetaContainer() {
m_customerId = -1;
m_id = -1;
m_passcodeLen = 0;
m_passcode[0] = '\0';
m_isLocked = false;
}
DataFeed::~DataFeed() {
}
void DataFeed::setUrl ( char *name,
int32_t nameLen ) {
if (!name || nameLen == 0)
return;
if (nameLen < 11 ||
strncasecmp(name, "datafeed://", 11) != 0) {
char tempUrl[MAX_USERNAMELEN+1];
setstr(tempUrl, MAX_USERNAMELEN-12, name, nameLen);
m_urlLen = sprintf(m_url, "datafeed://%s/", tempUrl);
}
else
m_urlLen = setstr(m_url, MAX_USERNAMELEN, name, nameLen);
// base name
int32_t i;
for (i = 0; m_url[i+11] != '/'; i++)
m_baseName[i] = m_url[i+11];
m_baseName[i] = '\0';
m_baseNameLen = i;
}
void DataFeed::set ( int32_t creationTime,
char *dataFeedUrl,
int32_t dataFeedUrlLen,
char *passcode,
int32_t passcodeLen,
bool isActive,
bool isLocked ) {
setUrl(dataFeedUrl, dataFeedUrlLen);
m_passcodeLen = setstr(m_passcode, MAX_PASSCODELEN,
passcode, passcodeLen);
// flags
m_isActive = isActive;
m_isLocked = isLocked;
// creation time
m_creationTime = creationTime;
}
void DataFeed::parse ( char *dataFeedPage,
int32_t dataFeedPageLen ) {
// use Xml Class to parse up the page
Xml xml;
xml.set ( csUTF8, dataFeedPage, dataFeedPageLen, false, 0, false,
TITLEREC_CURRENT_VERSION );
// get the nodes
int32_t numNodes = xml.getNumNodes();
XmlNode *nodes = xml.getNodes();
// to count the tiers, result levels, and level costs
int32_t currTier = 0;
int32_t currResultLevel = 0;
int32_t currLevelCost = 0;
// pull out the keywords for the data feed
for (int32_t i = 0; i < numNodes; i++) {
// skip if this isn't a meta tag, shouldn't happen
if (nodes[i].m_nodeId != 68)
continue;
// get the meta tag name
//int32_t tagLen;
//char *tag = xml.getString(i, "name", &tagLen);
int32_t ucTagLen;
char *ucTag = xml.getString(i, "name", &ucTagLen);
char tag[256];
int32_t tagLen = utf16ToLatin1 ( tag, 256,
(UChar*)ucTag, ucTagLen>>1 );
// skip if empty
if (!tag || tagLen <= 0)
continue;
// get the content
int32_t ucConLen;
char *ucCon = xml.getString(i, "content", &ucConLen);
char con[1024];
int32_t conLen = utf16ToLatin1 ( con, 1024,
(UChar*)ucCon, ucConLen>>1 );
if (!con || conLen <= 0)
continue;
// match the meta tag to its local var and copy content
if (tagLen == 10 && strncasecmp(tag, "customerid", 10) == 0)
m_customerId = atoll(con);
else if (tagLen == 11 && strncasecmp(tag, "datafeedurl", 11) == 0)
setUrl(con, conLen);
else if (tagLen == 8 && strncasecmp(tag, "passcode", 8) == 0)
m_passcodeLen = setstr(m_passcode, MAX_PASSCODELEN, con, conLen);
else if (tagLen == 6 && strncasecmp(tag, "status", 6) == 0)
m_isActive = (bool)atoi(con);
else if (tagLen == 6 && strncasecmp(tag, "locked", 6) == 0)
m_isLocked = (bool)atoi(con);
else if (tagLen == 14 &&
strncasecmp(tag, "dfcreationtime", 14) == 0)
m_creationTime = atol(con);
else if (tagLen == 8 && strncasecmp(tag, "numtiers", 8) == 0)
m_priceTable.m_numTiers = atol(con);
else if (tagLen == 15 && strncasecmp(tag, "numresultlevels", 15) == 0)
m_priceTable.m_numResultLevels = atol(con);
else if (tagLen == 10 && strncasecmp(tag, "monthlyfee", 10) == 0)
m_priceTable.m_monthlyFee = atol(con);
else if (tagLen == 7 && strncasecmp(tag, "tiermax", 7) == 0) {
m_priceTable.m_tierMax[currTier] = (uint32_t)atol(con);
currTier++;
}
else if (tagLen == 11 && strncasecmp(tag, "resultlevel", 11) == 0) {
m_priceTable.m_resultLevels[currResultLevel] = (uint32_t)atol(con);
currResultLevel++;
}
else if (tagLen == 9 && strncasecmp(tag, "levelcost", 9) == 0) {
m_priceTable.m_levelCosts[currLevelCost] = (uint32_t)atol(con);
currLevelCost++;
}
else
log(LOG_INFO, "datafeed: Invalid Meta Tag Parsed [%"INT32"]:"
" %s", tagLen, tag);
}
}
int32_t DataFeed::buildPage ( char *page ) {
// fill the page buffer with the data feed page
char *p = page;
p += sprintf(p, "<meta name=customerid content=\"%"INT64"\">\n"
"<meta name=datafeedurl content=\"%s\">\n"
"<meta name=passcode content=\"%s\">\n"
"<meta name=status content=\"%d\">\n"
"<meta name=locked content=\"%d\">\n"
"<meta name=dfcreationtime content=\"%"INT32"\">\n",
m_customerId,
m_url,
m_passcode,
m_isActive,
m_isLocked,
m_creationTime );
// write the pricetable
p += sprintf(p, "<meta name=numtiers content=\"%"INT32"\">\n"
"<meta name=numresultlevels content=\"%"INT32"\">\n"
"<meta name=monthlyfee content=\"%"INT32"\">\n",
m_priceTable.m_numTiers,
m_priceTable.m_numResultLevels,
m_priceTable.m_monthlyFee );
// write the tiers
for (int32_t i = 0; i < m_priceTable.m_numTiers; i++)
p += sprintf(p, "<meta name=tiermax content=\"%"UINT32"\">\n",
m_priceTable.m_tierMax[i] );
// write the result levels
for (int32_t i = 0; i < m_priceTable.m_numResultLevels; i++)
p += sprintf(p, "<meta name=resultlevel content=\"%"UINT32"\">\n",
m_priceTable.m_resultLevels[i] );
// write the costs
int32_t numCosts = m_priceTable.m_numTiers * m_priceTable.m_numResultLevels * 2;
for (int32_t i = 0; i < numCosts; i++)
p += sprintf(p, "<meta name=levelcost content=\"%"UINT32"\">\n",
m_priceTable.m_levelCosts[i] );
// return the length
return (p - page);
}
void DataFeed::buildPage ( SafeBuf *sb ) {
sb->safePrintf("<meta name=customerid content=\"%"INT64"\">\n"
"<meta name=datafeedurl content=\"%s\">\n"
"<meta name=passcode content=\"%s\">\n"
"<meta name=status content=\"%d\">\n"
"<meta name=locked content=\"%d\">\n"
"<meta name=dfcreationtime content=\"%"INT32"\">\n",
m_customerId,
m_url,
m_passcode,
m_isActive,
m_isLocked,
m_creationTime );
// write the pricetable
sb->safePrintf("<meta name=numtiers content=\"%"INT32"\">\n"
"<meta name=numresultlevels content=\"%"INT32"\">\n"
"<meta name=monthlyfee content=\"%"INT32"\">\n",
m_priceTable.m_numTiers,
m_priceTable.m_numResultLevels,
m_priceTable.m_monthlyFee );
// write the tiers
for (int32_t i = 0; i < m_priceTable.m_numTiers; i++)
sb->safePrintf("<meta name=tiermax content=\"%"UINT32"\">\n",
m_priceTable.m_tierMax[i] );
// write the result levels
for (int32_t i = 0; i < m_priceTable.m_numResultLevels; i++)
sb->safePrintf("<meta name=resultlevel content=\"%"UINT32"\">\n",
m_priceTable.m_resultLevels[i] );
// write the costs
int32_t numCosts = m_priceTable.m_numTiers*m_priceTable.m_numResultLevels*2;
for (int32_t i = 0; i < numCosts; i++)
sb->safePrintf("<meta name=levelcost content=\"%"UINT32"\">\n",
m_priceTable.m_levelCosts[i] );
}