celix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bpe...@apache.org
Subject svn commit: r1633943 - in /celix/trunk/remote_shell: ./ private/include/ private/src/
Date Thu, 23 Oct 2014 20:44:29 GMT
Author: bpetri
Date: Thu Oct 23 20:44:29 2014
New Revision: 1633943

URL: http://svn.apache.org/r1633943
Log:
CELIX-119: removed apr from remote_shell

Modified:
    celix/trunk/remote_shell/CMakeLists.txt
    celix/trunk/remote_shell/private/include/connection_listener.h
    celix/trunk/remote_shell/private/include/remote_shell.h
    celix/trunk/remote_shell/private/include/shell_mediator.h
    celix/trunk/remote_shell/private/src/activator.c
    celix/trunk/remote_shell/private/src/connection_listener.c
    celix/trunk/remote_shell/private/src/remote_shell.c
    celix/trunk/remote_shell/private/src/shell_mediator.c

Modified: celix/trunk/remote_shell/CMakeLists.txt
URL: http://svn.apache.org/viewvc/celix/trunk/remote_shell/CMakeLists.txt?rev=1633943&r1=1633942&r2=1633943&view=diff
==============================================================================
--- celix/trunk/remote_shell/CMakeLists.txt (original)
+++ celix/trunk/remote_shell/CMakeLists.txt Thu Oct 23 20:44:29 2014
@@ -37,5 +37,5 @@ if (REMOTE_SHELL)
     include_directories("${PROJECT_SOURCE_DIR}/utils/public/include")
     include_directories("${PROJECT_SOURCE_DIR}/shell/public/include")
     
-    target_link_libraries(remote_shell celix_framework ${APRUTIL_LIBRARY})
+    target_link_libraries(remote_shell celix_framework)
 endif (REMOTE_SHELL)
\ No newline at end of file

Modified: celix/trunk/remote_shell/private/include/connection_listener.h
URL: http://svn.apache.org/viewvc/celix/trunk/remote_shell/private/include/connection_listener.h?rev=1633943&r1=1633942&r2=1633943&view=diff
==============================================================================
--- celix/trunk/remote_shell/private/include/connection_listener.h (original)
+++ celix/trunk/remote_shell/private/include/connection_listener.h Thu Oct 23 20:44:29 2014
@@ -27,8 +27,6 @@
 #ifndef connectionListener_H_
 #define connectionListener_H_
 
-#include <apr_pools.h>
-
 #include <bundle_context.h>
 #include <celix_errno.h>
 
@@ -36,8 +34,8 @@
 
 typedef struct connection_listener *connection_listener_pt;
 
-celix_status_t connectionListener_create(apr_pool_t *pool, remote_shell_pt remoteShell, apr_size_t
port, connection_listener_pt *instance);
-
+celix_status_t connectionListener_create(remote_shell_pt remoteShell, int port, connection_listener_pt
*instance);
+celix_status_t connectionListener_destroy(connection_listener_pt instance);
 celix_status_t connectionListener_start(connection_listener_pt instance);
 celix_status_t connectionListener_stop(connection_listener_pt instance);
 

Modified: celix/trunk/remote_shell/private/include/remote_shell.h
URL: http://svn.apache.org/viewvc/celix/trunk/remote_shell/private/include/remote_shell.h?rev=1633943&r1=1633942&r2=1633943&view=diff
==============================================================================
--- celix/trunk/remote_shell/private/include/remote_shell.h (original)
+++ celix/trunk/remote_shell/private/include/remote_shell.h Thu Oct 23 20:44:29 2014
@@ -27,8 +27,6 @@
 #ifndef REMOTE_SHELL_H_
 #define REMOTE_SHELL_H_
 
-#include <apr_pools.h>
-
 #include <bundle_context.h>
 #include <celix_errno.h>
 
@@ -36,10 +34,9 @@
 
 typedef struct remote_shell *remote_shell_pt;
 
-celix_status_t remoteShell_create(apr_pool_t *pool, shell_mediator_pt mediator, apr_size_t
maximumConnections, remote_shell_pt *instance);
-
-celix_status_t remoteShell_addConnection(remote_shell_pt instance, apr_socket_t *socket);
-
+celix_status_t remoteShell_create(shell_mediator_pt mediator, int maximumConnections, remote_shell_pt
*instance);
+celix_status_t remoteShell_destroy(remote_shell_pt instance);
+celix_status_t remoteShell_addConnection(remote_shell_pt instance, int socket);
 celix_status_t remoteShell_stopConnections(remote_shell_pt instance);
 
 #endif /* REMOTE_SHELL_H_ */

Modified: celix/trunk/remote_shell/private/include/shell_mediator.h
URL: http://svn.apache.org/viewvc/celix/trunk/remote_shell/private/include/shell_mediator.h?rev=1633943&r1=1633942&r2=1633943&view=diff
==============================================================================
--- celix/trunk/remote_shell/private/include/shell_mediator.h (original)
+++ celix/trunk/remote_shell/private/include/shell_mediator.h Thu Oct 23 20:44:29 2014
@@ -28,16 +28,13 @@
 #ifndef shellMediator_H_
 #define shellMediator_H_
 
-#include <apr_pools.h>
-#include <apr_network_io.h>
-
 #include <bundle_context.h>
 #include <celix_errno.h>
 
 typedef struct shell_mediator *shell_mediator_pt;
 
-celix_status_t shellMediator_create(apr_pool_t *pool, bundle_context_pt context, shell_mediator_pt
*instance);
-
-celix_status_t shellMediator_executeCommand(shell_mediator_pt instance, char *command, apr_socket_t
*socket);
+celix_status_t shellMediator_create(bundle_context_pt context, shell_mediator_pt *instance);
+celix_status_t shellMediator_destroy(shell_mediator_pt instance);
+celix_status_t shellMediator_executeCommand(shell_mediator_pt instance, char *command, int
socket);
 
 #endif /* shellMediator_H_ */

Modified: celix/trunk/remote_shell/private/src/activator.c
URL: http://svn.apache.org/viewvc/celix/trunk/remote_shell/private/src/activator.c?rev=1633943&r1=1633942&r2=1633943&view=diff
==============================================================================
--- celix/trunk/remote_shell/private/src/activator.c (original)
+++ celix/trunk/remote_shell/private/src/activator.c Thu Oct 23 20:44:29 2014
@@ -27,7 +27,6 @@
 #include <celix_errno.h>
 
 #include <stdlib.h>
-#include <apr_strings.h>
 
 #include "bundle_activator.h"
 #include "bundle_context.h"
@@ -36,14 +35,13 @@
 #include "shell_mediator.h"
 #include "remote_shell.h"
 
-#define REMOTE_SHELL_TELNET_PORT_PROPERTY_NAME ("remote.shell.telnet.port")
-#define DEFAULT_REMOTE_SHELL_TELNET_PORT (6666)
+#define REMOTE_SHELL_TELNET_PORT_PROPERTY_NAME 	"remote.shell.telnet.port"
+#define DEFAULT_REMOTE_SHELL_TELNET_PORT 		6666
 
-#define REMOTE_SHELL_TELNET_MAXCONN_PROPERTY_NAME ("remote.shell.telnet.maxconn")
-#define DEFAULT_REMOTE_SHELL_TELNET_MAXCONN (2)
+#define REMOTE_SHELL_TELNET_MAXCONN_PROPERTY_NAME 	"remote.shell.telnet.maxconn"
+#define DEFAULT_REMOTE_SHELL_TELNET_MAXCONN 		2
 
 struct bundle_instance {
-	apr_pool_t *pool;
 	shell_mediator_pt shellMediator;
 	remote_shell_pt remoteShell;
 	connection_listener_pt connectionListener;
@@ -51,29 +49,29 @@ struct bundle_instance {
 
 typedef struct bundle_instance *bundle_instance_pt;
 
-static apr_size_t bundleActivator_getPort(bundle_context_pt context);
-static apr_size_t bundleActivator_getMaximumConnections(bundle_context_pt context);
-static apr_size_t bundleActivator_getProperty(bundle_context_pt context, char * propertyName,
apr_size_t defaultValue);
+static int bundleActivator_getPort(bundle_context_pt context);
+static int bundleActivator_getMaximumConnections(bundle_context_pt context);
+static int bundleActivator_getProperty(bundle_context_pt context, char * propertyName, int
defaultValue);
 
 celix_status_t bundleActivator_create(bundle_context_pt context, void **userData) {
 	celix_status_t status = CELIX_SUCCESS;
-    apr_pool_t *ctxpool;
-    apr_pool_t *pool;
 
-    status = bundleContext_getMemoryPool(context, &ctxpool);
-    apr_pool_create(&pool, ctxpool);
-    if (status == CELIX_SUCCESS) {
-    	bundle_instance_pt bi = (bundle_instance_pt) apr_palloc(pool, sizeof(struct bundle_instance));
-        if (userData != NULL) {
-        	bi->pool = pool;
+	bundle_instance_pt bi = (bundle_instance_pt) calloc(1, sizeof(struct bundle_instance));
+
+    if (!bi)
+    {
+    	status = CELIX_ENOMEM;
+    }
+    else if (userData != NULL) {
         	bi->shellMediator = NULL;
         	bi->remoteShell = NULL;
         	bi->connectionListener = NULL;
         	(*userData) = bi;
-        } else {
-        	status = CELIX_ENOMEM;
-        }
+    } else {
+    	status = CELIX_ILLEGAL_ARGUMENT;
+    	free(bi);
     }
+
     return status;
 }
 
@@ -81,12 +79,12 @@ celix_status_t bundleActivator_start(voi
     celix_status_t status = CELIX_SUCCESS;
     bundle_instance_pt bi = (bundle_instance_pt)userData;
 
-    apr_size_t port = bundleActivator_getPort(context);
-    apr_size_t maxConn = bundleActivator_getMaximumConnections(context);
+    int port = bundleActivator_getPort(context);
+    int maxConn = bundleActivator_getMaximumConnections(context);
 
-    status = shellMediator_create(bi->pool, context, &bi->shellMediator);
-	status = CELIX_DO_IF(status, remoteShell_create(bi->pool, bi->shellMediator,  maxConn,
&bi->remoteShell));
-	status = CELIX_DO_IF(status, connectionListener_create(bi->pool, bi->remoteShell,
port, &bi->connectionListener));
+    status = shellMediator_create(context, &bi->shellMediator);
+	status = CELIX_DO_IF(status, remoteShell_create(bi->shellMediator,  maxConn, &bi->remoteShell));
+	status = CELIX_DO_IF(status, connectionListener_create(bi->remoteShell, port, &bi->connectionListener));
     status = CELIX_DO_IF(status, connectionListener_start(bi->connectionListener));
 
     return status;
@@ -96,6 +94,7 @@ celix_status_t bundleActivator_stop(void
     celix_status_t status = CELIX_SUCCESS;
     bundle_instance_pt bi = (bundle_instance_pt)userData;
 
+    shellMediator_destroy(bi->shellMediator);
     connectionListener_stop(bi->connectionListener);
     remoteShell_stopConnections(bi->remoteShell);
 
@@ -103,31 +102,37 @@ celix_status_t bundleActivator_stop(void
 }
 
 celix_status_t bundleActivator_destroy(void * userData, bundle_context_pt context) {
+    bundle_instance_pt bi = (bundle_instance_pt)userData;
+    connectionListener_destroy(bi->connectionListener);
+
     return CELIX_SUCCESS;
 }
 
-static apr_size_t bundleActivator_getPort(bundle_context_pt context) {
+static int bundleActivator_getPort(bundle_context_pt context) {
 	return bundleActivator_getProperty(context, REMOTE_SHELL_TELNET_PORT_PROPERTY_NAME, DEFAULT_REMOTE_SHELL_TELNET_PORT);
 }
 
-static apr_size_t bundleActivator_getMaximumConnections(bundle_context_pt context) {
+static int bundleActivator_getMaximumConnections(bundle_context_pt context) {
 	return bundleActivator_getProperty(context, REMOTE_SHELL_TELNET_MAXCONN_PROPERTY_NAME, DEFAULT_REMOTE_SHELL_TELNET_MAXCONN);
 }
 
-static apr_size_t bundleActivator_getProperty(bundle_context_pt context, char * propertyName,
apr_size_t defaultValue) {
-	apr_size_t value;
+static int bundleActivator_getProperty(bundle_context_pt context, char* propertyName, int
defaultValue) {
+
 	char *strValue = NULL;
+	int value;
 
 	bundleContext_getProperty(context, propertyName, &strValue);
 	if (strValue != NULL) {
-        	char* endptr = strValue;
-        	errno = 0;
-        	value = (apr_size_t) strtol(strValue, &endptr, 10);
-        	if (*endptr || errno != 0) { 
+    	char* endptr = strValue;
+
+    	errno = 0;
+        value = strtol(strValue, &endptr, 10);
+        if (*endptr || errno != 0) {
 			printf("incorrect format for %s\n", propertyName);
 			value = defaultValue;
-		}
-	} else {
+        }
+	}
+	else {
 		value = defaultValue;
 	}
 

Modified: celix/trunk/remote_shell/private/src/connection_listener.c
URL: http://svn.apache.org/viewvc/celix/trunk/remote_shell/private/src/connection_listener.c?rev=1633943&r1=1633942&r2=1633943&view=diff
==============================================================================
--- celix/trunk/remote_shell/private/src/connection_listener.c (original)
+++ celix/trunk/remote_shell/private/src/connection_listener.c Thu Oct 23 20:44:29 2014
@@ -25,49 +25,50 @@
  */
 
 #include <stdlib.h>
-
-#include <apr_pools.h>
-#include <apr_thread_mutex.h>
-#include <apr_thread_proc.h>
-#include <apr_time.h>
-#include <apr_poll.h>
-#include <apr_thread_pool.h>
-
 #include <celix_errno.h>
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+#include <fcntl.h>
 
 #include "connection_listener.h"
 
 #include "shell_mediator.h"
 #include "remote_shell.h"
 
+#define CONNECTION_LISTENER_TIMEOUT_SEC		5
+
 struct connection_listener {
 	//constant
-    apr_pool_t *pool;
-    apr_size_t port;
+    int port;
     remote_shell_pt remoteShell;
-	apr_thread_mutex_t *mutex;
+    celix_thread_mutex_t mutex;
 
 	//protected by mutex
-	apr_thread_t *thread;
-	apr_pollset_t *pollset;
+    bool running;
+    celix_thread_t thread;
+	fd_set pollset;
 };
 
-static void* APR_THREAD_FUNC connection_listener_thread(apr_thread_t *thread, void *data);
-static apr_status_t connectionListener_cleanup(connection_listener_pt instance);
+static void*  connection_listener_thread(void *data);
 
-celix_status_t connectionListener_create(apr_pool_t *pool, remote_shell_pt remoteShell, apr_size_t
port, connection_listener_pt *instance) {
+celix_status_t connectionListener_create(remote_shell_pt remoteShell, int port, connection_listener_pt
*instance) {
 	celix_status_t status = CELIX_SUCCESS;
-    (*instance) = apr_palloc(pool, sizeof(**instance));
+    (*instance) = calloc(1, sizeof(**instance));
+
     if ((*instance) != NULL) {
-    	(*instance)->pool = pool;
     	(*instance)->port = port;
-		(*instance)->mutex = NULL;
 		(*instance)->remoteShell = remoteShell;
-		(*instance)->thread = NULL;
-		(*instance)->pollset = NULL;
-		apr_pool_pre_cleanup_register(pool, (*instance), (void *)connectionListener_cleanup);
+		(*instance)->running = false;
+
+		FD_ZERO(&(*instance)-> pollset);
 
-		status = apr_thread_mutex_create(&(*instance)->mutex, APR_THREAD_MUTEX_DEFAULT,
pool);
+		status = celixThreadMutex_create(&(*instance)->mutex, NULL);
 	} else {
 		status = CELIX_ENOMEM;
 	}
@@ -76,131 +77,141 @@ celix_status_t connectionListener_create
 
 celix_status_t connectionListener_start(connection_listener_pt instance) {
 	celix_status_t status = CELIX_SUCCESS;
-	apr_thread_mutex_lock(instance->mutex);
-
-	if (instance->thread == NULL) {
-		apr_threadattr_t *threadAttributes = NULL;
-		apr_threadattr_create(&threadAttributes, instance->pool);
-		apr_thread_create(&instance->thread, threadAttributes, connection_listener_thread,
instance, instance->pool);
-	} else {
-		status = CELIX_ILLEGAL_STATE;
-	}
-
-	apr_thread_mutex_unlock(instance->mutex);
+	celixThreadMutex_lock(&instance->mutex);
+	celixThread_create(&instance->thread, NULL, connection_listener_thread, instance);
+	celixThreadMutex_unlock(&instance->mutex);
 	return status;
 }
 
 celix_status_t connectionListener_stop(connection_listener_pt instance) {
 	celix_status_t status = CELIX_SUCCESS;
-	apr_thread_t *thread = NULL;
-	apr_pollset_t *pollset = NULL;
+	celix_thread_t thread;
+	fd_set pollset;
+
+	instance->running = false;
+	FD_ZERO(&pollset);
 
 	fw_log(logger, OSGI_FRAMEWORK_LOG_INFO, "CONNECTION_LISTENER: Stopping thread\n");
 
-	apr_thread_mutex_lock(instance->mutex);
+	celixThreadMutex_lock(&instance->mutex);
 	thread=instance->thread;
-	instance->thread = NULL;
-	pollset=instance->pollset;
-	apr_thread_mutex_unlock(instance->mutex);
 
-	if (thread != NULL && pollset != NULL) {
-		apr_status_t threadStatus = APR_SUCCESS;
-		apr_status_t stat = APR_SUCCESS;
-		char error[512];
-
-		fw_log(logger, OSGI_FRAMEWORK_LOG_DEBUG, "Stopping thread by waking poll on listen socket\n");
-		stat = apr_pollset_wakeup(pollset);
-		
-		apr_strerror(stat, error, 512);
-		fw_log(logger, OSGI_FRAMEWORK_LOG_DEBUG, "Got error %s.", error);
-
-		apr_thread_join(&threadStatus, thread);
-		fw_log(logger, OSGI_FRAMEWORK_LOG_INFO, "Done joining thread.");
-	} else if (thread != NULL) {
-		fw_log(logger, OSGI_FRAMEWORK_LOG_ERROR, "Cannot stop thread.");
-	} else {
-		fw_log(logger, OSGI_FRAMEWORK_LOG_INFO, "No running thread.");
-	}
+	pollset=instance->pollset;
+	celixThreadMutex_unlock(&instance->mutex);
 
+	celixThread_join(thread, NULL);
 	return status;
 }
 
-static void* APR_THREAD_FUNC connection_listener_thread(apr_thread_t *thread, void *data)
{
-	apr_status_t status = APR_SUCCESS;
-	connection_listener_pt instance = data;
 
-	apr_sockaddr_t *listenAddress = NULL;
-	apr_socket_t *listenSocket = NULL;
-	apr_socket_t *acceptedSocket = NULL;
-	apr_pollset_t *pollset = NULL;
-	apr_pollfd_t pfd = { instance->pool, APR_POLL_SOCKET, APR_POLLIN, 0, { NULL }, NULL };
-	apr_int32_t num;
-	const apr_pollfd_t *ret_pfd;
-
-	status = apr_sockaddr_info_get(&listenAddress, NULL, APR_UNSPEC, instance->port,
0, instance->pool);
-	status = CELIX_DO_IF(status, apr_socket_create(&listenSocket, APR_INET, SOCK_STREAM,
APR_PROTO_TCP, instance->pool));
-	status = CELIX_DO_IF(status, apr_socket_opt_set(listenSocket, APR_SO_REUSEADDR, TRUE));
-	status = CELIX_DO_IF(status, apr_socket_bind(listenSocket, listenAddress));
-	status = CELIX_DO_IF(status, apr_socket_listen(listenSocket, 2));
-	status = CELIX_DO_IF(status, apr_pollset_create(&pollset, 1, instance->pool, APR_POLLSET_WAKEABLE));
-	pfd.desc.s = listenSocket;
-	status = CELIX_DO_IF(status, apr_pollset_add(pollset, &pfd));
-
-
-    apr_thread_mutex_lock(instance->mutex);
-    instance->pollset=pollset;
-    apr_thread_mutex_unlock(instance->mutex);
-
-    if (status != APR_SUCCESS) {
-    	char error[64];
-		apr_strerror(status, error, 64);
-		fw_log(logger, OSGI_FRAMEWORK_LOG_ERROR, "Error creating and listing on socket: %s.", error);
-    } else {
-    	fw_log(logger, OSGI_FRAMEWORK_LOG_INFO, "Remote Shell accepting connections on port
%ld", instance->port);
+celix_status_t connectionListener_destroy(connection_listener_pt instance) {
+	free(instance);
+
+	return CELIX_SUCCESS;
+}
+
+
+
+static void* connection_listener_thread(void *data) {
+	celix_status_t status = CELIX_BUNDLE_EXCEPTION;
+	connection_listener_pt instance = data;
+	struct timeval timeout;  /* Timeout for select */
+	fd_set active_fd_set;
+	int listenSocket = 0;
+	int on = 1;
+
+	struct addrinfo *result, *rp;
+	struct addrinfo hints;
+
+    memset(&hints, 0, sizeof(struct addrinfo));
+    hints.ai_family = AF_UNSPEC;    /* Allow IPv4 or IPv6 */
+    hints.ai_socktype = SOCK_STREAM; /* Datagram socket */
+    hints.ai_flags = AI_PASSIVE;    /* For wildcard IP address */
+    hints.ai_protocol = 0;          /* Any protocol */
+    hints.ai_canonname = NULL;
+    hints.ai_addr = NULL;
+    hints.ai_next = NULL;
+
+    char portStr[10];
+    snprintf(&portStr[0], 10, "%d", instance->port);
+
+    getaddrinfo(NULL, portStr, &hints, &result);
+
+    for (rp = result; rp != NULL && status == CELIX_BUNDLE_EXCEPTION; rp = rp->ai_next)
{
+
+		status = CELIX_BUNDLE_EXCEPTION;
+
+		/* Create socket */
+		listenSocket = socket(rp->ai_family , rp->ai_socktype, rp->ai_protocol);
+		if (listenSocket < 0) {
+			fw_log(logger, OSGI_FRAMEWORK_LOG_ERROR, "Error creating socket: %s", strerror(errno));
+		}
+		else if(setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on))
< 0) {
+			fw_log(logger, OSGI_FRAMEWORK_LOG_ERROR, "cannot set socket option: %s", strerror(errno));
+		}
+		else if (bind(listenSocket, rp->ai_addr, rp->ai_addrlen) < 0) {
+			fw_log(logger, OSGI_FRAMEWORK_LOG_ERROR, "cannot bind: %s", strerror(errno));
+		}
+		else if (listen(listenSocket,5) < 0) {
+			fw_log(logger, OSGI_FRAMEWORK_LOG_ERROR, "listen failed: %s", strerror(errno));
+		}
+		else {
+			status = CELIX_SUCCESS;
+		}
     }
 
-	while (status == APR_SUCCESS) {
-		status = apr_pollset_poll(pollset, -1, &num, &ret_pfd); //blocks on fd till a connection
is made
-		if (status == APR_SUCCESS) {
-			apr_status_t socketStatus = APR_SUCCESS;
-			apr_pool_t *socketPool = NULL;
-
-			acceptedSocket = NULL;
-			apr_pool_create(&socketPool, instance->pool);
-			socketStatus = apr_socket_accept(&acceptedSocket, listenSocket, socketPool);
-
-			fw_log(logger, OSGI_FRAMEWORK_LOG_INFO, "REMOTE_SHELL: created connection socket.");
-			if (socketStatus == APR_SUCCESS) {
-				remoteShell_addConnection(instance->remoteShell, acceptedSocket);
-			} else {
-				apr_pool_destroy(socketPool);
-				fw_log(logger, OSGI_FRAMEWORK_LOG_ERROR, "REMOTE_SHELL: Could not accept connection.");
+    if (status == CELIX_SUCCESS) {
+
+		fw_log(logger, OSGI_FRAMEWORK_LOG_INFO, "Remote Shell accepting connections on port %d",
instance->port);
+
+		celixThreadMutex_lock(&instance->mutex);
+		instance->pollset=active_fd_set;
+		celixThreadMutex_unlock(&instance->mutex);
+
+		instance->running = true;
+
+		while (status == CELIX_SUCCESS && instance->running) {
+			int selectRet = -1;
+			do {
+				timeout.tv_sec = CONNECTION_LISTENER_TIMEOUT_SEC;
+				timeout.tv_usec = 0;
+
+		    	FD_ZERO (&active_fd_set);
+				FD_SET (listenSocket, &active_fd_set);
+
+				selectRet = select (listenSocket+1, &active_fd_set, NULL, NULL, &timeout);
+			} while (selectRet == -1 && errno == EINTR && instance->running ==
true);
+			if (selectRet < 0) {
+				 fw_log(logger, OSGI_FRAMEWORK_LOG_ERROR, "select on listenSocket failed: %s", strerror(errno));
+				 status = CELIX_BUNDLE_EXCEPTION;
+			}
+			else if (selectRet == 0){
+				/* do nothing here */
+			}
+			else if (FD_ISSET(listenSocket,&active_fd_set)) {
+				int acceptedSocket = accept (listenSocket, NULL, NULL);
+
+				if (acceptedSocket < 0) {
+					perror ("accept");
+					status = CELIX_BUNDLE_EXCEPTION;
+				}
+				else {
+					fw_log(logger, OSGI_FRAMEWORK_LOG_INFO, "REMOTE_SHELL: connection established.");
+					remoteShell_addConnection(instance->remoteShell, acceptedSocket);
+				}
+			}
+			else {
+				fw_log(logger, OSGI_FRAMEWORK_LOG_DEBUG, "REMOTE_SHELL: received data on a not-expected
file-descriptor?");
 			}
-		} else if (status == APR_EINTR) {
-			fw_log(logger, OSGI_FRAMEWORK_LOG_DEBUG, "REMOTE_SHELL: Poll interrupted.");
-		} else /*error*/ {
-			char error[64];
-			apr_strerror(status, error, 64);
-			fw_log(logger, OSGI_FRAMEWORK_LOG_ERROR, "REMOTE_SHELL: Got error %s.", error);
-			break;
 		}
-	}
+    }
 
-	if (pollset != NULL) {
-		apr_pollset_remove(pollset, &pfd);
-		apr_pollset_destroy(pollset);
-	}
-	if (listenSocket != NULL) {
-		apr_socket_shutdown(listenSocket, APR_SHUTDOWN_READWRITE);
-		apr_socket_close(listenSocket);
-	}
+    if (listenSocket > 0) {
+    	close(listenSocket);
+    }
+
+    freeaddrinfo(result);
 
-    apr_thread_exit(thread, status);
     return NULL;
 }
 
-static apr_status_t connectionListener_cleanup(connection_listener_pt instance) {
-        celix_status_t status = CELIX_SUCCESS;
-        status = connectionListener_stop(instance);
-        return APR_SUCCESS;
-}

Modified: celix/trunk/remote_shell/private/src/remote_shell.c
URL: http://svn.apache.org/viewvc/celix/trunk/remote_shell/private/src/remote_shell.c?rev=1633943&r1=1633942&r2=1633943&view=diff
==============================================================================
--- celix/trunk/remote_shell/private/src/remote_shell.c (original)
+++ celix/trunk/remote_shell/private/src/remote_shell.c Thu Oct 23 20:44:29 2014
@@ -26,12 +26,7 @@
 
 #include <stdlib.h>
 
-#include <apr_pools.h>
-#include <apr_thread_mutex.h>
-#include <apr_thread_pool.h>
-#include <apr_strings.h>
-#include <apr_poll.h>
-
+#include <unistd.h>
 #include <utils.h>
 #include <array_list.h>
 
@@ -45,47 +40,39 @@
 #define RS_ERROR ("Error executing command!\n")
 #define RS_MAXIMUM_CONNECTIONS_REACHED ("Maximum number of connections  reached. Disconnecting
...\n")
 
+#define CONNECTION_LISTENER_TIMEOUT_SEC		5
+
 struct remote_shell {
-	apr_pool_t *pool;
 	shell_mediator_pt mediator;
-	apr_thread_mutex_t *mutex;
-	apr_int64_t maximumConnections;
+	celix_thread_mutex_t mutex;
+	int maximumConnections;
 
-	//protected by mutex
-	apr_thread_pool_t *threadPool;
 	array_list_pt connections;
 };
 
 struct connection {
 	remote_shell_pt parent;
-	apr_socket_t *socket;
-	apr_pool_t *pool;
-	apr_pollset_t *pollset;
+	int socket;
+	fd_set pollset;
+	bool threadRunning;
 };
 
 typedef struct connection *connection_pt;
 
-static apr_status_t remoteShell_cleanup(remote_shell_pt instance); //gets called from apr
pool cleanup
-
 static celix_status_t remoteShell_connection_print(connection_pt connection, char * text);
 static celix_status_t remoteShell_connection_execute(connection_pt connection, char *command);
-static void* APR_THREAD_FUNC remoteShell_connection_run(apr_thread_t *thread, void *data);
+static void* remoteShell_connection_run(void *data);
 
-celix_status_t remoteShell_create(apr_pool_t *pool, shell_mediator_pt mediator, apr_size_t
maximumConnections, remote_shell_pt *instance) {
+
+celix_status_t remoteShell_create( shell_mediator_pt mediator, int maximumConnections, remote_shell_pt
*instance) {
 	celix_status_t status = CELIX_SUCCESS;
-	(*instance) = apr_palloc(pool, sizeof(**instance));
+	(*instance) = calloc(1, sizeof(**instance));
 	if ((*instance) != NULL) {
-		(*instance)->pool = pool;
 		(*instance)->mediator = mediator;
 		(*instance)->maximumConnections = maximumConnections;
-		(*instance)->threadPool = NULL;
-		(*instance)->mutex = NULL;
 		(*instance)->connections = NULL;
 
-		apr_pool_pre_cleanup_register(pool, (*instance), (void *)remoteShell_cleanup);
-
-		status = apr_thread_mutex_create(&(*instance)->mutex, APR_THREAD_MUTEX_DEFAULT,
pool);
-		status = apr_thread_pool_create(&(*instance)->threadPool, 0, maximumConnections,
pool);
+		status = celixThreadMutex_create(&(*instance)->mutex, NULL);
 		status = arrayList_create(&(*instance)->connections);
 	} else {
 		status = CELIX_ENOMEM;
@@ -93,98 +80,100 @@ celix_status_t remoteShell_create(apr_po
 	return status;
 }
 
-static apr_status_t remoteShell_cleanup(remote_shell_pt instance) {
+celix_status_t remoteShell_destroy(remote_shell_pt instance) {
 	remoteShell_stopConnections(instance);
 
-	apr_thread_mutex_lock(instance->mutex);
+	celixThreadMutex_lock(&instance->mutex);
 	arrayList_destroy(instance->connections);
-	apr_thread_pool_destroy(instance->threadPool);
-	apr_thread_mutex_unlock(instance->mutex);
+	celixThreadMutex_unlock(&instance->mutex);
 
-	return APR_SUCCESS;
+	return CELIX_SUCCESS;
 }
 
-celix_status_t remoteShell_addConnection(remote_shell_pt instance, apr_socket_t *socket)
{
+celix_status_t remoteShell_addConnection(remote_shell_pt instance, int socket) {
 	celix_status_t status = CELIX_SUCCESS;
-	apr_pool_t *pool = apr_socket_pool_get(socket);
-	connection_pt connection = apr_palloc(pool, sizeof(struct connection));
+	connection_pt connection = calloc(1, sizeof(struct connection));
+
 	if (connection != NULL) {
 		connection->parent = instance;
 		connection->socket = socket;
-		connection->pool = pool;
-		connection->pollset = NULL;
+		connection->threadRunning = false;
+
+		celixThreadMutex_lock(&instance->mutex);
 
-		apr_pollset_create(&connection->pollset, 1, connection->pool, APR_POLLSET_WAKEABLE);
-		apr_thread_mutex_lock(instance->mutex);
-		if (apr_thread_pool_busy_count(instance->threadPool) < instance->maximumConnections)
{
+		if (arrayList_size(instance->connections) < instance->maximumConnections) {
+			celix_thread_t connectionRunThread = NULL;
 			arrayList_add(instance->connections, connection);
-			status = apr_thread_pool_push(instance->threadPool, remoteShell_connection_run, connection,
0, instance);
+		    status = celixThread_create(&connectionRunThread, NULL, &remoteShell_connection_run,
connection);
 		} else {
-			status = APR_ECONNREFUSED;
+			status = CELIX_BUNDLE_EXCEPTION;
 			remoteShell_connection_print(connection, RS_MAXIMUM_CONNECTIONS_REACHED);
 		}
-		apr_thread_mutex_unlock(instance->mutex);
+		celixThreadMutex_unlock(&instance->mutex);
 	} else {
 		status = CELIX_ENOMEM;
 	}
 
 	if (status != CELIX_SUCCESS) {
-		apr_socket_close(socket);
-		apr_pool_destroy(pool);
+		close(connection->socket);
+		free(connection);
 	}
+
 	return status;
 }
 
 celix_status_t remoteShell_stopConnections(remote_shell_pt instance) {
 	celix_status_t status = CELIX_SUCCESS;
-	apr_status_t wakeupStatus = APR_SUCCESS;
-	char error[64];
 	int length = 0;
 	int i = 0;
 
-	apr_thread_mutex_lock(instance->mutex);
+	celixThreadMutex_lock(&instance->mutex);
 	length = arrayList_size(instance->connections);
+
 	for (i = 0; i < length; i += 1) {
 		connection_pt connection = arrayList_get(instance->connections, i);
-		wakeupStatus = apr_pollset_wakeup(connection->pollset);
-		if (wakeupStatus != APR_SUCCESS) {
-			apr_strerror(wakeupStatus, error, 64);
-			fw_log(logger, OSGI_FRAMEWORK_LOG_ERROR, "REMOTE_SHELLE: Error waking up connection %i:
'%s'", i, error);
-		}
+		connection->threadRunning = false;
 	}
-	apr_thread_mutex_unlock(instance->mutex);
-	apr_thread_pool_tasks_cancel(instance->threadPool,instance);
+
+	celixThreadMutex_unlock(&instance->mutex);
 
 	return status;
 }
 
-void *APR_THREAD_FUNC remoteShell_connection_run(apr_thread_t *thread, void *data) {
+void *remoteShell_connection_run(void *data) {
 	celix_status_t status = CELIX_SUCCESS;
 	connection_pt connection = data;
-
-	apr_size_t len;
-	char buff[COMMAND_BUFF_SIZE];
-	apr_pollfd_t pfd = { connection->pool, APR_POLL_SOCKET, APR_POLLIN, 0, { NULL }, NULL
};
-	apr_int32_t num;
-	const apr_pollfd_t *ret_pfd;
-
-	pfd.desc.s = connection->socket;
-
-	status = CELIX_DO_IF(status, apr_pollset_add(connection->pollset, &pfd));
-
-	remoteShell_connection_print(connection, RS_WELCOME);
-	while (status == CELIX_SUCCESS) {
-		status = apr_pollset_poll(connection->pollset, -1, &num, &ret_pfd); //blocks
on fd until a connection is made
-		if (status == APR_SUCCESS) {
-			len = COMMAND_BUFF_SIZE -1;
-			status = apr_socket_recv(connection->socket, buff, &len);
-			if (status == APR_SUCCESS && len < COMMAND_BUFF_SIZE) {
-				apr_status_t commandStatus = APR_SUCCESS;
+	size_t len;
+	int result;
+	struct timeval timeout;  /* Timeout for select */
+
+	connection->threadRunning = true;
+	status = remoteShell_connection_print(connection, RS_WELCOME);
+
+	while (status == CELIX_SUCCESS && connection->threadRunning == true) {
+		do {
+			timeout.tv_sec = CONNECTION_LISTENER_TIMEOUT_SEC;
+			timeout.tv_usec = 0;
+
+			FD_ZERO(&connection->pollset);
+			FD_SET(connection->socket, &connection->pollset);
+			result = select(connection->socket + 1, &connection->pollset, NULL, NULL, &timeout);
+		} while (result == -1 && errno == EINTR && connection->threadRunning
== true);
+
+		/* The socket_fd has data available to be read */
+		if (result > 0 && FD_ISSET(connection->socket, &connection->pollset))
{
+			char buff[COMMAND_BUFF_SIZE];
+
+			len = recv(connection->socket, buff, COMMAND_BUFF_SIZE-1, 0);
+			if (len < COMMAND_BUFF_SIZE) {
+				celix_status_t commandStatus = CELIX_SUCCESS;
 				buff[len]='\0';
+
 				commandStatus = remoteShell_connection_execute(connection, buff);
+
 				if (commandStatus == CELIX_SUCCESS) {
 					remoteShell_connection_print(connection, RS_PROMPT);
-				} else if (commandStatus == APR_EOF) {
+				} else if (commandStatus == CELIX_FILE_IO_EXCEPTION) {
 					//exit command
 					break;
 				} else { //error
@@ -193,60 +182,46 @@ void *APR_THREAD_FUNC remoteShell_connec
 				}
 
 			} else {
-				char error[64];
-				apr_strerror(status, error, 64);
-				fw_log(logger, OSGI_FRAMEWORK_LOG_ERROR, "REMOTE_SHELL: Got error %s", error);
+				fw_log(logger, OSGI_FRAMEWORK_LOG_ERROR, "REMOTE_SHELL: Error while retrieving data");
 			}
-		} else if (status == APR_EINTR) {
-			fw_log(logger, OSGI_FRAMEWORK_LOG_DEBUG, "REMOTE_SHELL: Poll interrupted.");
-		} else /*error*/ {
-			char error[64];
-			apr_strerror(status, error, 64);
-			fw_log(logger, OSGI_FRAMEWORK_LOG_ERROR, "REMOTE_SHELL: Got error %s", error);
-			break;
 		}
 	}
+
 	remoteShell_connection_print(connection, RS_GOODBYE);
 
 	fw_log(logger, OSGI_FRAMEWORK_LOG_INFO, "REMOTE_SHELL: Closing socket");
-	apr_thread_mutex_lock(connection->parent->mutex);
+	celixThreadMutex_lock(&connection->parent->mutex);
 	arrayList_removeElement(connection->parent->connections, connection);
-	apr_thread_mutex_unlock(connection->parent->mutex);
+	celixThreadMutex_unlock(&connection->parent->mutex);
 
-	apr_socket_shutdown(connection->socket, APR_SHUTDOWN_READWRITE);
-	apr_pollset_destroy(connection->pollset);
-	apr_socket_close(connection->socket);
-	apr_pool_destroy(connection->pool);
+	close(connection->socket);
 
 	return NULL;
 }
 
 static celix_status_t remoteShell_connection_execute(connection_pt connection, char *command)
{
-	celix_status_t status;
-
-	apr_pool_t *workPool = NULL;
-	status = apr_pool_create(&workPool,  connection->pool);
+	celix_status_t status = CELIX_SUCCESS;
 
 	if (status == CELIX_SUCCESS) {
-		char *dline = apr_pstrdup(workPool, command);
+		char *dline = strdup(command);
 		char *line = utils_stringTrim(dline);
 		int len = strlen(line);
 
 		if (len == 0) {
 			//ignore
 		} else if (len == 4 && strncmp("exit", line, 4) == 0) {
-			status = APR_EOF;
+			status = CELIX_FILE_IO_EXCEPTION;
 		} else {
 			status = shellMediator_executeCommand(connection->parent->mediator, line, connection->socket);
 		}
 
-		apr_pool_destroy(workPool);
+		free(dline);
 	}
 
 	return status;
 }
 
 celix_status_t remoteShell_connection_print(connection_pt connection, char *text) {
-	apr_size_t len = strlen(text);
-	return apr_socket_send(connection->socket, text, &len);
+	size_t len = strlen(text);
+	return (send(connection->socket, text, len, 0) > 0) ? CELIX_SUCCESS : CELIX_FILE_IO_EXCEPTION;
 }

Modified: celix/trunk/remote_shell/private/src/shell_mediator.c
URL: http://svn.apache.org/viewvc/celix/trunk/remote_shell/private/src/shell_mediator.c?rev=1633943&r1=1633942&r2=1633943&view=diff
==============================================================================
--- celix/trunk/remote_shell/private/src/shell_mediator.c (original)
+++ celix/trunk/remote_shell/private/src/shell_mediator.c Thu Oct 23 20:44:29 2014
@@ -26,9 +26,6 @@
 
 #include <stdlib.h>
 
-#include <apr_pools.h>
-#include <apr_thread_mutex.h>
-
 #include <utils.h>
 #include <shell.h>
 #include <service_tracker.h>
@@ -37,10 +34,10 @@
 #include "shell_mediator.h"
 
 struct shell_mediator {
-    apr_pool_t *pool;
+
 	bundle_context_pt context;
 	service_tracker_pt tracker;
-	apr_thread_mutex_t *mutex;
+	celix_thread_mutex_t mutex;
 
 	//protected by mutex
 	shell_service_pt shellService;
@@ -48,9 +45,8 @@ struct shell_mediator {
 
 //NOTE: multiple instances of shell_mediator are not supported, because we need
 // 		a non ADT - shared between instances - variable.
-static apr_socket_t *currentOutputSocket = NULL;
+static int currentOutputSocket = NULL;
 
-static apr_status_t shellMediator_cleanup(void *handle); //gets called from apr pool cleanup
 static void shellMediator_writeOnCurrentSocket(char *buff);
 
 static celix_status_t shellMediator_addingService(void *handler, service_reference_pt reference,
void **service);
@@ -58,24 +54,23 @@ static celix_status_t shellMediator_adde
 static celix_status_t shellMediator_modifiedService(void *handler, service_reference_pt reference,
void * service);
 static celix_status_t shellMediator_removedService(void *handler, service_reference_pt reference,
void * service);
 
-celix_status_t shellMediator_create(apr_pool_t *pool, bundle_context_pt context, shell_mediator_pt
*instance) {
+celix_status_t shellMediator_create(bundle_context_pt context, shell_mediator_pt *instance)
{
 	celix_status_t status = CELIX_SUCCESS;
 	service_tracker_customizer_pt customizer = NULL;
 
-	(*instance) = (shell_mediator_pt) apr_palloc(pool, sizeof(**instance));
+	(*instance) = (shell_mediator_pt) calloc(1, sizeof(**instance));
     if ((*instance) != NULL) {
-		apr_pool_pre_cleanup_register(pool, *instance, shellMediator_cleanup);
 
-    	(*instance)->pool = pool;
 		(*instance)->context = context;
 		(*instance)->tracker = NULL;
-		(*instance)->mutex = NULL;
 		(*instance)->shellService = NULL;
 
-		status = apr_thread_mutex_create(&(*instance)->mutex, APR_THREAD_MUTEX_DEFAULT,
pool);
+		status = celixThreadMutex_create(&(*instance)->mutex, NULL);
+
 		status = CELIX_DO_IF(status, serviceTrackerCustomizer_create((*instance), shellMediator_addingService,
shellMediator_addedService,
 				shellMediator_modifiedService, shellMediator_removedService, &customizer));
 		status = CELIX_DO_IF(status, serviceTracker_create(context, (char *)OSGI_SHELL_SERVICE_NAME,
customizer, &(*instance)->tracker));
+
 		if (status == CELIX_SUCCESS) {
 			serviceTracker_open((*instance)->tracker);
 		}
@@ -89,32 +84,35 @@ celix_status_t shellMediator_create(apr_
 	return status;
 }
 
-static apr_status_t shellMediator_cleanup(void *handle) {
-	shell_mediator_pt instance = (shell_mediator_pt) handle;
-    apr_thread_mutex_lock(instance->mutex);
+
+celix_status_t shellMediator_destroy(shell_mediator_pt instance) {
+    celixThreadMutex_lock(&instance->mutex);
 
     instance->shellService=NULL;
     serviceTracker_close(instance->tracker);
+    celixThreadMutex_unlock(&instance->mutex);
 
-    apr_thread_mutex_unlock(instance->mutex);
-    return APR_SUCCESS;
+    return CELIX_SUCCESS;
 }
 
 static void shellMediator_writeOnCurrentSocket(char *buff) {
-	apr_size_t len = strlen(buff);
-	apr_socket_send(currentOutputSocket, buff, &len);
+
+	size_t len = strlen(buff);
+	send(currentOutputSocket, buff, len, 0);
 }
 
-celix_status_t shellMediator_executeCommand(shell_mediator_pt instance, char *command, apr_socket_t
*socket) {
-	apr_status_t status = CELIX_SUCCESS;
+celix_status_t shellMediator_executeCommand(shell_mediator_pt instance, char *command, int
socket) {
+	celix_status_t status = CELIX_SUCCESS;
+
+	celixThreadMutex_lock(&instance->mutex);
 
-	apr_thread_mutex_lock(instance->mutex);
 	if (instance->shellService != NULL) {
 		currentOutputSocket=socket;
 		instance->shellService->executeCommand(instance->shellService->shell, command,
shellMediator_writeOnCurrentSocket, shellMediator_writeOnCurrentSocket);
-		currentOutputSocket=NULL;
+		currentOutputSocket=-1;
 	}
-	apr_thread_mutex_unlock(instance->mutex);
+
+	celixThreadMutex_unlock(&instance->mutex);
 
 	return status;
 }
@@ -129,9 +127,9 @@ static celix_status_t shellMediator_addi
 static celix_status_t shellMediator_addedService(void *handler, service_reference_pt reference,
void * service) {
 	celix_status_t status = CELIX_SUCCESS;
 	shell_mediator_pt instance = (shell_mediator_pt) handler;
-	apr_thread_mutex_lock(instance->mutex);
+	celixThreadMutex_lock(&instance->mutex);
 	instance->shellService = (shell_service_pt) service;
-	apr_thread_mutex_unlock(instance->mutex);
+	celixThreadMutex_unlock(&instance->mutex);
 	return status;
 }
 
@@ -144,9 +142,9 @@ static celix_status_t shellMediator_modi
 static celix_status_t shellMediator_removedService(void *handler, service_reference_pt reference,
void * service) {
 	celix_status_t status = CELIX_SUCCESS;
 	shell_mediator_pt instance = (shell_mediator_pt) handler;
-	apr_thread_mutex_lock(instance->mutex);
+	celixThreadMutex_lock(&instance->mutex);
 	instance->shellService = NULL;
-	apr_thread_mutex_unlock(instance->mutex);
+	celixThreadMutex_unlock(&instance->mutex);
 	return status;
 }
 



Mime
View raw message