Search in sources :

Example 1 with Pair

use of org.nustaq.kontraktor.util.Pair in project kontraktor by RuedigerMoeller.

the class UndertowHttpServerConnector method createLongPollTask.

protected Pair<Runnable, KHttpExchange> createLongPollTask(KHttpExchange exchange, HttpObjectSocket httpObjectSocket, StreamSinkChannel sinkchannel) {
    return new Pair<>(() -> {
        if (!sinkchannel.isOpen())
            return;
        Pair<byte[], Integer> nextQueuedMessage = httpObjectSocket.getNextQueuedMessage();
        byte[] response = nextQueuedMessage.getFirst();
        exchange.setResponseContentLength(response.length);
        if (response.length == 0) {
            exchange.endExchange();
        } else {
            httpObjectSocket.storeLPMessage(nextQueuedMessage.getSecond(), response);
            // FIXME: should be async ?
            // exchange.send(response);
            ByteBuffer responseBuf = ByteBuffer.wrap(response);
            try {
                while (responseBuf.remaining() > 0) {
                    sinkchannel.write(responseBuf);
                }
            } catch (Throwable e) {
                System.out.println("buffer size:" + response.length);
                try {
                    sinkchannel.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                e.printStackTrace();
            }
            exchange.endExchange();
        }
    }, exchange);
}
Also used : IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) Pair(org.nustaq.kontraktor.util.Pair)

Example 2 with Pair

use of org.nustaq.kontraktor.util.Pair in project kontraktor by RuedigerMoeller.

the class KlusterConf method interpret.

public void interpret(String grp, Properties par, Object[] prog) {
    Properties env = new Properties(par);
    int index = 0;
    while (index < prog.length) {
        Pair def = new Pair(prog[index], prog[index + 1]);
        index += 2;
        if (def.car().toString().startsWith("loop_")) {
            String[] split = def.car().toString().split("_");
            int start = Integer.parseInt(split[1]);
            int end = Integer.parseInt(split[2]);
            for (int i = start; i < end; i++) {
                List cdr = (List) def.cdr();
                Object[] newOb = new Object[cdr.size()];
                cdr.toArray(newOb);
                Properties props = new Properties(env);
                props.put("IDX", "" + i);
                interpret(grp, props, newOb);
            }
        } else if ("process".equals(def.car())) {
            // resolve commandline string
            String unresolvedString = (String) def.cdr();
            String resolvedString = resolve(unresolvedString, x -> (String) env.getProperty(x));
            String[] resolved = dequote(resolvedString);
            StarterClientArgs args = new StarterClientArgs();
            JCommander jc = new JCommander(args);
            args = StarterClient.parseStarterConf(resolved, args, jc);
            // System.out.println(args);
            args.setGroup(grp);
            toStart.add(args);
        } else {
            if (def.cdr() instanceof List) {
                if (!"tasks".equals(def.car()) && (groups == null || groups.size() == 0 || (groups.contains(def.car())))) {
                    System.out.println("start group " + def.car());
                } else {
                    System.out.println("ignoring group " + def.car());
                }
                List cdr = (List) def.cdr();
                Object[] newOb = new Object[cdr.size()];
                cdr.toArray(newOb);
                interpret("" + def.car(), env, newOb);
            } else {
                env.put(def.car(), def.cdr());
            }
        }
    }
}
Also used : java.util(java.util) java.io(java.io) Kson(org.nustaq.kson.Kson) JCommander(com.beust.jcommander.JCommander) Pair(org.nustaq.kontraktor.util.Pair) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) JCommander(com.beust.jcommander.JCommander) Pair(org.nustaq.kontraktor.util.Pair)

Example 3 with Pair

use of org.nustaq.kontraktor.util.Pair in project kontraktor by RuedigerMoeller.

the class UndertowRESTHandler method parseAndDispatch.

private void parseAndDispatch(HttpServerExchange exchange, String[] split, String rawPath, Method m, byte[] postData) {
    try {
        Class<?>[] parameterTypes = m.getParameterTypes();
        Annotation[][] parameterAnnotations = m.getParameterAnnotations();
        Object[] args = new Object[parameterTypes.length];
        int splitIndex = 1;
        try {
            for (int i = 0; i < parameterTypes.length; i++) {
                Class<?> parameterType = parameterTypes[i];
                Annotation[] parameterAnnotation = parameterAnnotations[i];
                if (parameterAnnotation != null && parameterAnnotation.length > 0) {
                    if (parameterAnnotation[0].annotationType() == FromQuery.class) {
                        String value = ((FromQuery) parameterAnnotation[0]).value();
                        Deque<String> strings = exchange.getQueryParameters().get(value);
                        if (strings != null) {
                            args[i] = inferValue(parameterType, strings.getFirst());
                        }
                        continue;
                    } else if (parameterAnnotation[0].annotationType() == RequestPath.class) {
                        args[i] = rawPath;
                        continue;
                    }
                }
                if (splitIndex < split.length) {
                    String stringVal = split[splitIndex];
                    Object val = inferValue(parameterType, stringVal);
                    if (val != NOVAL) {
                        args[i] = val;
                        splitIndex++;
                        continue;
                    }
                }
                // specials
                if (parameterType == HeaderMap.class) {
                    args[i] = exchange.getRequestHeaders();
                } else if (parameterType == String[].class) {
                    args[i] = split;
                } else if (parameterType == JsonObject.class || parameterType == JsonValue.class) {
                    args[i] = Json.parse(new String(postData, "UTF-8"));
                } else if (parameterType == byte[].class) {
                    args[i] = postData;
                } else if (parameterType == Map.class) {
                    args[i] = exchange.getQueryParameters();
                } else {
                    System.out.println("unsupported parameter type " + parameterType.getName());
                }
            }
        } catch (Throwable th) {
            th.printStackTrace();
            Log.Warn(this, th, postData != null ? new String(postData, 0) : "");
            exchange.setStatusCode(400);
            exchange.getResponseSender().send("" + th + "\n");
            return;
        }
        // change: allow incomplete parameters
        // if ( splitIndex != split.length ) {
        // exchange.setResponseCode(400);
        // exchange.endExchange();
        // return;
        // }
        Object invoke = m.invoke(facade.getActorRef(), args);
        if (invoke instanceof IPromise) {
            checkExchangeState(exchange);
            ((IPromise) invoke).then((r, e) -> {
                if (e != null) {
                    exchange.setStatusCode(500);
                    exchange.getResponseSender().send("" + e);
                } else {
                    checkExchangeState(exchange);
                    if (r instanceof String) {
                        exchange.setStatusCode(400);
                        exchange.getResponseSender().send("" + e);
                    } else if (r instanceof Integer) {
                        exchange.setStatusCode((Integer) r);
                        exchange.endExchange();
                    } else if (r instanceof Pair) {
                        exchange.setStatusCode((Integer) ((Pair) r).car());
                        exchange.getResponseSender().send("" + ((Pair) r).cdr());
                    } else if (r instanceof JsonValue) {
                        exchange.getResponseSender().send(r.toString());
                    } else if (r instanceof Serializable) {
                        byte[] bytes = jsonConf.asByteArray(r);
                        exchange.getResponseSender().send(ByteBuffer.wrap(bytes));
                    }
                }
            });
        } else if (invoke == null) {
            exchange.setStatusCode(200);
            exchange.endExchange();
        }
    } catch (Exception e) {
        Log.Warn(this, e);
        exchange.setStatusCode(500);
        exchange.getResponseSender().send("" + e);
        exchange.endExchange();
        return;
    }
}
Also used : Serializable(java.io.Serializable) JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) Annotation(java.lang.annotation.Annotation) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IPromise(org.nustaq.kontraktor.IPromise) JsonObject(com.eclipsesource.json.JsonObject) Pair(org.nustaq.kontraktor.util.Pair)

Example 4 with Pair

use of org.nustaq.kontraktor.util.Pair in project kontraktor by RuedigerMoeller.

the class Diff method reduced.

public Diff reduced(String[] reducedFields) {
    ArrayList<Pair<String, Object>> newDiff = new ArrayList();
    for (int i = 0; i < changedFields.length; i++) {
        String changedField = changedFields[i];
        for (int j = 0; j < reducedFields.length; j++) {
            String reducedField = reducedFields[j];
            if (changedField.equals(reducedField)) {
                newDiff.add(new Pair(reducedField, oldValues[i]));
            }
        }
    }
    String[] newChanged = new String[newDiff.size()];
    Object[] newVals = new Object[newDiff.size()];
    for (int i = 0; i < newDiff.size(); i++) {
        Pair<String, Object> stringObjectPair = newDiff.get(i);
        newChanged[i] = stringObjectPair.car();
        newVals[i] = stringObjectPair.cdr();
    }
    return new Diff(newChanged, newVals);
}
Also used : ArrayList(java.util.ArrayList) Pair(org.nustaq.kontraktor.util.Pair)

Example 5 with Pair

use of org.nustaq.kontraktor.util.Pair in project kontraktor by RuedigerMoeller.

the class ServiceRegistry method broadcastAvailable.

protected void broadcastAvailable(ServiceDescription desc) {
    Pair msg = new Pair(AVAILABLE, desc);
    listeners = listeners.stream().filter(cb -> !cb.isTerminated()).collect(Collectors.toList());
    listeners.forEach(cb -> {
        try {
            cb.pipe(msg);
        } catch (Throwable th) {
            Log.Info(this, th);
        }
    });
}
Also used : Pair(org.nustaq.kontraktor.util.Pair)

Aggregations

Pair (org.nustaq.kontraktor.util.Pair)9 IOException (java.io.IOException)3 JCommander (com.beust.jcommander.JCommander)1 JsonObject (com.eclipsesource.json.JsonObject)1 JsonValue (com.eclipsesource.json.JsonValue)1 java.io (java.io)1 Serializable (java.io.Serializable)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Annotation (java.lang.annotation.Annotation)1 ByteBuffer (java.nio.ByteBuffer)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 IPromise (org.nustaq.kontraktor.IPromise)1 Kson (org.nustaq.kson.Kson)1