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 12:41:30 UTC
svn commit: r448152 - in /geronimo/sandbox/gcache/server/src:
main/java/org/apache/geronimo/gcache/command/
main/java/org/apache/geronimo/gcache/transports/tcp/
main/java/org/apache/geronimo/gcache/util/
test/java/org/apache/geronimo/gcache/transports/...
Author: jgenender
Date: Wed Sep 20 03:41:29 2006
New Revision: 448152
URL: http://svn.apache.org/viewvc?view=rev&rev=448152
Log:
Fixed up some more marshalling
Added:
geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/Constants.java (with props)
geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/
geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteArrayInputStream.java (with props)
geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteArrayOutputStream.java (with props)
geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteSequence.java (with props)
geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteSequenceData.java (with props)
Modified:
geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/AddEntryCommand.java
geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/BaseCommand.java
geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/DefaultSelectionKeyProcessor.java
geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/TCPSocketHandler.java
geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/TCPSocketServer.java
geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/transports/tcp/TcpSocketServerTest.java
Modified: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/AddEntryCommand.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/AddEntryCommand.java?view=diff&rev=448152&r1=448151&r2=448152
==============================================================================
--- geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/AddEntryCommand.java (original)
+++ geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/AddEntryCommand.java Wed Sep 20 03:41:29 2006
@@ -26,7 +26,7 @@
import java.nio.channels.WritableByteChannel;
import org.apache.geronimo.gcache.marshal.MarshalAware;
-import java.io.ByteArrayOutputStream;
+import org.apache.geronimo.gcache.util.ByteArrayOutputStream;
public class AddEntryCommand extends BaseCommand {
private static final int DEFAULT_CAPACITY = 1024;
@@ -44,7 +44,7 @@
public void setKey(String key) {
this.key = key;
}
-
+
public void execute() {
//TODO: make this add the payload to the local cache then call back to the pusher
}
Modified: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/BaseCommand.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/BaseCommand.java?view=diff&rev=448152&r1=448151&r2=448152
==============================================================================
--- geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/BaseCommand.java (original)
+++ geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/BaseCommand.java Wed Sep 20 03:41:29 2006
@@ -19,12 +19,14 @@
package org.apache.geronimo.gcache.command;
import java.io.IOException;
+import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.charset.Charset;
public class BaseCommand implements Command {
+
private String commandId = null;
public String getCommandId() {
Added: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/Constants.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/Constants.java?view=auto&rev=448152
==============================================================================
--- geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/Constants.java (added)
+++ geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/Constants.java Wed Sep 20 03:41:29 2006
@@ -0,0 +1,23 @@
+/*
+ * 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.apache.geronimo.gcache.transports.tcp;
+
+public class Constants {
+ public final static byte MAGIC[] = new byte[]{'G', 'C', 'a', 'c', 'h', 'e'};
+}
Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/Constants.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/Constants.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/Constants.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/DefaultSelectionKeyProcessor.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/DefaultSelectionKeyProcessor.java?view=diff&rev=448152&r1=448151&r2=448152
==============================================================================
--- geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/DefaultSelectionKeyProcessor.java (original)
+++ geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/DefaultSelectionKeyProcessor.java Wed Sep 20 03:41:29 2006
@@ -22,19 +22,39 @@
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
+import java.util.Arrays;
import org.apache.geronimo.gcache.command.Command;
import org.apache.geronimo.gcache.command.CommandTypes;
public class DefaultSelectionKeyProcessor implements SelectionKeyProcessor {
+ private final static int HEADER_SIZE = Constants.MAGIC.length + 4;
+
public void process(SelectionKey key) throws IOException {
SocketChannel channel = (SocketChannel) key.channel();
+
// first byte is the identifier, the next 4 are the checksum
- ByteBuffer buffer = ByteBuffer.allocateDirect(5);
+ ByteBuffer buffer = ByteBuffer.allocateDirect(HEADER_SIZE);
int count;
while ((count = channel.read(buffer)) > 0) {
+ //Better at least have the size of the HEADER
+ if (count < HEADER_SIZE){
+ disconnect(channel);
+ return;
+ }
+
buffer.flip();
+
+ byte magic[] = new byte[Constants.MAGIC.length];
+ buffer.get(magic);
+
+ //Better match the Magic
+ if (!Arrays.equals(Constants.MAGIC, magic)){
+ disconnect(channel);
+ return;
+ }
+
int commandIdentifier = buffer.getInt();
Command command = CommandTypes.createCommand(commandIdentifier);
command.readExternal(channel);
@@ -45,6 +65,16 @@
if (count < 0) {
channel.close();
}
+ }
+
+ private void disconnect(SocketChannel channel) throws IOException{
+ try {
+ channel.socket().shutdownOutput();
+ } catch (IOException e) {
+ // It seems you have to ignore this due to lots of
+ // transport endpoint not connected issues.
+ }
+ channel.close();
}
}
Modified: 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=diff&rev=448152&r1=448151&r2=448152
==============================================================================
--- geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/TCPSocketHandler.java (original)
+++ geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/TCPSocketHandler.java Wed Sep 20 03:41:29 2006
@@ -32,14 +32,24 @@
*/
public class TCPSocketHandler extends ThreadSupport {
Log log = LogFactory.getLog(TCPSocketHandler.class);
-
- private SelectionKeyProcessor processor = new DefaultSelectionKeyProcessor();
+
+ private SelectionKeyProcessor processor;
private Selector selector = null;
private int timeOut = 0;
public TCPSocketHandler(int timeOut) throws IOException{
+ this(timeOut, null);
+ }
+
+ public TCPSocketHandler(int timeOut, SelectionKeyProcessor processor) throws IOException{
this.timeOut = timeOut;
+
+ if (processor == null)
+ this.processor = new DefaultSelectionKeyProcessor();
+ else
+ this.processor = processor;
+
selector = Selector.open();
}
Modified: 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=diff&rev=448152&r1=448151&r2=448152
==============================================================================
--- geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/TCPSocketServer.java (original)
+++ geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/transports/tcp/TCPSocketServer.java Wed Sep 20 03:41:29 2006
@@ -31,12 +31,18 @@
private ThreadPool pool = null;
private TCPSocketHandler handler = null;
private int timeOut = 0;
+ private SelectionKeyProcessor processor;
public TCPSocketServer(String address, int port, ThreadPool threadPool, int timeOut) {
+ this(address, port, threadPool, timeOut, null);
+ }
+
+ public TCPSocketServer(String address, int port, ThreadPool threadPool, int timeOut, SelectionKeyProcessor processor) {
inet = new InetSocketAddress(address, port);
pool = threadPool;
this.timeOut = timeOut;
+ this.processor = processor;
}
@@ -45,7 +51,7 @@
server = ServerSocketChannel.open();
server.configureBlocking(false);
//TODO: refactor so this impl is not created here but instead its deferred
- handler = new TCPSocketHandler(timeOut);
+ handler = new TCPSocketHandler(timeOut, processor);
// bind the server to the address
ServerSocket socket = server.socket();
Added: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteArrayInputStream.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteArrayInputStream.java?view=auto&rev=448152
==============================================================================
--- geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteArrayInputStream.java (added)
+++ geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteArrayInputStream.java Wed Sep 20 03:41:29 2006
@@ -0,0 +1,96 @@
+/*
+ * 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.apache.geronimo.gcache.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+public class ByteArrayInputStream extends InputStream {
+ byte buffer[];
+ int limit;
+ int pos;
+ int mark;
+
+ public ByteArrayInputStream(byte data[]) {
+ this(data, 0, data.length);
+ }
+
+ public ByteArrayInputStream(ByteSequence sequence) {
+ this(sequence.getData(), sequence.getOffset(), sequence.getLength());
+ }
+
+ public ByteArrayInputStream(byte data[], int offset, int size) {
+ this.buffer = data;
+ this.mark= this.pos = offset;
+ this.limit = offset+size;
+ }
+
+ public int read() throws IOException {
+ if( pos < limit )
+ return buffer[pos++] & 0xff;
+ else
+ return -1;
+ }
+
+ public int read(byte[] b) throws IOException {
+ return read(b, 0, b.length);
+ }
+
+ public int read(byte b[], int off, int len) {
+ if (pos < limit) {
+ len = Math.min(len, limit-pos);
+ if (len > 0) {
+ System.arraycopy(buffer, pos, b, off, len);
+ pos += len;
+ }
+ return len;
+ } else {
+ return -1;
+ }
+ }
+
+ public long skip(long len) throws IOException {
+ if (pos < limit) {
+ len = Math.min(len, limit-pos);
+ if (len > 0) {
+ pos += len;
+ }
+ return len;
+ } else {
+ return -1;
+ }
+ }
+
+ public int available() {
+ return limit - pos;
+ }
+
+ public boolean markSupported() {
+ return true;
+ }
+
+ public void mark(int markpos) {
+ mark = pos;
+ }
+
+ public void reset() {
+ pos = mark;
+ }
+
+}
Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteArrayInputStream.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteArrayInputStream.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteArrayInputStream.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteArrayOutputStream.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteArrayOutputStream.java?view=auto&rev=448152
==============================================================================
--- geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteArrayOutputStream.java (added)
+++ geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteArrayOutputStream.java Wed Sep 20 03:41:29 2006
@@ -0,0 +1,86 @@
+/*
+ * 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.apache.geronimo.gcache.util;
+
+import java.io.OutputStream;
+
+/**
+ * Very similar to the java.io.ByteArrayOutputStream but this version
+ * is not thread safe and the resulting data is returned in a ByteSequence
+ * to avoid an extra byte[] allocation.
+ */
+public class ByteArrayOutputStream extends OutputStream {
+
+ byte buffer[];
+ int size;
+
+ public ByteArrayOutputStream() {
+ this(512);
+ }
+
+ public ByteArrayOutputStream(int capacity) {
+ buffer = new byte[capacity];
+ }
+
+ public void write(int b) {
+ int newsize = size + 1;
+ checkCapacity(newsize);
+ buffer[size] = (byte) b;
+ size = newsize;
+ }
+
+ public void write(byte b[], int off, int len) {
+ int newsize = size + len;
+ checkCapacity(newsize);
+ System.arraycopy(b, off, buffer, size, len);
+ size = newsize;
+ }
+
+ /**
+ * Ensures the the buffer has at least the minimumCapacity specified.
+ *
+ * @param minimumCapacity
+ */
+ private void checkCapacity(int minimumCapacity) {
+ if (minimumCapacity > buffer.length) {
+ byte b[] = new byte[Math.max(buffer.length << 1, minimumCapacity)];
+ System.arraycopy(buffer, 0, b, 0, size);
+ buffer = b;
+ }
+ }
+
+ public void reset() {
+ size = 0;
+ }
+
+ public ByteSequence toByteSequence() {
+ return new ByteSequence(buffer, 0, size);
+ }
+
+ public byte[] toByteArray() {
+ byte rc[] = new byte[size];
+ System.arraycopy(buffer, 0, rc, 0, size);
+ return rc;
+ }
+
+ public int size() {
+ return size;
+ }
+}
+
Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteArrayOutputStream.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteArrayOutputStream.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteArrayOutputStream.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteSequence.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteSequence.java?view=auto&rev=448152
==============================================================================
--- geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteSequence.java (added)
+++ geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteSequence.java Wed Sep 20 03:41:29 2006
@@ -0,0 +1,77 @@
+/*
+ *
+ * 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.util;
+
+/**
+ * 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.
+ */
+public class ByteSequence {
+ public byte[] data;
+ public int offset;
+ public int length;
+
+ public ByteSequence(byte data[]) {
+ this.data = data;
+ this.offset = 0;
+ this.length = data.length;
+ }
+
+ public ByteSequence(byte data[], int offset, int length) {
+ this.data = data;
+ this.offset = offset;
+ this.length = length;
+ }
+
+ public byte[] getData() {
+ return data;
+ }
+
+ public int getLength() {
+ return length;
+ }
+
+ public int getOffset() {
+ return offset;
+ }
+
+ public void setData(byte[] data) {
+ this.data = data;
+ }
+
+ public void setLength(int length) {
+ this.length = length;
+ }
+
+ public void setOffset(int offset) {
+ this.offset = offset;
+ }
+}
Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteSequence.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteSequence.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteSequence.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteSequenceData.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteSequenceData.java?view=auto&rev=448152
==============================================================================
--- geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteSequenceData.java (added)
+++ geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteSequenceData.java Wed Sep 20 03:41:29 2006
@@ -0,0 +1,267 @@
+/*
+ * 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.apache.geronimo.gcache.util;
+
+import java.io.IOException;
+
+/**
+ * Used to write and read primitives to and from a ByteSequence.
+ */
+final public class ByteSequenceData {
+
+
+ public static byte[] toByteArray(ByteSequence packet) {
+ if( packet.offset==0 && packet.length == packet.data.length )
+ return packet.data;
+
+ byte rc[] = new byte[packet.length];
+ System.arraycopy(packet.data, packet.offset, rc, 0, packet.length);
+ return rc;
+ }
+
+ private static void spaceNeeded(ByteSequence packet, int i) {
+ assert packet.offset+i <= packet.length;
+ }
+
+ public static int remaining(ByteSequence packet) {
+ return packet.length - packet.offset;
+ }
+
+ public static int read(ByteSequence packet) {
+ return packet.data[packet.offset++] & 0xff;
+ }
+
+
+ public static void readFully(ByteSequence packet, byte[] b) throws IOException {
+ readFully(packet, b, 0, b.length);
+ }
+
+ public static void readFully(ByteSequence packet, byte[] b, int off, int len) throws IOException {
+ spaceNeeded(packet, len);
+ System.arraycopy(packet.data, packet.offset, b, off, len);
+ packet.offset += len;
+ }
+
+ public static int skipBytes(ByteSequence packet, int n) throws IOException {
+ int rc = Math.min(n, remaining(packet));
+ packet.offset += rc;
+ return rc;
+ }
+
+ public static boolean readBoolean(ByteSequence packet) throws IOException {
+ spaceNeeded(packet, 1);
+ return read(packet) != 0;
+ }
+
+ public static byte readByte(ByteSequence packet) throws IOException {
+ spaceNeeded(packet, 1);
+ return (byte) read(packet);
+ }
+
+ public static int readUnsignedByte(ByteSequence packet) throws IOException {
+ spaceNeeded(packet, 1);
+ return read(packet);
+ }
+
+ public static short readShortBig(ByteSequence packet) throws IOException {
+ spaceNeeded(packet, 2);
+ return (short) ((read(packet) << 8) + (read(packet) << 0));
+ }
+ public static short readShortLittle(ByteSequence packet) throws IOException {
+ spaceNeeded(packet, 2);
+ return (short) ((read(packet) << 0) + (read(packet) << 8) );
+ }
+
+ public static int readUnsignedShortBig(ByteSequence packet) throws IOException {
+ spaceNeeded(packet, 2);
+ return ((read(packet) << 8) + (read(packet) << 0));
+ }
+ public static int readUnsignedShortLittle(ByteSequence packet) throws IOException {
+ spaceNeeded(packet, 2);
+ return ((read(packet) << 0) + (read(packet) << 8) );
+ }
+
+ public static char readCharBig(ByteSequence packet) throws IOException {
+ spaceNeeded(packet, 2);
+ return (char) ((read(packet) << 8) + (read(packet) << 0));
+ }
+ public static char readCharLittle(ByteSequence packet) throws IOException {
+ spaceNeeded(packet, 2);
+ return (char) ((read(packet) << 0) + (read(packet) << 8) );
+ }
+
+ public static int readIntBig(ByteSequence packet) throws IOException {
+ spaceNeeded(packet, 4);
+ return ((read(packet) << 24) +
+ (read(packet) << 16) +
+ (read(packet) << 8) +
+ (read(packet) << 0));
+ }
+ public static int readIntLittle(ByteSequence packet) throws IOException {
+ spaceNeeded(packet, 4);
+ return ((read(packet) << 0) +
+ (read(packet) << 8) +
+ (read(packet) << 16) +
+ (read(packet) << 24));
+ }
+
+ public static long readLongBig(ByteSequence packet) throws IOException {
+ spaceNeeded(packet, 8);
+ return (((long) read(packet) << 56) +
+ ((long) read(packet) << 48) +
+ ((long) read(packet) << 40) +
+ ((long) read(packet) << 32) +
+ ((long) read(packet) << 24) +
+ ((read(packet)) << 16) +
+ ((read(packet)) << 8) +
+ ((read(packet)) << 0));
+ }
+ public static long readLongLittle(ByteSequence packet) throws IOException {
+ spaceNeeded(packet, 8);
+ return ((read(packet) << 0) +
+ (read(packet) << 8) +
+ (read(packet) << 16) +
+ ((long) read(packet) << 24) +
+ ((long) read(packet) << 32) +
+ ((long) read(packet) << 40) +
+ ((long) read(packet) << 48) +
+ ((long) read(packet) << 56));
+ }
+
+ public static double readDoubleBig(ByteSequence packet) throws IOException {
+ return Double.longBitsToDouble(readLongBig(packet));
+ }
+ public static double readDoubleLittle(ByteSequence packet) throws IOException {
+ return Double.longBitsToDouble(readLongLittle(packet));
+ }
+
+ public static float readFloatBig(ByteSequence packet) throws IOException {
+ return Float.intBitsToFloat(readIntBig(packet));
+ }
+ public static float readFloatLittle(ByteSequence packet) throws IOException {
+ return Float.intBitsToFloat(readIntLittle(packet));
+ }
+
+ public static void write(ByteSequence packet, int b) throws IOException {
+ spaceNeeded(packet, 1);
+ packet.data[packet.offset++] = (byte) b;
+ }
+
+ public static void write(ByteSequence packet, byte[] b) throws IOException {
+ write(packet, b, 0, b.length);
+ }
+ public static void write(ByteSequence packet, byte[] b, int off, int len) throws IOException {
+ spaceNeeded(packet, len);
+ System.arraycopy(b, off, packet.data, packet.offset, len);
+ packet.offset += len;
+ }
+ public static void writeBoolean(ByteSequence packet, boolean v) throws IOException {
+ spaceNeeded(packet, 1);
+ write(packet,v ? 1 : 0);
+ }
+ public static void writeByte(ByteSequence packet, int v) throws IOException {
+ spaceNeeded(packet, 1);
+ write(packet,v);
+ }
+ public static void writeShortBig(ByteSequence packet, int v) throws IOException {
+ spaceNeeded(packet, 2);
+ write(packet,(v >>> 8) & 0xFF);
+ write(packet,(v >>> 0) & 0xFF);
+ }
+ public static void writeShortLittle(ByteSequence packet, int v) throws IOException {
+ spaceNeeded(packet, 2);
+ write(packet,(v >>> 0) & 0xFF);
+ write(packet,(v >>> 8) & 0xFF);
+ }
+ public static void writeCharBig(ByteSequence packet, int v) throws IOException {
+ spaceNeeded(packet, 2);
+ write(packet,(v >>> 8) & 0xFF);
+ write(packet,(v >>> 0) & 0xFF);
+ }
+ public static void writeCharLittle(ByteSequence packet, int v) throws IOException {
+ spaceNeeded(packet, 2);
+ write(packet,(v >>> 0) & 0xFF);
+ write(packet,(v >>> 8) & 0xFF);
+ }
+ public static void writeIntBig(ByteSequence packet, int v) throws IOException {
+ spaceNeeded(packet, 4);
+ write(packet,(v >>> 24) & 0xFF);
+ write(packet,(v >>> 16) & 0xFF);
+ write(packet,(v >>> 8) & 0xFF);
+ write(packet,(v >>> 0) & 0xFF);
+ }
+ public static void writeIntLittle(ByteSequence packet, int v) throws IOException {
+ spaceNeeded(packet, 4);
+ write(packet,(v >>> 0) & 0xFF);
+ write(packet,(v >>> 8) & 0xFF);
+ write(packet,(v >>> 16) & 0xFF);
+ write(packet,(v >>> 24) & 0xFF);
+ }
+ public static void writeLongBig(ByteSequence packet, long v) throws IOException {
+ spaceNeeded(packet, 8);
+ write(packet,(int) (v >>> 56) & 0xFF);
+ write(packet,(int) (v >>> 48) & 0xFF);
+ write(packet,(int) (v >>> 40) & 0xFF);
+ write(packet,(int) (v >>> 32) & 0xFF);
+ write(packet,(int) (v >>> 24) & 0xFF);
+ write(packet,(int) (v >>> 16) & 0xFF);
+ write(packet,(int) (v >>> 8) & 0xFF);
+ write(packet,(int) (v >>> 0) & 0xFF);
+ }
+ public static void writeLongLittle(ByteSequence packet, long v) throws IOException {
+ spaceNeeded(packet, 8);
+ write(packet,(int) (v >>> 0) & 0xFF);
+ write(packet,(int) (v >>> 8) & 0xFF);
+ write(packet,(int) (v >>> 16) & 0xFF);
+ write(packet,(int) (v >>> 24) & 0xFF);
+ write(packet,(int) (v >>> 32) & 0xFF);
+ write(packet,(int) (v >>> 40) & 0xFF);
+ write(packet,(int) (v >>> 48) & 0xFF);
+ write(packet,(int) (v >>> 56) & 0xFF);
+ }
+
+ public static void writeDoubleBig(ByteSequence packet, double v) throws IOException {
+ writeLongBig(packet, Double.doubleToLongBits(v));
+ }
+ public static void writeDoubleLittle(ByteSequence packet, double v) throws IOException {
+ writeLongLittle(packet, Double.doubleToLongBits(v));
+ }
+
+ public static void writeFloatBig(ByteSequence packet, float v) throws IOException {
+ writeIntBig(packet, Float.floatToIntBits(v));
+ }
+ public static void writeFloatLittle(ByteSequence packet, float v) throws IOException {
+ writeIntLittle(packet, Float.floatToIntBits(v));
+ }
+
+ public static void writeRawDoubleBig(ByteSequence packet, double v) throws IOException {
+ writeLongBig(packet, Double.doubleToRawLongBits(v));
+ }
+ public static void writeRawDoubleLittle(ByteSequence packet, double v) throws IOException {
+ writeLongLittle(packet, Double.doubleToRawLongBits(v));
+ }
+
+ public static void writeRawFloatBig(ByteSequence packet, float v) throws IOException {
+ writeIntBig(packet, Float.floatToRawIntBits(v));
+ }
+ public static void writeRawFloatLittle(ByteSequence packet, float v) throws IOException {
+ writeIntLittle(packet, Float.floatToRawIntBits(v));
+ }
+
+}
Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteSequenceData.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteSequenceData.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteSequenceData.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: 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=diff&rev=448152&r1=448151&r2=448152
==============================================================================
--- geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/transports/tcp/TcpSocketServerTest.java (original)
+++ geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/transports/tcp/TcpSocketServerTest.java Wed Sep 20 03:41:29 2006
@@ -24,8 +24,9 @@
@Test()
public void runServer() throws Exception {
ThreadPool pool = new DefaultThreadPoolImpl(10);
+ MockSelectionKeyProcessor mock = new MockSelectionKeyProcessor();
+ //TCPSocketServer server = new TCPSocketServer("localhost", 45678, pool, 2000, mock);
TCPSocketServer server = new TCPSocketServer("localhost", 45678, pool, 2000);
- // TODO: need a way to substitute the MockSelectionKeyProcessor before invoking the server
server.start();
Thread.sleep(100000);
server.stop();