Search in sources :

Example 1 with RemoteOSGiException

use of ch.ethz.iks.r_osgi.RemoteOSGiException in project ecf by eclipse.

the class ChannelEndpointImpl method receivedMessage.

/**
 * process a recieved message. Called by the channel.
 *
 * @param msg
 *            the received message.
 * @see ch.ethz.iks.r_osgi.channels.ChannelEndpoint#receivedMessage(ch.ethz.iks.r_osgi.messages.RemoteOSGiMessage)
 * @category ChannelEndpoint
 */
public void receivedMessage(final RemoteOSGiMessage msg) {
    if (msg == null) {
        dispose();
        return;
    }
    final Integer xid = new Integer(msg.getXID());
    final WaitingCallback callback;
    synchronized (callbacks) {
        callback = (WaitingCallback) callbacks.remove(xid);
    }
    if (callback != null) {
        callback.result(msg);
        return;
    } else {
        final Runnable r = new Runnable() {

            public void run() {
                final RemoteOSGiMessage reply = handleMessage(msg);
                if (reply != null) {
                    try {
                        trace("reply(msg=" + reply + ";remoteAddress=" + networkChannel.getRemoteAddress() + ")");
                        networkChannel.sendMessage(reply);
                    } catch (final NotSerializableException nse) {
                        throw new RemoteOSGiException(// $NON-NLS-1$
                        "Error sending " + reply, nse);
                    } catch (NullPointerException npe) {
                    // channel got closed
                    } catch (final IOException e) {
                        dispose();
                    }
                }
            }
        };
        synchronized (workQueue) {
            workQueue.add(r);
            workQueue.notify();
        }
    }
}
Also used : NotSerializableException(java.io.NotSerializableException) RemoteOSGiException(ch.ethz.iks.r_osgi.RemoteOSGiException) RemoteOSGiMessage(ch.ethz.iks.r_osgi.messages.RemoteOSGiMessage) IOException(java.io.IOException)

Example 2 with RemoteOSGiException

use of ch.ethz.iks.r_osgi.RemoteOSGiException in project ecf by eclipse.

the class ChannelEndpointImpl method installResolveAndStartBundle.

private void installResolveAndStartBundle(final RemoteServiceReference ref, final InputStream in, final boolean isProxy) {
    try {
        final Bundle bundle = RemoteOSGiActivator.getActivator().getContext().installBundle(ref.getURI().toString(), in);
        retrieveDependencies((String) bundle.getHeaders().get(Constants.IMPORT_PACKAGE), (String) bundle.getHeaders().get(Constants.EXPORT_PACKAGE));
        if (isProxy) {
            // store the bundle for state updates and cleanup
            proxyBundles.put(ref.getURI().getFragment(), bundle);
        }
        // start the bundle
        bundle.start();
    } catch (final BundleException e) {
        final Throwable nested = e.getNestedException() == null ? e : e.getNestedException();
        // nested.printStackTrace();
        throw new RemoteOSGiException(// $NON-NLS-1$
        "Could not install the generated bundle " + ref.toString(), nested);
    }
}
Also used : RemoteOSGiException(ch.ethz.iks.r_osgi.RemoteOSGiException) Bundle(org.osgi.framework.Bundle) BundleException(org.osgi.framework.BundleException)

Example 3 with RemoteOSGiException

use of ch.ethz.iks.r_osgi.RemoteOSGiException in project ecf by eclipse.

the class ChannelEndpointImpl method getProxyBundle.

/**
 * fetch the service from the remote peer.
 *
 * @param ref
 *            the remote service reference.
 * @throws IOException
 *             in case of network errors.
 */
void getProxyBundle(final RemoteServiceReference ref) throws IOException, RemoteOSGiException {
    if (networkChannel == null) {
        // $NON-NLS-1$
        throw new RemoteOSGiException("Channel is closed.");
    }
    // build the RequestServiceMessage
    final RequestServiceMessage req = new RequestServiceMessage();
    req.setServiceID(ref.getURI().getFragment());
    // send the RequestServiceMessage and get a DeliverServiceMessage in
    // return. The DeliverServiceMessage contains a minimal description of
    // the resources
    // of a proxy bundle. This is the service interface plus type injections
    // plus import/export
    // declarations for the bundle.
    final DeliverServiceMessage deliv = (DeliverServiceMessage) sendAndWait(req);
    // generate a proxy bundle for the service
    final InputStream in = new ProxyGenerator().generateProxyBundle(ref.getURI(), deliv);
    installResolveAndStartBundle(ref, in, true);
}
Also used : RequestServiceMessage(ch.ethz.iks.r_osgi.messages.RequestServiceMessage) RemoteOSGiException(ch.ethz.iks.r_osgi.RemoteOSGiException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DeliverServiceMessage(ch.ethz.iks.r_osgi.messages.DeliverServiceMessage)

Example 4 with RemoteOSGiException

use of ch.ethz.iks.r_osgi.RemoteOSGiException in project ecf by eclipse.

the class RemoteOSGiServiceImpl method getNetworkChannelFactory.

private NetworkChannelFactory getNetworkChannelFactory(final String protocol) throws RemoteOSGiException {
    try {
        final Filter filter = RemoteOSGiActivator.getActivator().getContext().createFilter(// $NON-NLS-1$
        "(" + NetworkChannelFactory.PROTOCOL_PROPERTY + "=" + // $NON-NLS-1$
        protocol + // $NON-NLS-1$
        ")");
        final ServiceReference[] refs = networkChannelFactoryTracker.getServiceReferences();
        if (refs != null) {
            for (int i = 0; i < refs.length; i++) {
                if (filter.match(refs[i])) {
                    return (NetworkChannelFactory) networkChannelFactoryTracker.getService(refs[i]);
                }
            }
        }
        throw new RemoteOSGiException(// $NON-NLS-1$
        "No NetworkChannelFactory for " + protocol + // $NON-NLS-1$
        " found.");
    } catch (final InvalidSyntaxException e) {
        // does not happen
        e.printStackTrace();
        return null;
    }
}
Also used : NetworkChannelFactory(ch.ethz.iks.r_osgi.channels.NetworkChannelFactory) RemoteOSGiException(ch.ethz.iks.r_osgi.RemoteOSGiException) Filter(org.osgi.framework.Filter) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ChannelEndpoint(ch.ethz.iks.r_osgi.channels.ChannelEndpoint) ServiceReference(org.osgi.framework.ServiceReference) RemoteServiceReference(ch.ethz.iks.r_osgi.RemoteServiceReference)

Example 5 with RemoteOSGiException

use of ch.ethz.iks.r_osgi.RemoteOSGiException in project ecf by eclipse.

the class ChannelEndpointImpl method handleMessage.

/**
 * message handler method.
 *
 * @param msg
 *            the incoming message.
 * @return if reply is created, null otherwise.
 * @throws RemoteOSGiException
 *             if something goes wrong.
 */
RemoteOSGiMessage handleMessage(final RemoteOSGiMessage msg) throws RemoteOSGiException {
    trace("handleMessage(msg=" + msg + ";remoteAddress=" + networkChannel.getRemoteAddress() + ")");
    switch(msg.getFuncID()) {
        // requests
        case RemoteOSGiMessage.LEASE:
            {
                final LeaseMessage lease = (LeaseMessage) msg;
                processLease(lease);
                populateLease(lease, RemoteOSGiServiceImpl.getServices(networkChannel.getProtocol()), RemoteOSGiServiceImpl.getTopics());
                return lease;
            }
        case RemoteOSGiMessage.REQUEST_SERVICE:
            {
                final RequestServiceMessage reqSrv = (RequestServiceMessage) msg;
                final String serviceID = reqSrv.getServiceID();
                final RemoteServiceRegistration reg = getServiceRegistration(serviceID);
                final DeliverServiceMessage m = reg.getDeliverServiceMessage();
                m.setXID(reqSrv.getXID());
                m.setServiceID(reqSrv.getServiceID());
                return m;
            }
        case RemoteOSGiMessage.LEASE_UPDATE:
            {
                final LeaseUpdateMessage suMsg = (LeaseUpdateMessage) msg;
                final String serviceID = suMsg.getServiceID();
                final short stateUpdate = suMsg.getType();
                final String serviceURI = getRemoteAddress().resolve("#" + serviceID).toString();
                switch(stateUpdate) {
                    case LeaseUpdateMessage.TOPIC_UPDATE:
                        {
                            // There is an older r-OSGi version that incorrectly sends an ArrayList
                            // (1.0.0.RC4_v20131016-1848)
                            Object topicsAdded = suMsg.getPayload()[0];
                            if (topicsAdded instanceof List) {
                                topicsAdded = ((List) topicsAdded).toArray(new String[0]);
                            }
                            Object topicsRemoved = suMsg.getPayload()[1];
                            if (topicsRemoved instanceof List) {
                                topicsRemoved = ((List) topicsRemoved).toArray(new String[0]);
                            }
                            updateTopics((String[]) topicsAdded, (String[]) topicsRemoved);
                            return null;
                        }
                    case LeaseUpdateMessage.SERVICE_ADDED:
                        {
                            final Dictionary properties = (Dictionary) suMsg.getPayload()[1];
                            sanitizeServiceProperties(properties, serviceURI);
                            final RemoteServiceReferenceImpl ref = new RemoteServiceReferenceImpl((String[]) suMsg.getPayload()[0], serviceID, properties, this);
                            remoteServices.put(serviceURI, ref);
                            RemoteOSGiServiceImpl.notifyRemoteServiceListeners(new RemoteServiceEvent(RemoteServiceEvent.REGISTERED, ref));
                            return null;
                        }
                    case LeaseUpdateMessage.SERVICE_MODIFIED:
                        {
                            final Dictionary properties = (Dictionary) suMsg.getPayload()[1];
                            sanitizeServiceProperties(properties, serviceURI);
                            final ServiceRegistration reg = (ServiceRegistration) proxiedServices.get(serviceID);
                            if (reg != null) {
                                reg.setProperties(properties);
                            }
                            // $NON-NLS-1$
                            final RemoteServiceReferenceImpl ref = getRemoteReference(serviceURI);
                            // (see https://bugs.eclipse.org/420433)
                            if (ref == null && reg == null) {
                                return null;
                            }
                            ref.setProperties(properties);
                            RemoteOSGiServiceImpl.notifyRemoteServiceListeners(new RemoteServiceEvent(RemoteServiceEvent.MODIFIED, ref));
                            return null;
                        }
                    case LeaseUpdateMessage.SERVICE_REMOVED:
                        {
                            if (networkChannel == null) {
                                return null;
                            }
                            final RemoteServiceReference ref = (RemoteServiceReference) remoteServices.remove(serviceURI);
                            if (ref != null) {
                                RemoteOSGiServiceImpl.notifyRemoteServiceListeners(new RemoteServiceEvent(RemoteServiceEvent.UNREGISTERING, ref));
                            }
                            final Bundle bundle = (Bundle) proxyBundles.remove(serviceID);
                            if (bundle != null) {
                                try {
                                    bundle.uninstall();
                                } catch (final BundleException be) {
                                    be.printStackTrace();
                                }
                                proxiedServices.remove(serviceID);
                                // $NON-NLS-1$
                                remoteServices.remove(serviceURI);
                            }
                            return null;
                        }
                }
                return null;
            }
        case RemoteOSGiMessage.REMOTE_CALL:
            {
                final RemoteCallMessage invMsg = (RemoteCallMessage) msg;
                try {
                    RemoteServiceRegistration serv = (RemoteServiceRegistration) localServices.get(invMsg.getServiceID());
                    if (serv == null) {
                        final RemoteServiceRegistration reg = getServiceRegistration(invMsg.getServiceID());
                        if (reg == null) {
                            throw new IllegalStateException(toString() + "Could not get " + // $NON-NLS-1$
                            invMsg.getServiceID() + ", known services " + // $NON-NLS-1$
                            localServices);
                        } else {
                            serv = reg;
                        }
                    }
                    // get the invocation arguments and the local method
                    final Object[] arguments = invMsg.getArgs();
                    for (int i = 0; i < arguments.length; i++) {
                        if (arguments[i] instanceof InputStreamHandle) {
                            arguments[i] = getInputStreamProxy((InputStreamHandle) arguments[i]);
                        } else if (arguments[i] instanceof OutputStreamHandle) {
                            arguments[i] = getOutputStreamProxy((OutputStreamHandle) arguments[i]);
                        }
                    }
                    final Method method = serv.getMethod(invMsg.getMethodSignature());
                    // invoke method
                    try {
                        Object result = method.invoke(serv.getServiceObject(), arguments);
                        final RemoteCallResultMessage m = new RemoteCallResultMessage();
                        m.setXID(invMsg.getXID());
                        Class returnType = method.getReturnType();
                        if (result instanceof InputStream) {
                            m.setResult(getInputStreamPlaceholder((InputStream) result));
                        } else if (result instanceof OutputStream) {
                            m.setResult(getOutputStreamPlaceholder((OutputStream) result));
                        } else if (serv.isOSGiAsync() && AsyncReturnUtil.isAsyncType(returnType)) {
                            m.setResult(AsyncReturnUtil.convertAsyncToReturn(result, returnType, serv.getOSGiTimeout()));
                        } else
                            m.setResult(result);
                        return m;
                    } catch (final InvocationTargetException t) {
                        t.printStackTrace();
                        throw t.getTargetException();
                    }
                } catch (final Throwable t) {
                    // TODO: send to log
                    t.printStackTrace();
                    final RemoteCallResultMessage m = new RemoteCallResultMessage();
                    m.setXID(invMsg.getXID());
                    m.setException(t);
                    return m;
                }
            }
        case RemoteOSGiMessage.REMOTE_EVENT:
            {
                final RemoteEventMessage eventMsg = (RemoteEventMessage) msg;
                final Dictionary properties = eventMsg.getProperties();
                // transform the event timestamps
                final Long remoteTs;
                if ((remoteTs = (Long) properties.get(EventConstants.TIMESTAMP)) != null) {
                    properties.put(EventConstants.TIMESTAMP, getOffset().transform(remoteTs));
                }
                final Event event = new Event(eventMsg.getTopic(), properties);
                // and deliver the event to the local framework
                if (RemoteOSGiServiceImpl.eventAdminTracker.getTrackingCount() > 0) {
                    ((EventAdmin) RemoteOSGiServiceImpl.eventAdminTracker.getService()).postEvent(event);
                } else {
                    // TODO: to log
                    System.err.println(// $NON-NLS-1$
                    "Could not deliver received event: " + event + // $NON-NLS-1$
                    ". No EventAdmin available.");
                }
                return null;
            }
        case RemoteOSGiMessage.TIME_OFFSET:
            {
                // add timestamp to the message and return the message to sender
                ((TimeOffsetMessage) msg).timestamp();
                return msg;
            }
        case RemoteOSGiMessage.STREAM_REQUEST:
            {
                final StreamRequestMessage reqMsg = (StreamRequestMessage) msg;
                try {
                    // fetch stream object
                    final Object stream = streams.get(new Integer(reqMsg.getStreamID()));
                    if (stream == null) {
                        throw new IllegalStateException(// $NON-NLS-1$
                        "Could not get stream with ID " + reqMsg.getStreamID());
                    }
                    // invoke operation on stream
                    switch(reqMsg.getOp()) {
                        case StreamRequestMessage.READ:
                            {
                                final int result = ((InputStream) stream).read();
                                final StreamResultMessage m = new StreamResultMessage();
                                m.setXID(reqMsg.getXID());
                                m.setResult((short) result);
                                return m;
                            }
                        case StreamRequestMessage.READ_ARRAY:
                            {
                                final byte[] b = new byte[reqMsg.getLenOrVal()];
                                final int len = ((InputStream) stream).read(b, 0, reqMsg.getLenOrVal());
                                final StreamResultMessage m = new StreamResultMessage();
                                m.setXID(reqMsg.getXID());
                                m.setResult(StreamResultMessage.RESULT_ARRAY);
                                m.setLen(len);
                                if (len > 0) {
                                    m.setData(b);
                                }
                                return m;
                            }
                        case StreamRequestMessage.WRITE:
                            {
                                ((OutputStream) stream).write(reqMsg.getLenOrVal());
                                final StreamResultMessage m = new StreamResultMessage();
                                m.setXID(reqMsg.getXID());
                                m.setResult(StreamResultMessage.RESULT_WRITE_OK);
                                return m;
                            }
                        case StreamRequestMessage.WRITE_ARRAY:
                            {
                                ((OutputStream) stream).write(reqMsg.getData());
                                final StreamResultMessage m = new StreamResultMessage();
                                m.setXID(reqMsg.getXID());
                                m.setResult(StreamResultMessage.RESULT_WRITE_OK);
                                return m;
                            }
                        default:
                            throw new RemoteOSGiException(// $NON-NLS-1$
                            "Unimplemented op code for stream request " + msg);
                    }
                } catch (final IOException e) {
                    final StreamResultMessage m = new StreamResultMessage();
                    m.setXID(reqMsg.getXID());
                    m.setResult(StreamResultMessage.RESULT_EXCEPTION);
                    m.setException(e);
                    return m;
                }
            }
        case RemoteOSGiMessage.REQUEST_BUNDLE:
            final RequestBundleMessage reqB = (RequestBundleMessage) msg;
            try {
                final String serviceID = reqB.getServiceID();
                final RemoteServiceRegistration reg = getServiceRegistration(serviceID);
                final byte[] bytes = RemoteOSGiServiceImpl.getBundle(reg.getReference().getBundle());
                final DeliverBundlesMessage delB = new DeliverBundlesMessage();
                delB.setXID(reqB.getXID());
                delB.setDependencies(new byte[][] { bytes });
                return delB;
            } catch (IOException ioe) {
                ioe.printStackTrace();
                return null;
            }
        case RemoteOSGiMessage.REQUEST_DEPENDENCIES:
            final RequestDependenciesMessage reqDeps = (RequestDependenciesMessage) msg;
            try {
                final byte[][] bundleBytes = RemoteOSGiServiceImpl.getBundlesForPackages(reqDeps.getPackages());
                final DeliverBundlesMessage delDeps = new DeliverBundlesMessage();
                delDeps.setXID(reqDeps.getXID());
                delDeps.setDependencies(bundleBytes);
                return delDeps;
            } catch (IOException ioe) {
                ioe.printStackTrace();
                return null;
            }
        default:
            // $NON-NLS-1$
            throw new RemoteOSGiException("Unimplemented message " + msg);
    }
}
Also used : RequestServiceMessage(ch.ethz.iks.r_osgi.messages.RequestServiceMessage) Dictionary(java.util.Dictionary) DeliverServiceMessage(ch.ethz.iks.r_osgi.messages.DeliverServiceMessage) OutputStream(java.io.OutputStream) RequestBundleMessage(ch.ethz.iks.r_osgi.messages.RequestBundleMessage) RemoteCallResultMessage(ch.ethz.iks.r_osgi.messages.RemoteCallResultMessage) StreamRequestMessage(ch.ethz.iks.r_osgi.messages.StreamRequestMessage) InputStreamHandle(ch.ethz.iks.r_osgi.streams.InputStreamHandle) RemoteEventMessage(ch.ethz.iks.r_osgi.messages.RemoteEventMessage) RemoteOSGiException(ch.ethz.iks.r_osgi.RemoteOSGiException) List(java.util.List) ArrayList(java.util.ArrayList) BundleException(org.osgi.framework.BundleException) ServiceRegistration(org.osgi.framework.ServiceRegistration) OutputStreamHandle(ch.ethz.iks.r_osgi.streams.OutputStreamHandle) RequestDependenciesMessage(ch.ethz.iks.r_osgi.messages.RequestDependenciesMessage) Bundle(org.osgi.framework.Bundle) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RemoteServiceEvent(ch.ethz.iks.r_osgi.RemoteServiceEvent) Method(java.lang.reflect.Method) StreamResultMessage(ch.ethz.iks.r_osgi.messages.StreamResultMessage) IOException(java.io.IOException) LeaseUpdateMessage(ch.ethz.iks.r_osgi.messages.LeaseUpdateMessage) LeaseMessage(ch.ethz.iks.r_osgi.messages.LeaseMessage) InvocationTargetException(java.lang.reflect.InvocationTargetException) ChannelEndpoint(ch.ethz.iks.r_osgi.channels.ChannelEndpoint) RemoteCallMessage(ch.ethz.iks.r_osgi.messages.RemoteCallMessage) RemoteServiceReference(ch.ethz.iks.r_osgi.RemoteServiceReference) RemoteServiceEvent(ch.ethz.iks.r_osgi.RemoteServiceEvent) Event(org.osgi.service.event.Event) DeliverBundlesMessage(ch.ethz.iks.r_osgi.messages.DeliverBundlesMessage)

Aggregations

RemoteOSGiException (ch.ethz.iks.r_osgi.RemoteOSGiException)13 IOException (java.io.IOException)6 ChannelEndpoint (ch.ethz.iks.r_osgi.channels.ChannelEndpoint)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InputStream (java.io.InputStream)4 RemoteServiceReference (ch.ethz.iks.r_osgi.RemoteServiceReference)3 RemoteCallMessage (ch.ethz.iks.r_osgi.messages.RemoteCallMessage)3 RemoteCallResultMessage (ch.ethz.iks.r_osgi.messages.RemoteCallResultMessage)3 RemoteOSGiMessage (ch.ethz.iks.r_osgi.messages.RemoteOSGiMessage)3 InputStreamHandle (ch.ethz.iks.r_osgi.streams.InputStreamHandle)3 OutputStreamHandle (ch.ethz.iks.r_osgi.streams.OutputStreamHandle)3 OutputStream (java.io.OutputStream)3 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)3 NetworkChannelFactory (ch.ethz.iks.r_osgi.channels.NetworkChannelFactory)2 DeliverBundlesMessage (ch.ethz.iks.r_osgi.messages.DeliverBundlesMessage)2 DeliverServiceMessage (ch.ethz.iks.r_osgi.messages.DeliverServiceMessage)2 LeaseUpdateMessage (ch.ethz.iks.r_osgi.messages.LeaseUpdateMessage)2 RequestBundleMessage (ch.ethz.iks.r_osgi.messages.RequestBundleMessage)2 RequestServiceMessage (ch.ethz.iks.r_osgi.messages.RequestServiceMessage)2 ArrayList (java.util.ArrayList)2