/*
 * sslServer.h
 *
 *  Created on: Feb 9, 2012
 *      Author: jirka
 */


#ifndef SSLSERVER_H_
#define SSLSERVER_H_

#include <string>
#include <pthread.h>
#define PASSWORD "1234"
#define CERTIFICATE_NAME "cert.pem"
#define CERTIFICATE_KEY "certKey.pem"
#define CERTIFICATE_CA "certCA.pem"
#define PARAMS_DH "dh1024.pem"
#define BUFF_LENGTH 10000
int verify_callback(int ok, X509_STORE_CTX* store);
int password_callback(char* buffer, int num, int rwflag, void* userdata);
void * sslReader(void* input);
//typedef std::basic_string<char> string;
using namespace std;
class sslServer
{
  SSL_CTX *ctx;                                                 // Kontext spojeni
  BIO* bio;                                                     // IO struktura
  X509 *cert;                                                   // Certifikat

  string pathCert;                                              //Cesty k certifikatum
  string pathKey;
  string pathCA;

  string receivedData;                                          //Prijata data
  string pathDHParam;                                           //Cesta k parametrizacnimu souboru

  int port;                                                     //Port
  bool isConnected;                                             //Flag spojeni

  pthread_t reader;                                             //Cteci vlakno
  sslServer();                                                  //Vychozi konstruktor

  pthread_cond_t dataRdyCond;                                   //Zamky a podminene promenne
  pthread_mutex_t readerLock;
  pthread_cond_t connectionCond;
  pthread_mutex_t connectionLock;

public:
  sslServer(int port, char* confPath, char* pass);              //Konstruktor
  bool Init(int port, char* confPath, char* pass);              //Inicializacni funkce
  bool WriteData(string data);                                  //Zapise data do spojeni
  string ReadData(void);                                        //Cteni dat ze spojeni
  int AvailableData(void);                                      //Vraci mnozstvi prijatych dat
  bool CheckConnectionState(void);                              //Vraci informace o stavu spojeni

  /* Metody vraci ukazatele na zamky a podminene promenne */
  pthread_mutex_t *GetConnectionLock(void);
  pthread_cond_t* GetConnectionCond(void);
  pthread_mutex_t *GetReaderLock(void);
  pthread_cond_t* GetReaderCond(void);
  virtual
  ~sslServer();                                                 //destruktor

  friend int verify_callback(int ok, X509_STORE_CTX* store);    //Verifikacni callback
  friend int password_callback(char* buffer, int num, int rwflag, void* userdata);      //Callback kontroly hesla
  friend void * sslReader(void* input);                                                 //Funkce vlakna pro cteni
};

#endif /* SSLSERVER_H_ */
