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/10/19 18:12:53 UTC

svn commit: r465670 [2/2] - in /geronimo/sandbox/gcache/server: ./ src/main/java/org/apache/geronimo/gcache/command/ src/main/java/org/apache/geronimo/gcache/marshal/ src/main/java/org/apache/geronimo/gcache/server/ src/main/java/org/apache/geronimo/gc...

Modified: geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/transports/tcp/AbstractServer.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/transports/tcp/AbstractServer.java?view=diff&rev=465670&r1=465669&r2=465670
==============================================================================
--- geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/transports/tcp/AbstractServer.java (original)
+++ geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/transports/tcp/AbstractServer.java Thu Oct 19 09:12:51 2006
@@ -18,14 +18,11 @@
 package org.apache.geronimo.gcache.transports.tcp;
 
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.net.InetSocketAddress;
-import java.nio.ByteBuffer;
-import java.nio.channels.Channels;
-import java.nio.channels.ReadableByteChannel;
-import java.nio.channels.SocketChannel;
+import java.net.Socket;
 import java.util.Arrays;
-import java.util.zip.CRC32;
-import java.util.zip.Checksum;
 
 import net.sf.ehcache.CacheManager;
 
@@ -33,12 +30,8 @@
 import org.apache.geronimo.gcache.command.BaseCommand;
 import org.apache.geronimo.gcache.command.Command;
 import org.apache.geronimo.gcache.command.CommandTypes;
-import org.apache.geronimo.gcache.server.impl.DefaultThreadPoolImpl;
-import org.apache.geronimo.gcache.server.spi.ThreadPool;
 import org.apache.geronimo.gcache.transports.TransportServer;
-import org.apache.geronimo.gcache.util.BufferChannelReader;
-import org.apache.geronimo.gcache.util.BufferChannelWriter;
-import org.apache.geronimo.gcache.util.ByteArrayInputStream;
+import org.apache.mina.common.ByteBuffer;
 import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
 
@@ -47,50 +40,48 @@
     protected static final String host = "localhost";
 
     protected TransportServer server = null;
-    protected ThreadPool pool;
-    protected SocketChannel clientChannel;
+    protected Socket client;
     protected CacheInfoHolder info;
     
     @BeforeClass
     public void setUp() throws Exception{
-        pool = new DefaultThreadPoolImpl(10);
         CacheManager mgr = CacheManager.create();
         info = new CacheInfoHolder(mgr);
-        DefaultSelectionKeyProcessorFactory factory = new DefaultSelectionKeyProcessorFactory(info);
 
-        server = new TCPSocketTransportServer(host, port, pool, 2000, factory);
+        server = new TCPSocketTransportServer(host, port, info);
 
         server.start();
         
         //Create a client socket
-        clientChannel = SocketChannel.open();
-        clientChannel.connect(new InetSocketAddress(host, port));
+        client = new Socket();
+        client.connect(new InetSocketAddress(host, port),2000);
     }
 
     @AfterClass(alwaysRun=true)
     public void shutdown() throws Exception{
         server.stop();
-        pool.shutdown();
         
-        clientChannel.close();
+        client.close();
     }
     
     protected void sendCommand(BaseCommand command) throws IOException{
 	
 	byte bytes[] = command.createPacket(true);
-	BufferChannelWriter bcw = new BufferChannelWriter(clientChannel, ByteBuffer.wrap(bytes));
-        int written = bcw.writeBuffer(bytes.length);
-        assert written == bytes.length;
+	
+	OutputStream out = client.getOutputStream();
+	out.write(bytes);
+	out.flush();
 	
     }
     
     protected Command readCommand() throws IOException{
 	
-        ByteBuffer receiveHeader = ByteBuffer.allocate(Constants.MAGIC.length + 13);
-        BufferChannelReader bcr = new BufferChannelReader(clientChannel, receiveHeader);
-        int read = bcr.readBuffer(receiveHeader.capacity());
-        assert read == Constants.MAGIC.length + 13;
-        receiveHeader.flip();
+	byte recBuf[] = new byte[Constants.HEADER_SIZE];
+	
+        InputStream is = client.getInputStream();
+        int read = is.read(recBuf);
+        assert read == Constants.HEADER_SIZE;
+	ByteBuffer receiveHeader = ByteBuffer.wrap(recBuf);
 
         //Read the magic
         byte magic[] = new byte[Constants.MAGIC.length];
@@ -102,34 +93,19 @@
         //Get the command
         byte commandIdentifier = receiveHeader.get();
 
-        //Get the checksum
-        long checksum = receiveHeader.getLong();
-
         //Get the command length
         int length = receiveHeader.getInt();
+        receiveHeader.release();
 
         //pull the command
-        ByteBuffer commandBuffer = ByteBuffer.allocate(length);
-        bcr.reset(clientChannel, commandBuffer);
-        int count = bcr.readBuffer(length);
+        recBuf = new byte[length];
+        int count = is.read(recBuf);
         assert count == length;
-
-        //Calc a checksum
-        byte commandArray[] = commandBuffer.array();
-        Checksum calcChecksum = new CRC32();
-        calcChecksum.update(commandArray, 0, commandArray.length);
-        long newCheck  = calcChecksum.getValue();
-
-        //Checksums match
-        assert newCheck == checksum;
-        
-        //Now create the command
-        ByteArrayInputStream bias = new ByteArrayInputStream(commandBuffer.array());
-        ReadableByteChannel readChannel = Channels.newChannel(bias);
+        ByteBuffer commandBuffer = ByteBuffer.wrap(recBuf);
 
         //Create the command and unmarshal the data
         Command command = CommandTypes.createCommand(commandIdentifier);
-        command.readExternal(readChannel);
+        command.readExternal(ByteBuffer.wrap(commandBuffer.array()));
         
         return command;
 

Modified: geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/transports/tcp/TCPEndpointTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/transports/tcp/TCPEndpointTest.java?view=diff&rev=465670&r1=465669&r2=465670
==============================================================================
--- geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/transports/tcp/TCPEndpointTest.java (original)
+++ geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/transports/tcp/TCPEndpointTest.java Thu Oct 19 09:12:51 2006
@@ -17,7 +17,7 @@
  */
 package org.apache.geronimo.gcache.transports.tcp;
 
-import java.nio.channels.SocketChannel;
+import java.net.InetSocketAddress;
 import java.util.Set;
 
 import org.apache.geronimo.gcache.command.BulkSendCommand;
@@ -56,11 +56,11 @@
         
         Set<Endpoint> set = info.getEndpointManager().getEndpoints();
         TCPEndpoint endpoint =  (TCPEndpoint)set.iterator().next();
-        SocketChannel endpointChannel = endpoint.getChannel();
+        InetSocketAddress remoteAddr = (InetSocketAddress)endpoint.getIoSession().getRemoteAddress();
+        InetSocketAddress localAddr = (InetSocketAddress)client.getLocalSocketAddress();
         
         //Check that the socket addresses match (Remote on server == Local for client)
-        assert endpointChannel.socket().getRemoteSocketAddress().equals(clientChannel.socket().getLocalSocketAddress());
-        
+        assert remoteAddr.getPort() == localAddr.getPort();
         
     }