001: ///////////////////////////////////////////////////////////////////////////////
002: //
003: // Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
004: //
005: // All Rights Reserved
006: //
007: // This program is free software; you can redistribute it and/or modify
008: // it under the terms of the GNU General Public License and GNU Library
009: // General Public License as published by the Free Software Foundation;
010: // either version 2, or (at your option) any later version.
011: //
012: // This program is distributed in the hope that it will be useful,
013: // but WITHOUT ANY WARRANTY; without even the implied warranty of
014: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: // GNU General Public License and GNU Library General Public License
016: // for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // and GNU Library General Public License along with this program; if
020: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
021: // MA 02139, USA.
022: //
023: ///////////////////////////////////////////////////////////////////////////////
024:
025: package org.rdesktop.objects;
026:
027: import java.io.*;
028: import java.net.*;
029:
030: import org.rdesktop.server.rdp.*;
031: import org.rdesktop.server.rdp.rdp5.*;
032: import org.rdesktop.server.rdp.crypto.*;
033: import org.rdesktop.server.rdp.rdp5.cliprdr.*;
034:
035: public class RdpDesktopExchangeDbImpl extends StreamExchangeDbImpl
036: implements RdpDesktopExchange {
037: private transient Rdp5 m_rdpLayer;
038: private transient java.util.LinkedList m_data;
039:
040: public RdpDesktopExchangeDbImpl() {
041: }
042:
043: private Rdp5 getRdpLayer() {
044: for (int i = 0; (m_rdpLayer == null) && (i < 10); i++) {
045: try {
046: //System.out.println(" -- Desktop Exchange waiting on RDP initialization: " + i);
047:
048: Thread.sleep(1000);
049: } catch (java.lang.InterruptedException e) {
050: e.printStackTrace();
051: }
052: }
053:
054: if (m_rdpLayer == null) {
055: throw new org.myoodb.exception.PermissionException(
056: "Desktop Exchange timed out RDP initilization");
057: }
058:
059: return m_rdpLayer;
060: }
061:
062: private void addData(Object[] data) {
063: m_data.add(data);
064:
065: synchronized (m_data) {
066: m_data.notifyAll();
067: }
068: }
069:
070: private java.util.LinkedList getData() {
071: while ((m_rdpLayer != null) && (m_data.size() == 0)) {
072: synchronized (m_data) {
073: try {
074: m_data.wait(1000);
075: } catch (java.lang.InterruptedException e) {
076: e.printStackTrace();
077: }
078: }
079: }
080:
081: java.util.LinkedList queue = new java.util.LinkedList();
082:
083: synchronized (m_data) {
084: int totalSize = 0;
085:
086: while (m_data.size() != 0) {
087: Object[] data = (Object[]) m_data.removeFirst();
088: RdpPacket packet = (RdpPacket) data[0];
089: totalSize += packet.getSize();
090:
091: queue.add(data);
092:
093: if (totalSize > RdpClient.CLIENT_QUEUE_MAX_BUFFER_SIZE) {
094: break;
095: } else if (queue.size() > RdpClient.CLIENT_MAX_QUEUE_SIZE) {
096: break;
097: }
098: }
099: }
100:
101: return queue;
102: }
103:
104: public void connect(String host, int port, String username,
105: String password, int flags, String domain, int width,
106: int height, int bitsPerPixel) throws RdpDesktopException,
107: RdpConnectionException {
108: m_data = new java.util.LinkedList();
109:
110: // TODO: make this not global
111: if (RdpOptions.server_bpp != bitsPerPixel) {
112: RdpOptions.server_bpp = bitsPerPixel;
113: RdpOptions.Bpp = (RdpOptions.server_bpp + 7) / 8;
114: RdpOptions.bpp_mask = 0xFFFFFF >> 8 * (3 - RdpOptions.Bpp);
115: }
116:
117: // TODO: implement all relevant channels
118: /*
119: ClipChannel clipChannel = new ClipChannel();
120: // Initialise all RDP5 channels
121: if (Options.use_rdp5)
122: {
123: if (Options.map_clipboard)
124: {
125: channels.register(clipChannel);
126: }
127: }
128: */
129:
130: m_rdpLayer = new Rdp5(new VChannels());
131: m_rdpLayer.connect(host, port, username, password, flags,
132: domain, "", "", width, height, bitsPerPixel);
133:
134: Runnable drain = new Runnable() {
135: public void run() {
136: try {
137: int[] type = new int[1];
138:
139: while (m_rdpLayer != null) {
140: // TODO: chop up for performance ( DesktopApplet.MAX_BUFFER_SIZE )
141: RdpPacket packet = m_rdpLayer.receive(type);
142: int next_packet = m_rdpLayer.m_next_packet;
143: Object[] data = new Object[] { packet.copy(),
144: new Integer(type[0]),
145: new Integer(next_packet) };
146:
147: addData(data);
148: }
149: } catch (Exception e) {
150: e.printStackTrace();
151:
152: disconnect();
153: }
154: }
155: };
156:
157: Thread thread = new Thread(drain, "Receive Drain");
158: thread.setDaemon(true);
159: thread.start();
160: }
161:
162: public void disconnect() {
163: if (m_rdpLayer != null) {
164: Rdp5 rdpLayer = m_rdpLayer;
165: m_rdpLayer = null;
166: rdpLayer.disconnect();
167: }
168: }
169:
170: public Object[] receive(int type[]) throws RdpDesktopException,
171: IOException, CryptoException, RdpOrderException {
172: Rdp5 rdpLayer = getRdpLayer();
173: RdpPacket data = rdpLayer.receive(type);
174: int next_packet = rdpLayer.m_next_packet;
175: return new Object[] { data.copy(), new Integer(type[0]),
176: new Integer(next_packet) };
177: }
178:
179: public java.util.LinkedList receiveQueue()
180: throws RdpDesktopException, IOException, CryptoException,
181: RdpOrderException {
182: return getData();
183: }
184:
185: public void sendLogonInfo(int flags, String domain,
186: String username, String password, String command,
187: String directory) throws RdpDesktopException, IOException,
188: CryptoException {
189: Rdp5 rdpLayer = getRdpLayer();
190: rdpLayer.sendLogonInfo(flags, domain, username, password,
191: command, directory);
192: }
193:
194: public void sendConfirmActive(int rdp_shareid)
195: throws RdpDesktopException, IOException, CryptoException {
196: Rdp5 rdpLayer = getRdpLayer();
197: rdpLayer.sendConfirmActive(rdp_shareid);
198: }
199:
200: public void sendSynchronize() throws RdpDesktopException,
201: IOException, CryptoException {
202: Rdp5 rdpLayer = getRdpLayer();
203: rdpLayer.sendSynchronize();
204: }
205:
206: public void sendControl(int action) throws RdpDesktopException,
207: IOException, CryptoException {
208: Rdp5 rdpLayer = getRdpLayer();
209: rdpLayer.sendControl(action);
210: }
211:
212: public void sendInput(int time, int message_type, int device_flags,
213: int param1, int param2) {
214: Rdp5 rdpLayer = getRdpLayer();
215: rdpLayer.sendInput(time, message_type, device_flags, param1,
216: param2);
217: }
218:
219: public void sendInputQueue(java.util.LinkedList queue) {
220: Rdp5 rdpLayer = getRdpLayer();
221:
222: while (queue.size() != 0) {
223: Object[] tuple = (Object[]) queue.removeFirst();
224: int time = ((Integer) tuple[0]).intValue();
225: int message_type = ((Integer) tuple[1]).intValue();
226: int device_flags = ((Integer) tuple[2]).intValue();
227: int param1 = ((Integer) tuple[3]).intValue();
228: int param2 = ((Integer) tuple[4]).intValue();
229:
230: rdpLayer.sendInput(time, message_type, device_flags,
231: param1, param2);
232: }
233: }
234:
235: public void sendFonts(int seq) throws RdpDesktopException,
236: IOException, CryptoException {
237: Rdp5 rdpLayer = getRdpLayer();
238: rdpLayer.sendFonts(seq);
239: }
240: }
|