/*
 * Copyright (C) 2015 by the University of Southern California
 * $Id: server_proxy.hh 24224 2015-05-28 22:17:46Z liangzhu $
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * 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. 
 * 
 */

#ifndef SERVER_PROXY_HH
#define SERVER_PROXY_HH

#include <event2/event.h>
#include <openssl/ssl.h>
#include "safe_queue.hh"
#include "safe_map.hh"
#include "client.hh"

#define WRITE_TIMEOUT  60
#define READ_TIMEOUT   60

struct svr_arg_t {
  int svr_fd;
  safe_map<int, client_t *> *clt_map;
  safe_queue<int> *id_q;
  SSL_CTX *ctx;
  struct event_base *base;
};

struct clt_arg_t{
  safe_map<int, client_t *> *clt_map;
  safe_queue<int> *id_q;
  int svr_fd;
  SSL_CTX *svr_ctx;
};

class server_proxy {
public:
  server_proxy(const char *, int, const char *, int, 
	       int, const char *, const char *);
  ~server_proxy();
  void start();

private:
  const char *svr_ip;
  const char *lis_ip;
  const char *conn_str;
  const char *key_file;
  const char *cert_file;
  struct event_base *base;
  struct sockaddr_in tcp_addr;

  SSL_CTX *ctx; 
  int svr_port;
  int lis_port;
  int conn_type;
  
  int svr_fd;
  int clt_fd;
  pid_t mypid;
  safe_queue<int> *id_q;
  safe_map<int, client_t *> *clt_map;
  
  void check_input();
  void start_tcp();
  void start_ssl();
  struct event *start_server_event(void *);
};

#endif //SERVER_PROXY_HH

