Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8202abdb6e6b04ede5451f3554abc227cd3c9a68 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*******************************************************************************
  * Copyright (c) 2004 Composent, Inc. and others. All rights reserved. This
 * program and the accompanying materials are made available under the terms of
 * the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: Composent, Inc. - initial API and implementation
 ******************************************************************************/
package org.eclipse.ecf.provider.remoteservice.generic;

import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
import org.eclipse.ecf.core.util.ECFException;
import org.eclipse.ecf.remoteservice.*;
import org.eclipse.ecf.remoteservice.events.IRemoteCallCompleteEvent;
import org.eclipse.ecf.remoteservice.events.IRemoteCallEvent;

public class RemoteServiceImpl extends AbstractRemoteService {

	protected static final long DEFAULT_TIMEOUT = new Long(System.getProperty("ecf.remotecall.timeout", "30000")).longValue(); //$NON-NLS-1$ //$NON-NLS-2$

	protected RemoteServiceRegistrationImpl registration = null;
	protected RegistrySharedObject sharedObject = null;

	public RemoteServiceImpl(RegistrySharedObject sharedObject, RemoteServiceRegistrationImpl registration) {
		this.sharedObject = sharedObject;
		this.registration = registration;
	}

	protected IRemoteServiceID getRemoteServiceID() {
		return registration.getID();
	}

	protected IRemoteServiceReference getRemoteServiceReference() {
		return registration.getReference();
	}

	protected String[] getInterfaceClassNames() {
		return registration.getClasses();
	}

	/**
	 * @since 3.0
	 * @see org.eclipse.ecf.remoteservice.IRemoteService#callAsync(org.eclipse.ecf.remoteservice.IRemoteCall, org.eclipse.ecf.remoteservice.IRemoteCallListener)
	 */
	public void callAsync(final IRemoteCall call, final IRemoteCallListener listener) {
		getFutureExecutorService(call).submit(new Runnable() {
			public void run() {
				final AtomicReference<IRemoteCallEvent> l = new AtomicReference<IRemoteCallEvent>();
				sharedObject.sendCallRequestWithListener(registration, call, new IRemoteCallListener() {
					public void handleEvent(IRemoteCallEvent event) {
						if (event instanceof IRemoteCallCompleteEvent) {
							synchronized (l) {
								l.set(event);
								l.notify();
							}
						}
					}
				});
				long timeout = call.getTimeout();
				Exception exception = null;
				IRemoteCallEvent rce = null;
				long sysTimeout = System.currentTimeMillis() + timeout;
				synchronized (l) {
					try {
						while (rce == null && System.currentTimeMillis() < sysTimeout) {
							l.wait(timeout / 10);
							rce = l.get();
						}
					} catch (InterruptedException e) {
						exception = e;
					}
				}
				if (rce != null)
					listener.handleEvent(rce);
				else {
					if (exception == null)
						exception = new TimeoutException("remote call method=" + call.getMethod() + " timed out after " + timeout + "ms"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
					final Exception except = exception;
					listener.handleEvent(new IRemoteCallCompleteEvent() {
						public long getRequestId() {
							return 0;
						}

						public Object getResponse() {
							return null;
						}

						public boolean hadException() {
							return true;
						}

						public Throwable getException() {
							return except;
						}
					});
				}
			}
		});
	}

	/**
	 * @since 3.0
	 * @see org.eclipse.ecf.remoteservice.IRemoteService#callSync(org.eclipse.ecf.remoteservice.IRemoteCall)
	 */
	public Object callSync(IRemoteCall call) throws ECFException {
		return sharedObject.callSynch(registration, call);
	}

	/**
	 * @since 3.0
	 * @see org.eclipse.ecf.remoteservice.IRemoteService#fireAsync(org.eclipse.ecf.remoteservice.IRemoteCall)
	 */
	public void fireAsync(IRemoteCall call) throws ECFException {
		sharedObject.sendFireRequest(registration, call);
	}

}

Back to the top