00001
00010
#include "demo_db.h"
00011
00012
00019 int main(
int argc,
char* argv[]) {
00020
try {
00021
bool Stop=
false;
00022
demo_db DemoApp;
00023
00024 Stop = DemoApp.
Init(argc, argv);
00025
if (!Stop) {
00026
00027 DemoApp.
OpenDatabase();
00028
00029 DemoApp.
OpenFormatFiles();
00030
00031 DemoApp.
StartSocket();
00032
00033 DemoApp.
Execution();
00034 };
00035
return 0;
00036 }
catch(string Error) {
00037
if (!Error.empty()) {
00038 cerr <<
"ERROR: " << Error << endl;
00039 }
else {
00040 cerr <<
"ERROR: Unkown error..." << endl;
00041
throw;
00042 };
00043
return -1;
00044 };
00045
00046 };
00047
00048
00054 demo_db::demo_db() {
00055
00056 DBFilename =
"";
00057 ServerPort=0;
00058 ServerIP=
"";
00059 InputBuffer=
"";
00060 Exit=
false;
00061 PC2Format=
"";
00062 DS175Format=
"";
00063 command=
"";
00064 prompt=
false;
00065 }
00066
00071 demo_db::~demo_db() {}
00072
00073
00083 bool demo_db::Init(
int argc,
char* argv[]) {
00084 string option, optname, optdata;
00085
int pos;
00086 Exit =
false;
00087
00088 DBFilename = def_DBFILE;
00089 ServerPort = def_PORT;
00090 ServerIP = def_IP;
00091
00092
for (
int i=1; i<argc ; ++i) {
00093 option = argv[i];
00094
if ( (pos = option.find(
'=')) >1 ) {
00095 optname = option.substr(0,pos);
00096 optdata = option.substr(pos+1);
00097 }
else {
00098 optname=option;
00099 };
00100
if (optname[0] ==
'-') {
00101
switch (optname[1]) {
00102
case 'h':
00103 cout << VERSIONTEXT << HELPTEXT ;
00104 Exit =
true;
00105
break;
00106
case 'v':
00107 cout << VERSIONTEXT ;
00108 Exit =
true;
00109
break;
00110
case 'f':
00111
if (!optdata.empty()) {
00112 DBFilename = optdata;
00113 };
00114
break;
00115
case 'i':
00116
if (!optdata.empty()) {
00117 ServerIP = optdata;
00118 };
00119
break;
00120
case 'p':
00121
if (!optdata.empty()) {
00122 ServerPort = atoi(optdata.c_str());
00123 };
00124
break;
00125 }
00126 };
00127 };
00128
return Exit;
00129 }
00130
00135 void demo_db::OpenDatabase(string name) {
00136
if (name.empty() && DBFilename.empty()) {
00137
throw "OpenDatabase: Empty Database filename.";
00138 };
00139
if (!name.empty()) {
00140 DBFilename = name;
00141 };
00142 DB.
Load(DBFilename);
00143 cout<<
"Database: " << DBFilename <<
" opened." << endl;
00144 }
00145
00153 void demo_db::OpenFormatFiles() {
00154
char c;
00155
00156 ifstream ds175file(DS175FILENAME);
00157
if (ds175file) {
00158 DS175Format=
"";
00159
while ( (c=ds175file.get())!= EOF) {
00160 DS175Format += c;
00161 }
00162 DS175Format =
ExpandEsc(DS175Format);
00163 cout<<
"Format file for DS175 opened." << endl;
00164 ds175file.close();
00165 }
else {
00166 cerr <<
"WARNING: DS175 format file ("<< DS175FILENAME <<
") load: Unable to open file.\n" << endl;
00167 };
00168
00169
00170 ifstream PC2file(PC2FILENAME);
00171
if (PC2file) {
00172 PC2Format=
"";
00173
while ( (c=PC2file.get())!= EOF) {
00174 PC2Format += c;
00175 }
00176 PC2Format =
ExpandEsc(PC2Format);
00177 cout<<
"Format file for PC2 opened." << endl;
00178 PC2file.close();
00179 }
else {
00180 cerr <<
"WARNING: PC2 format file ("<< PC2FILENAME <<
") load: Unable to open file.\n" << endl;
00181 };
00182 }
00183
00188 void demo_db::StartSocket() {
00189 DBClientSocket.
Connect(ServerIP, ServerPort);
00190 cout<<
"Connected to PC_Ethernet_Driver."<<endl;
00191 }
00192
00198 void demo_db::Execution() {
00199 string Answer=
"";
00200 command=
"";
00201 prompt=
true;
00202
bool Quit =
false;
00203
struct TagType Tag;
00204
while (!Quit) {
00205
00206 InputBuffer += DBClientSocket.
Read();
00207 Tag =
ExtractTag(InputBuffer);
00208
if ( Tag.
Name !=
"" ) {
00209 Answer =
ProcessRequest(Tag);
00210
if (!Answer.empty()) {
00211 DBClientSocket.
Write(Answer);
00212 };
00213 };
00214
00215 Quit =
CommandParser();
00216
00217
mSleep(1);
00218 };
00219 }
00220
00230 string
demo_db::ProcessRequest(
struct TagType tag) {
00231 string db_answer=
"",
id, answer=
"", type, answer_text;
00232
if (tag.
Name ==
"request") {
00233 db_answer =
Database_Lookup(tag.
Data);
00234
if (db_answer ==
"") {
00235 db_answer =
Database_Lookup(
"unknown");
00236
if (db_answer ==
"") {
00237
return "";
00238 };
00239 };
00240
id =
GetOption(tag.
Options,
"id");
00241 type =
GetOption(tag.
Options,
"type");
00242
if (type==
"DS175" || type==
"ds175") {
00243 answer_text =
FormatDS175(db_answer);
00244 }
else if (type==
"PC2" || type==
"PC2") {
00245 answer_text =
FormatPC2(db_answer);
00246 }
else {
00247 answer_text = db_answer;
00248 };
00249 answer +=
"<data";
00250
if (
id !=
"") {
00251 answer+=
" id=" +
id;
00252 };
00253 answer+=
">" + answer_text +
"</data>";
00254 };
00255
return answer;
00256 }
00257
00264 struct TagType demo_db::ExtractTag(string& input) {
00265
struct TagType tag;
00266 tag.
Name=
"";
00267 tag.
Options=
"";
00268 tag.
Data=
"";
00269
bool NoTag, endtagfound=
false;
00270 string opentag, closetag;
00271
unsigned int pos, start, end, datastart, dataend;
00272
00273 start=input.find(
'<');
00274
if (start != string::npos) {
00275
00276
00277
if (input[start+1] ==
'/') {
00278 input = input.substr(start+2);
00279
return tag;
00280 };
00281 end = input.find(
'>', start);
00282
if (end != string::npos) {
00283
00284 opentag = input.substr(start+1, end-start-1);
00285
if ( (pos=opentag.find(
' ')) != string::npos) {
00286 tag.
Name = opentag.substr(0, pos);
00287 tag.
Options = opentag.substr(pos+1);
00288 }
else {
00289 tag.
Name = opentag;
00290 };
00291 datastart = end+1;
00292 dataend = datastart;
00293 NoTag =
false;
00294
00295
while (!endtagfound && !NoTag) {
00296
if ( (start=input.find(
"</",dataend)) != string::npos) {
00297
if ( (end = input.find(
'>', start)) != string::npos) {
00298
00299 closetag = input.substr(start+1, end-start-1);
00300
if (closetag.empty() || ((closetag.find(tag.
Name) != string::npos)) ) {
00301
00302 tag.
Data = input.substr(datastart, start-datastart);
00303 input = input.substr(end+1);
00304 endtagfound =
true;
00305 }
else {
00306
00307 endtagfound =
false;
00308 dataend = end;
00309 };
00310 };
00311 }
else {
00312 NoTag =
true;
00313 tag.
Name=
"";
00314 tag.
Options=
"";
00315 tag.
Data=
"";
00316 };
00317 };
00318 };
00319 };
00320
return tag;
00321 }
00322
00330 string
demo_db::GetOption(string options, string item) {
00331 string value, s;
00332
unsigned int start, end;
00333 s = item +
'=';
00334
00335 start = options.find(s)+s.size();
00336
if ( start != string::npos) {
00337
00338
if ((end=options.find(
' ', start)) != string::npos) {
00339 value = options.substr(start, start-end-1);
00340 }
else {
00341 value = options.substr(start);
00342 };
00343 }
else {
00344 value =
"";
00345 };
00346
return value;
00347 }
00348
00354 string
demo_db::Database_Lookup(string key) {
00355 string answer=
"";
00356 answer = DB.
Find(key);
00357
return answer;
00358 }
00359
00368 string
demo_db::FormatDS175(string data) {
00369
unsigned int i,j;
00370 string line, result,item, value, s;
00371 istringstream datastream(data);
00372 result = DS175Format;
00373
00374
while ( getline(datastream, line) ) {
00375
00376 i= line.find(
':');
00377
if (i!= string::npos) {
00378 item = line.substr(0, i);
00379 value = line.substr(i+1);
00380
00381 s =
'{'+item+
'}';
00382 j = result.find(s);
00383
if (j != string::npos) {
00384 result.replace(j, s.size(), value);
00385 };
00386 };
00387 };
00388
return result;
00389 }
00390
00399 string
demo_db::FormatPC2(string data) {
00400
unsigned int i,j;
00401 string line, result,item, value, s;
00402 istringstream datastream(data);
00403 result = PC2Format;
00404
00405
while ( getline(datastream, line) ) {
00406
00407 i= line.find(
':');
00408
if (i!= string::npos) {
00409 item = line.substr(0, i);
00410 value = line.substr(i+1);
00411
00412 s =
'{'+item+
'}';
00413 j = result.find(s);
00414
if (j != string::npos) {
00415 result.replace(j, s.size(), value);
00416 };
00417 };
00418 };
00419
return result;
00420 }
00421
00429 bool demo_db::CommandParser() {
00430
int j;
00431
bool CmdOk;
00432
if (prompt) {
00433 write(1,
"demo> ",6);
00434 prompt=
false;
00435 };
00436 CmdOk =
ReadCommand();
00437
if (CmdOk) {
00438
00439
if (command.find(
"quit") == 0) {
00440
return true;
00441
00442 }
else if ( (command.find(
"help")==0) || (command.find(
"?")==0) ) {
00443 cout << VERSIONTEXT << HELPCMDTEXT;
00444
00445 }
else if ( command.find(
"send ")==0 ) {
00446 j = command.find(
'\n');
00447 string filename = command.substr(5,j-5);
00448 string data=
"";
00449 ifstream file(filename.c_str());
00450
if (!file) {
00451 cerr <<
"Error in Send: File not found ("<< filename <<
")\n";
00452 prompt=
true;
00453
return false;
00454 };
00455
char c;
00456
while ((c=file.get())!= EOF) {
00457 data += c;
00458 };
00459 file.close();
00460 data =
ExpandEsc(data);
00461 DBClientSocket.
Write(data);
00462 }
else if ( command.find(
"loadformat")==0 ) {
00463
OpenFormatFiles();
00464 }
else if ( command.find(
"loaddb")==0 ) {
00465
try {
00466
int i;
00467 string name=
"";
00468
if (command.size()>7 && command[6]==
' ') {
00469 i=command.find_first_of(
"\n\r\t ",7);
00470 name = command.substr(7,i-7);
00471 };
00472
OpenDatabase(name);
00473 }
catch(string Error) {
00474
00475 cerr <<
"Warning: " << Error;
00476 };
00477 };
00478 command =
"";
00479 prompt=
true;
00480 };
00481
return false;
00482 }
00483
00489 bool demo_db::ReadCommand() {
00490
#ifdef WIN32
00491
char c;
00492
while (_kbhit()) {
00493 c = _getche();
00494
00495
00496
if (c==
'\r') {
00497 c =
'\n';
00498
00499 _putch(c);
00500 }
00501 command+=c;
00502 };
00503
#else
00504
00505
int DataReady,count;
00506
char buf[100];
00507 fd_set ReadSet;
00508
struct timeval timeout;
00509
00510 FD_ZERO(&ReadSet);
00511 FD_SET(0, &ReadSet);
00512 timeout.tv_sec = 0;
00513 timeout.tv_usec = 1;
00514
00515 DataReady = select(1, &ReadSet, NULL, NULL, &timeout);
00516
if (DataReady > 0) {
00517
00518 count = read(0, buf, 100 );
00519
if (count>0) {
00520 buf[count]=0;
00521 command += buf;
00522 };
00523 };
00524
#endif
00525
00526
if (command.find(
'\n') != string::npos) {
00527
return true;
00528 }
else {
00529
return false;
00530 };
00531 };