00001 // ******************************************************************* 00002 // Broker.cpp 00003 // ******************************************************************* 00004 // 00005 // DESCRIPTION: API for Broker class. Used by clients of the 00006 // DocConversion project to broker a conversion 00007 // of a given document. 00008 // NOTE: This program makes use of strings, streams and 00009 // Broker.h. 00010 // 00011 /*************************************************************************** 00012 * Copyright (C) 2003 by drs. Eric D. Schabell * 00013 * erics@cs.kun.nl * 00014 * * 00015 * This program is free software; you can redistribute it and/or modify * 00016 * it under the terms of the GNU General Public License as published by * 00017 * the Free Software Foundation; either version 2 of the License, or * 00018 * (at your option) any later version. * 00019 ***************************************************************************/ 00020 00021 //-------------------------------------------------------------------- 00022 //#includes 00023 //-------------------------------------------------------------------- 00024 #include <iostream> // file accessing. 00025 #include <string> // STL strings. 00026 #include <pqxx/all.h> // postgresql api. 00027 #include "Broker.h" 00028 #include "Server.h" 00029 00030 using namespace std; 00031 using namespace pqxx; 00032 00033 00034 //-------------------------------------------------------------------- 00035 //Variables 00036 //-------------------------------------------------------------------- 00037 int Broker::brokerCounter = 0; 00038 const string Broker::DEFAULT_NAME = "127.0.0.1"; 00039 00040 00041 //-------------------------------------------------------------------- 00042 //Purpose: default constructor. 00043 //Preconditions: none. 00044 //Postconditions: a Broker object is instantiated. 00045 //Arguments: none. 00046 //Returns: Broker. 00047 //Called Funcs: increaseCounter(). 00048 //-------------------------------------------------------------------- 00049 Broker::Broker() 00050 { 00051 // assign static address and ip. 00052 serverName = DEFAULT_NAME; 00053 Broker::increaseCounter(); 00054 return; 00055 } 00056 00057 //-------------------------------------------------------------------- 00058 //Purpose: constructor with a (remote) server name given. 00059 //Preconditions: must supply a valid server hostname. 00060 //Postconditions: a Broker object is instantiated with a 00061 // remote server hostname. 00062 //Arguments: string name. 00063 //Returns: Broker. 00064 //Called Funcs: increaseCounter(). 00065 //-------------------------------------------------------------------- 00066 Broker::Broker( const string name ) 00067 { 00068 // assign static address and ip. 00069 serverName = name; 00070 Broker::increaseCounter(); 00071 return; 00072 } 00073 00074 //-------------------------------------------------------------------- 00075 //Purpose: default destructor. 00076 //Preconditions: Broker object. 00077 //Postconditions: Broker object destroyed. 00078 //Arguments: none. 00079 //Returns: 0 00080 //Called Funcs: decreaseCounter(). 00081 //-------------------------------------------------------------------- 00082 Broker::~Broker() 00083 { 00084 // remove ourselves from the list. 00085 Broker::decreaseCounter(); 00086 return; 00087 } 00088 00089 //-------------------------------------------------------------------- 00090 //Purpose: obtain the Broker Counter's current value. 00091 //Preconditions: none. 00092 //Postconditions: returns brokerCounter value. 00093 //Arguments: none. 00094 //Returns: const int brokerCounter. 00095 //Called Funcs: none. 00096 //-------------------------------------------------------------------- 00097 const int Broker::getCounter() 00098 { 00099 return brokerCounter; 00100 }; 00101 00102 //-------------------------------------------------------------------- 00103 //Purpose: obtain the name of the server which the Broker 00104 // is brokering with. 00105 //Preconditions: none. 00106 //Postconditions: returns serverName. 00107 //Arguments: none. 00108 //Returns: const string serverName. 00109 //Called Funcs: none. 00110 //-------------------------------------------------------------------- 00111 const string Broker::getServerName() 00112 { 00113 return serverName; 00114 } 00115 00116 //-------------------------------------------------------------------- 00117 //Purpose: prepare for conversion of the provided Document, 00118 // and pass to Server for actual converison. 00119 //Preconditions: Document object. 00120 //Postconditions: returns true if conversion successfull, false if not. 00121 // Converted document location is registered in the 00122 // Document.conLocation attribute. 00123 //Arguments: Document& doc. 00124 //Returns: bool. 00125 //Called Funcs: Document.getDocumentConversion(), 00126 // getServerName(), Server(), 00127 // Server.requestConversion(). 00128 //-------------------------------------------------------------------- 00129 bool Broker::convertDocument( Document& doc ) 00130 { 00131 // check if conversion is set. 00132 if ( doc.getDocumentConversion() == "0" ) 00133 { 00134 00135 // conversion has not been set, error out. 00136 cout << "Broker Error: no conversion has been assigned to this Document yet..."; 00137 cout << endl; 00138 return false; 00139 } 00140 00141 // need to have a server now. 00142 Server myConversionServer( serverName ); 00143 00144 if ( !( myConversionServer.requestConversion( doc ) ) ) 00145 { 00146 cout << "Broker Error: File was not converted by Server..." << endl; 00147 return false; 00148 ; 00149 } 00150 else 00151 { 00152 return true; 00153 } 00154 } 00155 00156 //-------------------------------------------------------------------- 00157 //Purpose: to increase the brokerCounter attribute. 00158 //Preconditions: none. 00159 //Postconditions: brokerCounter attribute has increased by one. 00160 //Arguments: none. 00161 //Returns: 0. 00162 //Called Funcs: none. 00163 //-------------------------------------------------------------------- 00164 void Broker::increaseCounter() 00165 { 00166 brokerCounter++; 00167 } 00168 00169 //-------------------------------------------------------------------- 00170 //Purpose: to decrease the brokerCounter attribute. 00171 //Preconditions: none. 00172 //Postconditions: brokerCounter attribute has decreased by one. 00173 //Arguments: none. 00174 //Returns: 0. 00175 //Called Funcs: none. 00176 //-------------------------------------------------------------------- 00177 void Broker::decreaseCounter() 00178 { 00179 if ( brokerCounter == 1 ) 00180 { 00181 brokerCounter = 0; 00182 return; 00183 } 00184 00185 brokerCounter--; 00186 return; 00187 } 00188 00189 //-------------------------------------------------------------------- 00190 //Purpose: obtain list of conversions from Server.. 00191 //Preconditions: Server must be available. 00192 //Postconditions: returns Result object contianing the list of available 00193 // conversions. 00194 // This is a 5 column answer: 00195 // routine_name, 00196 // from_feature_type, 00197 // to_feature_type, 00198 // from_representation_type, 00199 // to_representation_type. 00200 //Arguments: none. 00201 //Returns: Result. 00202 //Called Funcs: Connection(), Transaction(), Result(), 00203 // Transaction.Exec(), Transaction.Commit(). 00204 //-------------------------------------------------------------------- 00205 Result Broker::getConversionListing() 00206 { 00207 // 00208 // Just call the server to do the work and assume it works.. 00209 // 00210 Server myConversionServer( serverName ); 00211 Result myResultsListing = myConversionServer.listConversionDB(); 00212 return myResultsListing; 00213 } 00214 00215 00216 //-------------------------------------------------------------------- 00217 //Purpose: obtain list of available conversions in conversion 00218 // database based on given representation type.. 00219 //Preconditions: Server must be available. 00220 //Postconditions: returns Result object contianing the list of available 00221 // conversions. 00222 // This is a 5 column answer: 00223 // routine_name, 00224 // from_feature_type, 00225 // to_feature_type, 00226 // from_representation_type, 00227 // to_representation_type. 00228 //Arguments: none. 00229 //Returns: Result. 00230 //Called Funcs: Broker(), Server.listRepresentaionTypes( string representationType ). 00231 //-------------------------------------------------------------------- 00232 Result Broker::getRepresentationTypeListing( string representationType ) 00233 { 00234 // 00235 // Just call the server to do the work and assume it works.. 00236 // 00237 Server myConversionServer( serverName ); 00238 Result myRepTypeListing = myConversionServer.listRepesentationTypes( representationType ); 00239 return myRepTypeListing; 00240 } 00241 00242 00243 //-------------------------------------------------------------------- 00244 //Purpose: obtain list of available conversions in conversion 00245 // database based on given feature type.. 00246 //Preconditions: Server must be available. 00247 //Postconditions: returns Result object contianing the list of available 00248 // conversions. 00249 // This is a 5 column answer: 00250 // routine_name, 00251 // from_feature_type, 00252 // to_feature_type, 00253 // from_representation_type, 00254 // to_representation_type. 00255 //Arguments: none. 00256 //Returns: Result. 00257 //Called Funcs: Broker(), Server.listFeatureTypes( string featureType ). 00258 //-------------------------------------------------------------------- 00259 Result Broker::getFeatureTypeListing( string featureType ) 00260 { 00261 // 00262 // Just call the server to do the work and assume it works.. 00263 // 00264 Server myConversionServer( serverName ); 00265 Result myFeatureTypeListing = myConversionServer.listFeatureTypes( featureType ); 00266 return myFeatureTypeListing; 00267 }