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