Commit f3265975 authored by Seblu's avatar Seblu
Browse files

sls architecture

parent cae882a6
Loading
Loading
Loading
Loading
+10 −7
Original line number Diff line number Diff line
bin_PROGRAMS=	sls

sls_SOURCES=	sls.hh			\
		sls.cc			\
		server.cc		\
		parser.cc		\
		getopt.cc		\
		error.cc
sls_SOURCES=	src/sls.hh		\
		src/sls.cc		\
		src/option.hh		\
		src/option.cc		\
		src/database.hh		\
		src/database.cc		\
		src/server.hh		\
		src/serer.cc


CLEANFILES= *~ '\#*'

sls/trunk/getopt.cc

deleted100644 → 0
+0 −93
Original line number Diff line number Diff line
//
// getopt.cc for sls
//
// Made by Seblu
// Login   <seblu@epita.fr>
//
// Started on  Tue Jun  5 22:36:50 2007 Seblu
// Last update Thu Jun  7 02:10:37 2007 Seblu
//

#include <stdlib.h>
#include <errno.h>
#include "sls.hh"

const int GetOption::min_argc = 4;

void GetOption::usage(const char *argv0)
{
  //print usage
  std::cerr << "Usage: "
	    << *argv0
	    << " -f <acces file> -p <port number> [-v] [-h]"
	    << std::endl;
  std::cerr << "-f : password map file" << std::endl;
  std::cerr << "-p : Port where server listen" << std::endl;
  std::cerr << "-v : Verbose mode" << std::endl;
  std::cerr << "-h : Display this messages" << std::endl;
}

GetOption::GetOption(int argc, char *argv[]) : port_ (0), verbose_ (false)
{
  // parse arg line
  for (int i = 1; i < argc; ++i) {
    if (!strcmp(argv[i], "-h")) {
      usage(*argv);
      exit(EXIT_OK);
    }
    else if (!strcmp(argv[i], "-v")) {
      verbose_ = true;
    }
    else if (!strcmp(argv[i], "-p")) {
      if (++i >= argc)
	throw Error("No enough argument for option -p.");
      char *endptr;
      port_ = strtol(argv[i], &endptr, 10);
      if (!(*argv[i] != '\0' && *endptr == '\0'))
	throw Error("Unable to convert port to a number.");
    }
    else if (!strcmp(argv[i], "-f")) {
      if (++i >= argc)
	throw Error("No enough argument for option -f.");
      accessfile_ = string(argv[i]);
    }
    else {
      Error *err = new Error();
      *err << "Invalid options : " << argv[i] << ".";
      throw *err;
    }
  }

  // Check validy of arguement
  if (port_ == 0)
    throw Error("No server port specified.");
  if (port_ < 1 || port_ > 65535)
    throw Error("Bad server port number (1 - 65535).");
  if (accessfile_ == "")
    throw Error("No access file specified.");

  // print info in verbose mode
  if (verbose_) {
    std::cout << "Server listen port is : " << port_ << "." << std::endl;
    std::cout << "Server access file : " << accessfile_ << "." << std::endl;
    std::cout << "Verbose mode : " << verbose_ << "." << std::endl;
  }
}

int
GetOption::port() const
{
  return port_;
}

const string&
GetOption::accessfile() const
{
  return accessfile_;
}

bool
GetOption::verbose() const
{
  return verbose_;
}

sls/trunk/sls.cc

deleted100644 → 0
+0 −55
Original line number Diff line number Diff line
//
// sls.cc for sls
//
// Made by Seblu
// Login   <seblu@epita.fr>
//
// Started on  Fri Jun  1 02:11:00 2007 Seblu
// Last update Thu Jun  7 02:54:58 2007 Seblu
//

#include "sls.hh"

int main(int argc, char *argv[])
{
  GetOption *getopt;
  Parser *parser;
  SLServer *server;

  // Check arg line count
  if (argc < GetOption::min_argc) {
    GetOption::usage(*argv);
    return EXIT_USAGE;
  }

  // Parse command line options
  try { getopt = new GetOption(argc, argv); }
  catch (const Error &e) {
    std::cerr << "ERROR: " << e.message() << std::endl;
    GetOption::usage(*argv);
    return EXIT_USAGE;
  }

  // Init server
  try { server = new SLServer(getopt->port()); }
  catch (const Error &e) {
    std::cerr << "ERROR: " << e.message() << std::endl;
    return e.code();
  }

  // Parse access database file
  try { parser = new Parser(getopt->accessfile(), *server, getopt->verbose()); }
  catch (const Error &e) {
    std::cerr << "ERROR: " << e.message() << std::endl;
    return e.code();
  }

  // Free parser and getopter
  delete getopt;
  delete parser;

  server->print_access();

  // Run server
  return server->run();
}

sls/trunk/sls.conf

deleted100644 → 0
+0 −1
Original line number Diff line number Diff line
seblu:toto:1
 No newline at end of file

sls/trunk/sls.hh

deleted100644 → 0
+0 −124
Original line number Diff line number Diff line
//
// sls.hh for sls
//
// Made by Seblu
// Login   <seblu@epita.fr>
//
// Started on  Mon Jun  4 14:32:04 2007 Seblu
// Last update Thu Jun  7 03:19:27 2007 Seblu
//

#ifndef SLS_HH
# define SLS_HH

# include <cassert>
# include <ios>
# include <iostream>
# include <fstream>
# include <sstream>
# include <map>

typedef std::string string;
typedef std::stringstream sstream;
typedef std::ostream ostream;
typedef std::ifstream ifstream;

enum {
  EXIT_OK = 0,
  EXIT_USAGE = 1,
  EXIT_FILE = 3,
  EXIT_UNKNOWN = 255
};

// -----------------------------------------------------------------------------
// ERROR CLASS
// -----------------------------------------------------------------------------

class Error
{
public:
  Error(int i);
  Error(string s = "", int i = EXIT_UNKNOWN);
  Error(const Error &e);
  void release() const;
  string message() const;
  int code() const;
  void code(int);
  Error &operator=(const Error &rhs);
  friend ostream &operator<<(ostream &o, const Error &e);
  friend Error &operator<<(Error &e, const string &s);
  friend Error &operator>>(Error &e, const string &s);
  friend Error &operator<<(Error &e, int val);
protected:
  sstream msg_;
  int val_;
};

// -----------------------------------------------------------------------------
// SLSERVER CLASS
// -----------------------------------------------------------------------------

class SLServer
{

  static const int backlog;

  struct AccessData
  {
    string client_pw;
    string server_pw;
    int privilege;
  };

  typedef std::map<string, AccessData *> AccessMap;

public:
  SLServer(int port);
  int run();

  void add_access(const char *login, const char *pw_server,
		 const char *pw_client, int priv);
  void del_access(const string &login);
  void print_access() const;
protected:
  int port_;
  AccessMap accessDB_;

  void connect();

};

// -----------------------------------------------------------------------------
// GETOPT CLASS
// -----------------------------------------------------------------------------
class GetOption
{
public:
  static void usage(const char *argv0);
  static const int min_argc;

  GetOption(int argv, char *argv[]);
  int port() const;
  bool verbose() const;
  const string& accessfile() const;

protected:
  int port_;
  string accessfile_;
  bool verbose_;
};

// -----------------------------------------------------------------------------
// PARSER CLASS
// -----------------------------------------------------------------------------
class Parser
{
public:
  Parser(const string filename, SLServer &server, bool verbose = false);
protected:
  string filename_;
  SLServer &server_;
  bool verbose_;
};

#endif
Loading