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 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
|
// -*- mode: c++ -*-
/*
GIFT, a flexible content based image retrieval system.
Copyright (C) 1998, 1999, 2000, 2001, 2002, CUI University of Geneva
Copyright (C) 2003, 2004 Bayreuth University
2005 Bamberg University
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/** my way of throwing exceptions */
#include "libMRML/include/mrml_const.h"
#include "libMRML/include/my_throw.h"
#include "libMRML/include/my_assert.h"
#include "libMRML/include/my_diagnose.h"
#include "libMRML/include/GIFTExceptions.h"
#include <cstdio>
#include <iterator>
//#include "expat/xmlparse/xmlparse.h"
#include <expat.h>
#include "libGIFTQuInvertedFile/include/CQInvertedFile.h"
#include "libMRML/include/CSessionManager.h"
#include "libMRML/include/CAttributeList.h"
#include "libMRML/include/CXMLHelper.h"
#include "libMRML/include/GIFTExceptions.h"
#include "libMRML/include/CQueryTreeNode.h"
#include <stdlib.h>
#include "libMRML/include/CAlgorithm.h" //getPropertySheet
#include "libMRML/include/directory.h" // directorys as determined by ./configure
#include "libMRML/include/createErrorMessage.h"
#include "libMRML/include/gift-config.h"
#ifdef HAVE_LIBUUID
extern "C"{
typedef unsigned char* uuid_t ;
void uuid_generate(uuid_t);
void uuid_unparse(uuid_t,char*);
}
#endif
//the "ID administration subsystem"
TID gID;
void startConfigElement(void *userData,
const char *inElementName,
const char **inAttributes)
{
bool lSuccessful=true;
//the calling CSessionManager
CSessionManager* lCaller((CSessionManager*)userData);
#ifdef CONFIG_PRINTOUT
cout << "<"
<< inElementName
<< " WhileReading=\"true\" "
<< flush;
#endif
CAttributeList lAttributes(inAttributes);
if(string(inElementName)==
string(mrml_const::cui_property_list)){
lCaller->startPropertySheetSubtree();
}
//if we have started a property sheet list
//everything goes there
if(lCaller->getPropertySheetSubtree()){
if((string(inElementName)==
string(mrml_const::cui_property_item))
||
(string(inElementName)==
string(mrml_const::property_sheet))){
lCaller->getPropertySheetSubtree()
->addChild(inElementName,
inAttributes);
}
}
// / removing for removing cui-algorithm-id-list-list
// //
// // a list of algorithms
// //
// if(string(inElementName)
// ==
// string(mrml_const::cui_algorithm_id_list)){
// string lListID=lAttributes[mrml_const::cui_algorithm_id_list_id];
// if(lListID.size()){
// lCaller->addAlgorithmIDList(lListID);
// }
// }
// if(string(inElementName)
// ==
// string(mrml_const::cui_algorithm_id)){
// cout << "HIER++" << endl;
// string lID=lAttributes[mrml_const::cui_algorithm_id];
// if(lID.size()){
// lCaller->addToCurrent(lID);
// }
// }
// if(string(inElementName)
// ==
// string(mrml_const::cui_algorithm_id_list_list)){
// cout << "HIER" << endl;
// }
if(string(inElementName)
==
string(mrml_const::algorithm_list)){
//nothing
}
// if(string(inElementName)
// ==
// string(mrml_const::algorithm)
// || (lCaller->getAlgorithmInConstruction())
// ){
// {
// if(!lCaller->getAlgorithmInConstruction()){
// lCaller->startAlgorithmConstruction(new CAlgorithm(inElementName,
// inAttributes));
// }else{
// lCaller->getAlgorithmInConstruction()->addChild(inElementName,
// inAttributes);
// }
// }
// }
#ifdef CONFIG_PRINTOUT
cout << " >"
<< endl
<< flush;
#endif
}
void endConfigElement(void *userData, const char *inElementName)
{
CSessionManager* lCaller((CSessionManager*)userData);
// if((string(inElementName)
// ==
// string(mrml_const::algorithm))
// ||lCaller->getAlgorithmInConstruction()){
// {
// my_assert(lCaller->getAlgorithmInConstruction(),
// "Algorithm in construction not found");
// lCaller->getAlgorithmInConstruction()->moveUp();
// if(lCaller->getAlgorithmInConstruction()->isSubtreeFinished()){
// lCaller->addAlgorithm(lCaller->getAlgorithmInConstruction());
// lCaller->startAlgorithmConstruction((CAlgorithm*)0);
// }
// }
// }
//if we have started a property sheet list
//everything goes there
if(lCaller->getPropertySheetSubtree()){
if((string(inElementName)==
string(mrml_const::cui_property_item))
||
(string(inElementName)==
string(mrml_const::property_sheet))){
lCaller->getPropertySheetSubtree()
->moveUp();
}
}
if(string(inElementName)==
string(mrml_const::cui_property_list)){
lCaller->endPropertySheetSubtree();
}
#ifdef CONFIG_PRINTOUT
cout << "</"
<< inElementName
<< " >"
<< endl;
#endif
}
#include <ctime>
#include "libMRML/include/CSessionManager.h"
#include "libMRML/include/TID.h"
#include <iostream>
extern TID gID;
string getNewID(){
#ifdef HAVE_LIBUUID
char lUUIDString[200];
unsigned char lUUIDBinary[16];
uuid_t lUUID(lUUIDBinary);
uuid_generate(lUUID);
uuid_unparse(lUUID,
lUUIDString);
return lUUIDString;
#else
char lID[20];
sprintf(lID,"%ld",++gID);
return lID;
#endif
}
/** */
CQueryTreeBuilder CSession::mQueryTreeBuilder;
ostream& CSession::write(ostream& outStream)const{
mMutexSession.lock();
outStream << mUser
<< " "
<< mID
<< " "
<< mSessionName
<< " "
//<< mActiveAlgorithm
<< endl;
mMutexSession.unlock();
return outStream;
};
istream& CSession::read(istream& inStream){
mMutexSession.lock();
inStream >> mUser
>> mID
>> mSessionName;
mMutexSession.unlock();
return inStream;
//>> mActiveAlgorithm
;
};
bool CSession::open(){
mMutexSession.lock();
if(mIsOpen){
return false;
}
mIsOpen=true;
mMutexSession.unlock();
return mIsOpen;
};
bool CSession::close(){
mMutexSession.lock();
mIsOpen=false;
mMutexSession.unlock();
return true;
};
bool CSession::rename(const string& inName){
mMutexSession.lock();
setSessionName(inName);
mMutexSession.unlock();
return true;
};
string CSession::toXML(bool isPrivate)const{
mMutexSession.lock();
CXMLHelper lHelper;
lHelper.setName(mrml_const::session);
lHelper.setEmpty();
lHelper.addAttribute(mrml_const::session_id,mID);
lHelper.addAttribute(mrml_const::session_name,mSessionName);
if(isPrivate){
lHelper.addAttribute("activealgorithm",mActiveAlgorithm->getID());
}
mMutexSession.unlock();
return lHelper.toString();
}
void CSession::clearLanguages(){
mLanguages.clear();
}
void CSession::addLanguage(string inLanguageCode){
mLanguages.push_back(inLanguageCode);
}
/** commit the list of languages. That means, here the
actual language that will be used throughout the
translation is determined */
void CSession::commitLanguages(const CI18nTranslator& inTranslator){
for(CLanguageList::const_iterator i=mLanguages.begin();
i!=mLanguages.end();
i++){
if(inTranslator.hasLanguage(*i)){
mPreferredLanguage=*i;
break;
}
}
};
list<string> CSession::getLanguages()const{
return mLanguages;
}
string CSession::getPreferredLanguage()const{
return mPreferredLanguage;
}
//
CSession::CSession():
mActiveAlgorithm(0),
mSessionName(""),
mID(""),
mIsOpen(0),
mUser(""),
mPreferredLanguage("de")
{
mMutexSession.lock();
// nothing: use it with read
/** The information about the current query tree */
mQueryTree.first=0;
mQueryTree.second=0;
mMutexSession.unlock();
};
//
CSession::CSession(string inUser,
string inID,
string inSessionName,
CAlgorithm* inActiveAlgorithm):
mPreferredLanguage("de")
{
// mutex not needed. if two call this
// at the same time, there is some
// s... happening any way.
mUser=inUser;
mID=inID;
mSessionName=inSessionName;
mActiveAlgorithm=inActiveAlgorithm;
mQueryTree.first=0;
mQueryTree.second=0;
mIsOpen=false;
};
/** delete all members */
CSession::~CSession(){
// mutex not needed. if two call this
// at the same time, there is some
// s... happening any way.
delete mQueryTree.first;
delete mQueryTree.second;
delete mActiveAlgorithm;
}
//
string CSession::getUser()const{
return mUser;
};
void CSession::setUser(const string& inUser){
mMutexSession.lock();
mUser=inUser;
mMutexSession.unlock();
};
string CSession::getID()const{
// mutex not needed. if two call this
// at the same time, there is some
// s... happening any way.
return mID;
};
void CSession::setID(const string& inID){
// mutex not needed. if two call this
// at the same time, there is some
// s... happening any way.
mID=inID;
};
string CSession::getSessionName()const{
return mSessionName;
};
void CSession::setSessionName(const string& inSessionName){
mMutexSession.lock();
mSessionName=inSessionName;
mMutexSession.unlock();
};
CAlgorithm* CSession::getActiveAlgorithm(){
return mActiveAlgorithm;
};
bool CSession::setActiveAlgorithm(CAccessorAdminCollection& inCaller,
CAlgorithmCollection& inAlgorithmCollection,
CAlgorithm* inActiveAlgorithm,
CStaticQueryFactory& inBaseTypeFactory){
mMutexSession.lock();
assert(inActiveAlgorithm);
// inActiveAlgorithm->check();
{
if(mQueryTree.first){
CQueryContainer::iterator lFound=mQueryTree.first->find(inActiveAlgorithm->getID());
if(lFound!=mQueryTree.first->end()){
mQueryTree.first->erase(lFound);
}
}
delete mQueryTree.second;
mQueryTree.second=0;
}
cout << "[set" << endl;
mActiveAlgorithm=inActiveAlgorithm;
if(inActiveAlgorithm->stringReadAttribute(mrml_const::algorithm_type).first){
cout << inActiveAlgorithm->stringReadAttribute(mrml_const::algorithm_type).second
<< endl;
cout << "set]" << endl;
cout << "[build"
<< endl;
mActiveAlgorithm->configure(inAlgorithmCollection);
string lString;
mActiveAlgorithm->toXML(lString);
cout << "-Algorithm CONFIGURED-: configuration"
<< endl
<< lString;
cout << "-Algorithm CONFIGURED-"
<< endl;
mQueryTree=mQueryTreeBuilder.buildQueryTree(*inActiveAlgorithm,
inCaller,
inBaseTypeFactory,
0);
}
cout << "build]" << endl;
mMutexSession.unlock();
return true;
};
//--------------------------------------------------
CXMLElement* CSession::query(CSessionManager& inCaller,
const CXMLElement& inQuery){
try{
mMutexSession.lock();
// i.e. if a user opens a session twice, he it is impossible to
// do two queries in parallel on this server
pair<bool,string> lAlgorithmID=inQuery.stringReadAttribute(mrml_const::algorithm_id);
if(lAlgorithmID.first){
cout << "THE ID "
<< lAlgorithmID.first
<< ";"
<< lAlgorithmID.second
<< endl;
mQueryTree.first->checkNPrint();
CQueryTreeNode* lQuery(mQueryTree.first->getQueryByID(lAlgorithmID.second));
CXMLElement* lReturnValue(lQuery->query(inQuery));
mMutexSession.unlock();
return lReturnValue;
}else{
CQueryTreeNode* lQuery(mQueryTree.first->getQueryByID("adefault"));
CXMLElement* lReturnValue(lQuery->query(inQuery));
mMutexSession.unlock();
return lReturnValue;
}
}
catch(GIFTException* inException){
cerr << "We have caught a GIFTException" << endl;
cout << "We have caught a GIFTException" << endl;
return createErrorMessage(inException->getMessage());
}
catch(GIFTException& inException){
cerr << "We have caught a GIFTException" << endl;
cout << "We have caught a GIFTException" << endl;
return createErrorMessage(inException.getMessage());
}
catch(...){
cout << "We have caught a non-GIFTException" << endl;
cerr << "We have caught a non-GIFTException" << endl;
exit(10);
}
};
//--------------------------------------------------
// retrieving random images as seeds
CXMLElement* CSession::getRandomImages(CSessionManager& inCaller,
const string& inAlgorithmID,
int inNumberOfImages){
mMutexSession.lock();
CXMLElement lQuery(mrml_const::query_step,
0);
lQuery.addAttribute(mrml_const::result_size,
long(inNumberOfImages));
CXMLElement* lReturnValue(mQueryTree.first->getQueryByID(inAlgorithmID)->query(lQuery));
mMutexSession.unlock();
return lReturnValue;
};
ostream& operator<<(ostream& outStream,
const CSession& inSession){
inSession.write(outStream);
return outStream;
}
///
CSessionManager::CSessionManager(string inSessionFileName,
string inConfigFileName,
string inI18nFileName):
mPropertySheetList(0),
mPropertySheetSubtree(0),
mAccessorAdminCollection(inConfigFileName),
CAlgorithmCollection(inConfigFileName),
mI18nTranslator(inI18nFileName),
mBaseTypeFactory(new CDynamicQueryFactory(__LIBDIR__)){
ifstream lSessions(inSessionFileName.c_str());
ifstream lConfigFile(inConfigFileName.c_str());
lConfigFile.seekg(0,ios::end);
int lSize=lConfigFile.tellg();
char lConfigFileContent[lSize+1];
lConfigFile.seekg(0,ios::beg);
lConfigFile.read(lConfigFileContent,
lSize);
lConfigFileContent[lSize]=0;
#ifdef PRINTOUT_CONFIG
cout << "I did it: I read the config file:"
<< lConfigFileContent
<< endl
<< flush;
#endif
XML_Parser lParser = XML_ParserCreate(NULL);//default encoding
XML_SetUserData(lParser,
this);
XML_SetElementHandler(lParser,
startConfigElement,
endConfigElement);
int lDone=true;
do {
if (!XML_Parse(lParser,
lConfigFileContent,
lSize,
lDone)) {
cerr << "CSessionManager: XML ERROR: "
<< XML_ErrorString(XML_GetErrorCode(lParser))
<< " at line "
<< XML_GetCurrentLineNumber(lParser)
<< endl;
exit(1);// this happens only when parsing the config file
}
} while (!lDone);
XML_ParserFree(lParser);
//
#ifdef CONFIG_PRINTOUT
cout << endl
<< endl
<< "Before generating XML"
<< endl
<< endl;
cout << toXML()
<< endl;
#endif
}
///
string CSessionManager::toXMLSessions(const string& inUser,
bool isPrivate)const{
mMutexSessionManager.lock();
string lReturnValue;
CXMLHelper lHelper;
lHelper.setName("ssessionlist");
if(isPrivate){
lHelper.addAttribute("user",inUser);
}
lReturnValue+=lHelper.toString();
const list<CSession>& lList=mUserToSessions.find(inUser)->second;
for(list<CSession>::const_iterator i=lList.begin();
i!=lList.end();
i++){
lReturnValue+=i->toXML(isPrivate);
}
lHelper.setEnd();
lReturnValue+=lHelper.toString();
mMutexSessionManager.unlock();
return lReturnValue;
}
string CSessionManager::toXML(bool isPrivate)const{
mMutexSessionManager.lock();
string lReturnValue(CAlgorithmCollection::toXML(isPrivate));
//lReturnValue += convertPropertyToXML(isPrivate);
//lReturnValue += CAlgorithmIDListList::toXML();
lReturnValue += mAccessorAdminCollection.toXML(isPrivate);
mMutexSessionManager.unlock();
return lReturnValue;
}
/** creates a full property sheet out of the data in this.
string CSessionManager::convertPropertyToXML(bool isPrivate)const{
string lReturnValue("\n\n<!-- generated by convertPropertyToXML -->\n");
//0. The root of the property sheet which we want to generate
CXMLElement lRootProperty(mrml_const::property_sheet,0);
lRootProperty.addAttribute("caption","Algorithms");
lRootProperty.addAttribute(mrml_const::property_sheet_type,"subset");
lRootProperty.addAttribute(mrml_const::property_sheet_id,"all-algorithms");
lRootProperty.addAttribute(mrml_const::send_type,"element");
lRootProperty.addAttribute(mrml_const::send_value,"algorithm");
// 2. make out of each algorithm in the collection a property sheet.
for(CAlgorithmCollection::CContent::const_iterator i=
CAlgorithmCollection::begin();
i!=CAlgorithmCollection::end();
i++){
//
CAlgorithm* lAlgorithm(i->second);
string lType(lAlgorithm->getType().second);
my_diagnose("in for loop: Algorithm type is " << lType);
my_assert(lAlgorithm && lType.size(),
"In the config file");
// 2.a. make a new XMLElement, initialise it with the algorithm
// base settings
CXMLElement* lAlgorithmSheet=new CXMLElement("property");
assert(lAlgorithmSheet);
//set the caption
lAlgorithmSheet->addAttribute("caption",
lAlgorithm
->stringReadAttribute("name").second);
lAlgorithmSheet->addAttribute("type","setelement");
//send the type of the algorithm
lAlgorithmSheet->addAttribute("sendtype","attribute");
lAlgorithmSheet->addAttribute("sendname","type");
lAlgorithmSheet->addAttribute("sendvalue",lAlgorithm->getType().second);
//send also an automatically generated id.
lAlgorithmSheet->addAttribute("autoid","yes");
lAlgorithmSheet->addAttribute("autoidname","id");
// 2.a. which collections can be used with this algorithm?
// look this up, make this the first child.
// ---- this is the part I dread:
// ---- this piece of code is amazingly ugly.
// ---- but please remember: this happens only once in a session
// ---- and this is what happens, if you change EVERYTHING and
// ---- you would like interfaces to stay about the same....
// ---- frustration starts to get visible here...
CXMLElement* lCollectionSheet=new CXMLElement("property");
//set the caption
lCollectionSheet->addAttribute("caption",
"Collections");
lCollectionSheet->addAttribute("type","subset");
lCollectionSheet->addAttribute("id","coll"+lAlgorithm->getType().second);
lCollectionSheet->addAttribute("sendtype","element");
lCollectionSheet->addAttribute("sendname","algorithm");
//go over all collections
for(CAccessorAdminCollection::const_iterator
j=mAccessorAdminCollection.begin();
j!=mAccessorAdminCollection.end();
j++){
my_diagnose("checking for collection "
<<
j->second->getAlgorithmIDListID());
//look if this algorithm is allowed for this collection
//if yes, than add this to the collectionNode
// j->first is the first element of the list
bool lIsMember=CAlgorithmIDListList::isIDListMember(j->second->getAlgorithmIDListID(),
lType);
if(lIsMember){
//here we insert something containing the right caption
CXMLElement* lCollectionSheetElement=new CXMLElement(mrml_const::property_sheet);
//set the caption
lCollectionSheetElement->addAttribute(mrml_const::caption,
j->second->getName());
lCollectionSheetElement->addAttribute(mrml_const::property_sheet_type,"set-element");
//send the type of the collection
lCollectionSheetElement->addAttribute(mrml_const::send_type,"attribute");
lCollectionSheetElement->addAttribute(mrml_const::send_name,mrml_const::collection_id);
lCollectionSheetElement->addAttribute(mrml_const::send_value,j->second->getID());
lCollectionSheet->addChild(lCollectionSheetElement);
lCollectionSheet->moveUp();
}
}
// 2.b. make the newly made lCollectionSheet a new child of the algorithm
// sheet
lAlgorithmSheet->addChild(lCollectionSheet);
lAlgorithmSheet->moveUp();
// ---- now things are getting a bit cleaner again
// 2.c. the property sheet chosen in the config
// look this up, if it exsists make it the second child.
{
pair<bool,string> lUserConfigID=
lAlgorithm->stringReadAttribute("vi-more-config");
if(lUserConfigID.first){
CXMLElement* lUserConfiguredProperties=
mPropertySheetList->newPropertySheet(lUserConfigID.second);
if(lUserConfiguredProperties){
lAlgorithmSheet->addChild(lUserConfiguredProperties);
lAlgorithmSheet->moveUp();
}
}
}
// 2.d. make the newly made lAlgorithmSheet a new child of the root.
lRootProperty.addChild(lAlgorithmSheet);
lRootProperty.moveUp();
}
lRootProperty.toXML(lReturnValue);
lReturnValue+="\n\n<!-- END: generated by convertPropertyToXML -->\n";
return lReturnValue;
}*/
///
pair<string,string> CSessionManager::toXMLHandshake(const string& inUser){
assert(!"I think I should delete this");
mMutexSessionManager.unlock();
//
pair<string,string> lReturnValue;
//need to make a new default session:
lReturnValue.first =newSession(inUser,
"");
lReturnValue.second+="<"+mrml_const::cui_shandshake+">";
lReturnValue.second+=toXMLSessions(inUser,false);//this is going to be public
lReturnValue.second+=toXML(false);//this is going to be public
lReturnValue.second+="</"+mrml_const::cui_shandshake+">";
mMutexSessionManager.unlock();
return lReturnValue;
}
///
string CSessionManager::toXMLSessionConfiguration()const{
string lReturnValue;
CXMLHelper lHelper;
assert(!"here is no code!");
return lReturnValue;
}
///
istream& CSessionManager::read(istream& inStream){
mMutexSessionManager.lock();
while(!inStream.eof() && inStream){
CSession lSession;
lSession.read(inStream);
if(lSession.getUser().size()){
mUserToSessions[lSession.getUser()].push_back(lSession);
}
}
mMutexSessionManager.unlock();
};
///
ostream& CSessionManager::write(ostream& outStream){
mMutexSessionManager.lock();
//for each list
for(map<string,list<CSession> >::const_iterator i=mUserToSessions.begin();
i!=mUserToSessions.end();
i++){
//write the list
copy(i->second.begin(),
i->second.end(),
ostream_iterator<CSession>(outStream));
}
mMutexSessionManager.unlock();
};
string CSessionManager::newSession(const string& inUser,
const string& inSessionName){
mMutexSessionManager.lock();
char now[20];
sprintf(now,"%ld",time(0));
string lID=getNewID();
lID=this->newSession(lID,
inUser,
inSessionName);
mMutexSessionManager.unlock();
return lID;
};
string CSessionManager::newSession(const string& inID,
const string& inUser,
const string& inSessionName){
mMutexSessionManager.lock();
char now[20];
sprintf(now,"%ld",time(0));
string lID=inID;
string lSessionName("Session"+string(now));
if(inSessionName.size()){
lSessionName=inSessionName;
}
CSession lDefault(inUser,
lID,
lSessionName,
0);
mUserToSessions[inUser].push_back(lDefault);
//a pointer to the same session
mIDToSession[lID]=&(mUserToSessions[inUser].back());
mIDToSession[lID]->open();
mIDToSession[lID]->setActiveAlgorithm(mAccessorAdminCollection,
*this,
makeDefaultAlgorithm(),
*mBaseTypeFactory);
mMutexSessionManager.unlock();
return inID;
};
///
CXMLElement* CSessionManager::openSession(string inUserName,
string inSessionID,
string inSessionName){
mMutexSessionManager.lock();
cout << "ENTERING OPENSESSION"
<< endl;
CXMLElement* lReturnValue(0);
string lReturnID("");
if(inSessionID.size()){
CIDToSession::const_iterator lFound(mIDToSession.find(inSessionID));
if(lFound!=mIDToSession.end()){
if(lFound->second->open())
cout << "USING AN OLD SESSION" << endl;
lReturnID=inSessionID;
}else{
cout << "making a new session I" << endl;
lReturnID= newSession(inUserName,
inSessionName);
}
}else{
cout << "making a new session II" << endl;
lReturnID= newSession(inUserName,
inSessionName);
}
if(lReturnID.size()){
lReturnValue=new CXMLElement(mrml_const::acknowledge_session_op,0);
if(lReturnValue){
lReturnValue->addAttribute(mrml_const::session_id,
lReturnID);
}
}else{
lReturnValue=new CXMLElement(mrml_const::error,0);
assert(lReturnValue);
lReturnValue->addAttribute(mrml_const::message,
"Open session: The session with name:"
+inSessionName
+" and ID:"
+inSessionID
+"could not be opened");
}
// this is added for making parsing log files easier,
// and to resolve possible ambiguities in log files.
if(lReturnValue){
lReturnValue->addAttribute(mrml_const::session_name,
inSessionName);
lReturnValue->addAttribute(mrml_const::user_name,
inUserName);
}
mMutexSessionManager.unlock();
return lReturnValue;
};
///
CXMLElement* CSessionManager::renameSession(string inSessionID,
string inSessionName){
mMutexSessionManager.lock();
CXMLElement* lReturnValue(0);
string lReturnID("");
if(inSessionID.size()){
CIDToSession::const_iterator lFound(mIDToSession.find(inSessionID));
if(lFound!=mIDToSession.end()){
if(lFound->second->rename(inSessionName))
lReturnID=inSessionID;
}
}
if(lReturnID.size()){
lReturnValue=new CXMLElement(mrml_const::acknowledge_session_op,0);
assert(lReturnValue);
lReturnValue->addAttribute(mrml_const::session_id,
lReturnID);
}else{
lReturnValue=new CXMLElement(mrml_const::error,0);
assert(lReturnValue);
lReturnValue->addAttribute(mrml_const::message,
"Rename session: The session with name: `"
+inSessionName
+"' and ID: `"
+inSessionID
+"' could not be renamed");
}
mMutexSessionManager.unlock();
return lReturnValue;
};
///
CXMLElement* CSessionManager::closeSession(string inSessionID){
mMutexSessionManager.lock();
string lReturnID;
if(inSessionID.size()){
CIDToSession::iterator lFound(mIDToSession.find(inSessionID));
if(lFound->second->close()){
lReturnID=inSessionID;
}
}
CXMLElement* lReturnValue=new CXMLElement(mrml_const::acknowledge_session_op,0);
lReturnValue->addAttribute(mrml_const::session_id,
lReturnID);
mMutexSessionManager.unlock();
return (CXMLElement*)0;
}
CXMLElement* CSessionManager::deleteSession(string inSessionID){
mMutexSessionManager.lock();
CXMLElement* lReturnValue(0);
bool lSuccess=false;
cout << endl << "BEFORE BEFORE Deleting session " << inSessionID << endl;
if(inSessionID.size()){
CIDToSession::iterator lFound(mIDToSession.find(inSessionID));
cout << endl << "BEFORE Deleting session " << inSessionID << endl;
if(lFound!=mIDToSession.end()){
lSuccess=true;
string lUser(lFound->second->getUser());
mIDToSession.erase(lFound);
cout << endl << "Deleting session " << inSessionID << " for user " << lUser << endl;
CUserToSessions::iterator lUserFound=mUserToSessions.find(lUser);
assert(lUserFound!=mUserToSessions.end());
for(list<CSession>::iterator i=lUserFound->second.begin();
i!=lUserFound->second.end();
i++){
if(i->getID()==inSessionID){
lUserFound->second.erase(i);
break;
}
}
cout << "after break" << endl;
}
}
if(lSuccess){
lReturnValue=new CXMLElement(mrml_const::acknowledge_session_op,0);
assert(lReturnValue);
lReturnValue->addAttribute(mrml_const::session_id,
inSessionID);
}else{
lReturnValue=new CXMLElement(mrml_const::error,0);
assert(lReturnValue);
lReturnValue->addAttribute(mrml_const::message,
"Close session: The session with ID: `"
+inSessionID
+"' could not be deleted");
}
mMutexSessionManager.unlock();
return lReturnValue;
};
bool CSessionManager::setAlgorithm(const string& inSessionID,
CAlgorithm* inAlgorithm){
mMutexSessionManager.lock();
//this should also kick of the
//creation of a suitable query tree
cout << "still here inSessionID: _"
<< inSessionID
<< "_"
<< flush
<< endl;
CIDToSession::const_iterator lFound(mIDToSession.find(inSessionID));
// assert(lSession);
if(lFound==mIDToSession.end()){
my_throw(VENotFound("Could not find Session "));
//
// the following code has been deleted, as it would
// generate a security problem: you could flood the GIFT
// by querying with a host of unknown session IDs
//
// //create a session with the required sessionID
// lSession=mIDToSession[this->newSession(inSessionID,
// "I-dunno",
// "I-know-even-less")];
// // if this fails this is a real programming mistake
// // it should never happen
// // please don't delete
// assert(lSession);
}
// if this fails this is a real bug
// it should never happen
// please don't delete
assert(lFound->second);
bool lResult=lFound->second->setActiveAlgorithm(mAccessorAdminCollection,
*this,
inAlgorithm,
*mBaseTypeFactory);
mMutexSessionManager.unlock();
return lResult;
};
bool CSessionManager::clearSessionLanguages(const string& inSessionID){
mMutexSessionManager.lock();
//this should also kick of the
//creation of a suitable query tree
CIDToSession::const_iterator lFound(mIDToSession.find(inSessionID));
// assert(lSession);
if(lFound==mIDToSession.end()){
my_throw(VENotFound("Could not find Session"));
}
// if this fails this is a real bug
// it should never happen
// please don't delete
assert(lFound->second);
lFound->second->clearLanguages();
mMutexSessionManager.unlock();
return true;
};
bool CSessionManager::addSessionLanguage(const string& inSessionID,
const string& inLanguageCode){
mMutexSessionManager.lock();
//this should also kick of the
//creation of a suitable query tree
CIDToSession::const_iterator lFound(mIDToSession.find(inSessionID));
// assert(lSession);
if(lFound==mIDToSession.end()){
my_throw(VENotFound("Could not find Session"));
}
// if this fails this is a real bug
// it should never happen
// please don't delete
assert(lFound->second);
lFound->second->addLanguage(inLanguageCode);
mMutexSessionManager.unlock();
return true;
};
bool CSessionManager::commitSessionLanguages(const string& inSessionID){
mMutexSessionManager.lock();
//this should also kick of the
//creation of a suitable query tree
CIDToSession::const_iterator lFound(mIDToSession.find(inSessionID));
// assert(lSession);
if(lFound==mIDToSession.end()){
my_throw(VENotFound("Could not find Session"));
}
// if this fails this is a real bug
// it should never happen
// please don't delete
assert(lFound->second);
lFound->second->commitLanguages(mI18nTranslator);
mMutexSessionManager.unlock();
return true;
};
list<string> CSessionManager::getSessionLanguages(const string& inSessionID)const{
mMutexSessionManager.lock();
//this should also kick of the
//creation of a suitable query tree
CIDToSession::const_iterator lFound(mIDToSession.find(inSessionID));
// assert(lSession);
if(lFound==mIDToSession.end()){
my_throw(VENotFound("Could not find Session"));
}
// if this fails this is a real bug
// it should never happen
// please don't delete
assert(lFound->second);
list<string> lResult=lFound->second->getLanguages();
mMutexSessionManager.unlock();
return lResult;
};
CXMLElement* CSessionManager::query(const string& inSessionID,
const CXMLElement& inRelevanceLevelList){
// this is function not completely locked, because the functions called do not neccessarily
// need a lock. LOCKING THIS FUNCTION COMPLETELY WOULD MEAN LOSING CONCURRENCY.
//mMutexSessionManager.lock();//debugging
CIDToSession::const_iterator lFound(mIDToSession.find(inSessionID));
//mMutexSessionManager.unlock();
//
try{
if(lFound==mIDToSession.end()){
#ifdef GIFT_THROW_UNKNOWN_SESSION
// cout << "throwing: "
// <<(VEUnknownSession(inSessionID.c_str()))
// << endl
// << flush;
my_throw(VEUnknownSession((string(Could not process query: unknown session)
+inSessionID).c_str()));
#else
//cout << "NOT throwing: " << endl;
// my_throw(VEUnknownSession(inSessionID.c_str()));
// cerr << "this line should not be reached"
// << endl;
// assert(0);
CXMLElement* lError(new CXMLElement(mrml_const::error,0));
lError->addAttribute(mrml_const::message,"Could not process query: unknown session (ID:"+inSessionID+").");
return lError;
#endif
}else{
//cout << "querying: " << endl;
CXMLElement* lReturnValue(lFound->second->query(*this,
inRelevanceLevelList));
//mMutexSessionManager.unlock();//debugging
return lReturnValue;
}
}
catch(GIFTException& inCaught){
//cout << "CAUGHT3" << endl;
return createErrorMessage(inCaught);
}
catch(GIFTException* inCaught){
//cout << "CAUGHT4" << endl;
return createErrorMessage(inCaught);
}
};
//--------------------------------------------------
// retrieving random images as seeds
CXMLElement* CSessionManager::getRandomImages(const string& inSessionID,
const string& inAlgorithm,
int inNumberOfImages){
//
mMutexSessionManager.lock();
CIDToSession::const_iterator lFound=mIDToSession.find(inSessionID);
mMutexSessionManager.unlock();
//
if(lFound==mIDToSession.end()){
#ifdef GIFT_THROW_UNKNOWN_SESSION
cout << "throwing: "
<<(VEUnknownSession(inSessionID.c_str()))
<< endl
<< flush;
#else
my_throw(VEUnknownSession(inSessionID.c_str()));
CXMLElement* lError(new CXMLElement("error",0));
lError->addAttribute("message","Could not process query: unknown session (ID:"+inSessionID+").");
#endif
}
//
return lFound->second->getRandomImages(*this,
inAlgorithm,
inNumberOfImages);
};
CQuery* CSessionManager::makeQuery(const string& inBaseType,
CAlgorithm& inAlgorithm){
mMutexSessionManager.lock();
CQuery* lReturnValue=mBaseTypeFactory->makeQuery(inBaseType,
mAccessorAdminCollection,
inAlgorithm);
mMutexSessionManager.unlock();
return lReturnValue;
}
//----------------------------------------
/** Building a property sheet list
I consider building a list on a call by call basis as
tedious. So what I do is:
I first build a tree, and then I break down this tree
into our list.
This function makes a new tree
*/
void CSessionManager::startPropertySheetSubtree(){
//don't FIXME this should work without any if in real c++
delete mPropertySheetSubtree;
mPropertySheetSubtree=new CXMLElement(mrml_const::cui_property_list,
0);
};
/** This function gets us the current property sheet subtree.
*/
CXMLElement* CSessionManager::getPropertySheetSubtree(){
return mPropertySheetSubtree;
};
/** This function does the actual breaking down into a
CPropertySheetList.
*/
void CSessionManager::endPropertySheetSubtree(){
mPropertySheetList=new CPropertySheetList(mPropertySheetSubtree);
mPropertySheetSubtree=0;
};
/** gets a property sheet for the algorithm inAlgorithmID */
CXMLElement* CSessionManager::getPropertySheet(string /*ignored inSessionID*/,
string inAlgorithmID)const{
mMutexSessionManager.lock();
CAlgorithm& lAlgorithm(getAlgorithmByType(inAlgorithmID));
CXMLElement* lReturnValue(0);
if(lAlgorithm.stringReadAttribute(mrml_const::cui_property_sheet_id).first){
lReturnValue=mPropertySheetList->newPropertySheet(lAlgorithm
.stringReadAttribute(mrml_const::cui_property_sheet_id).second);
}
mMutexSessionManager.unlock();
return lReturnValue;
};
CXMLElement* CSessionManager::getCollections()const{
mMutexSessionManager.lock();
cout << "getcollections"
<< endl;
CXMLElement* lReturnValue(mAccessorAdminCollection.toXMLElement());
mMutexSessionManager.unlock();
return lReturnValue;
};
/** */
CXMLElement* CSessionManager::getAlgorithms()const{
mMutexSessionManager.lock();
CXMLElement* lReturnValue(((CAlgorithmCollection*)this)->toXMLElement());
mMutexSessionManager.unlock();
return lReturnValue;
};
/** i18n: get the list of preferred languages of this session */
void CSessionManager::translate(string inSessionID,
CXMLElement& inoutToBeTranslated)const{
mMutexSessionManager.lock();
//this should also kick of the
//creation of a suitable query tree
CIDToSession::const_iterator lFound(mIDToSession.find(inSessionID));
// assert(lSession);
if(lFound==mIDToSession.end()){
//my_throw(VENotFound("Could not find Session"));
cerr << "Unknown session " << inSessionID << endl;
mMutexSessionManager.unlock();
return;
}
// if this fails this is a real bug
// it should never happen
// please don't delete
assert(lFound->second);
mI18nTranslator.translateXMLTree(lFound->second->getPreferredLanguage(),
inoutToBeTranslated);
mMutexSessionManager.unlock();
};
|