-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
578 lines (538 loc) · 19.8 KB
/
main.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
#include <iostream>
#include <fstream>
// strings and c-strings
#include <cstring>
#include <string>
#include <algorithm>
#include <cctype>
#include <mpi.h>
#include <unistd.h>
//#include <windows.h>
#include <curl/curl.h>
using namespace std;
string tokens[100];
//string data; //will hold the url's contents
class Token {
private:
std::string strToken;
int intMatches;
public:
Token *sig;
Token *ant;
Token(std::string strToken){
this->strToken = strToken;
this->intMatches = 0;
sig = NULL;
ant = NULL;
}
Token(std::string strToken, int intMatches){
this->strToken = strToken;
this->intMatches = intMatches;
sig = NULL;
ant = NULL;
}
std::string getStrToken(){
return strToken;
}
void setStrToken(std::string strToken){
this->strToken = strToken;
}
int getIntMatches(){
return intMatches;
}
void setIntMatches(int intMatches){
this->intMatches = intMatches;
}
void print(){
cout<<"Token: "<<this->strToken<<endl;
}
};
class Site{
private:
std::string name;
std::string body;
std::string address;
std::string summary;
Token *listTokensMatches;
public:
Site *sig;
Site *ant;
Site(std::string name, std::string address){
this->name = name;
this->address = address;
this->body = "";
this->summary = "";
this->listTokensMatches = NULL;
this->sig = NULL;
this->ant = NULL;
}
std::string getName()
{
return name;
}
void setName(std::string name){
this->name = name;
}
std::string getAddress(){
return address;
}
void setAddress(std::string address){
this->address = address;
}
std::string getBody(){
return body;
}
void setBody(std::string body){
this->body = body;
}
std::string getSummary()
{
return summary;
}
void setSummary(std::string summary){
this->summary = summary;
}
int getSizeListTokens(){
int intSize = 0;
for(Token *tempTokens = listTokensMatches; tempTokens != NULL; tempTokens = tempTokens->sig){
intSize++;
}
return intSize;
}
void addToken(Token *token){
if(this->listTokensMatches != NULL){
Token *tempTokens = listTokensMatches;
for(tempTokens; tempTokens->sig != NULL; tempTokens = tempTokens->sig){}
token->ant = tempTokens;
tempTokens->sig = token;
}
else{
listTokensMatches = token;
}
}
Token *getToken(int pos){
if(this->listTokensMatches != NULL){
Token *tempTokens = listTokensMatches;
int tempPos = 0;
for(tempTokens; tempTokens->sig != NULL; tempTokens = tempTokens->sig){
if(pos == tempPos){
return tempTokens;
}
}
return NULL;
}
}
Token *getListTokensMatches(){
return this->listTokensMatches;
}
void setListTokensMatches(Token *listTokensMatches){
this->listTokensMatches = listTokensMatches;
}
void open(){
string cmd="start " + this->address;
system(cmd.c_str());
//ShellExecute(NULL, "open", "http://google.com", NULL, NULL, SW_SHOWNORMAL);
}
void print(){
cout<<"Nombre: "<<this->name<<" | Direccion: "<<this->address<<endl;
}
void printAll(){
cout<<"-------------------------------------------------------"<<endl;
cout<<"Nombre: "<<this->name<<" | Direccion: "<<this->address<<endl;
for(Token *tempToken = listTokensMatches; tempToken != NULL; tempToken = tempToken->sig){
cout<<"Token: "<<tempToken->getStrToken()<< " | Apariciones: "<<tempToken->getIntMatches()<<endl;
}
cout<<"-------------------------------------------------------"<<endl;
}
};
class arrayString{
public:
std::string *str[100];
arrayString(){}
arrayString(std::string strArray[100]){
*this->str = strArray;
}
int arraySize(){
int n = sizeof(str)/sizeof(str[0]);
return n;
}
};
// ----------------- END CLASS DECLARATIONS --------------------//
// INTERFACE
void printListTokens(Token *listTokens);
void printListSites(Site *listSites, int optionToPrint);
void searchEngineConsoleView(Site *listSites, string strSearch, int pid);
Site *addSiteToList(Site *listSites, Site *site);
Site *loadWEBSites(Site *listSites);
Site *searchEngine(Site *listSites, Token *listTokens);
Site *searchByWebSite(Site *site, Token *listTokens);
Site *addSiteToList(Site *listSites, Site *site);
size_t writeCallback(char* buf, size_t size, size_t nmemb, void* up);
std::string connectSite(std::string address);
std::string getBody(std::string htmlContent);
void menu(int pid);
void orderListSitesAscending(Site *listSize);
int getSizeOfListSites(Site *listSites);
void printArraySites(Site *arraySites[], int sizeArray);
// END INTERFACE
// ----------------- GET SOURCE CODE FROM THE WEB SITES --------------------//
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp){
char *a = (char*)contents;
char b = ' ';
/*if(strcmp(a, b)){
cout<<"Encontro";
}*/
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
std::string connectSite(std::string address){
std::string readBuffer;
CURL *curl = NULL;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, address.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
readBuffer = getBody(readBuffer);
std::string::iterator end_pos = std::remove(readBuffer.begin(), readBuffer.end(), ' ');
readBuffer.erase(end_pos, readBuffer.end());
}
return readBuffer;
}
std::string getBody(std::string htmlContent){
std::string startBody ("<body");
std::string endBody ("</body>");
std::size_t foundStartBody = htmlContent.find(startBody);
std::size_t foundEndBody = htmlContent.find(endBody);
if (foundStartBody!=std::string::npos && foundEndBody!=std::string::npos){
htmlContent = htmlContent.substr(foundStartBody + 5);
std::string str(">");
std::size_t found = htmlContent.find(str);
if(found!=std::string::npos){
htmlContent = htmlContent.substr(found + 1);
}
foundEndBody = htmlContent.find(endBody);
htmlContent = htmlContent.substr(0, foundEndBody);
}
std::string str2 ("<");
std::string str3 (">");
int i = 1000; ;
// different member versions of find in the same order as above:
std::size_t found = htmlContent.find(str2);
std::size_t found2;
while(found!=std::string::npos){
found2 = htmlContent.find(str3);
if (found2!=std::string::npos){
//std::cout << "Pos <: "<<found << " | Pos >: "<<found2<<htmlContent.substr(found, (found2 - found) +1) << '\n';
htmlContent = htmlContent.substr(0, found) + htmlContent.substr(found2+1);
//std::cout <<"Resultado: "<< htmlContent << '\n';
}
else{
break;
}
if(i == 0)
break;
found = htmlContent.find(str2);
i = i-1;
}
return htmlContent;
}
// ----------------- END GET SOURCE CODE FROM THE WEB SITES --------------------//
// ----------------- SEARCH ENGINE CONSOLE VIEW --------------------//
void searchEngineConsoleView(Site *listSites, string strSearch, int pid){
Token *listTokensToSearch = NULL;
Site *listMatchSites = NULL;
char * cstr = new char [strSearch.length()+1];
std::strcpy(cstr, strSearch.c_str());
// cstr now contains a c-string copy of str
char * p = std::strtok (cstr," ");
for(int i = 0; p!=0; i++)
{
Token *newToken = new Token(p);
if(listTokensToSearch != NULL){
listTokensToSearch->ant = newToken;
newToken->sig = listTokensToSearch;
}
listTokensToSearch = newToken;
//std::cout << p << '\n';
p = std::strtok(NULL," ");
}
delete[] cstr;
//Site *listSites = loadWEBSites(NULL);
listMatchSites = searchEngine(listSites, listTokensToSearch);
cout<<">> Lista de coincidencias <<"<<endl;
//printListSites(listMatchSites, 1);
orderListSitesAscending(listMatchSites);
//printListTokens(listTokensToSearch);
}
// ----------------- END SEARCH ENGINE CONSOLE VIEW --------------------//
// ----------------- METHODS TO PRINT LIST --------------------//
void printListTokens(Token *listTokens){
int i = 0;
for(Token *tempListTokens = listTokens; tempListTokens != NULL; tempListTokens = tempListTokens->sig){
cout<<i<<" | ";
tempListTokens->print();
i++;
}
}
void printListSites(Site *listSites, int option){
int c = 0;
for(Site *tempSites = listSites; tempSites!=NULL;tempSites = tempSites->sig){
if(option == 0){
cout<<c<<"- ";
tempSites->print();
c++;
}
else if(option == 1){
tempSites->printAll();
}
}
}
void printArraySites(Site *arraySites[], int sizeArray){
for(int pos = 0; pos < sizeArray; pos ++){
arraySites[pos]->printAll();
}
}
// ----------------- END METHODS TO PRINT LIST --------------------//
// ----------------- METHOD TO GET THE SIZE OF THE LIST SITES --------------------//
int getSizeOfListSites(Site *listSites){
int intSize = 0;
for(Site *tempSites = listSites; tempSites!=NULL;tempSites = tempSites->sig){
intSize++;
}
return intSize;
}
// ----------------- END METHOD TO GET THE SIZE OF THE LIST SITES --------------------//
// ----------------- METHOD TO ORDER THE LIST IN ASCENDING --------------------//
void orderListSitesAscending(Site *listSites){
// Convert from a list to Array of web sites
Site *temp;
int Nelementos = getSizeOfListSites(listSites);
Site *arraySites[Nelementos]; int i = 0;
for(Site *tempSite = listSites; tempSite!=NULL; tempSite = tempSite->sig){
temp = new Site(tempSite->getName(), tempSite->getAddress());
temp->setListTokensMatches(tempSite->getListTokensMatches());
arraySites[i] = temp;
i++;
}
// END listToArray
int j; //Variables contadoras del ciclo.
temp=NULL; //Variable temporal.
for (i=1;i<Nelementos;i++)
{
for (j=0; j < Nelementos-1 ;j++)
{
if (arraySites[j]->getSizeListTokens() < arraySites[j+1]->getSizeListTokens())//Condicion mayor-menor
{
temp=arraySites[j];
arraySites[j]=arraySites[j+1];
arraySites[j+1]=temp;
}
}
}
printArraySites(arraySites, Nelementos);
}
// ----------------- END METHOD TO ORDER THE LIST IN ASCENDING --------------------//
// ----------------- METHOD TO ADD WEB SITE TO LIST --------------------//
Site *addSiteToList(Site *listSites, Site *site){
if(listSites == NULL){
listSites = site;
}
else{
Site *tempListSites = listSites;
for(tempListSites; tempListSites->sig != NULL; tempListSites = tempListSites->sig){}
if(tempListSites!=NULL && tempListSites->sig == NULL){
site->ant = tempListSites;
tempListSites->sig = site;
}
}
return listSites;
}
// ----------------- END METHOD TO ADD WEB SITE TO LIST --------------------//
// ----------------- METHODS TO LOAD WEB SITES ADDRESS FROM A TEXT FILE --------------------//
Site *loadWEBSites(Site *listSites){
//char cadena[128];
// Crea un fichero de salida
//ofstream fs("WEBSites.txt");
// Enviamos una cadena al fichero de salida:
//fs << "Hola, mundo" << endl;
// Cerrar el fichero,
// para luego poder abrirlo para lectura:
//fs.close();
// Abre un fichero de entrada
//ifstream fe("WEBSites.txt");
//char cadena[128];
std::string cadena;
ifstream fe("WEBSites.txt");
std::string name, address;
std::string separator ("|");
while(!fe.eof()) {
fe >> cadena;
std::size_t foundSeparator= cadena.find(separator);
if (foundSeparator!=std::string::npos){
name = cadena.substr(0, foundSeparator);
address = cadena.substr(foundSeparator+1);
Site *tempSite = new Site(name, address);
tempSite->setBody(connectSite(address));
listSites = addSiteToList(listSites, tempSite);
}
//cout << "Nombre: "<<name <<" | Dirección: "<<address<< endl;
}
fe.close();
return listSites;
// Leeremos mediante getline, si lo hiciéramos
// mediante el operador << sólo leeríamos
// parte de la cadena:
//fe.getline(cadena, 128);
//cout << cadena << endl;
}
// ----------------- METHODS TO LOAD WEB SITES ADDRESS FROM A TEXT FILE --------------------//
Site *searchEngine(Site *listSites, Token *listTokens){
bool match;Site *listMatchSites = NULL;
Site *tempSiteToAdd;
for(Site *tempSite = listSites; tempSite != NULL; tempSite = tempSite->sig){
tempSiteToAdd = searchByWebSite(tempSite, listTokens);
if(tempSiteToAdd->getListTokensMatches() != NULL)
{
//cout<<"SE ENCONTRARON VARIOS TOKENS en:"<<endl;
//tempSiteToAdd->print();
listMatchSites = addSiteToList(listMatchSites, tempSiteToAdd);
}
}
return listMatchSites;
}
Site *searchByWebSite(Site *site, Token *listTokens){
bool match;
Site *newSite = new Site(site->getName(), site->getAddress());
newSite->setAddress(site->getAddress());
int numberMatches;
for(Token *tempToken = listTokens; tempToken != NULL; tempToken = tempToken->sig){
//
numberMatches = 0;
string strToken = tempToken->getStrToken();
string strTitle = site->getName();
string strBody = site->getBody();
//cout<<"TITLE"<<endl;
for(int n = 0; n < strTitle.length(); n++){
if(strTitle[n] == strToken[0]){
int tempN = n; match = true;
for(int m = 0; m < strToken.length(); m++){
//cout<< strTitle[tempN] << " == "<< strToken[m]<<endl;
if(tempN >= strTitle.length() || strTitle[tempN] != strToken[m]){
match = false;
break;
}
tempN = tempN + 1;
}
if(match){
numberMatches ++;
}
}
}
//cout<<"BODY:"<<strBody<<endl;
for(int n = 0; n < strBody.length(); n++){
if(strBody[n] == strToken[0]){
int tempN = n; match = true;
for(int m = 0; m < strToken.length(); m++){
//cout<< strBody[tempN] << " == "<< strToken[m]<<endl;
if(tempN >= strBody.length() || strBody[tempN] != strToken[m]){
match = false;
break;
}
tempN = tempN + 1;
}
if(match){
numberMatches ++;
//cout<<">> Match in: "<<strToken<<endl;
}
}
}
//
if(numberMatches > 0){
//cout<<">> Match in: "<<strToken<<endl;
Token *newToken = new Token(tempToken->getStrToken(), numberMatches);
newSite->addToken(newToken);
}
}
return newSite;
}
void master(int nprocs){
//printf("Soy el procesador %d\n",pid);
char option[10];
option[0] = '0';
MPI_Status status;
//while(option!="9"){
cout<<"**** Menu ****"<<endl;
cout<<"1. Busqueda"<<endl;
cout<<"2. Ayuda"<<endl;
cout<<"9. Salir"<<endl;
cout<<"Escoja una opcion: ";
//cin.get(option)
//std::getline(cin, option);
cin.getline(option, 10);
cout<<"************"<<endl;
if(option[0] == '1'){
cout<<"Busqueda: ";
//string strSearch;
char strSearch[100];
//std::getline(cin, strSearch);
std::cin.get(strSearch, 100);
for (int intRank = 1; intRank < nprocs; intRank++) {
MPI_Send(strSearch, 100, MPI_CHAR, intRank, 0, MPI_COMM_WORLD);
}
}
/*int result;
MPI_Recv(&result, // message buffer
1, // one data item
MPI_INT, // data item is a double real
MPI_ANY_SOURCE, // receive from any sender
1, // receive any type of message
MPI_COMM_WORLD, // always use this
&status); // info about received message*/
}
void slave(int pid, int nprocs){
/*MPI::COMM_WORLD.Probe(0, 0, status);
int l = status.Get_count(MPI::CHAR);
char *buf = new char[l];*/
MPI_Status status;
char strSearch[100];
MPI_Recv(strSearch, 100, MPI_CHAR, 0, 0, MPI_COMM_WORLD, &status);
Site *listSites = loadWEBSites(NULL);
searchEngineConsoleView(listSites, strSearch, pid);
/*if(option == "2"){
}*/
}
// ----------------- MAIN --------------------//
int main(int argc, char **argv){
int pid, nprocs;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &pid);
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
//
// Have Process 0 say hello.
//
//printf("Soy el procesador %d de un total de %d\n",pid,nprocs);
/*Site *google = new Site("Google", "google.com");
listSites = google;
Site *facebook = new Site("Facebook", "http://facebook.com");
listSites->sig = facebook;*/
//facebook->open();
//connectSite(google->getAddress());
//printListSites(listSites);
//if(pid == 0){
if (pid == 0) {
master(nprocs);
} else {
slave(pid, nprocs);
}
//}
/*tipoToken *listTokensMatches = NULL;
tipoToken *tempTokens = listTokensMatches;*/
cout << "End program!" << endl;
MPI_Finalize();
return 0;
}
// ----------------- END MAIN --------------------//