Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 4d966561266b0e5532522e503ff55810be5700cb (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*******************************************************************************
 * Copyright (c) 2001, 2005 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jem.internal.proxy.vm.remote;
/*
 *  $RCSfile: CallbackHandler.java,v $
 *  $Revision: 1.7 $  $Date: 2005/08/24 20:39:08 $ 
 */

import java.net.Socket;
import java.io.*;
import org.eclipse.jem.internal.proxy.common.remote.Commands;
import org.eclipse.jem.internal.proxy.common.remote.UnexpectedExceptionCommandException;
import org.eclipse.jem.internal.proxy.common.*;
/**
 * This is a handler for doing callbacks to the
 * client.
 *
 * This is package protected because no one should
 * use it from the outside. It is too critical that
 * callback's work only in this specified way.
 */
class CallbackHandler extends ConnectionHandler implements ICallbackHandler {
	public CallbackHandler(Socket sc, RemoteVMServerThread svr) {
		super(sc, svr, null);
	}		
	
	/**
	 * Initiate a stream callback request.
	 */
	public void initiateCallbackStream(int callbackID, int msgID) throws CommandException {
		try {
			Commands.sendCallbackStreamCommand(out, callbackID, msgID);
			out.flush();
		} catch (CommandException e) {
			throw e;
		} catch (Exception e) {
			throw new UnexpectedExceptionCommandException(false, e);
		}
		run();	// Now run and wait for return. If no command exeception thrown, then it signals to continue.
	}
	
	/**
	 * Write bytes to the client. If bytesToWrite is -1, then this is end and
	 * no bytes are being written. Just write the -1 then.
	 */
	public void writeBytes(byte[] bytes, int bytesToWrite) throws CommandException {
		try {
			if (bytesToWrite != -1)
				Commands.writeBytes(out, bytes, bytesToWrite);
			else
				out.writeInt(-1);
		} catch (CommandException e) {
			throw e;
		} catch (Exception e) {
			throw new UnexpectedExceptionCommandException(false, e);
		}
	}

		
	/**
	 * Callback, but send the parm as an object, ie. it must
	 * be nothing but constants, e.g. String, Integer, or an
	 * array of constants. Constants should not be things like
	 * regular objects. This is because only standard java.lang
	 * type constants can be assured to be available on the other
	 * client. Also you don't want to send big objects, except
	 * maybe something like an array of bytes or strings. It must
	 * be constants that don't need to be sent back for any reason
	 * since their identity will be lost in the transfer.
	 *
	 * This should be used if there are no parms (i.e. it is null).
	 */
	public Object callbackAsConstants(int callbackID, int msgID, Object parm) throws CommandException {

		try {
			Commands.sendCallbackCommand(out, callbackID, msgID);
			Commands.ValueObject v = new Commands.ValueObject();
			sendObject(parm, SEND_AS_IS, v, out, false);
			out.flush();
		} catch (CommandException e) {
			throw e;
		} catch (Exception e) {
			throw new UnexpectedExceptionCommandException(false, e);
		}
		return run();	// Now run and wait for return.
	}
	
	
	// A retriever is what handles the array part.
	private class Retriever implements Commands.ValueRetrieve {
		int index=0;
		Object[] array;
		Commands.ValueObject worker = new Commands.ValueObject();
		
		
		public Retriever(Object[] anArray) {
			array = anArray;
		}
		
		public Commands.ValueObject nextValue() {
			Object parm = array[index++];
			if (parm != null) {
				if (parm instanceof ICallbackHandler.TransmitableArray) {
					// It is another array, create a new retriever.
					worker.setArrayIDS(new Retriever(((ICallbackHandler.TransmitableArray) parm).getArray()), ((ICallbackHandler.TransmitableArray) parm).getArray().length, Commands.OBJECT_CLASS);
				} else {
					// They may add some new ID's and if there is an error, they
					// won't get released, but that is a slight chance we will have
					// to take because we don't know which ones were new and which
					// ones weren't.
					fillInValue(parm, NOT_A_PRIMITIVE, worker);
				}
			} else
				worker.set();
			return worker;
		}
	};
		
	private static final Object[] NULL_SENT = new Object[1];
	/**
	 * Callback, but send the parm as IDs so that proxies
	 * will be created for the objects and can be referenced.
	 *
	 * This should be used even if there is only one parm.
	 */
	public Object callbackWithParms(int callbackID, int msgID, Object[] parms) throws CommandException {
		try {
			if (parms == null)
				parms = NULL_SENT;
				
			Commands.ValueObject v = new Commands.ValueObject();
							
			v.setArrayIDS(new Retriever(parms), parms.length, Commands.OBJECT_CLASS);
			Commands.sendCallbackCommand(out, callbackID, msgID);
			Commands.writeValue(out, v, false);
			out.flush();
		} catch (CommandException e) {
			throw e;
		} catch (Exception e) {
			throw new UnexpectedExceptionCommandException(false, e);
		}
		return run();	// Now run and wait for return.
	}	
	
	/**
	 * A closeHandler has been requested. This is called when
	 * not waiting within a loop, so we need to do the cleanup ourselves.
	 */
	public void closeHandler() {
		if (isConnected()) {
			try {
				Commands.sendQuitCommand(out);
			} catch (IOException e) {
			} finally {
				if (in != null)
					try {
						in.close();
					} catch (Exception e) {
					}
				if (out != null)
					try {
						out.close();
					} catch (Exception e) {
					}
				close();
				in = null;
				out = null;
				socket = null;
			}
		}
	}
			
}

Back to the top