You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jg...@apache.org on 2006/09/20 05:05:53 UTC
svn commit: r448040 - in /geronimo/sandbox/gcache/server/src:
main/java/org/apache/geronimo/gcache/
main/java/org/apache/geronimo/gcache/transports/
main/java/org/apache/geronimo/gcache/transports/tcp/
test/java/org/apache/geronimo/gcache/transports/ t...
Author: jgenender
Date: Tue Sep 19 20:05:52 2006
New Revision: 448040
URL: http://svn.apache.org/viewvc?view=rev&rev=448040
Log:
Adding TCP code - needs major work -just saving state
Added:
geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/ThreadSupport.java (with props)
geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/
geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/
geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/TCPSocketHandler.java (with props)
geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/TCPSocketServer.java (with props)
geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/transports/
geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/transports/tcp/
geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/transports/tcp/TcpSocketServerTest.java (with props)
Added: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/ThreadSupport.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/ThreadSupport.java?view=auto&rev=448040
==============================================================================
--- geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/ThreadSupport.java (added)
+++ geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/ThreadSupport.java Tue Sep 19 20:05:52 2006
@@ -0,0 +1,50 @@
+/*
+ *
+ * Copyright 2006 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 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.apache.geronimo.gcache;
+
+public abstract class ThreadSupport extends Thread{
+ private boolean stopped;
+ private boolean running;
+
+ public final void run() {
+
+ initialize();
+
+ stopped = false;
+ running = true;
+
+ while(!stopped){
+ execute();
+ }
+
+ running = false;
+ cleanUp();
+ }
+
+ protected abstract void initialize();
+ protected abstract void execute();
+ protected abstract void cleanUp();
+
+ public void halt(){
+ stopped = true;
+ }
+
+ public boolean isRunning(){
+ return running;
+ }
+
+}
Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/ThreadSupport.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/ThreadSupport.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/ThreadSupport.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/TCPSocketHandler.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/TCPSocketHandler.java?view=auto&rev=448040
==============================================================================
--- geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/TCPSocketHandler.java (added)
+++ geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/TCPSocketHandler.java Tue Sep 19 20:05:52 2006
@@ -0,0 +1,123 @@
+/*
+ *
+ * Copyright 2006 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 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.apache.geronimo.gcache.transports.tcp;
+
+import org.apache.geronimo.gcache.ThreadSupport;
+import org.apache.geronimo.openwire.util.ByteArrayInputStream;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.nio.channels.Selector;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.SocketChannel;
+import java.nio.channels.ServerSocketChannel;
+import java.nio.channels.SelectableChannel;
+import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.util.Iterator;
+import java.net.Socket;
+
+public class TCPSocketHandler extends ThreadSupport {
+ Log log = LogFactory.getLog(TCPSocketHandler.class);
+
+ private Selector selector = null;
+ private int timeOut = 0;
+
+ public TCPSocketHandler(int timeOut) throws IOException{
+ this.timeOut = timeOut;
+ selector = Selector.open();
+ }
+
+ public void register(SelectableChannel channel, int ops) throws IOException{
+ channel.configureBlocking(false);
+ channel.register(selector, ops);
+ }
+
+ protected void execute() {
+ try {
+ if (selector.keys().isEmpty()) {
+ Thread.sleep(timeOut);
+ return;
+ }
+
+ if (selector.select(timeOut) == 0)
+ return;
+
+ //Process the keys from the queue
+ Iterator iter = selector.selectedKeys().iterator();
+ while (iter.hasNext()) {
+ SelectionKey key = (SelectionKey) iter.next();
+
+ //Pull it off the queue for handling
+ iter.remove();
+
+ if (!key.isValid()){
+ key.cancel();
+ }
+
+ if (key.isAcceptable()) {
+ ServerSocketChannel server = (ServerSocketChannel) key.channel();
+ SocketChannel channel = server.accept();
+ if (channel == null)
+ continue;
+ channel.configureBlocking(false);
+ register(channel, SelectionKey.OP_READ);
+ }
+ if (key.isReadable()) {
+ System.out.println("READ");
+ SocketChannel channel = (SocketChannel)key.channel();
+
+ ByteBuffer buffer = ByteBuffer.allocateDirect(5);
+ buffer.clear();
+ int count;
+ while((count = channel.read(buffer)) > 0){
+ buffer.flip();
+ }
+ String test = Charset.forName("UTF-8").decode(buffer).toString();
+ System.out.print(test);
+ buffer.clear();
+ System.out.println();
+
+ //Test for closed connection
+ if (count < 0){
+ channel.close();
+ }
+ }
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (InterruptedException e) {
+ log.error("InterruptedException occured", e);
+ }
+ }
+
+ protected void initialize() {
+ }
+
+ protected void cleanUp() {
+ try {
+ if ((selector != null) && (selector.isOpen())) {
+ selector.close();
+ }
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+}
Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/TCPSocketHandler.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/TCPSocketHandler.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/TCPSocketHandler.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/TCPSocketServer.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/TCPSocketServer.java?view=auto&rev=448040
==============================================================================
--- geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/TCPSocketServer.java (added)
+++ geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/TCPSocketServer.java Tue Sep 19 20:05:52 2006
@@ -0,0 +1,64 @@
+/*
+ *
+ * Copyright 2006 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 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.apache.geronimo.gcache.transports.tcp;
+
+import org.apache.geronimo.gcache.server.spi.ThreadPool;
+
+import java.nio.channels.ServerSocketChannel;
+import java.nio.channels.SelectionKey;
+import java.net.InetSocketAddress;
+import java.net.ServerSocket;
+import java.io.IOException;
+
+public class TCPSocketServer {
+
+ private ServerSocketChannel server = null;
+ private InetSocketAddress inet = null;
+ private ThreadPool pool = null;
+ private TCPSocketHandler handler = null;
+ private int timeOut = 0;
+
+ public TCPSocketServer(String address, int port, ThreadPool threadPool, int timeOut) {
+
+ inet = new InetSocketAddress(address, port);
+ pool = threadPool;
+ this.timeOut = timeOut;
+
+ }
+
+ public void start() throws IOException {
+
+ server = ServerSocketChannel.open();
+ server.configureBlocking(false);
+
+ handler = new TCPSocketHandler(timeOut);
+
+ ServerSocket socket = server.socket();
+ socket.setReuseAddress(true);
+ socket.bind(inet);
+
+ handler.register(server, SelectionKey.OP_ACCEPT);
+ handler.start();
+ }
+
+ public void stop() throws IOException {
+ handler.halt();
+ if (server != null && server.isOpen())
+ server.close();
+ }
+
+}
Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/TCPSocketServer.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/TCPSocketServer.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/TCPSocketServer.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/transports/tcp/TcpSocketServerTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/transports/tcp/TcpSocketServerTest.java?view=auto&rev=448040
==============================================================================
--- geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/transports/tcp/TcpSocketServerTest.java (added)
+++ geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/transports/tcp/TcpSocketServerTest.java Tue Sep 19 20:05:52 2006
@@ -0,0 +1,32 @@
+/*
+ *
+ * Copyright 2006 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 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.apache.geronimo.gcache.transports.tcp;
+
+import org.testng.annotations.Test;
+import org.apache.geronimo.gcache.server.spi.ThreadPool;
+import org.apache.geronimo.gcache.server.impl.DefaultThreadPoolImpl;
+
+public class TcpSocketServerTest {
+ @Test()
+ public void runServer() throws Exception {
+ ThreadPool pool = new DefaultThreadPoolImpl(10);
+ TCPSocketServer server = new TCPSocketServer("localhost", 45678, pool, 2000);
+ server.start();
+ Thread.sleep(100000);
+ server.stop();
+ }
+}
Propchange: geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/transports/tcp/TcpSocketServerTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/transports/tcp/TcpSocketServerTest.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/transports/tcp/TcpSocketServerTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain