/usr/share/proton-0.14.0/examples/engine/java/src/main/java/org/apache/qpid/proton/examples/Server.java is in libqpid-proton8-dev-examples 0.14.0-5.1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | /*
*
* 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.examples;
import org.apache.qpid.proton.amqp.messaging.Accepted;
import org.apache.qpid.proton.engine.BaseHandler;
import org.apache.qpid.proton.engine.Collector;
import org.apache.qpid.proton.engine.Delivery;
import org.apache.qpid.proton.engine.Event;
import org.apache.qpid.proton.engine.Link;
import org.apache.qpid.proton.engine.Receiver;
import org.apache.qpid.proton.engine.Sender;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Server
*
*/
public class Server extends BaseHandler
{
private class MessageStore {
Map<String,Deque<Message>> messages = new HashMap<String,Deque<Message>>();
void put(String address, Message message) {
Deque<Message> queue = messages.get(address);
if (queue == null) {
queue = new ArrayDeque<Message>();
messages.put(address, queue);
}
queue.add(message);
}
Message get(String address) {
Deque<Message> queue = messages.get(address);
if (queue == null) { return null; }
Message msg = queue.remove();
if (queue.isEmpty()) {
messages.remove(address);
}
return msg;
}
}
final private MessageStore messages = new MessageStore();
final private Router router;
private boolean quiet;
private int tag = 0;
public Server(Router router, boolean quiet) {
this.router = router;
this.quiet = quiet;
}
private byte[] nextTag() {
return String.format("%s", tag++).getBytes();
}
private int send(String address) {
return send(address, null);
}
private int send(String address, Sender snd) {
if (snd == null) {
Router.Routes<Sender> routes = router.getOutgoing(address);
snd = routes.choose();
if (snd == null) {
return 0;
}
}
int count = 0;
while (snd.getCredit() > 0 && snd.getQueued() < 1024) {
Message msg = messages.get(address);
if (msg == null) {
snd.drained();
return count;
}
Delivery dlv = snd.delivery(nextTag());
byte[] bytes = msg.getBytes();
snd.send(bytes, 0, bytes.length);
dlv.settle();
count++;
if (!quiet) {
System.out.println(String.format("Sent message(%s): %s", address, msg));
}
}
return count;
}
@Override
public void onLinkFlow(Event evt) {
Link link = evt.getLink();
if (link instanceof Sender) {
Sender snd = (Sender) link;
send(router.getAddress(snd), snd);
}
}
@Override
public void onDelivery(Event evt) {
Delivery dlv = evt.getDelivery();
Link link = dlv.getLink();
if (link instanceof Sender) {
dlv.settle();
} else {
Receiver rcv = (Receiver) link;
if (!dlv.isPartial()) {
byte[] bytes = new byte[dlv.pending()];
rcv.recv(bytes, 0, bytes.length);
String address = router.getAddress(rcv);
Message message = new Message(bytes);
messages.put(address, message);
dlv.disposition(Accepted.getInstance());
dlv.settle();
if (!quiet) {
System.out.println(String.format("Got message(%s): %s", address, message));
}
send(address);
}
}
}
public static final void main(String[] argv) throws IOException {
List<String> switches = new ArrayList<String>();
List<String> args = new ArrayList<String>();
for (String s : argv) {
if (s.startsWith("-")) {
switches.add(s);
} else {
args.add(s);
}
}
boolean quiet = switches.contains("-q");
String host = !args.isEmpty() && !Character.isDigit(args.get(0).charAt(0)) ?
args.remove(0) : "localhost";
int port = !args.isEmpty() ? Integer.parseInt(args.remove(0)) : 5672;
Collector collector = Collector.Factory.create();
Router router = new Router();
Driver driver = new Driver(collector, new Handshaker(),
new FlowController(1024), router,
new Server(router, quiet));
driver.listen(host, port);
driver.run();
}
}
|