| 1 package com.quikj.application.web.echo.applet; 2 3 import com.quikj.client.framework.*; 4 import java.applet.*; 5 import java.awt.*; 6 import java.awt.event.*; 7 import java.net.*; 8 9 public class EchoApplet extends Applet 10 implements HTTPMessageListenerInterface, HTTPConnectionClosedInterface 11 { 12 public void init() 13 { 14 super.init(); 15 16 codeBase = getCodeBase(); 18 19 if (codeBase.getPort() >= 0) { 21 port = codeBase.getPort(); 22 } 23 24 String parm = getParameter("port"); 25 if (parm != null) 26 { 27 try 28 { 29 port = Integer.parseInt(parm); 30 } 31 catch (NumberFormatException ex) 32 { 33 ; 34 } 35 } 36 37 parm = getParameter("plugin"); 38 if (parm != null) 39 { 40 try 41 { 42 applicationId = Integer.parseInt(parm); 43 } 44 catch (NumberFormatException ex) 45 { 46 ; 47 } 48 } 49 50 51 initScreen(); 53 } 54 55 public void start() 56 { 57 if (com != null) { 59 disconnect(); 60 } 61 62 connect(); 63 } 64 65 public void stop() 66 { 67 disconnect(); 68 } 69 70 public void destroy() 71 { 72 super.destroy(); 73 74 disconnect(); 75 } 76 77 private void disconnect() 78 { 79 if (com.isConnected() == true) 80 { 81 com.disconnect(); 82 } 83 84 sendTextButton.setEnabled(false); 85 textField.setEnabled(false); 86 statusBar.setText("Disconnected"); 87 } 88 89 private void connect() 90 { 91 92 try 94 { 95 com = new ServerCommunications(codeBase.getHost(), port, applicationId); 96 } 97 catch (UnknownHostException ex) 98 { 99 System.out.println("Unknown host " + codeBase.getHost()); 100 return; 101 } 102 103 statusBar.setText("Connecting to " 104 + com.getHost() 105 + ':' 106 + com.getPort() 107 + " ....."); 108 if (com.connect() == false) 109 { 110 statusBar.setText("Connection to server " 111 + com.getHost() 112 + ':' 113 + com.getPort() 114 + " failed"); 115 return; 116 } 117 statusBar.setText("Connected to " 118 + com.getHost() 119 + ':' 120 + com.getPort()); 121 com.setClosedListener(this); 122 123 com.start(); 125 126 sendTextButton.setEnabled(true); 127 textField.setEnabled(true);; 128 129 } 130 131 private void initScreen() 132 { 133 GridBagLayout gbl = new GridBagLayout(); 134 GridBagConstraints gbc = new GridBagConstraints(); 135 setLayout(gbl); 136 137 addToContainer(this, 138 gbl, 139 gbc, 140 new Label("Echo text"), 141 0, 142 0, 143 1, 144 1, 145 GridBagConstraints.NONE, 146 GridBagConstraints.NORTHWEST, 147 0, 0); 148 149 echoField = new TextArea(10, 80); 150 echoField.setEditable(false); 151 addToContainer(this, 152 gbl, 153 gbc, 154 echoField, 155 0, 156 1, 157 1, 158 1, 159 GridBagConstraints.NONE, 160 GridBagConstraints.NORTHWEST, 161 0, 0); 162 163 addToContainer(this, 164 gbl, 165 gbc, 166 new Label("Enter text"), 167 0, 168 2, 169 1, 170 1, 171 GridBagConstraints.NONE, 172 GridBagConstraints.NORTHWEST, 173 0, 0); 174 175 textField = new TextField(80); 176 addToContainer(this, 177 gbl, 178 gbc, 179 textField, 180 0, 181 3, 182 1, 183 1, 184 GridBagConstraints.NONE, 185 GridBagConstraints.NORTHWEST, 186 0, 0); 187 188 sendTextButton = new Button(" Send "); 189 sendTextButton.addActionListener(new SendListener()); 190 addToContainer(this, 191 gbl, 192 gbc, 193 sendTextButton, 194 0, 195 4, 196 1, 197 1, 198 GridBagConstraints.NONE, 199 GridBagConstraints.NORTHWEST, 200 0, 0); 201 202 statusBar = new TextField("not connected", 80); 203 statusBar.setEditable(false); 204 addToContainer(this, 205 gbl, 206 gbc, 207 statusBar, 208 0, 209 5, 210 1, 211 1, 212 GridBagConstraints.NONE, 213 GridBagConstraints.NORTHWEST, 214 0, 0); 215 216 sendTextButton.setEnabled(false); 217 textField.setEnabled(false); 218 } 219 220 221 private void addToContainer(Container cont, 222 GridBagLayout gbl, 223 GridBagConstraints gbc, 224 Component comp, 225 int gridx, 226 int gridy, 227 int gridwidth, 228 int gridheight, 229 int fill, 230 int anchor, 231 int weightx, 232 int weighty, 233 Insets insets) 234 { 235 gbc.gridx = gridx; 236 gbc.gridy = gridy; 237 gbc.gridwidth = gridwidth; 238 gbc.gridheight = gridheight; 239 gbc.fill = fill; 240 gbc.anchor = anchor; 241 gbc.weightx = weightx; 242 gbc.weighty = weighty; 243 244 if (insets != null) 245 { 246 gbc.insets = insets; 247 } 248 249 gbl.setConstraints(comp, gbc); 250 cont.add(comp); 251 } 252 253 private void addToContainer(Container cont, 254 GridBagLayout gbl, 255 GridBagConstraints gbc, 256 Component comp, 257 int gridx, 258 int gridy, 259 int gridwidth, 260 int gridheight, 261 int fill, 262 int anchor, 263 int weightx, 264 int weighty) 265 { 266 addToContainer(cont, gbl, gbc, comp, gridx, gridy, gridwidth, gridheight, 267 fill, anchor, weightx, weighty, null); 268 } 269 270 public void messageReceived(int req_id, int status, String content_type, int http_status, String reason, String message) 271 { 272 if (status == HTTPMessageListenerInterface.TIMEOUT) 273 { 274 statusBar.setText("Timeout receiving response"); 275 } 276 else 277 { 278 if (message != null) 279 { 280 echoField.append(message + '\n'); 281 } 282 } 283 } 284 285 public void connectionClosed(String host, int port, int appl_id) 286 { 287 statusBar.setText(host + ':' + port 288 + " closed connection"); 289 } 290 291 class SendListener implements ActionListener 292 { 293 public void actionPerformed(ActionEvent e) 294 { 295 296 if (com.isConnected() == true) 297 { 298 String text = textField.getText(); 299 textField.setText(""); 300 if (text.length() > 0) 301 { 302 int req_id = com.sendRequestMessage("text/plain", 303 text, 304 EchoApplet.this, 305 10000, 306 false); 307 if (req_id == -1) 308 { 309 statusBar.setText("Error sending message to server : " 310 + com.getErrorNumber()); 311 } 312 } 313 } 314 315 } 316 } 317 318 private Button sendTextButton; 319 private TextArea echoField; 320 private TextField textField; 321 private TextField statusBar; 322 323 private int port = 80; 324 private int applicationId = 1; 325 private URL codeBase; 326 private ServerCommunications com; 327 } 328 329 330 331 332 333 334 335 336 337 338 339 340 | Popular Tags |