minimal changes to get the code to start up outside an applet environment
[rsa2client.git] / src / mcsClient / McsClient.java
1 // Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
2 // Jad home page: http://www.kpdus.com/jad.html
3 // Decompiler options: packimports(3) fieldsfirst nonlb space 
4
5 package mcsClient;
6
7 import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
8 import java.awt.*;
9 import java.io.*;
10 import java.net.URL;
11 import java.util.LinkedList;
12 import java.util.StringTokenizer;
13 import javax.swing.*;
14
15 // Referenced classes of package mcsClient:
16 //            McsTargetSize, McsVersion, LockingKeyStateManager, McsTcpConnection, 
17 //            LiveVideoPanel, McsKeyAdapter, McsMouseAdapter, McsMouseMotionAdapter, 
18 //            DatagramReader, McsToolBar, MessageReader, KeepAlive
19
20 public class McsClient extends JPanel/*Applet*/ {
21
22     public Container getContentPane() { return this; }
23
24     private String  _host;
25     private String  _userid;
26     private String  _ac;
27     private boolean _enc;
28     public McsClient(String host, String userid, String ac, boolean enc) {
29         this();
30         this._host = host;
31         this._userid = userid;
32         this._ac = ac;
33         this._enc = enc;
34     }
35     public String getParameter(String key) {
36         if (key.equals("bluescreen")) return "false";
37         if (key.equals("ac"))         return _ac;
38         if (key.equals("userid"))     return _userid;
39         if (key.equals("host"))       return _host;
40         if (key.equals("enc"))        return _enc ? "on" : "off";
41         throw new RuntimeException("unknown key \""+key+"\"");
42     }
43
44     static void go(String host, String user, String password) throws Exception {
45         JFrame jf = new JFrame();
46         McsClient mcs = new McsClient(host, user, password, false);
47         jf.add(mcs);
48         jf.setSize(1200,1000);
49         jf.pack();
50         jf.show();
51         jf.setSize(1200,1000);
52         mcs.init();
53         jf.repaint(1000);
54     }
55
56
57     // Changes appear above this line //////////////////////////////////////////////////////////////////////////////
58
59     private static final int FIXED_SIZE = 1;
60     private static final int FULL_SCREEN = 2;
61     private static final int SCROLL_SCREEN = 3;
62     private static final int DEFAULT_REMOTE_PORT = 2000;
63     private static final Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
64     private static final Dimension maxTargetSize = new Dimension(1024, 768);
65     private static int screenWidth;
66     private static int screenHeight;
67     private McsTargetSize mcsTargetSize;
68     private LinkedList datagramList;
69     private McsVersion mcsVersion;
70     private LockingKeyStateManager lockingKeyStateManager;
71     private McsTcpConnection mcsTcpConnection;
72     private McsKeyAdapter mcsKeyAdapter;
73     private LiveVideoPanel liveVideoPanel;
74     private McsMouseAdapter mcsMouseAdapter;
75     private McsMouseMotionAdapter mcsMouseMotionAdapter;
76     private DatagramReader datagramReader;
77     private MessageReader messageReader;
78     private KeepAlive keepAlive;
79     private boolean isStandalone;
80     private ThreadGroup threadGroup;
81     private static final String START = "Show Remote Console";
82     private static final String STOP = "Hide Remote Console";
83     private JButton button;
84     private static final boolean TCP = false;
85     private boolean blueScreenDump;
86     private boolean encryptVideo;
87
88     private McsClient() {
89         mcsTargetSize = new McsTargetSize(1024, 768);
90         datagramList = new LinkedList();
91         mcsVersion = new McsVersion();
92         lockingKeyStateManager = new LockingKeyStateManager();
93         isStandalone = true;
94         blueScreenDump = false;
95         encryptVideo = false;
96     }
97
98     public void init() {
99         try {
100             UIManager.setLookAndFeel(new WindowsLookAndFeel());
101         }
102         catch (Exception exception) { }
103         System.out.println("MCS v.3.6 initialized");
104         isStandalone = false;
105         String s = getParameter("host");
106         /*
107         if (s == null)
108             s = getCodeBase().getHost();
109         */
110         String s1 = s;
111         String s2 = getParameter("bluescreen");
112         if (s2 != null && s2.equalsIgnoreCase("true"))
113             blueScreenDump = true;
114         String s3 = getParameter("userid");
115         if (s3 == null)
116             s3 = "";
117         String s4 = s3;
118         String s5 = getParameter("enc");
119         if (s5 != null && s5.equalsIgnoreCase("on"))
120             encryptVideo = true;
121         String s6 = getParameter("ac");
122         if (s6 == null)
123             s6 = "";
124         String s7 = s6;
125         getContentPane().setBackground(Color.WHITE);
126         begin(s1, s4, s7, false);
127     }
128
129     public void stop() {
130         System.out.println("Stopping applet");
131         if (threadGroup != null)
132             threadGroup.interrupt();
133         if (mcsTcpConnection != null) {
134             mcsTcpConnection.close();
135             mcsTcpConnection = null;
136         }
137     }
138
139     public void begin(String s, String s1, String s2, boolean flag) {
140         int i = 2000;
141         StringTokenizer stringtokenizer = new StringTokenizer(s, ":");
142         String s3 = stringtokenizer.nextToken();
143         if (stringtokenizer.hasMoreTokens())
144             try {
145                 i = Integer.parseInt(stringtokenizer.nextToken());
146             }
147             catch (NumberFormatException numberformatexception) {
148                 i = 2000;
149             }
150         mcsTcpConnection = new McsTcpConnection(s3, i, encryptVideo, s2);
151         if (!mcsTcpConnection.connected()) {
152             if (isStandalone)
153                 System.exit(0);
154             stop();
155             return;
156         }
157         sendUsernameToServer(s1, s2);
158         liveVideoPanel = new LiveVideoPanel(mcsTcpConnection, datagramList, mcsTargetSize, lockingKeyStateManager);
159         mcsKeyAdapter = new McsKeyAdapter(mcsTcpConnection, lockingKeyStateManager);
160         mcsMouseAdapter = new McsMouseAdapter(mcsTcpConnection, liveVideoPanel);
161         mcsMouseMotionAdapter = new McsMouseMotionAdapter(mcsTcpConnection, mcsKeyAdapter);
162         if (!blueScreenDump) {
163             liveVideoPanel.addMouseListener(mcsMouseAdapter);
164             liveVideoPanel.addMouseMotionListener(mcsMouseMotionAdapter);
165             liveVideoPanel.addKeyListener(mcsKeyAdapter);
166             liveVideoPanel.addMouseWheelListener(mcsMouseAdapter);
167         }
168         liveVideoPanel.setPreferredSize(maxTargetSize);
169         liveVideoPanel.setFocusTraversalKeysEnabled(false);
170         datagramReader = new DatagramReader(mcsTcpConnection, liveVideoPanel, datagramList, s3, i, flag);
171         if (!blueScreenDump) {
172             McsToolBar mcstoolbar = new McsToolBar(mcsKeyAdapter, liveVideoPanel);
173             mcstoolbar.setFloatable(false);
174             mcstoolbar.setVisible(false);
175             McsToolBar mcstoolbar1 = new McsToolBar(s3, mcsKeyAdapter, liveVideoPanel, datagramReader, mcstoolbar);
176             getContentPane().add(mcstoolbar1, "North");
177             getContentPane().add(mcstoolbar, "West");
178         }
179         getContentPane().add(liveVideoPanel);
180         liveVideoPanel.requestFocusInWindow();
181         if (!datagramReader.initialized()) {
182             if (isStandalone)
183                 System.exit(0);
184             stop();
185             return;
186         }
187         messageReader = new MessageReader(mcsTcpConnection, mcsVersion, lockingKeyStateManager);
188         keepAlive = new KeepAlive(mcsTcpConnection, blueScreenDump);
189         threadGroup = new ThreadGroup("MCS Threads");
190         threadGroup.setDaemon(true);
191         (new Thread(threadGroup, liveVideoPanel, "liveVideoPanel thread")).start();
192         Thread thread = new Thread(threadGroup, datagramReader, "datagramReader thread");
193         thread.setPriority(thread.getPriority() + 2);
194         thread.start();
195         Thread thread1 = new Thread(threadGroup, messageReader, "messageReader thread");
196         if (flag)
197             thread1.setPriority(thread1.getPriority() + 2);
198         thread1.start();
199         Thread thread2 = new Thread(threadGroup, keepAlive, "keepAlive thread");
200         thread2.setPriority(thread2.getPriority() + 2);
201         thread2.start();
202         sendVersionToServer();
203         System.out.println("Command KVM Server to start sending images");
204         sendStartSendingToServer(mcsTcpConnection.getDetectedPPP());
205     }
206
207     private void sendUsernameToServer(String s, String s1) {
208         if (s1 == null)
209             s1 = "";
210         char ac[] = (s + ":" + s1).toCharArray();
211         int ai[] = new int[2 + ac.length];
212         ai[0] = ai.length * 4;
213         ai[1] = 11;
214         for (int i = 0; i < ac.length; i++)
215             ai[2 + i] = ac[i];
216
217         try {
218             mcsTcpConnection.sendMessage(ai);
219         }
220         catch (IOException ioexception) {
221             System.err.println("Could not send user name.");
222             stop();
223         }
224     }
225
226     private void sendVersionToServer() {
227         int ai[] = new int[4];
228         ai[0] = ai.length * 4;
229         ai[1] = 13;
230         ai[2] = mcsVersion.getClientMajorRev();
231         ai[3] = mcsVersion.getClientMinorRev();
232         try {
233             mcsTcpConnection.sendMessage(ai);
234         }
235         catch (IOException ioexception) {
236             System.err.println("Could not send version.");
237             stop();
238         }
239     }
240
241     private void sendStartSendingToServer(boolean flag) {
242         int ai[] = new int[3];
243         ai[0] = ai.length * 4;
244         if (blueScreenDump)
245             ai[1] = 24;
246         else
247         if (!flag)
248             ai[1] = 0;
249         else
250             ai[1] = 1;
251         ai[2] = 0;
252         try {
253             mcsTcpConnection.sendMessage(ai);
254         }
255         catch (IOException ioexception) {
256             System.err.println("Could not send start sending to server.");
257             stop();
258         }
259     }
260
261     public boolean isVideoEncrypted() {
262         return encryptVideo;
263     }
264
265 }