Skip to content

Commit

Permalink
Initial Comment that contains a mostly completed version of this
Browse files Browse the repository at this point in the history
little tool.
  • Loading branch information
ccorsi committed Jan 16, 2012
0 parents commit 24e097a
Show file tree
Hide file tree
Showing 10 changed files with 1,220 additions and 0 deletions.
49 changes: 49 additions & 0 deletions pom.xml
@@ -0,0 +1,49 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.valhalla.tools.net</groupId>
<artifactId>net-proc-comm-tool</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<name>net-ipc-tools</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
<optional>false</optional>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.valhalla.tools.process</groupId>
<artifactId>process-ipc-tools</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
45 changes: 45 additions & 0 deletions src/main/java/org/valhalla/tools/net/CommandExecutor.java
@@ -0,0 +1,45 @@
/**
* 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.
*/
package org.valhalla.tools.net;

import java.io.Serializable;

/**
* This is the main interface that all users of this library will be required to
* implement. This interface implementation will have intimate knowledge of what
* actions need to be performed on the passed instances. The current library
* has no claims on this and leaves it to the user of this library.
*
* @author Claudio Corsi
*
*/
public interface CommandExecutor<S, R> extends Serializable {

/**
* This method will apply the given changes to the passed instance.
* It will return a reply to be sent to the remote process. The
* returned instance can be null.
*
* @param destination The instance that this instance will perform
* the given request/reply
*
* @return An instance of a CommandExecutor that will be sent to
* the requesting/replying process. This can be null
*/
CommandExecutor<R, S> apply(S destination);

}
139 changes: 139 additions & 0 deletions src/main/java/org/valhalla/tools/net/ServiceClient.java
@@ -0,0 +1,139 @@
/**
* 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.
*/
package org.valhalla.tools.net;

import java.net.Socket;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This ServiceClient class is used to communicate with the ServiceServer instance and
* implements all of the required calls to be able to receive and send CommandExecutor
* instances between this process and the server process.
*
* @author Claudio Corsi
*
*/
public class ServiceClient<C, S> {

private static final Logger logger = LoggerFactory.getLogger(ServiceClient.class);

private CountDownLatch shutdown = new CountDownLatch(1);
private ServiceManager<C, S> manager = null;
private String hostname;
private AtomicBoolean started;

/**
* This default constructor will assume that the service server instance is located
* on the same host as this process. This is similar to passing "localhost" to the
* constructor that expects a hostname.
*/
public ServiceClient() {
this("localhost");
}

/**
* This method will create an instance of the service client that will be used to
* communicate to the service server instance that is located within the passed
* host name.
*
* @param hostname the name of the host that the service server instance is located
*
*/
public ServiceClient(String hostname) {
this.hostname = hostname;
}

/**
* This method is used to start the process of communicating with the service
* server instance for the passed host name. This method will setup the
* socket communication and instantiate the ServiceManager instance that is
* used to communicate with the service manager instance of the service
* server. </br>
*
* This method can only be called once per instance. Any subsequent calls to
* this method will generate and IllegalArgumentException.
*
* @param client the client instance that received CommandExecutor instances
* are applied to.
*
* @throws Exception If any exceptions are raised when trying to setup the
* socket communication between this service client and the service server.
*
*/
public void execute(final C client) throws Exception {
if (started.compareAndSet(false, true) == false) {
logger.error("The execute method was called multiple times which is not allowed.");
throw new IllegalStateException("This method can only be called once");
}

// TODO: Should we be creating a thread that will then perform a callback
// action to inform the calling process that the ServiceManager was
// successfully created and is ready to perform CommandExecutor
// processing.
// It might not be necessary since the ServiceManager instance already
// spawns a thread to process the CommandExecutor actions.

// This is the main point of entry that we will be using to
// test the new feature...
int port = Integer.getInteger(ServiceConstants.SERVICE_MANAGER_PORT_NAME, 0);
if (port <= 0) {
logger.info("No service manager port number was defined");
throw new IllegalArgumentException("The service manager port is invalid: " + port);
}
Socket socket = new Socket(hostname, port);
try {
logger.info("Creating Service Manager instance");
manager = new ServiceManager<C, S>(socket, client);
// Now that we started the manager we just wait until we are told to
// finish....
logger.info("Waiting to be told that I am done");
shutdown.await();
} finally {
logger.info("Exiting client application");
if (manager != null) {
// Closing the manager causes the socket to be closed also...
manager.stop();
} else {
// Close the opened socket...
socket.close();
}
}
}

/**
* This method is called whenever you want to shutdown the communication between
* this client service manager and the server service manager.
*
*/
public void shutdown() {
this.shutdown.countDown();
}

/**
* This method will return this instance service manager instance.
*
* @return the service manager instance
*/
public ServiceManager<C, S> getManager() {
return manager;
}

}
27 changes: 27 additions & 0 deletions src/main/java/org/valhalla/tools/net/ServiceConstants.java
@@ -0,0 +1,27 @@
/**
* 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.
*/
package org.valhalla.tools.net;

/**
* @author Claudio Corsi
*
*/
public interface ServiceConstants {

String SERVICE_MANAGER_PORT_NAME = "service.manager.port";

}

0 comments on commit 24e097a

Please sign in to comment.