You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by tr...@apache.org on 2005/12/01 06:19:07 UTC
svn commit: r350169 [2/16] - in /directory/network:
branches/chain_refactor/src/java/org/apache/mina/common/
trunk/src/examples/org/apache/mina/examples/echoserver/
trunk/src/examples/org/apache/mina/examples/httpserver/
trunk/src/examples/org/apache/m...
Modified: directory/network/trunk/src/examples/org/apache/mina/examples/sumup/ServerSessionHandler.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/examples/org/apache/mina/examples/sumup/ServerSessionHandler.java?rev=350169&r1=350168&r2=350169&view=diff
==============================================================================
--- directory/network/trunk/src/examples/org/apache/mina/examples/sumup/ServerSessionHandler.java (original)
+++ directory/network/trunk/src/examples/org/apache/mina/examples/sumup/ServerSessionHandler.java Wed Nov 30 21:17:41 2005
@@ -1,128 +1,128 @@
-/*
- * @(#) $Id$
- *
- * Copyright 2004 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.mina.examples.sumup;
-
-import org.apache.mina.common.IdleStatus;
-import org.apache.mina.common.IoHandler;
-import org.apache.mina.common.IoSession;
-import org.apache.mina.examples.sumup.codec.SumUpProtocolCodecFactory;
-import org.apache.mina.examples.sumup.message.AddMessage;
-import org.apache.mina.examples.sumup.message.ResultMessage;
-import org.apache.mina.filter.LoggingFilter;
-import org.apache.mina.filter.codec.ProtocolCodecFactory;
-import org.apache.mina.filter.codec.ProtocolCodecFilter;
-import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
-import org.apache.mina.util.SessionLog;
-
-/**
- * {@link IoHandler} for SumUp server.
- *
- * @author The Apache Directory Project
- * @version $Rev$, $Date$
- */
-public class ServerSessionHandler implements IoHandler
-{
- private final boolean useCustomCodec;
-
- public ServerSessionHandler( boolean useCustomCodec )
- {
- this.useCustomCodec = useCustomCodec;
- }
-
- public void sessionCreated( IoSession session ) throws Exception
- {
- ProtocolCodecFactory codec;
- if( useCustomCodec )
- {
- codec = new SumUpProtocolCodecFactory( true );
- }
- else
- {
- codec = new ObjectSerializationCodecFactory();
- }
-
- session.getFilterChain().addFirst(
- "protocolFilter", new ProtocolCodecFilter( codec ) );
- session.getFilterChain().addLast(
- "logger", new LoggingFilter() );
- }
-
- public void sessionOpened( IoSession session )
- {
- // set idle time to 60 seconds
- session.setIdleTime( IdleStatus.BOTH_IDLE, 60 );
-
- // initial sum is zero
- session.setAttachment( new Integer( 0 ) );
- }
-
- public void sessionClosed( IoSession session )
- {
- }
-
- public void messageReceived( IoSession session, Object message )
- {
- // client only sends AddMessage. otherwise, we will have to identify
- // its type using instanceof operator.
- AddMessage am = ( AddMessage ) message;
-
- // add the value to the current sum.
- int sum = ( ( Integer ) session.getAttachment() ).intValue();
- int value = am.getValue();
- long expectedSum = ( long ) sum + value;
- if( expectedSum > Integer.MAX_VALUE || expectedSum < Integer.MIN_VALUE )
- {
- // if the sum overflows or underflows, return error message
- ResultMessage rm = new ResultMessage();
- rm.setSequence( am.getSequence() ); // copy sequence
- rm.setOk( false );
- session.write( rm );
- }
- else
- {
- // sum up
- sum = ( int ) expectedSum;
- session.setAttachment( new Integer( sum ) );
-
- // return the result message
- ResultMessage rm = new ResultMessage();
- rm.setSequence( am.getSequence() ); // copy sequence
- rm.setOk( true );
- rm.setValue( sum );
- session.write( rm );
- }
- }
-
- public void messageSent( IoSession session, Object message )
- {
- }
-
- public void sessionIdle( IoSession session, IdleStatus status )
- {
- SessionLog.info( session, "Disconnecting the idle." );
- // disconnect an idle client
- session.close();
- }
-
- public void exceptionCaught( IoSession session, Throwable cause )
- {
- // close the connection on exceptional situation
- session.close();
- }
+/*
+ * @(#) $Id$
+ *
+ * Copyright 2004 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.mina.examples.sumup;
+
+import org.apache.mina.common.IdleStatus;
+import org.apache.mina.common.IoHandler;
+import org.apache.mina.common.IoSession;
+import org.apache.mina.examples.sumup.codec.SumUpProtocolCodecFactory;
+import org.apache.mina.examples.sumup.message.AddMessage;
+import org.apache.mina.examples.sumup.message.ResultMessage;
+import org.apache.mina.filter.LoggingFilter;
+import org.apache.mina.filter.codec.ProtocolCodecFactory;
+import org.apache.mina.filter.codec.ProtocolCodecFilter;
+import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
+import org.apache.mina.util.SessionLog;
+
+/**
+ * {@link IoHandler} for SumUp server.
+ *
+ * @author The Apache Directory Project
+ * @version $Rev$, $Date$
+ */
+public class ServerSessionHandler implements IoHandler
+{
+ private final boolean useCustomCodec;
+
+ public ServerSessionHandler( boolean useCustomCodec )
+ {
+ this.useCustomCodec = useCustomCodec;
+ }
+
+ public void sessionCreated( IoSession session ) throws Exception
+ {
+ ProtocolCodecFactory codec;
+ if( useCustomCodec )
+ {
+ codec = new SumUpProtocolCodecFactory( true );
+ }
+ else
+ {
+ codec = new ObjectSerializationCodecFactory();
+ }
+
+ session.getFilterChain().addFirst(
+ "protocolFilter", new ProtocolCodecFilter( codec ) );
+ session.getFilterChain().addLast(
+ "logger", new LoggingFilter() );
+ }
+
+ public void sessionOpened( IoSession session )
+ {
+ // set idle time to 60 seconds
+ session.setIdleTime( IdleStatus.BOTH_IDLE, 60 );
+
+ // initial sum is zero
+ session.setAttachment( new Integer( 0 ) );
+ }
+
+ public void sessionClosed( IoSession session )
+ {
+ }
+
+ public void messageReceived( IoSession session, Object message )
+ {
+ // client only sends AddMessage. otherwise, we will have to identify
+ // its type using instanceof operator.
+ AddMessage am = ( AddMessage ) message;
+
+ // add the value to the current sum.
+ int sum = ( ( Integer ) session.getAttachment() ).intValue();
+ int value = am.getValue();
+ long expectedSum = ( long ) sum + value;
+ if( expectedSum > Integer.MAX_VALUE || expectedSum < Integer.MIN_VALUE )
+ {
+ // if the sum overflows or underflows, return error message
+ ResultMessage rm = new ResultMessage();
+ rm.setSequence( am.getSequence() ); // copy sequence
+ rm.setOk( false );
+ session.write( rm );
+ }
+ else
+ {
+ // sum up
+ sum = ( int ) expectedSum;
+ session.setAttachment( new Integer( sum ) );
+
+ // return the result message
+ ResultMessage rm = new ResultMessage();
+ rm.setSequence( am.getSequence() ); // copy sequence
+ rm.setOk( true );
+ rm.setValue( sum );
+ session.write( rm );
+ }
+ }
+
+ public void messageSent( IoSession session, Object message )
+ {
+ }
+
+ public void sessionIdle( IoSession session, IdleStatus status )
+ {
+ SessionLog.info( session, "Disconnecting the idle." );
+ // disconnect an idle client
+ session.close();
+ }
+
+ public void exceptionCaught( IoSession session, Throwable cause )
+ {
+ // close the connection on exceptional situation
+ session.close();
+ }
}
Modified: directory/network/trunk/src/examples/org/apache/mina/examples/sumup/codec/Constants.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/examples/org/apache/mina/examples/sumup/codec/Constants.java?rev=350169&r1=350168&r2=350169&view=diff
==============================================================================
--- directory/network/trunk/src/examples/org/apache/mina/examples/sumup/codec/Constants.java (original)
+++ directory/network/trunk/src/examples/org/apache/mina/examples/sumup/codec/Constants.java Wed Nov 30 21:17:41 2005
@@ -1,44 +1,44 @@
-/*
- * @(#) $Id$
- *
- * Copyright 2004 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.mina.examples.sumup.codec;
-
-/**
- * Provides SumUp protocol constants.
- *
- * @author The Apache Directory Project
- * @version $Rev$, $Date$
- */
-public class Constants
-{
- public static final int TYPE_LEN = 2;
- public static final int SEQUENCE_LEN = 4;
- public static final int HEADER_LEN = TYPE_LEN + SEQUENCE_LEN;
- public static final int BODY_LEN = 12;
- public static final int RESULT = 0;
- public static final int ADD = 1;
- public static final int RESULT_CODE_LEN = 2;
- public static final int RESULT_VALUE_LEN = 4;
- public static final int ADD_BODY_LEN = 4;
- public static final int RESULT_OK = 0;
- public static final int RESULT_ERROR = 1;
-
- private Constants()
- {
- }
-}
+/*
+ * @(#) $Id$
+ *
+ * Copyright 2004 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.mina.examples.sumup.codec;
+
+/**
+ * Provides SumUp protocol constants.
+ *
+ * @author The Apache Directory Project
+ * @version $Rev$, $Date$
+ */
+public class Constants
+{
+ public static final int TYPE_LEN = 2;
+ public static final int SEQUENCE_LEN = 4;
+ public static final int HEADER_LEN = TYPE_LEN + SEQUENCE_LEN;
+ public static final int BODY_LEN = 12;
+ public static final int RESULT = 0;
+ public static final int ADD = 1;
+ public static final int RESULT_CODE_LEN = 2;
+ public static final int RESULT_VALUE_LEN = 4;
+ public static final int ADD_BODY_LEN = 4;
+ public static final int RESULT_OK = 0;
+ public static final int RESULT_ERROR = 1;
+
+ private Constants()
+ {
+ }
+}
Modified: directory/network/trunk/src/examples/org/apache/mina/examples/sumup/message/AbstractMessage.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/examples/org/apache/mina/examples/sumup/message/AbstractMessage.java?rev=350169&r1=350168&r2=350169&view=diff
==============================================================================
--- directory/network/trunk/src/examples/org/apache/mina/examples/sumup/message/AbstractMessage.java (original)
+++ directory/network/trunk/src/examples/org/apache/mina/examples/sumup/message/AbstractMessage.java Wed Nov 30 21:17:41 2005
@@ -1,42 +1,42 @@
-/*
- * @(#) $Id$
- *
- * Copyright 2004 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.mina.examples.sumup.message;
-
-import java.io.Serializable;
-
-/**
- * A base message for SumUp protocol messages.
- *
- * @author The Apache Directory Project
- * @version $Rev$, $Date$
- */
-public abstract class AbstractMessage implements Serializable
-{
- private int sequence;
-
- public int getSequence()
- {
- return sequence;
- }
-
- public void setSequence( int sequence )
- {
- this.sequence = sequence;
- }
+/*
+ * @(#) $Id$
+ *
+ * Copyright 2004 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.mina.examples.sumup.message;
+
+import java.io.Serializable;
+
+/**
+ * A base message for SumUp protocol messages.
+ *
+ * @author The Apache Directory Project
+ * @version $Rev$, $Date$
+ */
+public abstract class AbstractMessage implements Serializable
+{
+ private int sequence;
+
+ public int getSequence()
+ {
+ return sequence;
+ }
+
+ public void setSequence( int sequence )
+ {
+ this.sequence = sequence;
+ }
}
Modified: directory/network/trunk/src/examples/org/apache/mina/examples/sumup/message/AddMessage.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/examples/org/apache/mina/examples/sumup/message/AddMessage.java?rev=350169&r1=350168&r2=350169&view=diff
==============================================================================
--- directory/network/trunk/src/examples/org/apache/mina/examples/sumup/message/AddMessage.java (original)
+++ directory/network/trunk/src/examples/org/apache/mina/examples/sumup/message/AddMessage.java Wed Nov 30 21:17:41 2005
@@ -1,52 +1,52 @@
-/*
- * @(#) $Id$
- *
- * Copyright 2004 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.mina.examples.sumup.message;
-
-/**
- * <code>ADD</code> message in SumUp protocol.
- *
- * @author The Apache Directory Project
- * @version $Rev$, $Date$
- */
-public class AddMessage extends AbstractMessage
-{
- private static final long serialVersionUID = -940833727168119141L;
-
- private int value;
-
- public AddMessage()
- {
- }
-
- public int getValue()
- {
- return value;
- }
-
- public void setValue( int value )
- {
- this.value = value;
- }
-
- public String toString()
- {
- // it is a good practice to create toString() method on message classes.
- return getSequence() + ":ADD(" + value + ')';
- }
-}
+/*
+ * @(#) $Id$
+ *
+ * Copyright 2004 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.mina.examples.sumup.message;
+
+/**
+ * <code>ADD</code> message in SumUp protocol.
+ *
+ * @author The Apache Directory Project
+ * @version $Rev$, $Date$
+ */
+public class AddMessage extends AbstractMessage
+{
+ private static final long serialVersionUID = -940833727168119141L;
+
+ private int value;
+
+ public AddMessage()
+ {
+ }
+
+ public int getValue()
+ {
+ return value;
+ }
+
+ public void setValue( int value )
+ {
+ this.value = value;
+ }
+
+ public String toString()
+ {
+ // it is a good practice to create toString() method on message classes.
+ return getSequence() + ":ADD(" + value + ')';
+ }
+}
Modified: directory/network/trunk/src/examples/org/apache/mina/examples/sumup/message/ResultMessage.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/examples/org/apache/mina/examples/sumup/message/ResultMessage.java?rev=350169&r1=350168&r2=350169&view=diff
==============================================================================
--- directory/network/trunk/src/examples/org/apache/mina/examples/sumup/message/ResultMessage.java (original)
+++ directory/network/trunk/src/examples/org/apache/mina/examples/sumup/message/ResultMessage.java Wed Nov 30 21:17:41 2005
@@ -1,69 +1,69 @@
-/*
- * @(#) $Id$
- *
- * Copyright 2004 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.mina.examples.sumup.message;
-
-/**
- * <code>RESULT</code> message in SumUp protocol.
- *
- * @author The Apache Directory Project
- * @version $Rev$, $Date$
- */
-public class ResultMessage extends AbstractMessage
-{
- private static final long serialVersionUID = 7371210248110219946L;
-
- private boolean ok;
- private int value;
-
- public ResultMessage()
- {
- }
-
- public boolean isOk()
- {
- return ok;
- }
-
- public void setOk( boolean ok )
- {
- this.ok = ok;
- }
-
- public int getValue()
- {
- return value;
- }
-
- public void setValue( int value )
- {
- this.value = value;
- }
-
- public String toString()
- {
- if( ok )
- {
- return getSequence() + ":RESULT(" + value + ')';
- }
- else
- {
- return getSequence() + ":RESULT(ERROR)";
- }
- }
+/*
+ * @(#) $Id$
+ *
+ * Copyright 2004 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.mina.examples.sumup.message;
+
+/**
+ * <code>RESULT</code> message in SumUp protocol.
+ *
+ * @author The Apache Directory Project
+ * @version $Rev$, $Date$
+ */
+public class ResultMessage extends AbstractMessage
+{
+ private static final long serialVersionUID = 7371210248110219946L;
+
+ private boolean ok;
+ private int value;
+
+ public ResultMessage()
+ {
+ }
+
+ public boolean isOk()
+ {
+ return ok;
+ }
+
+ public void setOk( boolean ok )
+ {
+ this.ok = ok;
+ }
+
+ public int getValue()
+ {
+ return value;
+ }
+
+ public void setValue( int value )
+ {
+ this.value = value;
+ }
+
+ public String toString()
+ {
+ if( ok )
+ {
+ return getSequence() + ":RESULT(" + value + ')';
+ }
+ else
+ {
+ return getSequence() + ":RESULT(ERROR)";
+ }
+ }
}
Modified: directory/network/trunk/src/examples/org/apache/mina/examples/tennis/Main.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/examples/org/apache/mina/examples/tennis/Main.java?rev=350169&r1=350168&r2=350169&view=diff
==============================================================================
--- directory/network/trunk/src/examples/org/apache/mina/examples/tennis/Main.java (original)
+++ directory/network/trunk/src/examples/org/apache/mina/examples/tennis/Main.java Wed Nov 30 21:17:41 2005
@@ -1,57 +1,57 @@
-/*
- * @(#) $Id$
- */
-package org.apache.mina.examples.tennis;
-
-import org.apache.mina.common.ConnectFuture;
-import org.apache.mina.common.IoSession;
-import org.apache.mina.common.TransportType;
-import org.apache.mina.registry.Service;
-import org.apache.mina.registry.ServiceRegistry;
-import org.apache.mina.registry.SimpleServiceRegistry;
-import org.apache.mina.transport.vmpipe.VmPipeAddress;
-import org.apache.mina.transport.vmpipe.VmPipeConnector;
-
-/**
- * (<b>Entry point</b>) An 'in-VM pipe' example which simulates a tennis game
- * between client and server.
- * <ol>
- * <li>Client connects to server</li>
- * <li>At first, client sends {@link TennisBall} with TTL value '10'.</li>
- * <li>Received side (either server or client) decreases the TTL value of the
- * received ball, and returns it to remote peer.</li>
- * <li>Who gets the ball with 0 TTL loses.</li>
- * </ol>
- *
- * @author The Apache Directory Project (dev@directory.apache.org)
- * @version $Rev$, $Date$
- */
-public class Main
-{
-
- public static void main( String[] args ) throws Exception
- {
- ServiceRegistry registry = new SimpleServiceRegistry();
-
- VmPipeAddress address = new VmPipeAddress( 8080 );
-
- // Set up server
- Service service = new Service( "tennis", TransportType.VM_PIPE, address );
- registry.bind( service, new TennisPlayer() );
-
- // Connect to the server.
- VmPipeConnector connector = new VmPipeConnector();
- ConnectFuture future = connector.connect( address,
- new TennisPlayer() );
- future.join();
- IoSession session = future.getSession();
-
- // Send the first ping message
- session.write( new TennisBall( 10 ) );
-
- // Wait until the match ends.
- session.getCloseFuture().join();
-
- registry.unbind( service );
- }
-}
+/*
+ * @(#) $Id$
+ */
+package org.apache.mina.examples.tennis;
+
+import org.apache.mina.common.ConnectFuture;
+import org.apache.mina.common.IoSession;
+import org.apache.mina.common.TransportType;
+import org.apache.mina.registry.Service;
+import org.apache.mina.registry.ServiceRegistry;
+import org.apache.mina.registry.SimpleServiceRegistry;
+import org.apache.mina.transport.vmpipe.VmPipeAddress;
+import org.apache.mina.transport.vmpipe.VmPipeConnector;
+
+/**
+ * (<b>Entry point</b>) An 'in-VM pipe' example which simulates a tennis game
+ * between client and server.
+ * <ol>
+ * <li>Client connects to server</li>
+ * <li>At first, client sends {@link TennisBall} with TTL value '10'.</li>
+ * <li>Received side (either server or client) decreases the TTL value of the
+ * received ball, and returns it to remote peer.</li>
+ * <li>Who gets the ball with 0 TTL loses.</li>
+ * </ol>
+ *
+ * @author The Apache Directory Project (dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class Main
+{
+
+ public static void main( String[] args ) throws Exception
+ {
+ ServiceRegistry registry = new SimpleServiceRegistry();
+
+ VmPipeAddress address = new VmPipeAddress( 8080 );
+
+ // Set up server
+ Service service = new Service( "tennis", TransportType.VM_PIPE, address );
+ registry.bind( service, new TennisPlayer() );
+
+ // Connect to the server.
+ VmPipeConnector connector = new VmPipeConnector();
+ ConnectFuture future = connector.connect( address,
+ new TennisPlayer() );
+ future.join();
+ IoSession session = future.getSession();
+
+ // Send the first ping message
+ session.write( new TennisBall( 10 ) );
+
+ // Wait until the match ends.
+ session.getCloseFuture().join();
+
+ registry.unbind( service );
+ }
+}
Modified: directory/network/trunk/src/examples/org/apache/mina/examples/tennis/TennisBall.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/examples/org/apache/mina/examples/tennis/TennisBall.java?rev=350169&r1=350168&r2=350169&view=diff
==============================================================================
--- directory/network/trunk/src/examples/org/apache/mina/examples/tennis/TennisBall.java (original)
+++ directory/network/trunk/src/examples/org/apache/mina/examples/tennis/TennisBall.java Wed Nov 30 21:17:41 2005
@@ -1,68 +1,68 @@
-/*
- * @(#) $Id$
- */
-package org.apache.mina.examples.tennis;
-
-/**
- * A tennis ball which has TTL value and state whose value is one of 'PING' and
- * 'PONG'.
- *
- * @author The Apache Directory Project (dev@directory.apache.org)
- * @version $Rev$, $Date$
- */
-public class TennisBall
-{
- private final boolean ping;
-
- private final int ttl;
-
- /**
- * Creates a new ball with the specified TTL (Time To Live) value.
- */
- public TennisBall( int ttl )
- {
- this( ttl, true );
- }
-
- /**
- * Creates a new ball with the specified TTL value and PING/PONG state.
- */
- private TennisBall( int ttl, boolean ping )
- {
- this.ttl = ttl;
- this.ping = ping;
- }
-
- /**
- * Returns the TTL value of this ball.
- */
- public int getTTL()
- {
- return ttl;
- }
-
- /**
- * Returns the ball after {@link TennisPlayer}'s stroke.
- * The returned ball has decreased TTL value and switched PING/PONG state.
- */
- public TennisBall stroke()
- {
- return new TennisBall( ttl - 1, !ping );
- }
-
- /**
- * Returns string representation of this message (<code>[PING|PONG]
- * (TTL)</code>).
- */
- public String toString()
- {
- if( ping )
- {
- return "PING (" + ttl + ")";
- }
- else
- {
- return "PONG (" + ttl + ")";
- }
- }
+/*
+ * @(#) $Id$
+ */
+package org.apache.mina.examples.tennis;
+
+/**
+ * A tennis ball which has TTL value and state whose value is one of 'PING' and
+ * 'PONG'.
+ *
+ * @author The Apache Directory Project (dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class TennisBall
+{
+ private final boolean ping;
+
+ private final int ttl;
+
+ /**
+ * Creates a new ball with the specified TTL (Time To Live) value.
+ */
+ public TennisBall( int ttl )
+ {
+ this( ttl, true );
+ }
+
+ /**
+ * Creates a new ball with the specified TTL value and PING/PONG state.
+ */
+ private TennisBall( int ttl, boolean ping )
+ {
+ this.ttl = ttl;
+ this.ping = ping;
+ }
+
+ /**
+ * Returns the TTL value of this ball.
+ */
+ public int getTTL()
+ {
+ return ttl;
+ }
+
+ /**
+ * Returns the ball after {@link TennisPlayer}'s stroke.
+ * The returned ball has decreased TTL value and switched PING/PONG state.
+ */
+ public TennisBall stroke()
+ {
+ return new TennisBall( ttl - 1, !ping );
+ }
+
+ /**
+ * Returns string representation of this message (<code>[PING|PONG]
+ * (TTL)</code>).
+ */
+ public String toString()
+ {
+ if( ping )
+ {
+ return "PING (" + ttl + ")";
+ }
+ else
+ {
+ return "PONG (" + ttl + ")";
+ }
+ }
}
Modified: directory/network/trunk/src/examples/org/apache/mina/examples/tennis/TennisPlayer.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/examples/org/apache/mina/examples/tennis/TennisPlayer.java?rev=350169&r1=350168&r2=350169&view=diff
==============================================================================
--- directory/network/trunk/src/examples/org/apache/mina/examples/tennis/TennisPlayer.java (original)
+++ directory/network/trunk/src/examples/org/apache/mina/examples/tennis/TennisPlayer.java Wed Nov 30 21:17:41 2005
@@ -1,65 +1,65 @@
-/*
- * @(#) $Id$
- */
-package org.apache.mina.examples.tennis;
-
-import org.apache.mina.common.IoHandler;
-import org.apache.mina.common.IoHandlerAdapter;
-import org.apache.mina.common.IoSession;
-
-/**
- * A {@link IoHandler} implementation which plays a tennis game.
- *
- * @author The Apache Directory Project (dev@directory.apache.org)
- * @version $Rev$, $Date$
- */
-public class TennisPlayer extends IoHandlerAdapter
-{
- private static int nextId = 0;
-
- /** Player ID **/
- private final int id = nextId++;
-
- public void sessionOpened( IoSession session )
- {
- System.out.println( "Player-" + id + ": READY" );
- }
-
- public void sessionClosed( IoSession session )
- {
- System.out.println( "Player-" + id + ": QUIT" );
- }
-
- public void messageReceived( IoSession session, Object message )
- {
- System.out.println( "Player-" + id + ": RCVD " + message );
-
- TennisBall ball = ( TennisBall ) message;
-
- // Stroke: TTL decreases and PING/PONG state changes.
- ball = ball.stroke();
-
- if( ball.getTTL() > 0 )
- {
- // If the ball is still alive, pass it back to peer.
- session.write( ball );
- }
- else
- {
- // If the ball is dead, this player loses.
- System.out.println( "Player-" + id + ": LOSE" );
- session.close();
- }
- }
-
- public void messageSent( IoSession session, Object message )
- {
- System.out.println( "Player-" + id + ": SENT " + message );
- }
-
- public void exceptionCaught( IoSession session, Throwable cause )
- {
- cause.printStackTrace();
- session.close();
- }
+/*
+ * @(#) $Id$
+ */
+package org.apache.mina.examples.tennis;
+
+import org.apache.mina.common.IoHandler;
+import org.apache.mina.common.IoHandlerAdapter;
+import org.apache.mina.common.IoSession;
+
+/**
+ * A {@link IoHandler} implementation which plays a tennis game.
+ *
+ * @author The Apache Directory Project (dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class TennisPlayer extends IoHandlerAdapter
+{
+ private static int nextId = 0;
+
+ /** Player ID **/
+ private final int id = nextId++;
+
+ public void sessionOpened( IoSession session )
+ {
+ System.out.println( "Player-" + id + ": READY" );
+ }
+
+ public void sessionClosed( IoSession session )
+ {
+ System.out.println( "Player-" + id + ": QUIT" );
+ }
+
+ public void messageReceived( IoSession session, Object message )
+ {
+ System.out.println( "Player-" + id + ": RCVD " + message );
+
+ TennisBall ball = ( TennisBall ) message;
+
+ // Stroke: TTL decreases and PING/PONG state changes.
+ ball = ball.stroke();
+
+ if( ball.getTTL() > 0 )
+ {
+ // If the ball is still alive, pass it back to peer.
+ session.write( ball );
+ }
+ else
+ {
+ // If the ball is dead, this player loses.
+ System.out.println( "Player-" + id + ": LOSE" );
+ session.close();
+ }
+ }
+
+ public void messageSent( IoSession session, Object message )
+ {
+ System.out.println( "Player-" + id + ": SENT " + message );
+ }
+
+ public void exceptionCaught( IoSession session, Throwable cause )
+ {
+ cause.printStackTrace();
+ session.close();
+ }
}