]> code.citadel.org Git - citadel.git/blob - shaggy/net.java
* 'Enable system auth' site config option has been renamed
[citadel.git] / shaggy / net.java
1
2 import javax.swing.*;
3 import java.io.*;
4 import java.net.*;
5 import java.util.*;
6
7 public class net implements Runnable {
8     Queue               theQueue;
9
10     Socket              theSocket;
11     DataInputStream     in;
12     DataOutputStream    out;
13
14     boolean             done;
15
16     public net() {
17         theQueue = new Queue();
18     }
19
20     private void println( String s ) {
21         System.out.println( ">" + s );
22         try {
23             if( theSocket != null )
24                 out.writeBytes( s + "\n" );
25         } catch( IOException ioe ) {
26             citadel.me.lostNetwork( "Connection dropped (write)" );
27             if( theSocket != null ) {
28                 try { theSocket.close(); }
29                 catch( Exception e ) {};
30             }
31             theSocket = null;
32         }
33     }
34
35     private String readLine( ) {
36         try {
37             if( theSocket != null ) {
38                 String s = in.readLine();
39                 System.out.println( "<" + s );
40                 return s;
41             }
42         } catch( IOException ioe ) {
43             citadel.me.lostNetwork( "Network error: read" );
44             if( theSocket != null ) {
45                 try { theSocket.close(); }
46                 catch( Exception e ) {};
47             }
48             theSocket = null;
49         }
50         return null;
51     }
52
53     public String getArch() {
54         try {
55             Properties  p = System.getProperties();
56             return p.get( "os.name" ) + "/" + p.get( "os.arch" );
57         } catch( SecurityException se ) {
58             return "<unknown>";
59         }
60     }
61
62     public String getHostName() {
63         try {
64             InetAddress me = InetAddress.getLocalHost();
65             return me.getHostName();
66         } catch( Exception e ) {
67             return "dunno";
68         }
69     }
70
71     public void run() {
72         String  server = citadel.me.server;
73         int             port = citadel.me.port;
74         boolean         proxy = false;
75
76         done = false;
77         try {
78             if( proxy )
79                 proxy = !server.equals( "127.0.0.1" );
80           
81             if( proxy )
82                 theSocket = new Socket( "armstrong.cac.psu.edu", 13579 );
83             else
84                 theSocket = new Socket( server, port );
85             
86
87             in = new DataInputStream( theSocket.getInputStream() );
88             out = new DataOutputStream( theSocket.getOutputStream() );
89         } catch( IOException ioe ) {
90             citadel.me.lostNetwork( "Couldn't connect to server." );
91             return;
92         }
93         
94         if( proxy ) {
95             println( server );
96             println( ""+port );
97         }
98
99         citReply        rep = getReply();
100         if( !rep.ok() ) {
101             citadel.me.lostNetwork( "Couldn't connect: " + rep.line );
102             return;
103         }
104
105         getReply( "IDEN 0|7|" + citadel.VERSION + "|" + citadel.NAME + " " +
106                   citadel.VERSION + " (" + getArch() + ")|" + getHostName() );
107
108         Thread  t = new Thread() {
109             public void run() {
110                 while( !citadel.me.theNet.done ) {
111                     try {
112                         Thread.sleep( 30000 );
113                     } catch( Exception e ) {}
114
115                     System.out.println( "Idle event" );
116                     if( citadel.me.wo == null ) {
117                       if( theQueue.empty() )
118                         citadel.me.networkEvent( "NOOP" );
119                       else
120                         System.out.println( "...events pending" );
121                     }
122                     else
123                         citadel.me.wo.refresh();
124                 }
125             } };
126
127         t.start();
128
129         MsgCmd  m;
130         while( ((m = (MsgCmd)theQueue.get()) != null) && !done ) {
131             citReply    r = getReply( m.cmd, m.data );
132             m.setReply( r );
133             SwingUtilities.invokeLater( m );
134         }
135
136         try {
137         if( theSocket != null )
138             theSocket.close();
139         } catch( IOException ioe ) {
140         }
141         if( !done )
142             citadel.me.lostNetwork( "Connection closed." );
143
144         try {
145             t.stop();
146         } catch( Exception e ) {}
147     }
148
149     public void done() {
150         done = true;
151         theQueue.append( null );
152     }
153
154     public void append( MsgCmd m ) {
155         theQueue.append( m );
156     }
157
158     public citReply getReply() {
159         return getReply( (String)null, (String)null );
160     }
161
162     public citReply getReply( String cmd ) {
163         return getReply( cmd, (String)null );
164     }
165
166     public citReply getReply( String cmd, String data ) {
167         if( cmd != null ) println( cmd );
168
169         citReply r = new citReply( readLine() );
170         if( r.listingFollows() ) {
171             while( r.addData( readLine() ) ) ;
172         }
173
174         if( r.sendListing() ) {
175             if( data != null )
176                 println( data );
177             println( "000" );
178         }
179
180         if( r.expressMessage() )
181             citadel.me.expressMsg();
182
183         return r;
184     }
185 }