Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members   Examples  

Server Class Reference

The server class. More...

#include <server.h>

List of all members.

Public Methods

 Server (const string &config_fn) throw (runtime_error)
 Constructor. More...

 ~Server ()
bool ok_to_index (const string &path, ostream &err) const
 Does path refer to a file suitable for indexing. More...

bool serve (iostream &ios)
 Perform a single session with an iostream (usually a Socket) ios. More...

const string & config_fn () const
const string & index_fn () const
const string & ignore_fn () const
const string & log_fn () const
const int port () const
const string::size_type wordlength () const
const string::size_type max_cmd () const
const string error () const
const string ok () const
ostream & log () const
const Indexindex () const
Indexindex ()


Detailed Description

The server class.

Note that it does not use any network classes or functions. The core member function is serve(iostream&) which handles a single connection.

Definition at line 16 of file server.h.


Constructor & Destructor Documentation

Server::Server const string &    conf_fn throw (runtime_error)
 

Constructor.

The configuration should contain the following parameters:

 portnr -- not interpreted, usually port# of serversocket
 index  -- filename of index (this file need not exist)
 wordlength -- size of words used in the index
 ignore     -- filename containing words to be ignored
 logfile    -- name of logfile
 maxcmd -- maximal size (in chars) of a commandline
                   (optional, default is 2048).
Parameters:
config_fn  pathname of server configuration file.
Exceptions:
runtime_error  is some parameters are missing from the configuration file, some file cannot be opened etc.

Definition at line 31 of file server.C.

00031                                                          :
00032  config_fn_(conf_fn), max_cmd_(2048), log_(0) {
00033 ifstream ifs(config_fn().c_str());
00034 if (!ifs)
00035   throw runtime_error(config_fn() + ": cannot open");
00036 Util::Props     config(ifs);
00037 
00038 port_ = config["portnr"];
00039 index_fn_ = config["index"];
00040 wordlength_ = config["wordlength"];
00041 ignore_fn_ = config["ignore"];
00042 log_fn_ = config["logfile"];
00043 if (config("maxcmd"))
00044   max_cmd_ = config["maxcmd"];
00045 
00046 log_ = new Dv::Util::logstream(log_fn(), index_fn() );
00047 if (!*log_)
00048   throw runtime_error(log_fn() + ": cannot open");
00049 
00050 // Build index structure.
00051 index_ = new Index(wordlength());
00052 
00053 // Get words to be ignored and add to index.
00054 ifstream ignore_is(ignore_fn().c_str());
00055 if (!ignore_is)
00056   throw runtime_error(ignore_fn() + ": cannot open");
00057 index().ignore(ignore_is);
00058 
00059 // Read index structure from file, if it exists and can be read.
00060 ifstream index_is(index_fn().c_str());
00061 if (index_is)
00062   index_is >> index();
00063 }

Server::~Server  
 

Definition at line 65 of file server.C.

00065                 {
00066 delete log_;
00067 delete index_;
00068 }


Member Function Documentation

bool Server::ok_to_index const string &    path,
ostream &    err
const
 

Does path refer to a file suitable for indexing.

Parameters:
path  to check.
err  stream to write error messages to
Returns:
true iff the following conditions hold:
  • File f(path) succeeds and f.exists() returns true.
  • f.type() == File::REGULAR
  • ifstream ifs(path) succeeds.
  • the first 100 chars of the file are printable or space ('
    ' etc.).

Definition at line 71 of file server.C.

References error().

Referenced by serve().

00071                                                           {
00072 try {
00073   Util::File f(path);
00074   f.expand();
00075 
00076   if (!f.exists()) {
00077     err << error() << path << ": does not exist";
00078     return false;
00079     }
00080   if (f.type() != Util::File::REGULAR) {
00081     err << error() << path << ": not a regular file";
00082     return false;
00083     }
00084   ifstream ifs(path.c_str());
00085   if (!ifs) {
00086     err << error() << path << ": cannot open for reading";
00087     return false;
00088     }
00089   for (size_t i=0; (i<100); ++i) {
00090     char c(ifs.get());
00091     if (c==EOF)
00092       break;
00093     if (isprint(c) || isspace(c)) {
00094       }
00095     else {
00096       err << error() << path << ": not a text file";
00097       return false;
00098       }
00099     }
00100   }
00101 catch (...) {
00102   return false;
00103   }
00104 return true;
00105 }

bool Server::serve iostream &    ios
 

Perform a single session with an iostream (usually a Socket) ios.

Returns:
true iff "stop" command was not received.

Definition at line 109 of file server.C.

References error(), Query::exec(), Index::Files, getmaxline(), index(), index_fn(), Index::insert(), log(), max_cmd(), ok(), and ok_to_index().

Referenced by main().

00109                            {
00110 string line;
00111 while (getmaxline(ios, line, max_cmd())) {
00112   if (line.size() > max_cmd() ) {
00113     log() << error() << "command too long, max = " << max_cmd() << endl;
00114     return true;
00115     }
00116 
00117   if (line.size() == 0) {
00118     log() << error() << "empty command" << endl;
00119     return true;
00120     }
00121 
00122   log() << "CMD: " << line << endl;
00123   
00124   istringstream iss(line);
00125 
00126   string command;
00127   iss >> command;
00128 
00129   if (command == "stop") {
00130     log() << "STOP" << endl;
00131     ios << ok() << endl;
00132     return false;
00133     }
00134   else if (command == "save") {
00135     log() << "SAVE" << endl;
00136     ofstream index_os(index_fn().c_str());
00137     if (index_os) {
00138       index_os << index();
00139       index_os.close();
00140           log() << ok() << endl;
00141           ios << ok() << endl;
00142           }
00143     else {
00144       log() << "ERROR SAVING" << endl;
00145       ios << error() << "cannot save to \"" << index_fn() << "\"" << endl;
00146       }
00147     }
00148   else if (command == "add") {
00149     string path;
00150     iss >> path;
00151     ostringstream oss;
00152     if (ok_to_index(path, oss)) {
00153       Util::File f(path); // Should succeed.
00154       path = f.expand().str();
00155       // Check whether path is not already indexed.
00156       time_t ti(index().date(path)); // 0 if not found.
00157       time_t tn(f.last_modified());
00158       if (tn>ti) { // File modified since it was last indexed.
00159         ifstream ifs(f.path());
00160         index().insert(f.str(),ifs);
00161         }
00162       log() << ok() << endl;
00163       ios << ok() << endl;
00164       }
00165     else {
00166       log() << oss.str() << endl;
00167       ios << oss.str() << endl;
00168       }
00169     }
00170   else if (command == "query") {
00171     string qs; // Query text.
00172     getline(iss,qs);
00173     log() << "QUERY " << qs << endl;
00174     try {
00175       Query q(index(), qs);
00176       const Index::Files& r(q.exec());
00177       log() << "ANSWER: " << r.size() << " FILES" << endl;
00178       for (Index::Files::const_iterator f=r.begin(); f!=r.end(); ++f) 
00179         ios << **f << endl;
00180       // end with empty line
00181       ios << endl;
00182       }
00183     catch (exception& e) {
00184       log() << error() << e.what() << endl;
00185       }
00186     }
00187   else {
00188     ios << error() << "unknown command: \"" << command << "\""  << endl;
00189     log() << error() << "unknown command: \"" << command << "\""  << endl;
00190     }
00191   }
00192 return true;
00193 }

const string& Server::config_fn   const [inline]
 

Definition at line 54 of file server.h.

00054 { return config_fn_; }

const string& Server::index_fn   const [inline]
 

Definition at line 55 of file server.h.

Referenced by serve().

00055 { return index_fn_; }

const string& Server::ignore_fn   const [inline]
 

Definition at line 56 of file server.h.

00056 { return ignore_fn_; }

const string& Server::log_fn   const [inline]
 

Definition at line 57 of file server.h.

00057 { return log_fn_; }

const int Server::port   const [inline]
 

Definition at line 58 of file server.h.

Referenced by main().

00058 { return port_; }

const string::size_type Server::wordlength   const [inline]
 

Definition at line 59 of file server.h.

00059 { return wordlength_; }

const string::size_type Server::max_cmd   const [inline]
 

Definition at line 60 of file server.h.

Referenced by serve().

00060 { return max_cmd_; } 

const string Server::error   const [inline]
 

Definition at line 61 of file server.h.

Referenced by ok_to_index(), and serve().

00061 { return "ERROR "; }

const string Server::ok   const [inline]
 

Definition at line 62 of file server.h.

Referenced by serve().

00062 { return "OK"; }

ostream& Server::log   const [inline]
 

Definition at line 63 of file server.h.

Referenced by main(), and serve().

00063 { return *log_; }

const Index& Server::index   const [inline]
 

Definition at line 64 of file server.h.

Referenced by serve().

00064 { return *index_; }

Index& Server::index   [inline]
 

Definition at line 65 of file server.h.

00065 { return *index_; }


The documentation for this class was generated from the following files:
textindexer-0.2 [27 March, 2002]