#include #include #include #include #include #include "Port.hh" #include "TcpLayer_socketFactory.hh" #include "loggers.hh" TcpLayer_socketLayer::TcpLayer_socketLayer(const std::string & p_type, const std::string & param) : SSL_Socket(), Layer(p_type), PORT(p_type.c_str()), _params(), _client_id{-1}, _time_key("TcpLayer_socketLayer::Handle_Fd_Event_Readable") { loggers::get_instance().log(">>> TcpLayer_socketLayer::TcpLayer_socketLayer: %s, %s", to_string().c_str(), param.c_str()); // Setup parameters Params::convert(_params, param); _params.log(); set_socket_debugging(false); Params::const_iterator it = _params.find(Params::debug); if (it == _params.cend()) { _params.insert(std::pair(std::string("debug"), "0")); } else if (it->second.compare("1") == 0) { set_socket_debugging(true); } it = _params.find(Params::server); if (it == _params.cend()) { _params.insert(std::pair(std::string("server"), "127.0.0.1")); } if (!parameter_set(Params::server.c_str(), _params[Params::server].c_str())) { loggers::get_instance().warning("TcpLayer_socketLayer::set_parameter: Unprocessed parameter: %s", Params::server.c_str()); } it = _params.find(Params::port); if (it == _params.cend()) { _params.insert(std::pair(std::string("port"), "80")); } if (!parameter_set(Params::port.c_str(), _params[Params::port].c_str())) { loggers::get_instance().warning("TcpLayer_socketLayer::set_parameter: Unprocessed parameter: %s", Params::server.c_str()); } set_ssl_use_ssl(false); it = _params.find(Params::use_ssl); if (it == _params.cend()) { _params.insert(std::pair(std::string("use_ssl"), "0")); } else if (it->second.compare("1") == 0) { set_ssl_use_ssl(true); } parameter_set(use_connection_ASPs_name(), "no"); parameter_set(server_backlog_name(), "1024"); set_server_mode(false); set_ttcn_buffer_usercontrol(true); set_handle_half_close(true); set_ssl_use_ssl(false); map_user(); _client_id = open_client_connection(_params[Params::server].c_str(), _params[Params::port].c_str(), NULL, NULL); } TcpLayer_socketLayer::~TcpLayer_socketLayer() { if (_client_id != -1) { remove_client(_client_id); } unmap_user(); } void TcpLayer_socketLayer::Handle_Fd_Event(int fd, boolean is_readable, boolean is_writable, boolean is_error) { loggers::get_instance().log(">>> TcpLayer_socketLayer::Handle_Fd_Event: %d", fd); Handle_Socket_Event(fd, is_readable, is_writable, is_error); log_debug("<<< TcpLayer_socketLayer::Handle_Fd_Event"); } void TcpLayer_socketLayer::Handle_Timeout(double time_since_last_call) { loggers::get_instance().log(">>> TcpLayer_socketLayer::Handle_Timeout: %f", time_since_last_call); Handle_Timeout_Event(time_since_last_call); loggers::get_instance().log("<<< TcpLayer_socketLayer::Handle_Timeout"); } void TcpLayer_socketLayer::sendData(OCTETSTRING& data, Params& params) { loggers::get_instance().log_msg(">>> TcpLayer_socketLayer::sendData: ", data); send_message_on_fd(_client_id, static_cast(data), data.lengthof()); } void TcpLayer_socketLayer::receiveData(OCTETSTRING& data, Params& params) { loggers::get_instance().log_msg(">>> TcpLayer_socketLayer::receiveData: ", data); // // float duration; // loggers::get_instance().set_start_time(_time_key); // this->receiveData(os, params); // TODO Check execution time for decoding operation // loggers::get_instance().set_stop_time(_time_key, duration); receiveToAllLayers(data, params); } void TcpLayer_socketLayer::message_incoming(const unsigned char* message_buffer, int length, int client_id) { loggers::get_instance().log(">>> TcpLayer_socketLayer::message_incoming"); loggers::get_instance().log_to_hexa("TcpLayer_socketLayer::message_incoming: ", message_buffer, length); float duration; loggers::get_instance().set_start_time(_time_key); OCTETSTRING data(length, message_buffer); Params params; this->receiveData(data, params); // TODO Check execution time for decoding operation loggers::get_instance().set_stop_time(_time_key, duration); } void TcpLayer_socketLayer::client_connection_opened(int p_client_id) { loggers::get_instance().log(">>> TcpLayer_socketLayer::client_connection_opened: %d", p_client_id); } int TcpLayer_socketLayer::receive_message_on_fd(int p_client_id) { loggers::get_instance().log(">>> TcpLayer_socketLayer::receive_message_on_fd: %d", p_client_id); if(get_user_data(p_client_id)) { // INFO: it is assumed that only SSL_Socket assigns user data to each peer loggers::get_instance().log("TcpLayer_socketLayer::receive_message_on_fd: Call SSL_Socket::receive_message_on_fd"); return SSL_Socket::receive_message_on_fd(p_client_id); } loggers::get_instance().log("TcpLayer_socketLayer::receive_message_on_fd: Call Abstract_Socket::receive_message_on_fd"); return Abstract_Socket::receive_message_on_fd(p_client_id); } void TcpLayer_socketLayer::peer_half_closed(int p_client_id) { loggers::get_instance().log(">>> TcpLayer_socketLayer::peer_half_closed: %d", p_client_id); Abstract_Socket::peer_half_closed(p_client_id); } void TcpLayer_socketLayer::peer_disconnected(int p_client_id) { loggers::get_instance().log(">>> TcpLayer_socketLayer::peer_disconnected: %d", p_client_id); Abstract_Socket::peer_disconnected(p_client_id); } TcpLayer_socketFactory TcpLayer_socketFactory::_f;