summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2008-04-12 03:45:27 (EDT)
committerEike Stepper2008-04-12 03:45:27 (EDT)
commit9676c7f945f2fdba3d809eeacb6147bbe69e627a (patch)
treeebb73255d243bda2da985eb2b0d5eef64ced91d1
parentf8db92014f016c5901a406fc9d93384cd5b5c368 (diff)
downloadcdo-9676c7f945f2fdba3d809eeacb6147bbe69e627a.zip
cdo-9676c7f945f2fdba3d809eeacb6147bbe69e627a.tar.gz
cdo-9676c7f945f2fdba3d809eeacb6147bbe69e627a.tar.bz2
[226793] Provide an ECHOProtocol example
https://bugs.eclipse.org/bugs/show_bug.cgi?id=226793
-rw-r--r--plugins/org.eclipse.net4j.examples/src/org/eclipse/net4j/examples/echo/client/EchoClient.java2
-rw-r--r--plugins/org.eclipse.net4j.examples/src/org/eclipse/net4j/examples/echo/client/EchoClientProtocol.java56
-rw-r--r--plugins/org.eclipse.net4j.examples/src/org/eclipse/net4j/examples/echo/client/EchoRequest.java51
-rw-r--r--plugins/org.eclipse.net4j.examples/src/org/eclipse/net4j/examples/echo/server/EchoIndication.java48
-rw-r--r--plugins/org.eclipse.net4j.examples/src/org/eclipse/net4j/examples/echo/server/EchoServer.java61
-rw-r--r--plugins/org.eclipse.net4j.examples/src/org/eclipse/net4j/examples/echo/server/EchoServerProtocol.java62
-rw-r--r--plugins/org.eclipse.net4j/src/org/eclipse/net4j/signal/SignalProtocol.java5
7 files changed, 279 insertions, 6 deletions
diff --git a/plugins/org.eclipse.net4j.examples/src/org/eclipse/net4j/examples/echo/client/EchoClient.java b/plugins/org.eclipse.net4j.examples/src/org/eclipse/net4j/examples/echo/client/EchoClient.java
index ee907dc..b125473 100644
--- a/plugins/org.eclipse.net4j.examples/src/org/eclipse/net4j/examples/echo/client/EchoClient.java
+++ b/plugins/org.eclipse.net4j.examples/src/org/eclipse/net4j/examples/echo/client/EchoClient.java
@@ -44,7 +44,7 @@ public class EchoClient
container.registerFactory(new EchoClientProtocol.Factory());
LifecycleUtil.activate(container);
- // Start a connector
+ // Start a connector that represents the client side of a physical connection
IConnector connector = (IConnector)container.getElement("org.eclipse.net4j.connectors", "tcp", "localhost:2036");
// Open a virtual channel with the ECHO protocol, send an ECHO request and close the channel
diff --git a/plugins/org.eclipse.net4j.examples/src/org/eclipse/net4j/examples/echo/client/EchoClientProtocol.java b/plugins/org.eclipse.net4j.examples/src/org/eclipse/net4j/examples/echo/client/EchoClientProtocol.java
new file mode 100644
index 0000000..9117dc0
--- /dev/null
+++ b/plugins/org.eclipse.net4j.examples/src/org/eclipse/net4j/examples/echo/client/EchoClientProtocol.java
@@ -0,0 +1,56 @@
+/***************************************************************************
+ * Copyright (c) 2004 - 2008 Eike Stepper, Germany.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ **************************************************************************/
+package org.eclipse.net4j.examples.echo.client;
+
+import org.eclipse.net4j.examples.echo.EchoProtocol;
+import org.eclipse.net4j.signal.SignalProtocol;
+import org.eclipse.net4j.signal.SignalReactor;
+import org.eclipse.net4j.util.factory.ProductCreationException;
+
+import org.eclipse.internal.net4j.protocol.ClientProtocolFactory;
+
+/**
+ * @author Eike Stepper
+ */
+public class EchoClientProtocol extends SignalProtocol implements EchoProtocol
+{
+ public EchoClientProtocol()
+ {
+ }
+
+ public String getType()
+ {
+ return PROTOCOL_NAME;
+ }
+
+ @Override
+ protected SignalReactor doCreateSignalReactor(short signalID)
+ {
+ // On client side only needed for server initiated requests
+ return null;
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ public static final class Factory extends ClientProtocolFactory
+ {
+ public Factory()
+ {
+ super(PROTOCOL_NAME);
+ }
+
+ public Object create(String description) throws ProductCreationException
+ {
+ return new EchoClientProtocol();
+ }
+ }
+}
diff --git a/plugins/org.eclipse.net4j.examples/src/org/eclipse/net4j/examples/echo/client/EchoRequest.java b/plugins/org.eclipse.net4j.examples/src/org/eclipse/net4j/examples/echo/client/EchoRequest.java
new file mode 100644
index 0000000..f31af6a
--- /dev/null
+++ b/plugins/org.eclipse.net4j.examples/src/org/eclipse/net4j/examples/echo/client/EchoRequest.java
@@ -0,0 +1,51 @@
+/***************************************************************************
+ * Copyright (c) 2004 - 2008 Eike Stepper, Germany.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ **************************************************************************/
+package org.eclipse.net4j.examples.echo.client;
+
+import org.eclipse.net4j.channel.IChannel;
+import org.eclipse.net4j.examples.echo.EchoProtocol;
+import org.eclipse.net4j.signal.RequestWithConfirmation;
+import org.eclipse.net4j.util.io.ExtendedDataInputStream;
+import org.eclipse.net4j.util.io.ExtendedDataOutputStream;
+
+import java.io.IOException;
+
+/**
+ * @author Eike Stepper
+ */
+public class EchoRequest extends RequestWithConfirmation<String> implements EchoProtocol
+{
+ private String message;
+
+ public EchoRequest(IChannel channel, String message)
+ {
+ super(channel);
+ this.message = message;
+ }
+
+ @Override
+ protected short getSignalID()
+ {
+ return ECHO_SIGNAL;
+ }
+
+ @Override
+ protected void requesting(ExtendedDataOutputStream out) throws IOException
+ {
+ out.writeString(message);
+ }
+
+ @Override
+ protected String confirming(ExtendedDataInputStream in) throws IOException
+ {
+ return in.readString();
+ }
+}
diff --git a/plugins/org.eclipse.net4j.examples/src/org/eclipse/net4j/examples/echo/server/EchoIndication.java b/plugins/org.eclipse.net4j.examples/src/org/eclipse/net4j/examples/echo/server/EchoIndication.java
new file mode 100644
index 0000000..f9748bc
--- /dev/null
+++ b/plugins/org.eclipse.net4j.examples/src/org/eclipse/net4j/examples/echo/server/EchoIndication.java
@@ -0,0 +1,48 @@
+/***************************************************************************
+ * Copyright (c) 2004 - 2008 Eike Stepper, Germany.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ **************************************************************************/
+package org.eclipse.net4j.examples.echo.server;
+
+import org.eclipse.net4j.examples.echo.EchoProtocol;
+import org.eclipse.net4j.signal.IndicationWithResponse;
+import org.eclipse.net4j.util.io.ExtendedDataInputStream;
+import org.eclipse.net4j.util.io.ExtendedDataOutputStream;
+
+import java.io.IOException;
+
+/**
+ * @author Eike Stepper
+ */
+public class EchoIndication extends IndicationWithResponse implements EchoProtocol
+{
+ private String message;
+
+ public EchoIndication()
+ {
+ }
+
+ @Override
+ protected short getSignalID()
+ {
+ return ECHO_SIGNAL;
+ }
+
+ @Override
+ protected void indicating(ExtendedDataInputStream in) throws IOException
+ {
+ message = in.readString();
+ }
+
+ @Override
+ protected void responding(ExtendedDataOutputStream out) throws IOException
+ {
+ out.writeString(message);
+ }
+}
diff --git a/plugins/org.eclipse.net4j.examples/src/org/eclipse/net4j/examples/echo/server/EchoServer.java b/plugins/org.eclipse.net4j.examples/src/org/eclipse/net4j/examples/echo/server/EchoServer.java
new file mode 100644
index 0000000..608558d
--- /dev/null
+++ b/plugins/org.eclipse.net4j.examples/src/org/eclipse/net4j/examples/echo/server/EchoServer.java
@@ -0,0 +1,61 @@
+/***************************************************************************
+ * Copyright (c) 2004 - 2008 Eike Stepper, Germany.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ **************************************************************************/
+package org.eclipse.net4j.examples.echo.server;
+
+import org.eclipse.net4j.Net4jUtil;
+import org.eclipse.net4j.acceptor.IAcceptor;
+import org.eclipse.net4j.internal.examples.bundle.OM;
+import org.eclipse.net4j.internal.util.om.log.PrintLogHandler;
+import org.eclipse.net4j.internal.util.om.trace.PrintTraceHandler;
+import org.eclipse.net4j.tcp.TCPUtil;
+import org.eclipse.net4j.util.container.ContainerUtil;
+import org.eclipse.net4j.util.container.IManagedContainer;
+import org.eclipse.net4j.util.lifecycle.LifecycleUtil;
+import org.eclipse.net4j.util.om.OMPlatform;
+
+/**
+ * @author Eike Stepper
+ */
+public class EchoServer
+{
+ public static void main(String[] args) throws Exception
+ {
+ // Send all traces and logs to the console
+ OMPlatform.INSTANCE.setDebugging(true);
+ OMPlatform.INSTANCE.addTraceHandler(PrintTraceHandler.CONSOLE);
+ OMPlatform.INSTANCE.addLogHandler(PrintLogHandler.CONSOLE);
+
+ // Use this container to create and wire the components
+ IManagedContainer container = ContainerUtil.createContainer();
+
+ try
+ {
+ Net4jUtil.prepareContainer(container);
+ TCPUtil.prepareContainer(container);
+ container.registerFactory(new EchoServerProtocol.Factory());
+ LifecycleUtil.activate(container);
+
+ // Start an acceptor
+ IAcceptor acceptor = (IAcceptor)container.getElement("org.eclipse.net4j.acceptors", "tcp", "0.0.0.0:2036");
+ OM.LOG.info("Accepting connections: " + acceptor);
+
+ System.out.println("Press any key to shutdown");
+ while (System.in.read() == -1)
+ {
+ Thread.sleep(200);
+ }
+ }
+ finally
+ {
+ LifecycleUtil.deactivate(container);
+ }
+ }
+}
diff --git a/plugins/org.eclipse.net4j.examples/src/org/eclipse/net4j/examples/echo/server/EchoServerProtocol.java b/plugins/org.eclipse.net4j.examples/src/org/eclipse/net4j/examples/echo/server/EchoServerProtocol.java
new file mode 100644
index 0000000..8fbf7bc
--- /dev/null
+++ b/plugins/org.eclipse.net4j.examples/src/org/eclipse/net4j/examples/echo/server/EchoServerProtocol.java
@@ -0,0 +1,62 @@
+/***************************************************************************
+ * Copyright (c) 2004 - 2008 Eike Stepper, Germany.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ **************************************************************************/
+package org.eclipse.net4j.examples.echo.server;
+
+import org.eclipse.net4j.examples.echo.EchoProtocol;
+import org.eclipse.net4j.signal.SignalProtocol;
+import org.eclipse.net4j.signal.SignalReactor;
+import org.eclipse.net4j.util.factory.ProductCreationException;
+
+import org.eclipse.internal.net4j.protocol.ServerProtocolFactory;
+
+/**
+ * @author Eike Stepper
+ */
+public class EchoServerProtocol extends SignalProtocol implements EchoProtocol
+{
+ public EchoServerProtocol()
+ {
+ }
+
+ public String getType()
+ {
+ return PROTOCOL_NAME;
+ }
+
+ @Override
+ protected SignalReactor doCreateSignalReactor(short signalID)
+ {
+ switch (signalID)
+ {
+ case ECHO_SIGNAL:
+ return new EchoIndication();
+
+ default:
+ return null;
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ public static final class Factory extends ServerProtocolFactory
+ {
+ public Factory()
+ {
+ super(PROTOCOL_NAME);
+ }
+
+ public Object create(String description) throws ProductCreationException
+ {
+ return new EchoServerProtocol();
+ }
+ }
+}
diff --git a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/signal/SignalProtocol.java b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/signal/SignalProtocol.java
index 3124eef..405e84d 100644
--- a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/signal/SignalProtocol.java
+++ b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/signal/SignalProtocol.java
@@ -199,11 +199,6 @@ public abstract class SignalProtocol extends Protocol
return MessageFormat.format("SignalProtocol[{0}]", getType()); //$NON-NLS-1$
}
- public String getType()
- {
- return null;
- }
-
protected final SignalReactor createSignalReactor(short signalID)
{
checkActive();