]> andersk Git - moira.git/blob - clients/regapplet/regapplet/Worker.java
f3aded9d29534b683b64057de3765a67490e7b3b
[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   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       applet.showError(true);
156       state = DIALOG;
157       nextstate = INIT;
158       e.printStackTrace();
159       // Moby reset applet here too.
160     } catch (RegAppletException a) {
161       a.printStackTrace();
162       applet.showError(true);
163       state = DIALOG;
164       nextstate = INIT;
165     }
166   }
167   public void setState(int i) {
168          state = i;
169   }
170   public void setState(int i, int j) {
171          state = i;
172          nextstate = j;
173   }
174   public void close() {
175          if (sock != null) {
176             try {
177                 sock.close();
178             } catch (Exception e) {
179             }
180             sock = null;
181          }
182   }
183   public void finalize() {
184     if (sock != null) {
185       System.err.println("Worker Finalized with open socket! Closing it.");
186       close();
187     }
188   }
189 }
This page took 0.040226 seconds and 3 git commands to generate.