qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r..@apache.org
Subject [03/38] qpid-proton git commit: PROTON-881: Write an Echo example and get it working
Date Sun, 05 Jul 2015 23:45:04 GMT
PROTON-881: Write an Echo example and get it working


Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/739005e7
Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/739005e7
Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/739005e7

Branch: refs/heads/master
Commit: 739005e7ca49b0e85873406ea4a6b9392252a1fe
Parents: e018701
Author: Adrian Preston <prestona@uk.ibm.com>
Authored: Fri Apr 17 15:34:53 2015 +0100
Committer: Adrian Preston <prestona@uk.ibm.com>
Committed: Wed May 6 23:23:24 2015 +0100

----------------------------------------------------------------------
 .../qpid/proton/example/reactor/Echo.java       | 73 ++++++++++++++++++++
 .../proton/reactor/impl/SelectableImpl.java     |  5 +-
 2 files changed, 77 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/739005e7/examples/java/reactor/src/main/java/org/apache/qpid/proton/example/reactor/Echo.java
----------------------------------------------------------------------
diff --git a/examples/java/reactor/src/main/java/org/apache/qpid/proton/example/reactor/Echo.java
b/examples/java/reactor/src/main/java/org/apache/qpid/proton/example/reactor/Echo.java
new file mode 100644
index 0000000..3913e17
--- /dev/null
+++ b/examples/java/reactor/src/main/java/org/apache/qpid/proton/example/reactor/Echo.java
@@ -0,0 +1,73 @@
+/*
+ *
+ * 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.qpid.proton.example.reactor;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.Pipe.SourceChannel;
+
+import org.apache.qpid.proton.Proton;
+import org.apache.qpid.proton.engine.BaseHandler;
+import org.apache.qpid.proton.engine.Event;
+import org.apache.qpid.proton.reactor.Reactor;
+import org.apache.qpid.proton.reactor.Selectable;
+
+public class Echo extends BaseHandler {
+
+    @Override
+    public void onSelectableInit(Event event) {
+        Selectable selectable = event.getSelectable();
+        Reactor reactor = event.getReactor();
+        selectable.setReading(true);
+        reactor.update(selectable);
+    }
+
+    @Override
+    public void onSelectableReadable(Event event) {
+        Selectable selectable = event.getSelectable();
+        SourceChannel channel = (SourceChannel)selectable.getChannel();
+        ByteBuffer buffer = ByteBuffer.allocate(1024);
+        try {
+            while(true) {
+                int amount = channel.read(buffer);
+                if (amount < 0) selectable.release();
+                if (amount <= 0) break;
+                System.out.write(buffer.array(), 0, buffer.position());
+                buffer.clear();
+            }
+        } catch(IOException ioException) {
+            ioException.printStackTrace();
+            selectable.release();
+        }
+    }
+
+    public static void main(String[] args) throws IOException {
+
+        Reactor reactor = Proton.reactor(new Echo());
+
+        SourceChannel inChannel = EchoInputStreamWrapper.wrap(System.in);
+        Selectable selectable = reactor.selectable();
+        selectable.setChannel(inChannel);
+
+        reactor.run();
+    }
+}

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/739005e7/proton-j/src/main/java/org/apache/qpid/proton/reactor/impl/SelectableImpl.java
----------------------------------------------------------------------
diff --git a/proton-j/src/main/java/org/apache/qpid/proton/reactor/impl/SelectableImpl.java
b/proton-j/src/main/java/org/apache/qpid/proton/reactor/impl/SelectableImpl.java
index cf3839d..7d911a5 100644
--- a/proton-j/src/main/java/org/apache/qpid/proton/reactor/impl/SelectableImpl.java
+++ b/proton-j/src/main/java/org/apache/qpid/proton/reactor/impl/SelectableImpl.java
@@ -259,9 +259,12 @@ public class SelectableImpl implements Selectable {
     }
 
     // TODO: all this gets stuffed into records in the C code...
-    private BaseHandler _handler = new BaseHandler();
+    private BaseHandler _handler;
     @Override
     public void add(Handler handler) {
+        if (_handler == null) {
+            _handler = new BaseHandler();
+        }
         _handler.add(handler);
     }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org


Mime
View raw message