blob: 3481e95befbfd8ff6bde424b392e2ba4498f21e2 (
plain) (
blame)
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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus.jdisc.test;
import com.yahoo.cloud.config.SlobroksConfig;
import com.yahoo.jrt.ListenFailedException;
import com.yahoo.jrt.slobrok.server.Slobrok;
import com.yahoo.messagebus.DestinationSession;
import com.yahoo.messagebus.DestinationSessionParams;
import com.yahoo.messagebus.Message;
import com.yahoo.messagebus.MessageBus;
import com.yahoo.messagebus.MessageBusParams;
import com.yahoo.messagebus.Protocol;
import com.yahoo.messagebus.Reply;
import com.yahoo.messagebus.network.Identity;
import com.yahoo.messagebus.network.rpc.RPCNetwork;
import com.yahoo.messagebus.network.rpc.RPCNetworkParams;
import com.yahoo.messagebus.test.SimpleProtocol;
import java.util.concurrent.TimeUnit;
/**
* @author Simon Thoresen Hult
*/
public class RemoteServer {
private final Slobrok slobrok;
private final MessageBus mbus;
private final MessageQueue queue = new MessageQueue();
private final DestinationSession session;
private RemoteServer(Protocol protocol, String identity) {
this.slobrok = newSlobrok();
mbus = new MessageBus(new RPCNetwork(new RPCNetworkParams()
.setSlobroksConfig(slobroksConfig())
.setIdentity(new Identity(identity))),
new MessageBusParams().addProtocol(protocol));
session = mbus.createDestinationSession(new DestinationSessionParams().setMessageHandler(queue));
}
public String connectionSpec() {
return session.getConnectionSpec();
}
public Message awaitMessage(int timeout, TimeUnit unit) throws InterruptedException {
return queue.awaitMessage(timeout, unit);
}
public void ackMessage(Message msg) {
session.acknowledge(msg);
}
public void sendReply(Reply reply) {
session.reply(reply);
}
public SlobroksConfig slobroksConfig() {
return TestUtils.configFor(slobrok);
}
public void close() {
session.destroy();
mbus.destroy();
slobrok.stop();
}
public static RemoteServer newInstanceWithInternSlobrok() {
return new RemoteServer(new SimpleProtocol(), "remote");
}
public static RemoteServer newInstance(String identity, Protocol protocol) {
return new RemoteServer(protocol, identity);
}
private static Slobrok newSlobrok() {
try {
return new Slobrok();
} catch (ListenFailedException e) {
throw new IllegalStateException(e);
}
}
}
|