org.jgroups.demos.RelayDemoRpc Maven / Gradle / Ivy
package org.jgroups.demos;
import org.jgroups.Address;
import org.jgroups.JChannel;
import org.jgroups.ReceiverAdapter;
import org.jgroups.View;
import org.jgroups.blocks.MethodCall;
import org.jgroups.blocks.RequestOptions;
import org.jgroups.blocks.ResponseMode;
import org.jgroups.blocks.RpcDispatcher;
import org.jgroups.protocols.relay.SiteMaster;
import org.jgroups.util.Rsp;
import org.jgroups.util.RspList;
import org.jgroups.util.Util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
/** Demos RELAY. Create 2 *separate* clusters with RELAY as top protocol. Each RELAY has bridge_props="tcp.xml" (tcp.xml
* needs to be present). Then start 2 instances in the first cluster and 2 instances in the second cluster. They should
* find each other, and typing in a window should send the text to everyone, plus we should get 4 responses.
* @author Bela Ban
*/
public class RelayDemoRpc extends ReceiverAdapter {
protected JChannel ch;
protected RpcDispatcher disp;
protected String local_addr;
protected View view;
protected static final long RPC_TIMEOUT=10000;
public static void main(String[] args) throws Exception {
String props="udp.xml";
String name=null;
for(int i=0; i < args.length; i++) {
if(args[i].equals("-props")) {
props=args[++i];
continue;
}
if(args[i].equals("-name")) {
name=args[++i];
continue;
}
System.out.println("RelayDemo [-props props] [-name name]");
return;
}
RelayDemoRpc demo=new RelayDemoRpc();
demo.start(props, name);
}
public void start(String props, String name) throws Exception {
ch=new JChannel(props);
if(name != null)
ch.setName(name);
disp=new RpcDispatcher(ch, null, this, this);
ch.connect("RelayDemo");
local_addr=ch.getAddress().toString();
MethodCall call=new MethodCall(getClass().getMethod("handleMessage", String.class, String.class));
for(;;) {
String line=Util.readStringFromStdin(": ");
if(line.startsWith("help")) {
System.out.println("unicast // unicasts to all members of local view\n" +
"site + // unicasts to all listed site masters, e.g. \"site sfo lon\"\n" +
"mcast + // anycasts to all local members, plus listed site masters \n" +
" // multicast, RELAY2 will relay to all members of sites");
continue;
}
call.setArgs(line, local_addr);
// unicast to every member of the local cluster
if(line.equalsIgnoreCase("unicast")) {
for(Address dest: view.getMembers()) {
System.out.println("invoking method in " + dest + ": ");
try {
Object rsp=disp.callRemoteMethod(dest, call, new RequestOptions(ResponseMode.GET_ALL, RPC_TIMEOUT));
System.out.println("rsp from " + dest + ": " + rsp);
}
catch(Throwable throwable) {
throwable.printStackTrace();
}
}
}
// unicast to 1 SiteMaster
else if(line.startsWith("site")) {
Collection site_masters=parseSiteMasters(line.substring("site".length()));
for(String site_master: site_masters) {
try {
SiteMaster dest=new SiteMaster(site_master);
System.out.println("invoking method in " + dest + ": ");
Object rsp=disp.callRemoteMethod(dest, call, new RequestOptions(ResponseMode.GET_ALL, RPC_TIMEOUT));
System.out.println("rsp from " + dest + ": " + rsp);
}
catch(Throwable t) {
t.printStackTrace();
}
}
}
// mcast to all local members and N SiteMasters
else if(line.startsWith("mcast")) {
Collection site_masters=parseSiteMasters(line.substring("mcast".length()));
Collection dests=new ArrayList(site_masters.size());
for(String site_master: site_masters) {
try {
dests.add(new SiteMaster(site_master));
}
catch(Throwable t) {
System.err.println("failed adding SiteMaster for " + site_master + ": " + t);
}
}
dests.addAll(view.getMembers());
System.out.println("invoking method in " + dests + ": ");
RspList
© 2015 - 2025 Weber Informatics LLC | Privacy Policy