]> andersk Git - moira.git/blame - clients/regapplet/regapplet/Worker.java
The java reg client
[moira.git] / clients / regapplet / regapplet / Worker.java
CommitLineData
1d3501d2 1package regapplet;
2
3import java.awt.*;
4import java.net.*;
5import java.io.*;
6import java.util.Properties;
7import mit.cipher.*;
8
9public class Worker implements Runnable {
10 public static final int SENDNAME = 0;
11 public static final int SENDWORDS = 1;
12 public static final int SENDLOGIN = 2;
13 public static final int SENDPASSWD = 3;
14 public static final int INIT = 4;
15 public static final int DIALOG = 5;
16 public static final int INIT1 = 6;
17 int oldstate = INIT;
18 int state = INIT;
19 int nextstate = INIT;
20 Socket sock = null;
21 Sender send;
22 Parser parse;
23 public Regapplet applet;
24 public Worker(Regapplet tapplet) {
25 applet = tapplet;
26 }
27 public void run () {
28 try {
29 switch (state) {
30 case DIALOG:
31 state = nextstate;
32 switch (state) {
33 case SENDWORDS:
34 applet.showWordsDiag();
35 break;
36 case SENDNAME:
37 applet.showNameDiag();
38 break;
39 case SENDLOGIN:
40 applet.showLoginDiag();
41 break;
42 case SENDPASSWD:
43 applet.showPassDiag();
44 break;
45 case INIT:
46 applet.worker.close();
47 applet.workthread = null;
48 applet.worker = null;
49 if (applet.isStandalone) {
50 System.exit(0);
51 }
52 applet.showInit();
53 break;
54 }
55 return;
56 case INIT:
57 if (Regapplet.doRules) {
58 state = INIT1;
59 applet.showRules();
60 // Hmm. the lines below caused problems on the Mac
61 // Thread z = new Thread(new Seeder()); // Generate numbers in thread
62 // z.start();
63 return;
64 }
65 // Fall through
66 case INIT1:
67 applet.showMessage("Connecting to Server...");
68 try {
69 String host;
70 if (applet.isStandalone) {
71 host = (System.getProperties()).getProperty("host", "");
72 if (host == null || host.equals("")) {
73 System.err.println("Cannot learn host name (application) using localhost");
74 host = "localhost";
75 }
76 } else {
77 URL cb = applet.getCodeBase();
78 String protocol = cb.getProtocol();
79 if (protocol.equals("file") || protocol.equals("FILE")) {
80 System.err.println("Applet: FILE protocol in use, connecting to localhost");
81 host = "localhost";
82 } else {
83 host = cb.getHost();
84 }
85 }
86 sock = new Socket(host, 9001);
87 } catch (Exception e) {
88 e.printStackTrace();
89 applet.showError1(true);
90 applet.show();
91 state = DIALOG;
92 nextstate = INIT;
93 return;
94 // try {
95 // Thread.sleep(5000);
96 // } catch (java.lang.InterruptedException ae) {
97 // // Ignore exception for now
98 // }
99 // applet.showInit();
100 // return;
101 }
102 send = new Sender(sock);
103 parse = new Parser(sock, this);
104 parse.encap = send.encap; // Mumble, they need to share
105 // this
106 break; // We now expect a GETN after connect
107 // applet.showNameDiag();
108 // state = SENDNAME;
109 // return;
110 case SENDNAME:
111 applet.showMessage("Please wait...");
112 send.SendNameData(applet.FirstName.getText(),
113 applet.MiddleName.getText(),
114 applet.LastName.getText(),
115 applet.MITID.getText());
116 break;
117 case SENDWORDS:
118 applet.showMessage("Please wait...");
119 send.SendWords(applet.Word1.getText(),
120 applet.Word2.getText(),
121 applet.Word3.getText(),
122 applet.Word4.getText(),
123 applet.Word5.getText(),
124 applet.Word6.getText());
125 break;
126 case SENDLOGIN:
127 applet.showMessage("Please wait...");
128 send.SendLogin(applet.LoginName.getText());
129 break;
130 case SENDPASSWD:
131 if (!(applet.Password1.getText().equals(applet.Password2.getText()))) {
132 applet.Password1.setText("");
133 applet.Password2.setText("");
134 applet.showMessage("Entered Passwords do not match, try again");
135 try {
136 Thread.sleep(3000);
137 } catch (java.lang.InterruptedException e) {
138 }
139 applet.showPassDiag();
140 return;
141 }
142 applet.showMessage("Please wait...");
143 send.SendPassword(applet.Password1.getText());
144 break;
145 }
146 parse.read();
147 // Add code to parse results and switch on new state
148 } catch (IOException e) {
149 e.printStackTrace();
150 applet.showError(true);
151 state = DIALOG;
152 nextstate = INIT;
153 // Moby reset applet
154 } catch (MITCipherException e) {
155 e.printStackTrace();
156 // Moby reset applet here too.
157 } catch (RegAppletException a) {
158 a.printStackTrace();
159 applet.showError(true);
160 state = DIALOG;
161 nextstate = INIT;
162 }
163 }
164 public void setState(int i) {
165 state = i;
166 }
167 public void setState(int i, int j) {
168 state = i;
169 nextstate = j;
170 }
171 public void close() {
172 if (sock != null) {
173 try {
174 sock.close();
175 } catch (Exception e) {
176 }
177 sock = null;
178 }
179 }
180 public void finalize() {
181 if (sock != null) {
182 System.err.println("Worker Finalized with open socket! Closing it.");
183 close();
184 }
185 }
186}
This page took 0.061744 seconds and 5 git commands to generate.