Search in sources :

Example 1 with JoynrIllegalStateException

use of io.joynr.exceptions.JoynrIllegalStateException in project joynr by bmwcarit.

the class WebSocketJettyClient method start.

@Override
public synchronized void start() {
    if (jettyClient == null) {
        jettyClient = new WebSocketClient();
        jettyClient.getPolicy().setMaxTextMessageSize(maxMessageSize);
        jettyClient.getPolicy().setMaxBinaryMessageSize(maxMessageSize);
        jettyClient.setMaxIdleTimeout(websocketIdleTimeout);
    }
    try {
        jettyClient.start();
        sessionFuture = jettyClient.connect(this, toUrl(serverAddress));
        sendInitializationMessage();
    } catch (JoynrShutdownException | JoynrIllegalStateException e) {
        logger.error("unrecoverable error starting WebSocket client: {}", e);
        return;
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    } catch (Exception e) {
        // TODO which exceptions are recoverable? Only catch those ones
        // JoynrCommunicationExeption is thrown if the initialization message could not be sent
        logger.debug("error starting WebSocket client. Will retry", e);
        if (shutdown) {
            return;
        }
        if (reconnectTimerRunning.compareAndSet(false, true)) {
            reconnectTimer.schedule(new TimerTask() {

                @Override
                public void run() {
                    reconnectTimerRunning.set(false);
                    start();
                }
            }, reconnectDelay);
        }
    }
}
Also used : TimerTask(java.util.TimerTask) JoynrShutdownException(io.joynr.exceptions.JoynrShutdownException) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) JoynrIllegalStateException(io.joynr.exceptions.JoynrIllegalStateException) WebSocketException(org.eclipse.jetty.websocket.api.WebSocketException) TimeoutException(java.util.concurrent.TimeoutException) JoynrShutdownException(io.joynr.exceptions.JoynrShutdownException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ExecutionException(java.util.concurrent.ExecutionException) JoynrDelayMessageException(io.joynr.exceptions.JoynrDelayMessageException) JoynrIllegalStateException(io.joynr.exceptions.JoynrIllegalStateException) JoynrCommunicationException(io.joynr.exceptions.JoynrCommunicationException)

Example 2 with JoynrIllegalStateException

use of io.joynr.exceptions.JoynrIllegalStateException in project joynr by bmwcarit.

the class MqttPahoClient method start.

@Override
public void start() {
    while (!shutdown.get() && !mqttClient.isConnected()) {
        try {
            synchronized (this) {
                logger.debug("Started MqttPahoClient");
                mqttClient.setCallback(this);
                mqttClient.setTimeToWait(timeToWaitMs);
                mqttClient.connect(getConnectOptions());
                logger.debug("MQTT Connected client");
                mqttStatusReceiver.notifyConnectionStatusChanged(MqttStatusReceiver.ConnectionStatus.CONNECTED);
                reestablishSubscriptions();
            }
        } catch (MqttException mqttError) {
            logger.error("MQTT Connect failed. Error code {}", mqttError.getReasonCode(), mqttError);
            switch(mqttError.getReasonCode()) {
                case MqttException.REASON_CODE_CLIENT_EXCEPTION:
                    if (isSecureConnection) {
                        logger.error("Failed to establish TLS connection, error: " + mqttError);
                        if (mqttError instanceof MqttSecurityException || (mqttError.getCause() != null && mqttError.getCause() instanceof SSLHandshakeException)) {
                            throw new JoynrIllegalStateException("Unable to create TLS MqttPahoClient: " + mqttError);
                        }
                    }
                case MqttException.REASON_CODE_BROKER_UNAVAILABLE:
                case MqttException.REASON_CODE_CLIENT_DISCONNECTING:
                case MqttException.REASON_CODE_CLIENT_NOT_CONNECTED:
                case MqttException.REASON_CODE_CLIENT_TIMEOUT:
                case MqttException.REASON_CODE_CONNECT_IN_PROGRESS:
                case MqttException.REASON_CODE_CONNECTION_LOST:
                case MqttException.REASON_CODE_MAX_INFLIGHT:
                case MqttException.REASON_CODE_NO_MESSAGE_IDS_AVAILABLE:
                case MqttException.REASON_CODE_SERVER_CONNECT_ERROR:
                case MqttException.REASON_CODE_SUBSCRIBE_FAILED:
                case MqttException.REASON_CODE_UNEXPECTED_ERROR:
                case MqttException.REASON_CODE_WRITE_TIMEOUT:
                    if (shutdown.get()) {
                        return;
                    }
                    synchronized (this) {
                        try {
                            this.wait(reconnectSleepMs);
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                            return;
                        }
                    }
                    continue;
                case MqttException.REASON_CODE_CLIENT_CONNECTED:
                    continue;
            }
        } catch (Exception e) {
            throw new JoynrIllegalStateException("Unable to start MqttPahoClient: " + e.getMessage(), e);
        }
    }
}
Also used : MqttException(org.eclipse.paho.client.mqttv3.MqttException) MqttSecurityException(org.eclipse.paho.client.mqttv3.MqttSecurityException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) JoynrIllegalStateException(io.joynr.exceptions.JoynrIllegalStateException) URISyntaxException(java.net.URISyntaxException) JoynrMessageNotSentException(io.joynr.exceptions.JoynrMessageNotSentException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) MqttException(org.eclipse.paho.client.mqttv3.MqttException) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) JoynrDelayMessageException(io.joynr.exceptions.JoynrDelayMessageException) JoynrIllegalStateException(io.joynr.exceptions.JoynrIllegalStateException) MqttSecurityException(org.eclipse.paho.client.mqttv3.MqttSecurityException)

Example 3 with JoynrIllegalStateException

use of io.joynr.exceptions.JoynrIllegalStateException in project joynr by bmwcarit.

the class MqttPahoClient method subscribe.

@Override
public void subscribe(String topic) {
    boolean subscribed = false;
    while (!subscribed && !shutdown.get()) {
        logger.debug("MQTT subscribing to: {}", topic);
        try {
            synchronized (subscribedTopics) {
                if (!subscribedTopics.contains(topic)) {
                    mqttClient.subscribe(topic);
                    subscribedTopics.add(topic);
                }
                subscribed = true;
            }
        } catch (MqttException mqttError) {
            logger.debug("MQTT subscribe to {} failed: {}. Error code {}", topic, mqttError.getMessage(), mqttError.getReasonCode(), mqttError);
            switch(mqttError.getReasonCode()) {
                case MqttException.REASON_CODE_CLIENT_EXCEPTION:
                case MqttException.REASON_CODE_BROKER_UNAVAILABLE:
                case MqttException.REASON_CODE_CLIENT_TIMEOUT:
                case MqttException.REASON_CODE_CONNECT_IN_PROGRESS:
                case MqttException.REASON_CODE_MAX_INFLIGHT:
                case MqttException.REASON_CODE_NO_MESSAGE_IDS_AVAILABLE:
                case MqttException.REASON_CODE_SERVER_CONNECT_ERROR:
                case MqttException.REASON_CODE_SUBSCRIBE_FAILED:
                case MqttException.REASON_CODE_UNEXPECTED_ERROR:
                case MqttException.REASON_CODE_WRITE_TIMEOUT:
                case MqttException.REASON_CODE_CONNECTION_LOST:
                case MqttException.REASON_CODE_CLIENT_NOT_CONNECTED:
                case MqttException.REASON_CODE_CLIENT_DISCONNECTING:
                    try {
                        Thread.sleep(reconnectSleepMs);
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                        return;
                    }
                    continue;
                default:
                    throw new JoynrIllegalStateException("Unexpected exception while subscribing to " + topic + ", error: " + mqttError);
            }
        } catch (Exception e) {
            throw new JoynrRuntimeException("Unable to start MqttPahoClient", e);
        }
    }
}
Also used : MqttException(org.eclipse.paho.client.mqttv3.MqttException) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) JoynrIllegalStateException(io.joynr.exceptions.JoynrIllegalStateException) URISyntaxException(java.net.URISyntaxException) JoynrMessageNotSentException(io.joynr.exceptions.JoynrMessageNotSentException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) MqttException(org.eclipse.paho.client.mqttv3.MqttException) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) JoynrDelayMessageException(io.joynr.exceptions.JoynrDelayMessageException) JoynrIllegalStateException(io.joynr.exceptions.JoynrIllegalStateException) MqttSecurityException(org.eclipse.paho.client.mqttv3.MqttSecurityException)

Example 4 with JoynrIllegalStateException

use of io.joynr.exceptions.JoynrIllegalStateException in project joynr by bmwcarit.

the class JoynrMessagingConnectorInvocationHandler method executeSyncMethod.

@CheckForNull
@Override
public Object executeSyncMethod(Method method, Object[] args) throws ApplicationException {
    // TODO does a method with 0 args pass in an empty args array, or null for args?
    if (method == null) {
        throw new IllegalArgumentException("Method cannot be null");
    }
    if (toDiscoveryEntries.size() > 1) {
        throw new JoynrIllegalStateException("You can't execute sync methods for multiple participants.");
    }
    if (toDiscoveryEntries.isEmpty()) {
        throw new JoynrIllegalStateException("You must have exactly one participant to be able to execute a sync method.");
    }
    MethodMetaInformation methodMetaInformation = JoynrMessagingConnectorFactory.ensureMethodMetaInformationPresent(method);
    Request request = new Request(method.getName(), args, method.getParameterTypes());
    Reply reply;
    String requestReplyId = request.getRequestReplyId();
    SynchronizedReplyCaller synchronizedReplyCaller = new SynchronizedReplyCaller(fromParticipantId, requestReplyId, request);
    ExpiryDate expiryDate = DispatcherUtils.convertTtlToExpirationDate(qosSettings.getRoundTripTtl_ms());
    replyCallerDirectory.addReplyCaller(requestReplyId, synchronizedReplyCaller, expiryDate);
    reply = (Reply) requestReplyManager.sendSyncRequest(fromParticipantId, toDiscoveryEntries.iterator().next(), request, synchronizedReplyCaller, qosSettings);
    if (reply.getError() == null) {
        if (method.getReturnType().equals(void.class)) {
            return null;
        }
        Object response = RpcUtils.reconstructReturnedObject(method, methodMetaInformation, reply.getResponse());
        logger.debug("REQUEST returns successful: requestReplyId: {}, method {}, response: {}", requestReplyId, method.getName(), response);
        return response;
    } else if (reply.getError() instanceof ApplicationException) {
        logger.debug("REQUEST returns error: requestReplyId: {}, method {}, response: {}", requestReplyId, method.getName(), reply.getError());
        throw (ApplicationException) reply.getError();
    } else {
        logger.debug("REQUEST returns error: requestReplyId: {}, method {}, response: {}", requestReplyId, method.getName(), reply.getError());
        throw (JoynrRuntimeException) reply.getError();
    }
}
Also used : ExpiryDate(io.joynr.common.ExpiryDate) ApplicationException(joynr.exceptions.ApplicationException) MethodMetaInformation(joynr.MethodMetaInformation) OneWayRequest(joynr.OneWayRequest) Request(joynr.Request) Reply(joynr.Reply) SynchronizedReplyCaller(io.joynr.dispatching.rpc.SynchronizedReplyCaller) JoynrIllegalStateException(io.joynr.exceptions.JoynrIllegalStateException) CheckForNull(javax.annotation.CheckForNull)

Example 5 with JoynrIllegalStateException

use of io.joynr.exceptions.JoynrIllegalStateException in project joynr by bmwcarit.

the class ProxyInvocationHandlerImpl method invoke.

@Override
@CheckForNull
public Object invoke(@Nonnull Method method, Object[] args) throws ApplicationException {
    logger.trace("calling proxy.{}({}) on domain: {} and interface {}, proxy participant ID: {}", method.getName(), args, domains, interfaceName, proxyParticipantId);
    Class<?> methodInterfaceClass = method.getDeclaringClass();
    try {
        if (JoynrSubscriptionInterface.class.isAssignableFrom(methodInterfaceClass) || JoynrBroadcastSubscriptionInterface.class.isAssignableFrom(methodInterfaceClass)) {
            return executeSubscriptionMethod(method, args);
        } else if (methodInterfaceClass.getAnnotation(FireAndForget.class) != null) {
            return executeOneWayMethod(method, args);
        } else if (methodInterfaceClass.getAnnotation(Sync.class) != null) {
            return executeSyncMethod(method, args);
        } else if (methodInterfaceClass.getAnnotation(Async.class) != null) {
            return executeAsyncMethod(method, args);
        } else {
            throw new JoynrIllegalStateException("Method is not part of sync, async or subscription interface");
        }
    } catch (JoynrRuntimeException | ApplicationException e) {
        throw e;
    } catch (Exception e) {
        throw new JoynrRuntimeException(e);
    }
}
Also used : JoynrSubscriptionInterface(io.joynr.dispatcher.rpc.JoynrSubscriptionInterface) ApplicationException(joynr.exceptions.ApplicationException) Sync(io.joynr.Sync) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) JoynrBroadcastSubscriptionInterface(io.joynr.dispatcher.rpc.JoynrBroadcastSubscriptionInterface) JoynrIllegalStateException(io.joynr.exceptions.JoynrIllegalStateException) DiscoveryException(io.joynr.exceptions.DiscoveryException) JoynrException(io.joynr.exceptions.JoynrException) ApplicationException(joynr.exceptions.ApplicationException) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) JoynrIllegalStateException(io.joynr.exceptions.JoynrIllegalStateException) CheckForNull(javax.annotation.CheckForNull)

Aggregations

JoynrIllegalStateException (io.joynr.exceptions.JoynrIllegalStateException)19 JoynrRuntimeException (io.joynr.exceptions.JoynrRuntimeException)5 JoynrDelayMessageException (io.joynr.exceptions.JoynrDelayMessageException)4 Query (javax.persistence.Query)4 OneWayRequest (joynr.OneWayRequest)3 ApplicationException (joynr.exceptions.ApplicationException)3 Test (org.junit.Test)3 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)2 ExpiryDate (io.joynr.common.ExpiryDate)2 DiscoveryException (io.joynr.exceptions.DiscoveryException)2 JoynrMessageNotSentException (io.joynr.exceptions.JoynrMessageNotSentException)2 SubscriptionException (io.joynr.exceptions.SubscriptionException)2 MessagingQos (io.joynr.messaging.MessagingQos)2 Method (java.lang.reflect.Method)2 URISyntaxException (java.net.URISyntaxException)2 CheckForNull (javax.annotation.CheckForNull)2 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)2 MethodMetaInformation (joynr.MethodMetaInformation)2 PeriodicSubscriptionQos (joynr.PeriodicSubscriptionQos)2 Reply (joynr.Reply)2