OSDN Git Service

Ver.1.4.4: Add auto-start of applet at failing http watch. Modify http-get form....
[opengate/opengate.git] / opengate / javahtml / Opengate.java
1 /**************************************************/\r
2 /* opengate client                                */\r
3 /**************************************************\r
4 opengate client\r
5  java applet downloaded to client machine \r
6 \r
7 Copyright (C) 1999-2002 Opengate Project Team\r
8 \r
9 This program is free software; you can redistribute it and/or\r
10 modify it under the terms of the GNU General Public License\r
11 as published by the Free Software Foundation; either version 2\r
12 of the License, or (at your option) any later version.\r
13 \r
14 This program is distributed in the hope that it will be useful,\r
15 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
17 GNU General Public License for more details.\r
18 \r
19 You should have received a copy of the GNU General Public License\r
20 along with this program; if not, write to the Free Software\r
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,\r
22 USA.\r
23 \r
24 Email: watanaby@is.saga-u.ac.jp\r
25 \r
26 Written by Yoshiaki WATANABE, Saga University\r
27 Modified by Shin-ichi TADAKI, Saga University\r
28 **************************************************/\r
29 import java.awt.*;\r
30 import java.applet.*;\r
31 import java.net.*;\r
32 import java.io.*;\r
33  \r
34 /** Wrapper to make Applet **/\r
35 public class Opengate extends Applet{\r
36 \r
37   String hostname;\r
38   int port;\r
39   String userid;\r
40   String sessionId;\r
41   String language;\r
42   int intlang=0;/* if language is japanese, set 1*/\r
43   OpengateClient client;\r
44   Label headlabel;\r
45   Label userlabel;\r
46   Label msglabel;\r
47 \r
48    /* Applet initialize */\r
49   public void init(){\r
50 \r
51      /* Get Applet Parameters */\r
52     port=Integer.parseInt(getParameter("port"));\r
53     userid=getParameter("user");\r
54     sessionId=getParameter("sessionid");\r
55     hostname =getCodeBase().getHost();\r
56     language = getParameter("lang");\r
57     if(language.equals("ja")) intlang=1;\r
58     if(language.equals("en")) intlang=2;\r
59 \r
60      /* Panel Layout */\r
61     setLayout(new FlowLayout(FlowLayout.CENTER));\r
62     setBackground(Color.yellow);\r
63  \r
64       /* Make Label */\r
65     if(intlang ==1){\r
66       add(headlabel=new Label("\83l\83b\83g\83\8f\81[\83N\97\98\97p\8b\96\89Â"));\r
67       add(userlabel=new Label("\83\86\81[\83U\96¼="+userid));\r
68       add(msglabel=new Label("Java Applet\82ª\8bN\93®\82µ\82Ü\82µ\82½ \81@\81@\81@    \81@\81@\81@\81@\81@ "));\r
69     } else {\r
70       add(headlabel=new Label("Authenticated for network use"));\r
71       add(userlabel=new Label("User name="+userid));\r
72       add(msglabel=new Label("Java Applet is running                       "));\r
73     }\r
74 \r
75      /* Connect to Server */\r
76     client=new OpengateClient(hostname, port, userid, sessionId, intlang, this);\r
77     client.start();\r
78   }\r
79   public void msgout(String msg){\r
80     /*    byte[] code = msg.getBytes("EUCJIS");\r
81     String msgj=new String(code,"EUCJIS");\r
82     */\r
83         /* padding space char to required length */\r
84         for(int i=msg.length();i<40;i++) msg+=" ";\r
85     msglabel.setText(msg);\r
86     repaint();\r
87   }\r
88   public void stop(){\r
89     try{\r
90       client.join();\r
91     }catch(InterruptedException e){\r
92       msgout("Error at Client Join            ");\r
93     }\r
94   }\r
95 }\r
96 \r
97 /**  Class for Client to communicate with host **/\r
98 class OpengateClient extends Thread{\r
99   int port;                  /* opengate port */\r
100   String hostname;           /* java download host name */\r
101   Socket socket;             /* Socket for server */\r
102   String userid;\r
103   String sessionId;\r
104   PrintStream outStream;     /* output stream to server */\r
105   BufferedReader inStream;   /* input stream from server */\r
106   String currentReply;       /* reserve last reply from server */\r
107   boolean onLine;            /* Client is connected to server or not */\r
108   Opengate main;             /* main class */\r
109   int intlang;\r
110 \r
111   /* Instance Creater */\r
112   public OpengateClient(String hostname, int port, \r
113                         String userid, String sessionId, int intlang, Opengate main){\r
114     this.hostname=hostname;\r
115     this.port=port;\r
116     this.userid=userid;\r
117     this.sessionId=sessionId;\r
118     this.main=main;\r
119     this.intlang=intlang;\r
120     onLine=false;\r
121   }\r
122 \r
123   /* connect to server */\r
124   boolean connect(){\r
125     try{\r
126       if(intlang==1){\r
127         main.msgout("\83z\83X\83g"+hostname+":"+Integer.toString(port)+"\82É\90Ú\91±\92\86\82Å\82·");\r
128       } else {\r
129         main.msgout("Connecting to host "+hostname+":"+Integer.toString(port));\r
130       }\r
131       socket=new Socket(hostname, port);\r
132       outStream=new PrintStream(socket.getOutputStream());\r
133       inStream=new BufferedReader(\r
134                    new InputStreamReader(\r
135                        socket.getInputStream()));\r
136     }catch(UnknownHostException e){\r
137       if(intlang==1)\r
138         main.msgout("SocketConnect\92\86\82ÉUnknownHost\97á\8aO\82ª\94­\90\82µ\82Ü\82µ\82½");\r
139       else \r
140         main.msgout("UnknownHost exception in SocketConnect");\r
141       return false;\r
142     }catch(IOException e){\r
143       if(intlang==1)\r
144         main.msgout("SocketConnect\92\86\82ÉI/O\97á\8aO\82ª\94­\90\82µ\82Ü\82µ\82½");\r
145       else\r
146         main.msgout("I/O exception in SocketConnect");\r
147       return false;\r
148     }\r
149     onLine=true;\r
150     return true;\r
151   }\r
152 \r
153   /* disconnect from server */\r
154   void disconnect(){\r
155     if(onLine){\r
156       putLine("quit");\r
157     }\r
158     if(socket!=null){\r
159       try{\r
160         socket.close();\r
161       }catch(IOException e){\r
162         if(intlang==1)\r
163           main.msgout("SocketClose\92\86\82ÉI/O\97á\8aO\82ª\94­\90\82µ\82Ü\82µ\82½");\r
164         else\r
165           main.msgout("I/O exception in SocketClose");\r
166       }\r
167     }\r
168   }\r
169 \r
170   /* put out one line to server */\r
171   void putLine(String string){\r
172     outStream.print(string+"\r\n");\r
173   }\r
174 \r
175   /* get one line from server */\r
176   String getLine(){\r
177     try{\r
178       currentReply=inStream.readLine();\r
179     }catch(IOException e){\r
180       if(intlang==1)\r
181         main.msgout("SocketRead\92\86\82ÉI/O\97á\8aO\82ª\94­\90\82µ\82Ü\82µ\82½");\r
182       else\r
183         main.msgout("I/O exception in SocketRead");\r
184       return "";\r
185     }\r
186     return currentReply;\r
187   }\r
188 \r
189   /* called back from client.start */\r
190   public void run(){\r
191     int count=0;\r
192     java.text.SimpleDateFormat formatter \r
193               =new java.text.SimpleDateFormat("H:mm") ;\r
194     if(connect()==false){\r
195       disconnect();\r
196       return;\r
197     }\r
198     putLine(userid+"-"+sessionId);\r
199     if(getLine().indexOf("accept")==-1){\r
200       if(intlang==1)\r
201         main.msgout("\83T\81[\83o\82©\82çAccept\82ª\91\97\82ç\82ê\82Ä\97\88\82Ü\82¹\82ñ");\r
202       else \r
203         main.msgout("Not receive Accept from the server");\r
204       disconnect();\r
205       return;\r
206     }\r
207     if(intlang==1)\r
208       main.msgout("\90³\8fí\82É\8aJ\8en\82µ\82Ü\82µ\82½");\r
209     else\r
210       main.msgout("Start Normally");\r
211 \r
212     while(getLine().indexOf("hello")!=-1){\r
213       putLine("hello");\r
214       if(intlang==1){\r
215         main.msgout("\90Ú\91±\8am\94F="+formatter.format(new java.util.Date()));\r
216       } else {\r
217         main.msgout("Confirm connection="+formatter.format(new java.util.Date()));  \r
218       }\r
219     }\r
220     if(intlang==1)\r
221       main.msgout("\83l\83b\83g\83\8f\81[\83N\82Í\95Â\8d½\82³\82ê\82Ü\82µ\82½");\r
222     else \r
223       main.msgout("Connection closed");\r
224     return;\r
225   }                                          \r
226 }\r
227 \r