diff options
Diffstat (limited to 'src/core/transport/tcp')
17 files changed, 2455 insertions, 0 deletions
diff --git a/src/core/transport/tcp/Makefile.am b/src/core/transport/tcp/Makefile.am new file mode 100644 index 0000000..79f45b7 --- /dev/null +++ b/src/core/transport/tcp/Makefile.am @@ -0,0 +1,3 @@ +SUBDIRS = sender receiver server +EXTRA_DIST=axis2_simple_tcp_svr_conn.h axis2_tcp_svr_thread.h axis2_tcp_transport_sender.h axis2_tcp_server.h axis2_tcp_transport.h axis2_tcp_worker.h + diff --git a/src/core/transport/tcp/axis2_simple_tcp_svr_conn.h b/src/core/transport/tcp/axis2_simple_tcp_svr_conn.h new file mode 100644 index 0000000..c0d3b89 --- /dev/null +++ b/src/core/transport/tcp/axis2_simple_tcp_svr_conn.h @@ -0,0 +1,155 @@ + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * tcp://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef AXIS2_SIMPLE_HTTP_SVR_CONN_H +#define AXIS2_SIMPLE_HTTP_SVR_CONN_H + +/** + * @ingroup axis2_core_transport_tcp + * @{ + */ + +/** + * @file axis2_simple_tcp_svr_conn.h + * @brief Axis2 simple tcp server connection + */ + +#include <axis2_const.h> +#include <axis2_defines.h> +#include <axutil_env.h> +#include <axutil_stream.h> + +#ifdef __cplusplus +extern "C" +{ +#endif + + typedef struct axis2_simple_tcp_svr_conn axis2_simple_tcp_svr_conn_t; + + /** + * @param svr_conn pointer to server connection struct + * @param env pointer to environment struct + * @return AXIS2_SUCCESS on success, else AXIS2_FAILURE + */ + AXIS2_EXTERN axis2_status_t AXIS2_CALL + axis2_simple_tcp_svr_conn_close( + axis2_simple_tcp_svr_conn_t * svr_conn, + const axutil_env_t * env); + + /** + * @param svr_conn pointer to server connection struct + * @param env pointer to environment struct + */ + AXIS2_EXTERN axis2_bool_t AXIS2_CALL + axis2_simple_tcp_svr_conn_is_open( + axis2_simple_tcp_svr_conn_t * svr_conn, + const axutil_env_t * env); + + /** + * @param svr_conn pointer to server connection struct + * @param env pointer to environment struct + */ + + AXIS2_EXTERN axutil_stream_t *AXIS2_CALL + + axis2_simple_tcp_svr_conn_get_stream( + const axis2_simple_tcp_svr_conn_t * svr_conn, + const axutil_env_t * env); + + /** + * @param svr_conn pointer to server connection struct + * @param env pointer to environment struct + */ + AXIS2_EXTERN axis2_char_t *AXIS2_CALL + + axis2_simple_tcp_svr_conn_read_request( + axis2_simple_tcp_svr_conn_t * svr_conn, + const axutil_env_t * env); + + /** + * @param svr_conn pointer to server connection struct + * @param env pointer to environment struct + * @param timeout timeout + * @return AXIS2_SUCCESS on success, else AXIS2_FAILURE + */ + AXIS2_EXTERN axis2_status_t AXIS2_CALL + + axis2_simple_tcp_svr_conn_set_rcv_timeout( + axis2_simple_tcp_svr_conn_t * svr_conn, + const axutil_env_t * env, + int timeout); + + /** + * @param svr_conn pointer to server connection struct + * @param env pointer to environment struct + * @param timeout timeout + * @return AXIS2_SUCCESS on success, else AXIS2_FAILURE + */ + AXIS2_EXTERN axis2_status_t AXIS2_CALL + + axis2_simple_tcp_svr_conn_set_snd_timeout( + axis2_simple_tcp_svr_conn_t * svr_conn, + const axutil_env_t * env, + int timeout); + + /** + * @param svr_conn pointer to server connection struct + * @param env pointer to environment struct + */ + AXIS2_EXTERN axis2_char_t *AXIS2_CALL + + axis2_simple_tcp_svr_conn_get_svr_ip( + const axis2_simple_tcp_svr_conn_t * svr_conn, + const axutil_env_t * env); + + /** + * @param svr_conn pointer to server connection struct + * @param env pointer to environment struct + */ + AXIS2_EXTERN axis2_char_t *AXIS2_CALL + + axis2_simple_tcp_svr_conn_get_peer_ip( + const axis2_simple_tcp_svr_conn_t * svr_conn, + const axutil_env_t * env); + + /** + * @param svr_conn pointer to server connection struct + * @param env pointer to environment struct + * @return AXIS2_SUCCESS on success, else AXIS2_FAILURE + */ + AXIS2_EXTERN void AXIS2_CALL + axis2_simple_tcp_svr_conn_free( + axis2_simple_tcp_svr_conn_t * svr_conn, + const axutil_env_t * env); + + /** + * creates axis2_simple_tcp_svr_conn struct + * @param env pointer to environment struct + * @param sockfd sockfd + */ + AXIS2_EXTERN axis2_simple_tcp_svr_conn_t *AXIS2_CALL + + axis2_simple_tcp_svr_conn_create( + const axutil_env_t * env, + int sockfd); + +#ifdef __cplusplus +} +#endif + +#endif /* AXIS2_SIMPLE_HTTP_SVR_CONN_H */ diff --git a/src/core/transport/tcp/axis2_tcp_server.h b/src/core/transport/tcp/axis2_tcp_server.h new file mode 100644 index 0000000..e4557a1 --- /dev/null +++ b/src/core/transport/tcp/axis2_tcp_server.h @@ -0,0 +1,60 @@ + +/* +* Licensed to the Apache Software Foundation (ASF) under one or more +* contributor license agreements. See the NOTICE file distributed with +* this work for additional information regarding copyright ownership. +* The ASF licenses this file to You under the Apache License, Version 2.0 +* (the "License"); you may not use this file except in compliance with +* the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef AXIS2_TCP_SERVER_H +#define AXIS2_TCP_SERVER_H + +/** + * @defgroup axis2_tcp_server tcp server + * @ingroup axis2_core_trans_tcp + * @{ + */ + +/** + * @file axis2_tcp_server.h + * @brief axis2 TCP Server implementation + */ + +#include <axis2_const.h> +#include <axis2_defines.h> +#include <axutil_env.h> +#include <axis2_conf_ctx.h> +#include <axis2_transport_receiver.h> + +#ifdef __cplusplus +extern "C" +{ +#endif + + AXIS2_EXTERN axis2_transport_receiver_t *AXIS2_CALL + axis2_tcp_server_create( + const axutil_env_t * env, + const axis2_char_t * repo, + const int port); + + AXIS2_EXTERN axis2_status_t AXIS2_CALL + axis2_tcp_server_stop( + axis2_transport_receiver_t * server, + const axutil_env_t * env); + + /** @} */ +#ifdef __cplusplus +} +#endif + +#endif /* AXIS2_TCP_SERVER_H */ diff --git a/src/core/transport/tcp/axis2_tcp_svr_thread.h b/src/core/transport/tcp/axis2_tcp_svr_thread.h new file mode 100644 index 0000000..aa22ea1 --- /dev/null +++ b/src/core/transport/tcp/axis2_tcp_svr_thread.h @@ -0,0 +1,117 @@ + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef AXIS2_TCP_SVR_THREAD_H +#define AXIS2_TCP_SVR_THREAD_H + +/** + * @defgroup axis2_tcp_svr_thread tcp server thread + * @ingroup axis2_core_trans_tcp + * @{ + */ + +/** + * @file axis2_tcp_svr_thread.h + * @brief axis2 TCP server listning thread implementation + */ + +#include <axis2_const.h> +#include <axis2_defines.h> +#include <axutil_env.h> +#include <axis2_tcp_worker.h> + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** Type name for struct axist_tcp_svr_thread */ + typedef struct axis2_tcp_svr_thread axis2_tcp_svr_thread_t; + + /** + * @param svr_thread pointer to server thread + * @param env pointer to environment struct + */ + AXIS2_EXTERN axis2_status_t AXIS2_CALL + axis2_tcp_svr_thread_run( + axis2_tcp_svr_thread_t * svr_thread, + const axutil_env_t * env); + + /** + * @param svr_thread pointer to server thread + * @param env pointer to environment struct + */ + AXIS2_EXTERN axis2_status_t AXIS2_CALL + axis2_tcp_svr_thread_destroy( + axis2_tcp_svr_thread_t * svr_thread, + const axutil_env_t * env); + + /** + * @param svr_thread pointer to server thread + * @param env pointer to environment struct + */ + AXIS2_EXTERN int AXIS2_CALL + axis2_tcp_svr_thread_get_local_port( + const axis2_tcp_svr_thread_t * svr_thread, + const axutil_env_t * env); + + /** + * @param svr_thread pointer to server thread + * @param env pointer to environment struct + */ + AXIS2_EXTERN axis2_bool_t AXIS2_CALL + axis2_tcp_svr_thread_is_running( + axis2_tcp_svr_thread_t * svr_thread, + const axutil_env_t * env); + + /** + * @param svr_thread pointer to server thread + * @param env pointer to environment struct + * @param worker pointer to worker + */ + AXIS2_EXTERN axis2_status_t AXIS2_CALL + axis2_tcp_svr_thread_set_worker( + axis2_tcp_svr_thread_t * svr_thread, + const axutil_env_t * env, + axis2_tcp_worker_t * worker); + + /** + * @param svr_thread pointer to server thread + * @param env pointer to environment struct + */ + AXIS2_EXTERN void AXIS2_CALL + axis2_tcp_svr_thread_free( + axis2_tcp_svr_thread_t * svr_thread, + const axutil_env_t * env); + + /** + * @param env pointer to environment struct + * @param port + */ + AXIS2_EXTERN axis2_tcp_svr_thread_t *AXIS2_CALL + + axis2_tcp_svr_thread_create( + const axutil_env_t * env, + int port); + + /** @} */ +#ifdef __cplusplus +} +#endif + +#endif /* AXIS2_TCP_SVR_THREAD_H */ diff --git a/src/core/transport/tcp/axis2_tcp_transport.h b/src/core/transport/tcp/axis2_tcp_transport.h new file mode 100644 index 0000000..9c8c7db --- /dev/null +++ b/src/core/transport/tcp/axis2_tcp_transport.h @@ -0,0 +1,89 @@ + +/* +* Copyright 2004,2005 The Apache Software Foundation. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain count copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef AXIS2_TCP_TRANSPORT_H +#define AXIS2_TCP_TRANSPORT_H + +#include <axis2_const.h> + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** @defgroup axis2_core_trans_tcp tcp transport + * @ingroup axis2_transport + * Description. + * @{ + */ + + /** + * @defgroup axis2_core_transport_tcp core tcp transport + * @ingroup axis2_core_trans_tcp + * @{ + */ + + /** + * @brief TCP protocol and message context constants. + * + */ +#define AXIS2_TCP_OUT_TRANSPORT_INFO "TCPOutTransportInfo" + + /** + * PROTOCOL_VERSION + */ +#define AXIS2_TCP_PROTOCOL_VERSION "PROTOCOL" + + /** + * SOCKET + */ +#define AXIS2_SOCKET "SOCKET" + + /** + * HEADER_HOST + */ +#define AXIS2_TCP_HOST "Host" + + /** + * SO_TIMEOUT + */ +#define AXIS2_TCP_SO_TIMEOUT "SO_TIMEOUT" + + /** + * CONNECTION_TIMEOUT + */ +#define AXIS2_TCP_CONNECTION_TIMEOUT "CONNECTION_TIMEOUT" + + /** + * DEFAULT_SO_TIMEOUT + */ +#define AXIS2_TCP_DEFAULT_SO_TIMEOUT 60000 + + /** + * DEFAULT_CONNECTION_TIMEOUT + */ +#define AXIS2_TCP_DEFAULT_CONNECTION_TIMEOUT 60000 + + /** + * Field TRANSPORT_TCP + */ +#define AXIS2_TRANSPORT_TCP "tcp" + +#ifdef __cplusplus +} +#endif +#endif /* AXIS2_TCP_TRANSPORT_H */ diff --git a/src/core/transport/tcp/axis2_tcp_transport_sender.h b/src/core/transport/tcp/axis2_tcp_transport_sender.h new file mode 100644 index 0000000..9d706e5 --- /dev/null +++ b/src/core/transport/tcp/axis2_tcp_transport_sender.h @@ -0,0 +1,59 @@ + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef AXIS2_TCP_TRANSPORT_SENDER_H +#define AXIS2_TCP_TRANSPORT_SENDER_H + +/** + * @defgroup axis2_tcp_transport_sender tcp transport sender + * @ingroup axis2_core_trans_tcp + * @{ + */ + +/** + * @file axis2_tcp_transport_sender.h + * @brief axis2 TCP Transport Sender (Handler) implementation + */ + +#include <axis2_const.h> +#include <axis2_defines.h> +#include <axutil_env.h> +#include <axis2_msg_ctx.h> +#include <axis2_conf_ctx.h> +#include <axis2_transport_out_desc.h> +#include <axis2_transport_sender.h> + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** + * @param env pointer to environment struct + */ + AXIS2_EXTERN axis2_transport_sender_t *AXIS2_CALL + + axis2_tcp_transport_sender_create( + const axutil_env_t * env); + + /** @} */ +#ifdef __cplusplus +} +#endif + +#endif /* AXIS2_TCP_TRANSPORT_SENDER_H */ diff --git a/src/core/transport/tcp/axis2_tcp_worker.h b/src/core/transport/tcp/axis2_tcp_worker.h new file mode 100644 index 0000000..dcd74fc --- /dev/null +++ b/src/core/transport/tcp/axis2_tcp_worker.h @@ -0,0 +1,95 @@ + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * tcp://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef AXIS2_TCP_WORKER_H +#define AXIS2_TCP_WORKER_H + +/** + * @defgroup axis2_tcp_worker tcp worker + * @ingroup axis2_core_trans_tcp + * @{ + */ + +/** + * @file axis2_tcp_worker.h + * @brief axis2 TCP Worker + */ + +#include <axis2_const.h> +#include <axis2_defines.h> +#include <axutil_env.h> +#include <axis2_simple_tcp_svr_conn.h> +#include <axis2_conf_ctx.h> + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** Type name for struct axis2_tcp_worker */ + typedef struct axis2_tcp_worker axis2_tcp_worker_t; + + /** + * @param tcp_worker pointer to tcp worker + * @param env pointer to environment struct + * @param svr_conn pointer to svr conn + * @param simple_request pointer to simple request + */ + AXIS2_EXTERN axis2_bool_t AXIS2_CALL + axis2_tcp_worker_process_request( + axis2_tcp_worker_t * tcp_worker, + const axutil_env_t * env, + axis2_simple_tcp_svr_conn_t * svr_conn, + axis2_char_t * simple_request); + + /** + * @param tcp_worker pointer to tcp worker + * @param env pointer to environment struct + * @param port + * @return AXIS2_SUCCESS on success, else AXIS2_FAILURE + */ + AXIS2_EXTERN axis2_status_t AXIS2_CALL + axis2_tcp_worker_set_svr_port( + axis2_tcp_worker_t * tcp_worker, + const axutil_env_t * env, + int port); + + /** + * @param tcp_worker pointer to tcp worker + * @param env pointer to environment strut + * @return void + */ + AXIS2_EXTERN void AXIS2_CALL + axis2_tcp_worker_free( + axis2_tcp_worker_t * tcp_worker, + const axutil_env_t * env); + + /** + * @param env pointer to environment struct + * @param conf_ctx pointer to configuration context + */ + AXIS2_EXTERN axis2_tcp_worker_t *AXIS2_CALL + axis2_tcp_worker_create( + const axutil_env_t * env, + axis2_conf_ctx_t * conf_ctx); + +#ifdef __cplusplus +} +#endif + +#endif /* AXIS2_TCP_WORKER_H */ diff --git a/src/core/transport/tcp/receiver/Makefile.am b/src/core/transport/tcp/receiver/Makefile.am new file mode 100644 index 0000000..e3543dc --- /dev/null +++ b/src/core/transport/tcp/receiver/Makefile.am @@ -0,0 +1,23 @@ +lib_LTLIBRARIES = libaxis2_tcp_receiver.la +libaxis2_tcp_receiver_la_LIBADD=$(top_builddir)/util/src/libaxutil.la\ + $(top_builddir)/src/core/transport/http/common/libaxis2_http_common.la + + +libaxis2_tcp_receiver_la_SOURCES = tcp_svr_thread.c \ + tcp_worker.c \ + simple_tcp_svr_conn.c \ + tcp_receiver.c + + +libaxis2_tcp_receiver_la_LDFLAGS = -version-info $(VERSION_NO) + +INCLUDES = -I$(top_builddir)/include \ + -I$(top_builddir)/src/core/transport\ + -I$(top_builddir)/src/core/transport/tcp \ + -I$(top_builddir)/src/core/description \ + -I$(top_builddir)/src/core/context \ + -I$(top_builddir)/src/core/phaseresolver \ + -I$(top_builddir)/src/core/engine \ + -I$(top_builddir)/src/core/deployment \ + -I$(top_builddir)/util/include \ + -I$(top_builddir)/axiom/include diff --git a/src/core/transport/tcp/receiver/simple_tcp_svr_conn.c b/src/core/transport/tcp/receiver/simple_tcp_svr_conn.c new file mode 100644 index 0000000..a5fd9a9 --- /dev/null +++ b/src/core/transport/tcp/receiver/simple_tcp_svr_conn.c @@ -0,0 +1,181 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <axis2_simple_tcp_svr_conn.h> +#include <axis2_tcp_transport.h> +#include <axutil_string.h> +#include <axutil_network_handler.h> +#include <platforms/axutil_platform_auto_sense.h> + +struct axis2_simple_tcp_svr_conn +{ + int socket; + axutil_stream_t *stream; + axis2_char_t *buffer; +}; + +AXIS2_EXTERN axis2_simple_tcp_svr_conn_t *AXIS2_CALL +axis2_simple_tcp_svr_conn_create( + const axutil_env_t * env, + int sockfd) +{ + axis2_simple_tcp_svr_conn_t *svr_conn = NULL; + AXIS2_ENV_CHECK(env, NULL); + svr_conn = (axis2_simple_tcp_svr_conn_t *)AXIS2_MALLOC(env->allocator, + sizeof(axis2_simple_tcp_svr_conn_t)); + + if(!svr_conn) + { + AXIS2_ERROR_SET(env->error, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE); + return NULL; + } + svr_conn->socket = sockfd; + svr_conn->stream = NULL; + svr_conn->buffer = NULL; + + if(-1 != svr_conn->socket) + { + svr_conn->stream = axutil_stream_create_socket(env, svr_conn->socket); + if(!svr_conn->stream) + { + axis2_simple_tcp_svr_conn_free((axis2_simple_tcp_svr_conn_t *)svr_conn, env); + return NULL; + } + } + return svr_conn; +} + +AXIS2_EXTERN void AXIS2_CALL +axis2_simple_tcp_svr_conn_free( + axis2_simple_tcp_svr_conn_t * svr_conn, + const axutil_env_t * env) +{ + axis2_simple_tcp_svr_conn_close(svr_conn, env); + + AXIS2_FREE(env->allocator, svr_conn); + + return; +} + +AXIS2_EXTERN axis2_status_t AXIS2_CALL +axis2_simple_tcp_svr_conn_close( + axis2_simple_tcp_svr_conn_t * svr_conn, + const axutil_env_t * env) +{ + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + + axutil_stream_free(svr_conn->stream, env); + if(-1 != svr_conn->socket) + { + axutil_network_handler_close_socket(env, svr_conn->socket); + svr_conn->socket = -1; + } + return AXIS2_SUCCESS; +} + +AXIS2_EXTERN axis2_bool_t AXIS2_CALL +axis2_simple_tcp_svr_conn_is_open( + axis2_simple_tcp_svr_conn_t * svr_conn, + const axutil_env_t * env) +{ + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + if(-1 != svr_conn->socket) + { + return AXIS2_TRUE; + } + return AXIS2_FALSE; +} + +AXIS2_EXTERN axutil_stream_t *AXIS2_CALL +axis2_simple_tcp_svr_conn_get_stream( + const axis2_simple_tcp_svr_conn_t * svr_conn, + const axutil_env_t * env) +{ + return svr_conn->stream; +} + +AXIS2_EXTERN axis2_char_t *AXIS2_CALL +axis2_simple_tcp_svr_conn_read_request( + axis2_simple_tcp_svr_conn_t * svr_conn, + const axutil_env_t * env) +{ + int size = 32000; + axis2_char_t str_line[32000]; + axis2_char_t tmp_buf[32000]; + int read = -1; + + AXIS2_ENV_CHECK(env, NULL); + + memset(str_line, 0, size); + while((read = axutil_stream_peek_socket(svr_conn->stream, env, tmp_buf, size - 1)) > 0) + { + tmp_buf[read] = '\0'; + if(read > 0) + { + read = axutil_stream_read(svr_conn->stream, env, tmp_buf, size - 1); + if(read > 0) + { + tmp_buf[read] = '\0'; + strcat(str_line, tmp_buf); + break; + } + else + { + break; + } + } + } + if(str_line > 0) + { + svr_conn->buffer = str_line; + } + return svr_conn->buffer; +} + +AXIS2_EXTERN axis2_status_t AXIS2_CALL +axis2_simple_tcp_svr_conn_set_rcv_timeout( + axis2_simple_tcp_svr_conn_t * svr_conn, + const axutil_env_t * env, + int timeout) +{ + return axutil_network_handler_set_sock_option(env, svr_conn->socket, SO_RCVTIMEO, timeout); +} + +AXIS2_EXTERN axis2_status_t AXIS2_CALL +axis2_simple_tcp_svr_conn_set_snd_timeout( + axis2_simple_tcp_svr_conn_t * svr_conn, + const axutil_env_t * env, + int timeout) +{ + return axutil_network_handler_set_sock_option(env, svr_conn->socket, SO_SNDTIMEO, timeout); +} + +AXIS2_EXTERN axis2_char_t *AXIS2_CALL +axis2_simple_tcp_svr_conn_get_svr_ip( + const axis2_simple_tcp_svr_conn_t * svr_conn, + const axutil_env_t * env) +{ + return axutil_network_handler_get_svr_ip(env, svr_conn->socket); +} + +AXIS2_EXTERN axis2_char_t *AXIS2_CALL +axis2_simple_tcp_svr_conn_get_peer_ip( + const axis2_simple_tcp_svr_conn_t * svr_conn, + const axutil_env_t * env) +{ + return axutil_network_handler_get_peer_ip(env, svr_conn->socket); +} diff --git a/src/core/transport/tcp/receiver/tcp_receiver.c b/src/core/transport/tcp/receiver/tcp_receiver.c new file mode 100644 index 0000000..0451217 --- /dev/null +++ b/src/core/transport/tcp/receiver/tcp_receiver.c @@ -0,0 +1,400 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <axis2_tcp_transport.h> +#include <axis2_tcp_svr_thread.h> +#include <axis2_tcp_server.h> +#include <axis2_transport_in_desc.h> +#include <axutil_param_container.h> +#include <axutil_url.h> +#include <axis2_conf_init.h> +#include <stdlib.h> + +/** + * @brief TCP Client struct impl + * Axis2 TCP Client impl + */ + +typedef struct axis2_tcp_server_impl +{ + axis2_transport_receiver_t tcp_server; + axis2_tcp_svr_thread_t *svr_thread; + int port; + axis2_char_t *svr_ip; + axis2_conf_ctx_t *conf_ctx; + axis2_conf_ctx_t *conf_ctx_private; + axis2_conf_t *conf; + axis2_bool_t is_application_client_side; +} axis2_tcp_server_impl_t; + +#define AXIS2_INTF_TO_IMPL(tcp_server) \ + ((axis2_tcp_server_impl_t *)(tcp_server)) + +/***************************** Function headers *******************************/ + +axis2_status_t AXIS2_CALL +axis2_tcp_server_init( + axis2_transport_receiver_t * server, + const axutil_env_t * env, + axis2_conf_ctx_t * conf_ctx, + axis2_transport_in_desc_t * in_desc); + +axis2_status_t AXIS2_CALL +axis2_tcp_server_start( + axis2_transport_receiver_t * server, + const axutil_env_t * env); + +axis2_status_t AXIS2_CALL +axis2_tcp_server_stop( + axis2_transport_receiver_t * server, + const axutil_env_t * env); + +axis2_conf_ctx_t *AXIS2_CALL axis2_tcp_server_get_conf_ctx( + axis2_transport_receiver_t * server, + const axutil_env_t * env); + +axis2_endpoint_ref_t *AXIS2_CALL axis2_tcp_server_get_reply_to_epr( + axis2_transport_receiver_t * server, + const axutil_env_t * env, + const axis2_char_t * svc_name); + + +axis2_endpoint_ref_t *AXIS2_CALL axis2_tcp_server_get_epr_for_service( + axis2_transport_receiver_t * server, + const axutil_env_t * env, + const axis2_char_t * svc_name); + + +axis2_bool_t AXIS2_CALL +axis2_tcp_server_is_running( + axis2_transport_receiver_t * server, + const axutil_env_t * env); + +static void AXIS2_CALL +axis2_tcp_server_set_is_application_client_side( + axis2_transport_receiver_t * server, + const axutil_env_t * env, + axis2_bool_t is_application_client_side); + +void AXIS2_CALL axis2_tcp_server_free( + axis2_transport_receiver_t * server, + const axutil_env_t * env); + +static axis2_char_t* AXIS2_CALL +axis2_tcp_server_get_server_ip( +axis2_transport_receiver_t *server, +const axutil_env_t *env); + +static void AXIS2_CALL +axis2_tcp_server_set_server_ip( +axis2_transport_receiver_t *transport_receiver, +const axutil_env_t *env, + axis2_char_t *serverip); + +static const axis2_transport_receiver_ops_t tcp_transport_receiver_ops_var = { + axis2_tcp_server_init, + axis2_tcp_server_start, + axis2_tcp_server_get_reply_to_epr, + axis2_tcp_server_get_epr_for_service, + axis2_tcp_server_get_server_ip, + axis2_tcp_server_set_server_ip, + axis2_tcp_server_get_conf_ctx, + axis2_tcp_server_is_running, + axis2_tcp_server_set_is_application_client_side, + axis2_tcp_server_stop, + axis2_tcp_server_free }; + +AXIS2_EXTERN axis2_transport_receiver_t *AXIS2_CALL +axis2_tcp_server_create( + const axutil_env_t * env, + const axis2_char_t * repo, + const int port) +{ + axis2_tcp_server_impl_t *server_impl = NULL; + AXIS2_ENV_CHECK(env, NULL); + + server_impl = (axis2_tcp_server_impl_t *)AXIS2_MALLOC(env->allocator, + sizeof(axis2_tcp_server_impl_t)); + + if(!server_impl) + { + AXIS2_ERROR_SET(env->error, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE); + return NULL; + } + + server_impl->svr_thread = NULL; + server_impl->conf_ctx = NULL; + server_impl->conf_ctx_private = NULL; + server_impl->port = port; + server_impl->svr_ip = NULL; + server_impl->tcp_server.ops = &tcp_transport_receiver_ops_var; + + if(repo) + { + + /** + * We first create a private conf ctx which is owned by this server + * we only free this private conf context. We should never free the + * server_impl->conf_ctx because it may own to any other object which + * may lead to double free + */ + server_impl->conf_ctx_private = axis2_build_conf_ctx(env, repo); + if(!server_impl->conf_ctx_private) + { + axis2_tcp_server_free((axis2_transport_receiver_t *)server_impl, env); + return NULL; + } + server_impl->conf_ctx = server_impl->conf_ctx_private; + } + return &(server_impl->tcp_server); +} + +void AXIS2_CALL +axis2_tcp_server_free( + axis2_transport_receiver_t * server, + const axutil_env_t * env) +{ + axis2_tcp_server_impl_t *server_impl = NULL; + AXIS2_ENV_CHECK(env, void); + server_impl = AXIS2_INTF_TO_IMPL(server); + if(server_impl->svr_thread) + { + axis2_tcp_svr_thread_destroy(server_impl->svr_thread, env); + axis2_tcp_svr_thread_free(server_impl->svr_thread, env); + server_impl->svr_thread = NULL; + } + + if(server_impl->conf_ctx_private) + { + axis2_conf_ctx_free(server_impl->conf_ctx_private, env); + server_impl->conf_ctx_private = NULL; + } + + /** + * Do not free this. It may own to some other object + */ + server_impl->conf_ctx = NULL; + AXIS2_FREE(env->allocator, server_impl); + return; +} + +static void AXIS2_CALL +axis2_tcp_server_set_is_application_client_side( + axis2_transport_receiver_t * server, + const axutil_env_t * env, + axis2_bool_t is_application_client_side) +{ + axis2_tcp_server_impl_t *server_impl = NULL; + server_impl = AXIS2_INTF_TO_IMPL(server); + server_impl->is_application_client_side = is_application_client_side; +} + +axis2_status_t AXIS2_CALL +axis2_tcp_server_init( + axis2_transport_receiver_t * server, + const axutil_env_t * env, + axis2_conf_ctx_t * conf_ctx, + axis2_transport_in_desc_t * in_desc) +{ + axis2_tcp_server_impl_t *server_impl = NULL; + axis2_char_t *port_str = NULL; + axutil_param_t *param = NULL; + + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + server_impl = AXIS2_INTF_TO_IMPL(server); + + server_impl->conf_ctx = conf_ctx; + param = (axutil_param_t *)axutil_param_container_get_param( + axis2_transport_in_desc_param_container(in_desc, env), env, "port"); + if(param) + { + port_str = axutil_param_get_value(param, env); + } + if(port_str) + { + server_impl->port = atoi(port_str); + } + return AXIS2_SUCCESS; +} + +axis2_status_t AXIS2_CALL +axis2_tcp_server_start( + axis2_transport_receiver_t * server, + const axutil_env_t * env) +{ + + axis2_tcp_server_impl_t *server_impl = NULL; + axis2_tcp_worker_t *worker = NULL; + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + + server_impl = AXIS2_INTF_TO_IMPL(server); + server_impl->svr_thread = axis2_tcp_svr_thread_create(env, server_impl->port); + if(!server_impl->svr_thread) + { + return AXIS2_FAILURE; + } + worker = axis2_tcp_worker_create(env, server_impl->conf_ctx); + axis2_tcp_worker_set_svr_port(worker, env, server_impl->port); + if(!worker) + { + axis2_tcp_svr_thread_free(server_impl->svr_thread, env); + return AXIS2_FAILURE; + } + AXIS2_LOG_INFO(env->log, "Starting TCP server thread"); + axis2_tcp_svr_thread_set_worker(server_impl->svr_thread, env, worker); + axis2_tcp_svr_thread_run(server_impl->svr_thread, env); + return AXIS2_SUCCESS; +} + +AXIS2_EXTERN axis2_status_t AXIS2_CALL +axis2_tcp_server_stop( + axis2_transport_receiver_t * server, + const axutil_env_t * env) +{ + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + + AXIS2_LOG_INFO(env->log, "Terminating TCP server thread"); + if(AXIS2_INTF_TO_IMPL(server)->svr_thread) + { + axis2_tcp_svr_thread_destroy(AXIS2_INTF_TO_IMPL(server)->svr_thread, env); + } + AXIS2_LOG_INFO(env->log, "Successfully terminated TCP server" " thread"); + return AXIS2_SUCCESS; +} + +axis2_conf_ctx_t *AXIS2_CALL +axis2_tcp_server_get_conf_ctx( + axis2_transport_receiver_t * server, + const axutil_env_t * env) +{ + AXIS2_ENV_CHECK(env, NULL); + return AXIS2_INTF_TO_IMPL(server)->conf_ctx; +} + +axis2_endpoint_ref_t *AXIS2_CALL +axis2_tcp_server_get_reply_to_epr( + axis2_transport_receiver_t * server, + const axutil_env_t * env, + const axis2_char_t * svc_name) +{ + axis2_endpoint_ref_t *epr = NULL; + const axis2_char_t *host_address = NULL; + axis2_char_t *svc_path = NULL; + axutil_url_t *url = NULL; + AXIS2_ENV_CHECK(env, NULL); + AXIS2_PARAM_CHECK(env->error, svc_name, NULL); + + host_address = "127.0.0.1"; /* TODO : get from axis2.xml */ + svc_path = axutil_stracat(env, "/axis2/services/", svc_name); + url = axutil_url_create(env, "tcp", host_address, AXIS2_INTF_TO_IMPL(server)->port, svc_path); + AXIS2_FREE(env->allocator, svc_path); + if(!url) + { + return NULL; + } + epr = axis2_endpoint_ref_create(env, axutil_url_to_external_form(url, env)); + axutil_url_free(url, env); + return epr; +} + +axis2_endpoint_ref_t *AXIS2_CALL +axis2_tcp_server_get_epr_for_service( + axis2_transport_receiver_t * server, + const axutil_env_t * env, + const axis2_char_t * svc_name) +{ + axis2_endpoint_ref_t *epr = NULL; + const axis2_char_t *host_address = NULL; + axis2_char_t *svc_path = NULL; + axutil_url_t *url = NULL; + + AXIS2_PARAM_CHECK(env->error, svc_name, NULL); + host_address = "127.0.0.1"; /* TODO : get from axis2.xml */ + svc_path = axutil_stracat(env, "/axis2/services/", svc_name); + + url = axutil_url_create(env, "tcp", host_address, AXIS2_INTF_TO_IMPL(server)->port, svc_path); + AXIS2_FREE(env->allocator, svc_path); + if(!url) + { + return NULL; + } + epr = axis2_endpoint_ref_create(env, axutil_url_to_external_form(url, env)); + axutil_url_free(url, env); + return epr; +} + +axis2_bool_t AXIS2_CALL +axis2_tcp_server_is_running( + axis2_transport_receiver_t * server, + const axutil_env_t * env) +{ + axis2_tcp_server_impl_t *server_impl = NULL; + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + server_impl = AXIS2_INTF_TO_IMPL(server); + if(!server_impl->svr_thread) + { + return AXIS2_FALSE; + } + return axis2_tcp_svr_thread_is_running(server_impl->svr_thread, env); +} + +static axis2_char_t* AXIS2_CALL +axis2_tcp_server_get_server_ip( +axis2_transport_receiver_t *server, +const axutil_env_t *env) +{ + return AXIS2_INTF_TO_IMPL(server)->svr_ip; +} + +static void AXIS2_CALL +axis2_tcp_server_set_server_ip( +axis2_transport_receiver_t *server, +const axutil_env_t *env, + axis2_char_t *serverip) +{ + AXIS2_INTF_TO_IMPL(server)->svr_ip = serverip; +} + + +/** + * Following block distinguish the exposed part of the dll. + */ +AXIS2_EXPORT int +axis2_get_instance( + struct axis2_transport_receiver **inst, + const axutil_env_t * env) +{ + *inst = axis2_tcp_server_create(env, NULL, -1); + if(!(*inst)) + { + return AXIS2_FAILURE; + } + + return AXIS2_SUCCESS; +} + +AXIS2_EXPORT int +axis2_remove_instance( + axis2_transport_receiver_t * inst, + const axutil_env_t * env) +{ + if(inst) + { + axis2_transport_receiver_free(inst, env); + } + return AXIS2_SUCCESS; +} diff --git a/src/core/transport/tcp/receiver/tcp_svr_thread.c b/src/core/transport/tcp/receiver/tcp_svr_thread.c new file mode 100644 index 0000000..5543394 --- /dev/null +++ b/src/core/transport/tcp/receiver/tcp_svr_thread.c @@ -0,0 +1,297 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * tcp://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <platforms/axutil_platform_auto_sense.h> +#include <axis2_tcp_svr_thread.h> +#include <axis2_simple_tcp_svr_conn.h> +#include <axis2_tcp_server.h> +#include <axis2_tcp_transport.h> +#include <axutil_string.h> +#include <axutil_network_handler.h> +#include <axis2_simple_tcp_svr_conn.h> +#include <axutil_url.h> +#include <axutil_error_default.h> +#include <axiom_xml_reader.h> +#include <signal.h> + +AXIS2_EXPORT int axis2_tcp_socket_read_timeout = AXIS2_TCP_DEFAULT_SO_TIMEOUT; + +struct axis2_tcp_svr_thread +{ + int listen_socket; + axis2_bool_t stopped; + axis2_tcp_worker_t *worker; + int port; +}; + +typedef struct axis2_tcp_svr_thd_args +{ + axutil_env_t *env; + axis2_socket_t socket; + axis2_tcp_worker_t *worker; + axutil_thread_t *thread; +} axis2_tcp_svr_thd_args_t; + +AXIS2_EXTERN const axutil_env_t *AXIS2_CALL init_thread_env( + const axutil_env_t ** system_env); + +void *AXIS2_THREAD_FUNC axis2_svr_thread_worker_func( + axutil_thread_t * thd, + void *data); + +axis2_tcp_svr_thread_t *AXIS2_CALL +axis2_tcp_svr_thread_create( + const axutil_env_t * env, + int port) +{ + axis2_tcp_svr_thread_t *svr_thread = NULL; + AXIS2_ENV_CHECK(env, NULL); + + svr_thread = (axis2_tcp_svr_thread_t *)AXIS2_MALLOC(env->allocator, + sizeof(axis2_tcp_svr_thread_t)); + + if(!svr_thread) + { + AXIS2_ERROR_SET(env->error, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE); + return NULL; + } + + svr_thread->worker = NULL; + svr_thread->stopped = AXIS2_FALSE; + svr_thread->port = port; + svr_thread->listen_socket = (int)axutil_network_handler_create_server_socket(env, + svr_thread->port); + if(-1 == svr_thread->listen_socket) + { + axis2_tcp_svr_thread_free((axis2_tcp_svr_thread_t *)svr_thread, env); + return NULL; + } + + return svr_thread; +} + +void AXIS2_CALL +axis2_tcp_svr_thread_free( + axis2_tcp_svr_thread_t * svr_thread, + const axutil_env_t * env) +{ + AXIS2_ENV_CHECK(env, void); + + if(svr_thread->worker) + { + axis2_tcp_worker_free(svr_thread->worker, env); + svr_thread->worker = NULL; + } + if(-1 != svr_thread->listen_socket) + { + axutil_network_handler_close_socket(env, svr_thread->listen_socket); + svr_thread->listen_socket = -1; + } + svr_thread->stopped = AXIS2_TRUE; + + AXIS2_FREE(env->allocator, svr_thread); + return; +} + +axis2_status_t AXIS2_CALL +axis2_tcp_svr_thread_run( + axis2_tcp_svr_thread_t * svr_thread, + const axutil_env_t * env) +{ + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + + while(AXIS2_FALSE == svr_thread->stopped) + { + int socket = -1; + axis2_tcp_svr_thd_args_t *arg_list = NULL; +#ifdef AXIS2_SVR_MULTI_THREADED + axutil_thread_t *worker_thread = NULL; +#endif + + socket = (int)axutil_network_handler_svr_socket_accept(env, svr_thread-> listen_socket); + if(!svr_thread->worker) + { + AXIS2_LOG_WARNING(env->log, AXIS2_LOG_SI, "Worker not ready yet." + " Cannot serve the request"); + axutil_network_handler_close_socket(env, socket); + continue; + } + arg_list = AXIS2_MALLOC(env->allocator, sizeof(axis2_tcp_svr_thd_args_t)); + if(!arg_list) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, + "Memory allocation error in the svr thread loop"); + continue; + } + arg_list->env = (axutil_env_t *)env; + arg_list->socket = socket; + arg_list->worker = svr_thread->worker; +#ifdef AXIS2_SVR_MULTI_THREADED + worker_thread = axutil_thread_pool_get_thread(env->thread_pool, + axis2_svr_thread_worker_func, + (void *) arg_list); + if (!worker_thread) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Thread creation failed" + "server thread loop"); + continue; + } + axutil_thread_pool_thread_detach(env->thread_pool, worker_thread); +#else + axis2_svr_thread_worker_func(NULL, (void *)arg_list); +#endif + } + return AXIS2_SUCCESS; +} + +axis2_status_t AXIS2_CALL +axis2_tcp_svr_thread_destroy( + axis2_tcp_svr_thread_t * svr_thread, + const axutil_env_t * env) +{ + AXIS2_ENV_CHECK(env, AXIS2_CRITICAL_FAILURE); + + if(AXIS2_TRUE == svr_thread->stopped) + { + return AXIS2_SUCCESS; + } + svr_thread->stopped = AXIS2_TRUE; + AXIS2_LOG_DEBUG(env->log, AXIS2_LOG_SI, "Terminating TCP server " + "thread."); + if(svr_thread->listen_socket) + { + axutil_network_handler_close_socket(env, svr_thread->listen_socket); + svr_thread->listen_socket = -1; + } + return AXIS2_SUCCESS; +} + +int AXIS2_CALL +axis2_tcp_svr_thread_get_local_port( + const axis2_tcp_svr_thread_t * svr_thread, + const axutil_env_t * env) +{ + return svr_thread->port; +} + +axis2_bool_t AXIS2_CALL +axis2_tcp_svr_thread_is_running( + axis2_tcp_svr_thread_t * svr_thread, + const axutil_env_t * env) +{ + return svr_thread->port; +} + +axis2_status_t AXIS2_CALL +axis2_tcp_svr_thread_set_worker( + axis2_tcp_svr_thread_t * svr_thread, + const axutil_env_t * env, + axis2_tcp_worker_t * worker) +{ + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + AXIS2_PARAM_CHECK(env->error, worker, AXIS2_FAILURE); + svr_thread->worker = worker; + return AXIS2_SUCCESS; +} + +/** + * Thread worker function. + */ +void *AXIS2_THREAD_FUNC +axis2_svr_thread_worker_func( + axutil_thread_t * thd, + void *data) +{ + struct AXIS2_PLATFORM_TIMEB t1, t2; + axis2_simple_tcp_svr_conn_t *svr_conn = NULL; + axis2_char_t *request = NULL; + int millisecs = 0; + double secs = 0; + axis2_tcp_worker_t *tmp = NULL; + axis2_status_t status = AXIS2_FAILURE; + axutil_env_t *env = NULL; + axis2_socket_t socket; + axutil_env_t *thread_env = NULL; + axis2_tcp_svr_thd_args_t *arg_list = NULL; + +#ifndef WIN32 +#ifdef AXIS2_SVR_MULTI_THREADED + signal(SIGPIPE, SIG_IGN); +#endif +#endif + + arg_list = (axis2_tcp_svr_thd_args_t *)data; + if(!arg_list) + { + return NULL; + } + AXIS2_PLATFORM_GET_TIME_IN_MILLIS(&t1); + env = arg_list->env; + thread_env = axutil_init_thread_env(env); + socket = arg_list->socket; + svr_conn = axis2_simple_tcp_svr_conn_create(thread_env, (int)socket); + axis2_simple_tcp_svr_conn_set_rcv_timeout(svr_conn, thread_env, axis2_tcp_socket_read_timeout); + request = axis2_simple_tcp_svr_conn_read_request(svr_conn, thread_env); + AXIS2_LOG_DEBUG(env->log, AXIS2_LOG_SI, "tcp request %s", request); + tmp = arg_list->worker; + status = axis2_tcp_worker_process_request(tmp, thread_env, svr_conn, request); + axis2_simple_tcp_svr_conn_free(svr_conn, thread_env); + + AXIS2_PLATFORM_GET_TIME_IN_MILLIS(&t2); + millisecs = t2.millitm - t1.millitm; + secs = difftime(t2.time, t1.time); + if(millisecs < 0) + { + millisecs += 1000; + secs--; + } + secs += millisecs / 1000.0; + + if(status == AXIS2_SUCCESS) + { +#if defined(WIN32) + AXIS2_LOG_INFO(thread_env->log, "Request served successfully"); +#else + AXIS2_LOG_INFO(thread_env->log, "Request served in %.3f seconds", secs); +#endif + } + else + { +#if defined(WIN32) + AXIS2_LOG_WARNING(thread_env->log, AXIS2_LOG_SI, + "Error occured in processing request "); +#else + AXIS2_LOG_WARNING(thread_env->log, AXIS2_LOG_SI, + "Error occured in processing request (%.3f seconds)", secs); +#endif + } + + AXIS2_FREE(thread_env->allocator, arg_list); + + if(thread_env) + { + /* There is a persistant problem: Uncomment this after fix + * the issue */ + /* axutil_free_thread_env(thread_env); */ + thread_env = NULL; + } +#ifdef AXIS2_SVR_MULTI_THREADED + axutil_thread_pool_exit_thread(env->thread_pool, thd); +#endif + + return NULL; +} diff --git a/src/core/transport/tcp/receiver/tcp_worker.c b/src/core/transport/tcp/receiver/tcp_worker.c new file mode 100644 index 0000000..217519d --- /dev/null +++ b/src/core/transport/tcp/receiver/tcp_worker.c @@ -0,0 +1,180 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * tcp://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <axis2_tcp_worker.h> +#include <axutil_string.h> +#include <axis2_tcp_transport.h> +#include <axis2_conf.h> +#include <axutil_string.h> +#include <axutil_stream.h> +#include <axis2_msg_ctx.h> +#include <axis2_op_ctx.h> +#include <axis2_engine.h> +#include <axutil_uuid_gen.h> +#include <axutil_url.h> +#include <axutil_property.h> +#include <string.h> +#include <axiom_soap.h> +#include <axiom.h> +#include <axis2_simple_tcp_svr_conn.h> + +struct axis2_tcp_worker +{ + axis2_conf_ctx_t *conf_ctx; + int svr_port; +}; + +AXIS2_EXTERN axis2_tcp_worker_t *AXIS2_CALL +axis2_tcp_worker_create( + const axutil_env_t * env, + axis2_conf_ctx_t * conf_ctx) +{ + axis2_tcp_worker_t *tcp_worker = NULL; + AXIS2_ENV_CHECK(env, NULL); + tcp_worker = (axis2_tcp_worker_t *)AXIS2_MALLOC(env->allocator, sizeof(axis2_tcp_worker_t)); + + if(!tcp_worker) + { + AXIS2_ERROR_SET(env->error, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE); + return NULL; + } + tcp_worker->conf_ctx = conf_ctx; + tcp_worker->svr_port = 9090; /* default - must set later */ + + return tcp_worker; +} + +AXIS2_EXTERN void AXIS2_CALL +axis2_tcp_worker_free( + axis2_tcp_worker_t * tcp_worker, + const axutil_env_t * env) +{ + AXIS2_ENV_CHECK(env, void); + + tcp_worker->conf_ctx = NULL; + + AXIS2_FREE(env->allocator, tcp_worker); + + return; +} + +AXIS2_EXTERN axis2_bool_t AXIS2_CALL +axis2_tcp_worker_process_request( + axis2_tcp_worker_t * tcp_worker, + const axutil_env_t * env, + axis2_simple_tcp_svr_conn_t * svr_conn, + axis2_char_t * simple_request) +{ + axis2_conf_ctx_t *conf_ctx = NULL; + axis2_transport_out_desc_t *out_desc = NULL; + axis2_transport_in_desc_t *in_desc = NULL; + axis2_msg_ctx_t *msg_ctx = NULL; + axiom_xml_reader_t *reader = NULL; + axiom_stax_builder_t *builder = NULL; + axiom_soap_builder_t *soap_builder = NULL; + axiom_soap_envelope_t *soap_envelope = NULL; + axis2_engine_t *engine = NULL; + axis2_status_t status = AXIS2_FALSE; + axutil_stream_t *svr_stream = NULL; + axis2_char_t *buffer = NULL; + int len = 0; + int write = -1; + axutil_stream_t *out_stream = NULL; + + AXIS2_LOG_TRACE(env->log, AXIS2_LOG_SI, "start:axis2_tcp_worker_process_request"); + + out_stream = axutil_stream_create_basic(env); + reader = axiom_xml_reader_create_for_memory(env, simple_request, axutil_strlen(simple_request), + NULL, AXIS2_XML_PARSER_TYPE_BUFFER); + if(!reader) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Failed to create XML reader"); + return AXIS2_FAILURE; + } + + builder = axiom_stax_builder_create(env, reader); + if(!builder) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Failed to create Stax builder"); + return AXIS2_FAILURE; + } + + soap_builder + = axiom_soap_builder_create(env, builder, AXIOM_SOAP12_SOAP_ENVELOPE_NAMESPACE_URI); + + if(!soap_builder) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Failed to create SOAP builder"); + return AXIS2_FAILURE; + } + + conf_ctx = tcp_worker->conf_ctx; + + if(!conf_ctx) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "conf ctx not available"); + return AXIS2_FAILURE; + } + + out_desc = axis2_conf_get_transport_out(axis2_conf_ctx_get_conf(conf_ctx, env), env, + AXIS2_TRANSPORT_ENUM_TCP); + if(!out_desc) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Transport out not set"); + return AXIS2_FAILURE; + } + + in_desc = axis2_conf_get_transport_in(axis2_conf_ctx_get_conf(conf_ctx, env), env, + AXIS2_TRANSPORT_ENUM_TCP); + + msg_ctx = axis2_msg_ctx_create(env, conf_ctx, in_desc, out_desc); + axis2_msg_ctx_set_server_side(msg_ctx, env, AXIS2_TRUE); + axis2_msg_ctx_set_transport_out_stream(msg_ctx, env, out_stream); + + soap_envelope = axiom_soap_builder_get_soap_envelope(soap_builder, env); + axis2_msg_ctx_set_soap_envelope(msg_ctx, env, soap_envelope); + + engine = axis2_engine_create(env, conf_ctx); + status = axis2_engine_receive(engine, env, msg_ctx); + + svr_stream = axis2_simple_tcp_svr_conn_get_stream(svr_conn, env); + buffer = out_stream->buffer; + len = out_stream->len; + buffer[len] = 0; + if(svr_stream && buffer) + { + write = axutil_stream_write(svr_stream, env, buffer, len + 1); + if(write < 0) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "stream write failed"); + return AXIS2_FAILURE; + } + AXIS2_LOG_DEBUG(env->log, AXIS2_LOG_SI, "stream wrote:%s", buffer); + } + AXIS2_LOG_TRACE(env->log, AXIS2_LOG_SI, "end:axis2_tcp_worker_process_request"); + return AXIS2_SUCCESS; +} + +AXIS2_EXTERN axis2_status_t AXIS2_CALL +axis2_tcp_worker_set_svr_port( + axis2_tcp_worker_t * worker, + const axutil_env_t * env, + int port) +{ + worker->svr_port = port; + return AXIS2_SUCCESS; +} diff --git a/src/core/transport/tcp/sender/Makefile.am b/src/core/transport/tcp/sender/Makefile.am new file mode 100644 index 0000000..1e35b3d --- /dev/null +++ b/src/core/transport/tcp/sender/Makefile.am @@ -0,0 +1,26 @@ +lib_LTLIBRARIES = libaxis2_tcp_sender.la + +libaxis2_tcp_sender_la_SOURCES = tcp_transport_sender.c + +libaxis2_tcp_sender_la_LIBADD = $(top_builddir)/src/core/transport/http/common/libaxis2_http_common.la\ + $(top_builddir)/src/core/transport/http/util/libaxis2_http_util.la\ + $(top_builddir)/axiom/src/om/libaxis2_axiom.la\ + $(top_builddir)/util/src/libaxutil.la\ + $(LIBCURL_LIBS)\ + $(SSL_LIBS) + + +libaxis2_tcp_sender_la_LDFLAGS = -version-info $(VERSION_NO) + +INCLUDES = -I$(top_builddir)/include \ + -I$(top_builddir)/src/core/transport\ + -I$(top_builddir)/src/core/transport/tcp \ + -I$(top_builddir)/src/core/transport/tcp/sender/libcurl \ + -I$(top_builddir)/src/core/description \ + -I$(top_builddir)/src/core/context \ + -I$(top_builddir)/src/core/phaseresolver \ + -I$(top_builddir)/src/core/engine \ + -I$(top_builddir)/src/core/deployment \ + -I$(top_builddir)/util/include \ + -I.. \ + -I$(top_builddir)/axiom/include diff --git a/src/core/transport/tcp/sender/tcp_transport_sender.c b/src/core/transport/tcp/sender/tcp_transport_sender.c new file mode 100644 index 0000000..62f34d3 --- /dev/null +++ b/src/core/transport/tcp/sender/tcp_transport_sender.c @@ -0,0 +1,477 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * tcp://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <axis2_tcp_transport_sender.h> +#include <axutil_string.h> +#include <axis2_endpoint_ref.h> +#include <axis2_addr.h> +#include <axiom_xml_writer.h> +#include <axiom_output.h> +#include <axis2_tcp_transport.h> +#include <axiom_soap_body.h> +#include <axiom_soap.h> +#include <axutil_generic_obj.h> +#include <axutil_types.h> +#include <axutil_url.h> +#include <axutil_network_handler.h> + +#define RES_BUFF 50 + +/** + * TCP Transport Sender struct impl + * Axis2 TCP Transport Sender impl + */ + +typedef struct axis2_tcp_transport_sender_impl +{ + axis2_transport_sender_t transport_sender; + int connection_timeout; + int so_timeout; +} axis2_tcp_transport_sender_impl_t; + +#define AXIS2_INTF_TO_IMPL(transport_sender) \ + ((axis2_tcp_transport_sender_impl_t *) \ + (transport_sender)) + +/***************************** Function headers *******************************/ +axis2_status_t AXIS2_CALL +axis2_tcp_transport_sender_invoke( + axis2_transport_sender_t * transport_sender, + const axutil_env_t * env, + axis2_msg_ctx_t * msg_ctx); + +axis2_status_t AXIS2_CALL +axis2_tcp_transport_sender_clean_up( + axis2_transport_sender_t * transport_sender, + const axutil_env_t * env, + axis2_msg_ctx_t * msg_ctx); + +axis2_status_t AXIS2_CALL +axis2_tcp_transport_sender_init( + axis2_transport_sender_t * transport_sender, + const axutil_env_t * env, + axis2_conf_ctx_t * conf_ctx, + axis2_transport_out_desc_t * out_desc); + +axis2_status_t AXIS2_CALL +axis2_tcp_transport_sender_write_message( + axis2_transport_sender_t * transport_sender, + const axutil_env_t * env, + axis2_msg_ctx_t * msg_ctx, + axis2_endpoint_ref_t * epr, + axiom_soap_envelope_t * out, + axiom_output_t * om_output); + +void AXIS2_CALL axis2_tcp_transport_sender_free( + axis2_transport_sender_t * transport_sender, + const axutil_env_t * env); + +static const axis2_transport_sender_ops_t tcp_transport_sender_ops_var = { + axis2_tcp_transport_sender_init, axis2_tcp_transport_sender_invoke, + axis2_tcp_transport_sender_clean_up, axis2_tcp_transport_sender_free }; + +axis2_transport_sender_t *AXIS2_CALL +axis2_tcp_transport_sender_create( + const axutil_env_t * env) +{ + axis2_tcp_transport_sender_impl_t *transport_sender_impl = NULL; + AXIS2_ENV_CHECK(env, NULL); + + transport_sender_impl = (axis2_tcp_transport_sender_impl_t *)AXIS2_MALLOC(env->allocator, + sizeof(axis2_tcp_transport_sender_impl_t)); + + if(!transport_sender_impl) + { + AXIS2_ERROR_SET(env->error, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE); + return NULL; + } + transport_sender_impl->connection_timeout = AXIS2_TCP_DEFAULT_CONNECTION_TIMEOUT; + transport_sender_impl->so_timeout = AXIS2_TCP_DEFAULT_SO_TIMEOUT; + transport_sender_impl->transport_sender.ops = &tcp_transport_sender_ops_var; + return &(transport_sender_impl->transport_sender); +} + +void AXIS2_CALL +axis2_tcp_transport_sender_free( + axis2_transport_sender_t * transport_sender, + const axutil_env_t * env) +{ + axis2_tcp_transport_sender_impl_t *transport_sender_impl = NULL; + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + transport_sender_impl = AXIS2_INTF_TO_IMPL(transport_sender); + AXIS2_FREE(env->allocator, transport_sender_impl); + return; +} + +axis2_status_t AXIS2_CALL +axis2_tcp_transport_sender_invoke( + axis2_transport_sender_t * transport_sender, + const axutil_env_t * env, + axis2_msg_ctx_t * msg_ctx) +{ + axis2_op_t *op = NULL; + const axis2_char_t *mep_uri = NULL; + axis2_bool_t is_server = AXIS2_TRUE; + axiom_soap_envelope_t *soap_envelope = NULL; + axiom_xml_writer_t *xml_writer = NULL; + axiom_output_t *om_output = NULL; + axis2_char_t *buffer = NULL; + axutil_stream_t *out_stream = NULL; + int buffer_size = 0; + axis2_conf_ctx_t *conf_ctx = NULL; + axis2_conf_t *conf = NULL; + axis2_transport_out_desc_t *trans_desc = NULL; + axutil_param_t *write_xml_declaration_param = NULL; + axutil_hash_t *transport_attrs = NULL; + axis2_bool_t write_xml_declaration = AXIS2_FALSE; + + AXIS2_LOG_DEBUG(env->log, AXIS2_LOG_SI, "start:tcp transport sender invoke"); + + op = axis2_msg_ctx_get_op(msg_ctx, env); + mep_uri = axis2_op_get_msg_exchange_pattern(op, env); + + is_server = axis2_msg_ctx_get_server_side(msg_ctx, env); + + soap_envelope = axis2_msg_ctx_get_soap_envelope(msg_ctx, env); + + xml_writer = axiom_xml_writer_create_for_memory(env, NULL, AXIS2_TRUE, 0, + AXIS2_XML_PARSER_TYPE_BUFFER); + if(!xml_writer) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "[tcp]Failed to create XML writer"); + return AXIS2_FAILURE; + } + + om_output = axiom_output_create(env, xml_writer); + if(!om_output) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "[tcp]Failed to create OM output"); + axiom_xml_writer_free(xml_writer, env); + xml_writer = NULL; + return AXIS2_FAILURE; + } + + conf_ctx = axis2_msg_ctx_get_conf_ctx(msg_ctx, env); + if(conf_ctx) + { + conf = axis2_conf_ctx_get_conf(conf_ctx, env); + } + if(conf) + { + trans_desc = axis2_conf_get_transport_out(conf, env, AXIS2_TRANSPORT_ENUM_TCP); + } + if(trans_desc) + { + write_xml_declaration_param = axutil_param_container_get_param( + axis2_transport_out_desc_param_container(trans_desc, env), env, AXIS2_XML_DECLARATION); + } + if(write_xml_declaration_param) + { + transport_attrs = axutil_param_get_attributes(write_xml_declaration_param, env); + if(transport_attrs) + { + axutil_generic_obj_t *obj = NULL; + axiom_attribute_t *write_xml_declaration_attr = NULL; + axis2_char_t *write_xml_declaration_attr_value = NULL; + + obj + = axutil_hash_get(transport_attrs, AXIS2_ADD_XML_DECLARATION, AXIS2_HASH_KEY_STRING); + if(obj) + { + write_xml_declaration_attr = (axiom_attribute_t *)axutil_generic_obj_get_value(obj, + env); + } + if(write_xml_declaration_attr) + { + write_xml_declaration_attr_value = axiom_attribute_get_value( + write_xml_declaration_attr, env); + } + if(write_xml_declaration_attr_value && 0 == axutil_strcasecmp( + write_xml_declaration_attr_value, AXIS2_VALUE_TRUE)) + { + write_xml_declaration = AXIS2_TRUE; + } + } + } + + if(write_xml_declaration) + { + axiom_output_write_xml_version_encoding(om_output, env); + } + + axiom_soap_envelope_serialize(soap_envelope, env, om_output, AXIS2_FALSE); + + buffer = (axis2_char_t *)axiom_xml_writer_get_xml(xml_writer, env); + if(!buffer) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "[tcp]Failed to serialize the SOAP envelope"); + return AXIS2_FAILURE; + } + buffer_size = axiom_xml_writer_get_xml_size(xml_writer, env); + buffer[buffer_size] = 0; + if(is_server) + { + out_stream = axis2_msg_ctx_get_transport_out_stream(msg_ctx, env); + axutil_stream_write(out_stream, env, buffer, buffer_size); + } + else + { + axis2_endpoint_ref_t *to = NULL; + axutil_url_t *to_url = NULL; + const axis2_char_t *to_str = NULL; + const axis2_char_t *host = NULL; + int port = 0; + int socket = -1; + axutil_stream_t *stream; + int write = -1; + int read = -1; + axis2_char_t buff[1]; + axis2_char_t *res_buffer = (axis2_char_t *)AXIS2_MALLOC(env->allocator, RES_BUFF); + int res_size = 0; + int size = 0; + axiom_xml_reader_t *reader = NULL; + axiom_stax_builder_t *builder = NULL; + axiom_soap_builder_t *soap_builder = NULL; + axiom_soap_envelope_t *soap_envelope = NULL; + + to = axis2_msg_ctx_get_to(msg_ctx, env); + + if(!to) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "To epr not presant"); + return AXIS2_FAILURE; + } + + to_str = axis2_endpoint_ref_get_address(to, env); + if(!to_str) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "unable to convert epr to string"); + return AXIS2_FAILURE; + } + + to_url = axutil_url_parse_string(env, to_str); + + if(!to_url) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "unable to parser string to url"); + return AXIS2_FAILURE; + } + + host = axutil_url_get_host(to_url, env); + if(!host) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "retrieving host failed"); + return AXIS2_FAILURE; + } + + port = axutil_url_get_port(to_url, env); + if(!port) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "retrieving port failed"); + return AXIS2_FAILURE; + } + socket = (int)axutil_network_handler_open_socket(env, (char *)host, port); + if(!socket) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "socket creation failed"); + return AXIS2_FAILURE; + } + + AXIS2_LOG_DEBUG(env->log, AXIS2_LOG_SI, "open socket for host:%s port:%d", host, port); + + stream = axutil_stream_create_socket(env, socket); + if(!stream) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "stream creation failed"); + return AXIS2_FAILURE; + } + + write = axutil_stream_write(stream, env, buffer, buffer_size); + if(write < 0) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "stream write error"); + return AXIS2_FAILURE; + } + AXIS2_LOG_TRACE(env->log, AXIS2_LOG_SI, "stream wrote soap msg: %s", buffer); + write = axutil_stream_write(stream, env, "\r\n\r\n", 4); + size = RES_BUFF; + while((read = axutil_stream_read(stream, env, &buff, 1)) > 0) + { + if(res_size >= size) + { + axis2_char_t *tmp_buff = NULL; + size <<= 2; + tmp_buff = AXIS2_MALLOC(env->allocator, size); + memcpy(tmp_buff, res_buffer, res_size); + AXIS2_FREE(env->allocator, res_buffer); + res_buffer = tmp_buff; + } + memcpy(res_buffer + res_size, buff, 1); + res_size++; + } + + axutil_network_handler_close_socket(env, stream->socket); + axutil_stream_close(stream, env); + axutil_stream_free(stream, env); + + AXIS2_LOG_TRACE(env->log, AXIS2_LOG_SI, "%s", res_buffer); + + reader = axiom_xml_reader_create_for_memory(env, res_buffer, (res_size - 1), NULL, + AXIS2_XML_PARSER_TYPE_BUFFER); + + if(!reader) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Failed to create XML reader"); + return AXIS2_FAILURE; + } + + builder = axiom_stax_builder_create(env, reader); + if(!builder) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Failed to create Stax builder"); + return AXIS2_FAILURE; + } + + soap_builder = axiom_soap_builder_create(env, builder, + AXIOM_SOAP12_SOAP_ENVELOPE_NAMESPACE_URI); + if(!soap_builder) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Failed to create SOAP builder"); + return AXIS2_FAILURE; + } + soap_envelope = axiom_soap_builder_get_soap_envelope(soap_builder, env); + + if(!soap_envelope) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Failed to create SOAP envelope"); + return AXIS2_FAILURE; + } + + axis2_msg_ctx_set_response_soap_envelope(msg_ctx, env, soap_envelope); + + } + AXIS2_LOG_DEBUG(env->log, AXIS2_LOG_SI, "end:tcp transport sender invoke"); + return AXIS2_SUCCESS; +} + +axis2_status_t AXIS2_CALL +axis2_tcp_transport_sender_clean_up( + axis2_transport_sender_t * transport_sender, + const axutil_env_t * env, + axis2_msg_ctx_t * msg_ctx) +{ + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + AXIS2_PARAM_CHECK(env->error, msg_ctx, AXIS2_FAILURE); + /* + * Clean up is not used. If the tcp sender needs + * to be cleaned up it should be done here. + */ + return AXIS2_SUCCESS; +} + +axis2_status_t AXIS2_CALL +axis2_tcp_transport_sender_init( + axis2_transport_sender_t * transport_sender, + const axutil_env_t * env, + axis2_conf_ctx_t * conf_ctx, + axis2_transport_out_desc_t * out_desc) +{ + axis2_char_t *temp = NULL; + axutil_param_t *temp_param = NULL; + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + AXIS2_PARAM_CHECK(env->error, conf_ctx, AXIS2_FAILURE); + AXIS2_PARAM_CHECK(env->error, out_desc, AXIS2_FAILURE); + + temp_param = axutil_param_container_get_param(axis2_transport_out_desc_param_container( + out_desc, env), env, AXIS2_TCP_SO_TIMEOUT); + if(temp_param) + { + temp = axutil_param_get_value(temp_param, env); + } + if(temp) + { + AXIS2_INTF_TO_IMPL(transport_sender)->so_timeout = AXIS2_ATOI(temp); + } + temp = (axis2_char_t *)axutil_param_container_get_param( + axis2_transport_out_desc_param_container(out_desc, env), env, AXIS2_TCP_CONNECTION_TIMEOUT); + if(temp_param) + { + temp = axutil_param_get_value(temp_param, env); + } + if(temp) + { + AXIS2_INTF_TO_IMPL(transport_sender)->connection_timeout = AXIS2_ATOI(temp); + } + + return AXIS2_SUCCESS; +} + +axis2_status_t AXIS2_CALL +axis2_tcp_transport_sender_write_message( + axis2_transport_sender_t * transport_sender, + const axutil_env_t * env, + axis2_msg_ctx_t * msg_ctx, + axis2_endpoint_ref_t * epr, + axiom_soap_envelope_t * out, + axiom_output_t * om_output) +{ + + return AXIS2_TRUE; +} + +/** + * Following block distinguish the exposed part of the dll. + */ + +/* When building for static deployment, give the get and remove methods + * unique names. This avoids having the linker fail with duplicate symbol + * errors. + */ + +AXIS2_EXPORT int +#ifndef AXIS2_STATIC_DEPLOY +axis2_get_instance( +#else + axis2_tcp_transport_sender_get_instance( +#endif + struct axis2_transport_sender **inst, + const axutil_env_t * env) +{ + *inst = axis2_tcp_transport_sender_create(env); + if(!(*inst)) + { + return AXIS2_FAILURE; + } + + return AXIS2_SUCCESS; +} + +AXIS2_EXPORT int +#ifndef AXIS2_STATIC_DEPLOY +axis2_remove_instance( +#else + axis2_tcp_transport_sender_remove_instance( +#endif + axis2_transport_sender_t * inst, + const axutil_env_t * env) +{ + if(inst) + { + AXIS2_TRANSPORT_SENDER_FREE(inst, env); + } + return AXIS2_SUCCESS; +} diff --git a/src/core/transport/tcp/server/Makefile.am b/src/core/transport/tcp/server/Makefile.am new file mode 100644 index 0000000..5d7b263 --- /dev/null +++ b/src/core/transport/tcp/server/Makefile.am @@ -0,0 +1 @@ +SUBDIRS=simple_tcp_server diff --git a/src/core/transport/tcp/server/simple_tcp_server/Makefile.am b/src/core/transport/tcp/server/simple_tcp_server/Makefile.am new file mode 100644 index 0000000..1dafbad --- /dev/null +++ b/src/core/transport/tcp/server/simple_tcp_server/Makefile.am @@ -0,0 +1,30 @@ +prgbindir=$(bindir) +prgbin_PROGRAMS = axis2_tcp_server +SUBDIRS = +AM_CFLAGS = -g -pthread +axis2_tcp_server_SOURCES = tcp_server_main.c + +axis2_tcp_server_LDADD = $(LDFLAGS) \ + $(top_builddir)/src/core/transport/http/common/libaxis2_http_common.la \ + $(top_builddir)/axiom/src/om/libaxis2_axiom.la \ + $(top_builddir)/util/src/libaxutil.la \ + $(top_builddir)/axiom/src/parser/$(WRAPPER_DIR)/libaxis2_parser.la \ + $(top_builddir)/src/core/description/libaxis2_description.la \ + $(top_builddir)/src/core/deployment/libaxis2_deployment.la \ + $(top_builddir)/src/core/context/libaxis2_context.la \ + $(top_builddir)/src/core/clientapi/libaxis2_clientapi.la \ + $(top_builddir)/src/core/engine/libaxis2_engine.la \ + $(top_builddir)/src/core/transport/tcp/sender/libaxis2_tcp_sender.la \ + $(top_builddir)/src/core/transport/tcp/receiver/libaxis2_tcp_receiver.la \ + $(top_builddir)/neethi/src/libneethi.la \ + -lpthread + +INCLUDES = -I$(top_builddir)/include \ + -I$(top_builddir)/src/core/description\ + -I$(top_builddir)/src/core/context \ + -I$(top_builddir)/src/core/deployment\ + -I$(top_builddir)/src/core/phaseresolver \ + -I$(top_builddir)/src/core/engine \ + -I$(top_builddir)/util/include \ + -I../.. \ + -I$(top_builddir)/axiom/include diff --git a/src/core/transport/tcp/server/simple_tcp_server/tcp_server_main.c b/src/core/transport/tcp/server/simple_tcp_server/tcp_server_main.c new file mode 100644 index 0000000..dc2dbf9 --- /dev/null +++ b/src/core/transport/tcp/server/simple_tcp_server/tcp_server_main.c @@ -0,0 +1,262 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * tcp://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <axis2_tcp_server.h> +#include <axis2_tcp_transport.h> +#include <platforms/axutil_platform_auto_sense.h> +#include <stdio.h> +#include <stdlib.h> +#include <axutil_error_default.h> +#include <axutil_log_default.h> +#include <axutil_thread_pool.h> +#include <signal.h> +#include <axutil_types.h> +#include <ctype.h> +#include <axiom_xml_reader.h> + +#ifndef AXIS2_TCP_SERVER_LOG_FILE_NAME +#define AXIS2_TCP_SERVER_LOG_FILE_NAME "axis2_tcp_server.log" +#endif + +#ifndef AXIS2_TCP_SERVER_PORT +#define AXIS2_TCP_SERVER_PORT 9091 +#endif + +#ifndef AXIS2_TCP_SERVER_REPO_PATH +#define AXIS2_TCP_SERVER_REPO_PATH "../" +#endif + +axutil_env_t *system_env = NULL; +axis2_transport_receiver_t *server = NULL; +int axis2_tcp_socket_read_timeout = 60000; + +/***************************** Function headers *******************************/ +axutil_env_t * +init_syetem_env( + axutil_allocator_t * allocator, + const axis2_char_t * log_file); + +void +system_exit( + axutil_env_t * env, + int status); + +void +usage( + axis2_char_t * prog_name); + +void +sig_handler( + int signal); + +/***************************** End of function headers ************************/ +axutil_env_t * +init_syetem_env( + axutil_allocator_t * allocator, + const axis2_char_t * log_file) +{ + axutil_error_t *error = axutil_error_create(allocator); + axutil_log_t *log = axutil_log_create(allocator, NULL, log_file); + axutil_thread_pool_t *thread_pool = axutil_thread_pool_init(allocator); + /* We need to init the parser in main thread before spawning child + * threads + */ + axiom_xml_reader_init(); + return axutil_env_create_with_error_log_thread_pool(allocator, error, log, thread_pool); +} + +void +system_exit( + axutil_env_t * env, + int status) +{ + axutil_allocator_t *allocator = NULL; + if(server) + { + axis2_transport_receiver_free(server, system_env); + } + if(env) + { + allocator = env->allocator; + axutil_env_free(env); + } + axiom_xml_reader_cleanup(); + exit(status); +} + +int +main( + int argc, + char *argv[]) +{ + axutil_allocator_t *allocator = NULL; + axutil_env_t *env = NULL; + extern char *optarg; + extern int optopt; + int c; + int log_file_size = AXUTIL_LOG_FILE_SIZE; + axutil_log_levels_t log_level = AXIS2_LOG_LEVEL_DEBUG; + const axis2_char_t *log_file = AXIS2_TCP_SERVER_LOG_FILE_NAME; + int port = AXIS2_TCP_SERVER_PORT; + const axis2_char_t *repo_path = AXIS2_TCP_SERVER_REPO_PATH; + + while((c = AXIS2_GETOPT(argc, argv, ":p:r:ht:l:s:f:")) != -1) + { + + switch(c) + { + case 'p': + port = AXIS2_ATOI(optarg); + break; + case 'r': + repo_path = optarg; + break; + case 't': + axis2_tcp_socket_read_timeout = AXIS2_ATOI(optarg) * 1000; + break; + case 'l': + log_level = AXIS2_ATOI(optarg); + if(log_level < AXIS2_LOG_LEVEL_CRITICAL) + log_level = AXIS2_LOG_LEVEL_CRITICAL; + if(log_level > AXIS2_LOG_LEVEL_TRACE) + log_level = AXIS2_LOG_LEVEL_TRACE; + break; + case 's': + log_file_size = 1024 * 1024 * AXIS2_ATOI(optarg); + break; + case 'f': + log_file = optarg; + break; + case 'h': + usage(argv[0]); + return 0; + case ':': + fprintf(stderr, "\nOption -%c requires an operand\n", optopt); + usage(argv[0]); + return -1; + case '?': + if(isprint(optopt)) + fprintf(stderr, "\nUnknown option `-%c'.\n", optopt); + usage(argv[0]); + return -1; + } + } + + allocator = axutil_allocator_init(NULL); + if(!allocator) + { + system_exit(NULL, -1); + } + env = init_syetem_env(allocator, log_file); + env->log->level = log_level; + env->log->size = log_file_size; + axutil_error_init(); + system_env = env; + +#ifndef WIN32 + signal(SIGINT, sig_handler); + signal(SIGPIPE, sig_handler); +#endif + + AXIS2_LOG_INFO(env->log, "Starting Axis2 TCP server...."); + AXIS2_LOG_INFO(env->log, "Server port : %d", port); + AXIS2_LOG_INFO(env->log, "Repo location : %s", repo_path); + AXIS2_LOG_INFO(env->log, "Read Timeout : %d ms", axis2_tcp_socket_read_timeout); + + server = axis2_tcp_server_create(env, repo_path, port); + if(!server) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Server creation failed: Error code:" " %d :: %s", + env->error->error_number, AXIS2_ERROR_GET_MESSAGE(env->error)); + system_exit(env, -1); + + } + printf("Started Simple Axis2 TCP Server ...\n"); + if(axis2_transport_receiver_start(server, env) == AXIS2_FAILURE) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Server start failed: Error code:" " %d :: %s", + env->error->error_number, AXIS2_ERROR_GET_MESSAGE(env->error)); + system_exit(env, -1); + } + return 0; +} + +void +usage( + axis2_char_t * prog_name) +{ + fprintf(stdout, "\n Usage : %s", prog_name); + fprintf(stdout, " [-p PORT]"); + fprintf(stdout, " [-t TIMEOUT]"); + fprintf(stdout, " [-r REPO_PATH]"); + fprintf(stdout, " [-l LOG_LEVEL]"); + fprintf(stdout, " [-f LOG_FILE]\n"); + fprintf(stdout, " [-s LOG_FILE_SIZE]\n"); + fprintf(stdout, " Options :\n"); + fprintf(stdout, "\t-p PORT \t port number to use, default port is %d\n", AXIS2_TCP_SERVER_PORT); + fprintf(stdout, "\t-r REPO_PATH \t repository path, default is ../\n"); + fprintf(stdout, "\t-t TIMEOUT\t socket read timeout, default is 30 seconds\n"); + fprintf(stdout, "\t-l LOG_LEVEL\t log level, available log levels:" + "\n\t\t\t 0 - critical 1 - errors 2 - warnings" + "\n\t\t\t 3 - information 4 - debug 5- user 6 - trace" + "\n\t\t\t Default log level is 4(debug).\n"); +#ifndef WIN32 + fprintf(stdout, "\t-f LOG_FILE\t log file, default is $AXIS2C_HOME/logs/axis2.log" + "\n\t\t\t or axis2.log in current folder if AXIS2C_HOME not set\n"); +#else + fprintf(stdout, + "\t-f LOG_FILE\t log file, default is %%AXIS2C_HOME%%\\logs\\axis2.log" + "\n\t\t\t or axis2.log in current folder if AXIS2C_HOME not set\n"); +#endif + fprintf(stdout, + "\t-s LOG_FILE_SIZE\t Maximum log file size in mega bytes, default maximum size is 1MB.\n"); + fprintf(stdout, " Help :\n\t-h \t display this help screen.\n\n"); +} + +/** + * Signal handler + */ +#ifndef WIN32 + +void +sig_handler( + int signal) +{ + switch(signal) + { + case SIGINT: + { + AXIS2_LOG_INFO(system_env->log, "Received signal SIGINT. Server " + "shutting down"); + axis2_tcp_server_stop(server, system_env); + AXIS2_LOG_INFO(system_env->log, "Shutdown complete ..."); + system_exit(system_env, 0); + } + case SIGPIPE: + { + AXIS2_LOG_INFO(system_env->log, "Received signal SIGPIPE. Client " + "request serve aborted"); + return; + } + case SIGSEGV: + { + fprintf(stderr, "Received deadly signal SIGSEGV. Terminating\n"); + _exit(-1); + } + } +} +#endif |