Search in sources :

Example 11 with MethodCall

use of org.jgroups.blocks.MethodCall in project JGroups by belaban.

the class JChannelProbeHandler method handleOperation.

/**
 * Invokes an operation and puts the return value into map
 * @param map
 * @param operation Protocol.OperationName[args], e.g. STABLE.foo[arg1 arg2 arg3]
 */
protected void handleOperation(Map<String, String> map, String operation) throws Exception {
    int index = operation.indexOf('.');
    if (index == -1)
        throw new IllegalArgumentException("operation " + operation + " is missing the protocol name");
    String prot_name = operation.substring(0, index);
    Protocol prot = ch.getProtocolStack().findProtocol(prot_name);
    if (prot == null)
        // less drastic than throwing an exception...
        return;
    int args_index = operation.indexOf('[');
    String method_name;
    if (args_index != -1)
        method_name = operation.substring(index + 1, args_index).trim();
    else
        method_name = operation.substring(index + 1).trim();
    String[] args = null;
    if (args_index != -1) {
        int end_index = operation.indexOf(']');
        if (end_index == -1)
            throw new IllegalArgumentException("] not found");
        List<String> str_args = Util.parseCommaDelimitedStrings(operation.substring(args_index + 1, end_index));
        Object[] strings = str_args.toArray();
        args = new String[strings.length];
        for (int i = 0; i < strings.length; i++) args[i] = (String) strings[i];
    }
    Object target = prot;
    Method method = MethodCall.findMethod(target.getClass(), method_name, args);
    if (method == null) {
        if (prot instanceof AdditionalJmxObjects) {
            for (Object obj : ((AdditionalJmxObjects) prot).getJmxObjects()) {
                method = MethodCall.findMethod(obj.getClass(), method_name, args);
                if (method != null) {
                    target = obj;
                    break;
                }
            }
        }
        if (method == null) {
            log.warn(Util.getMessage("MethodNotFound"), ch.getAddress(), target.getClass().getSimpleName(), method_name);
            return;
        }
    }
    MethodCall call = new MethodCall(method);
    Object[] converted_args = null;
    if (args != null) {
        converted_args = new Object[args.length];
        Class<?>[] types = method.getParameterTypes();
        for (int i = 0; i < args.length; i++) converted_args[i] = Util.convert(args[i], types[i]);
    }
    Object retval = call.invoke(target, converted_args);
    if (retval != null)
        map.put(prot_name + "." + method_name, retval.toString());
}
Also used : Method(java.lang.reflect.Method) MethodCall(org.jgroups.blocks.MethodCall) AdditionalJmxObjects(org.jgroups.jmx.AdditionalJmxObjects) Protocol(org.jgroups.stack.Protocol)

Example 12 with MethodCall

use of org.jgroups.blocks.MethodCall in project JGroups by belaban.

the class RelayDemoRpc method start.

public void start(String props, String name) throws Exception {
    ch = new JChannel(props);
    if (name != null)
        ch.setName(name);
    disp = new RpcDispatcher(ch, this).setMembershipListener(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 <text>  // unicasts to all members of local view\n" + "site <site>+    // unicasts to all listed site masters, e.g. \"site sfo lon\"\n" + "mcast <site>+   // anycasts to all local members, plus listed site masters \n" + "<text>          // multicast, RELAY2 will relay to all members of sites");
            continue;
        }
        call.args(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();
                }
            }
        } else // unicast to 1 SiteMaster
        if (line.startsWith("site")) {
            Collection<String> 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();
                }
            }
        } else // mcast to all local members and N SiteMasters
        if (line.startsWith("mcast")) {
            Collection<String> site_masters = parseSiteMasters(line.substring("mcast".length()));
            Collection<Address> 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<Object> rsps = disp.callRemoteMethods(dests, call, new RequestOptions(ResponseMode.GET_ALL, RPC_TIMEOUT).anycasting(true));
            for (Map.Entry<Address, Rsp<Object>> entry : rsps.entrySet()) {
                Address sender = entry.getKey();
                Rsp<Object> rsp = entry.getValue();
                if (rsp.wasUnreachable())
                    System.out.println("<< unreachable: " + sender);
                else
                    System.out.println("<< " + rsp.getValue() + " from " + sender);
            }
        } else {
            // mcasting the call to all local cluster members
            RspList<Object> rsps = disp.callRemoteMethods(null, call, new RequestOptions(ResponseMode.GET_ALL, RPC_TIMEOUT).anycasting(false));
            rsps.entrySet().stream().forEach(entry -> {
                Rsp<Object> val = entry.getValue();
                System.out.println("<< " + val.getValue() + " from " + entry.getKey());
            });
        }
    }
}
Also used : RpcDispatcher(org.jgroups.blocks.RpcDispatcher) JChannel(org.jgroups.JChannel) Address(org.jgroups.Address) RequestOptions(org.jgroups.blocks.RequestOptions) MethodCall(org.jgroups.blocks.MethodCall) Rsp(org.jgroups.util.Rsp) SiteMaster(org.jgroups.protocols.relay.SiteMaster)

Example 13 with MethodCall

use of org.jgroups.blocks.MethodCall in project JGroups by belaban.

the class GraphPanel method mouseReleased.

public void mouseReleased(MouseEvent e) {
    Point p = e.getPoint();
    if (pick == null)
        return;
    pick.x = p.x;
    pick.y = p.y;
    pick.fixed = pickfixed;
    try {
        MethodCall call = new MethodCall("moveNode", new Object[] { pick }, new Class[] { Node.class });
        wb.disp.callRemoteMethods(null, call, new RequestOptions(ResponseMode.GET_ALL, 0));
    } catch (Exception ex) {
        log.error(ex.toString());
    }
    pick = null;
}
Also used : RequestOptions(org.jgroups.blocks.RequestOptions) MethodCall(org.jgroups.blocks.MethodCall)

Example 14 with MethodCall

use of org.jgroups.blocks.MethodCall in project JGroups by belaban.

the class RpcDispatcherSpeedTest method invokeRpcs.

protected void invokeRpcs() throws Exception {
    Average avg = new Average();
    long min = Long.MAX_VALUE, max = 0;
    RequestOptions opts = new RequestOptions(ResponseMode.GET_FIRST, 0).transientFlags(Message.TransientFlag.DONT_LOOPBACK);
    MethodCall call = new MethodCall((short) 0);
    int print = num / 10;
    if (oob)
        opts.flags(Message.Flag.OOB);
    if (dont_bundle)
        opts.flags(Message.Flag.DONT_BUNDLE);
    if (channel.getView().size() != 2) {
        System.err.printf("Cluster must have exactly 2 members: %s\n", channel.getView());
        return;
    }
    System.out.printf("\nInvoking %d blocking RPCs (oob: %b, dont_bundle: %b)\n", num, oob, dont_bundle);
    for (int i = 0; i < num; i++) {
        long start = System.nanoTime();
        RspList<Void> rsps = disp.callRemoteMethods(null, call, opts);
        long time_ns = System.nanoTime() - start;
        if (i > 0 && i % print == 0)
            System.out.print(".");
        boolean all_received = rsps.values().stream().allMatch(Rsp::wasReceived);
        if (!all_received)
            System.err.printf("didn't receive all responses: %s\n", rsps);
        avg.add(time_ns);
        min = Math.min(min, time_ns);
        max = Math.max(max, time_ns);
    }
    System.out.println("");
    System.out.printf("\nround-trip = min/avg/max: %.2f / %.2f / %.2f us\n\n", min / 1000.0, avg.getAverage() / 1000.0, max / 1000.0);
}
Also used : RequestOptions(org.jgroups.blocks.RequestOptions) Average(org.jgroups.util.Average) MethodCall(org.jgroups.blocks.MethodCall) Rsp(org.jgroups.util.Rsp)

Example 15 with MethodCall

use of org.jgroups.blocks.MethodCall in project JGroups by belaban.

the class MethodCallTest method testMarshallingMETHOD.

public static void testMarshallingMETHOD() throws Exception {
    Method m = TargetClass.class.getMethod("foo", int.class, String.class);
    MethodCall mc = new MethodCall(m, 22, "Bela");
    MethodCall call2 = marshalAndUnmarshal(mc);
    System.out.println("call2 = " + call2);
    mc = new MethodCall(CALL, true, Boolean.FALSE, 322649, 3.24, (float) 54.345, new byte[] { 'b', 'e', 'l', 'a' }, new String[] { "Bela", "Michelle" });
    call2 = marshalAndUnmarshal(mc);
    System.out.println("call2 = " + call2);
}
Also used : Method(java.lang.reflect.Method) MethodCall(org.jgroups.blocks.MethodCall)

Aggregations

MethodCall (org.jgroups.blocks.MethodCall)31 RequestOptions (org.jgroups.blocks.RequestOptions)9 Method (java.lang.reflect.Method)6 Address (org.jgroups.Address)5 RpcDispatcher (org.jgroups.blocks.RpcDispatcher)5 Rsp (org.jgroups.util.Rsp)3 SiteMaster (org.jgroups.protocols.relay.SiteMaster)2 RspList (org.jgroups.util.RspList)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 JChannel (org.jgroups.JChannel)1 AdditionalJmxObjects (org.jgroups.jmx.AdditionalJmxObjects)1 RELAY2 (org.jgroups.protocols.relay.RELAY2)1 Route (org.jgroups.protocols.relay.Route)1 Protocol (org.jgroups.stack.Protocol)1 Average (org.jgroups.util.Average)1 AfterMethod (org.testng.annotations.AfterMethod)1 BeforeMethod (org.testng.annotations.BeforeMethod)1 Test (org.testng.annotations.Test)1