Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: FTreeP/FTPBase.java


1   package FTreeP;
2   
3   import java.io.*;
4   import java.net.*;
5   import java.util.*;
6   
7   import java.awt.*;
8   import java.awt.event.*;
9   
10  import javax.swing.*;
11  import javax.swing.tree.*;
12  import javax.swing.event.*;
13  
14  import ExitThread.*;
15  import DirectoryIndexer.*;
16  
17  public class FTPBase
18  {
19    public static String CRLF = "" + (char) 13 + (char) 10;
20    public static final boolean DEBUG = true;
21  
22    private static boolean master = false;
23    public static boolean gui = false;
24  
25    public static DirectoryIndexer directory_index = null;
26  
27    public static JTabbedPane tabbed_pane = null;
28  
29    private static FTPConsole gui_console = null;
30    private static JFrame frame = null;
31  
32    public static void main(String[] args)
33    {
34      try {
35  
36      if(args.length != 0) throw new Exception("Usage: java FTreeP");
37  
38      File f = new File("FTreeP/ftreep.cfg");
39  
40      BufferedReader fin = new BufferedReader(new FileReader(f));
41  
42      String master_server = "";
43      int master_port = 0;
44      int listen_port = 0;
45      String dir_cfg_file = "";
46  
47      while(fin.ready())
48      {
49        String line = fin.readLine();
50        StringTokenizer st = new StringTokenizer(line);
51        st.nextToken();
52        String value = st.nextToken();
53  
54        try {
55  
56          if(line.startsWith("#") | line.equals("")) continue;
57          else if(line.startsWith("master "))
58          {
59            if(value.equalsIgnoreCase("yes")) master = true;
60            else if(value.equalsIgnoreCase("no")) master = false;
61            else { throw new Exception(); }
62          }
63          else if(line.startsWith("master_server"))
64          {
65            master_server = value;
66          }
67          else if(line.startsWith("master_port"))
68          {
69            master_port = Integer.parseInt(value);
70          }
71          else if(line.startsWith("listen_port"))
72          {
73            listen_port = Integer.parseInt(value);
74          }
75          else if(line.startsWith("gui yes"))
76          {
77            gui = true;
78          }
79          else if(line.startsWith("directory_cfg_file"))
80          {
81            dir_cfg_file = value;
82          }
83  
84        } catch(Exception e)
85        {
86          e.printStackTrace();
87          throw new Exception("Error: Badly formed configuration file: '" + line + "'.");
88        }
89      }
90  
91      if(dir_cfg_file.equals("")) throw new Exception("Error: directory_cfg_file must be present in configuration file.");
92      else directory_index = new DirectoryIndexer(dir_cfg_file);
93  
94      if(gui) initGUI();
95  
96      new ExitThread().start();
97  
98      if(master == true)
99      {
100       system_println("Waiting for incoming FTP connections...\n");
101 
102       new FTPDirectoryIndexListener(master_port).start();
103 
104       ServerSocket ss = new ServerSocket(listen_port);
105 
106       while(true)
107       {
108         Socket command = ss.accept();
109         new FTPCommandRelay(command).start();
110       }
111     }
112     else
113     {
114       system_println("Waiting for incoming FTPCommandRelay connections...\n");
115 
116       {
117         system_print("Sending current Directory Index... ");
118         Socket dir_socket = new Socket(master_server, master_port);
119         BufferedWriter dir_out = new BufferedWriter(new OutputStreamWriter(dir_socket.getOutputStream()));
120         f = new File(dir_cfg_file);
121         fin = new BufferedReader(new FileReader(f));
122 
123         while(fin.ready())
124         {
125           String line = fin.readLine();
126           dir_out.write(line + "\n");
127           dir_out.flush();
128         }
129 
130         fin.close();
131         dir_out.close();
132         dir_socket.close();
133 
134         system_println("Done!");
135       }
136 
137       ServerSocket ss = new ServerSocket(listen_port);
138       system_println("Listening on port " + listen_port + "... ");
139 
140       while(true)
141       {
142         Socket command = ss.accept();
143         new FTPDataHost(command).start();
144       }
145     }
146 
147     } catch(Exception e) { e.printStackTrace(); }
148   }
149 
150   public static void initGUI()
151   {
152     gui_console = new FTPConsole();
153 
154     tabbed_pane = new JTabbedPane();
155 
156     tabbed_pane.addTab("FTreeP Console", gui_console);
157     tabbed_pane.setSelectedIndex(0);
158 
159     frame = new JFrame();
160 
161     frame.addWindowListener(
162       new WindowAdapter()
163       {
164         public void windowClosing(WindowEvent e)
165         {
166           try{
167 
168           System.exit(0);
169 
170           } catch(Exception ex) {}
171         }
172       }
173     );
174 
175     frame.getContentPane().setLayout(new BorderLayout());
176     frame.getContentPane().add(tabbed_pane);
177 
178     if(master) frame.setTitle("FTreeP - Command Relay");
179     else frame.setTitle("FTreeP - Data Host");
180 
181     frame.setSize(640, 480);
182     frame.setVisible(true);
183   }
184 
185   public static void system_print(String s) throws Exception
186   {
187     if(!gui) System.out.print(s);
188     else gui_console.print(s);
189   }
190 
191   public static void system_println(String s) throws Exception
192   {
193     if(!gui) System.out.println(s);
194     else gui_console.println(s);
195   }
196 }
197 
198 class FTPDirectoryIndexListener
199 extends Thread
200 {
201   private ServerSocket ss = null;
202 
203   public FTPDirectoryIndexListener(int master_port) throws Exception
204   {
205     ss = new ServerSocket(master_port);
206   }
207 
208   public void run()
209   {
210     try {
211 
212     while(true)
213     {
214       Socket s = ss.accept();
215       new FTPDirectoryIndexReceive(s).start();
216     }
217 
218     } catch(Exception e) { e.printStackTrace(); }
219   }
220 }
221 
222 class FTPDirectoryIndexReceive
223 extends Thread
224 {
225   private Socket dir_socket = null;
226   private BufferedReader dir_in = null;
227 
228   public FTPDirectoryIndexReceive(Socket dir_socket) throws Exception
229   {
230     this.dir_socket = dir_socket;
231     dir_in = new BufferedReader(new InputStreamReader(dir_socket.getInputStream()));
232   }
233 
234   public void run()
235   {
236     /* FIX THIS HACK!!! */
237     try {
238 
239       FTPBase.system_print("Receiving new Directory Index... ");
240 
241       try {
242 
243         boolean t = true;
244         while(t)
245         {
246           String line = dir_in.readLine();
247 
248           if(line.startsWith("#") | line.equals("")) continue;
249 
250           StringTokenizer st = new StringTokenizer(line, "\t");
251 
252           Resource r = new Resource(st.nextToken(), st.nextToken(), st.nextToken(), Integer.parseInt(st.nextToken()));
253 
254           if(FTPBase.directory_index.contains(r) == false) FTPBase.directory_index.add(r);
255         }
256 
257         dir_in.close();
258         dir_socket.close();
259 
260       } catch(Exception e) {}
261 
262       FTPBase.system_println("Done!");
263 
264     } catch(Exception e) {}
265   }
266 }
267 
268 class FTPConsole
269 extends JScrollPane
270 {
271   private JTextArea console = null;
272 
273   public FTPConsole()
274   {
275     if(FTPBase.gui)
276     {
277       console = new JTextArea();
278       console.setEditable(false);
279 
280       this.setViewportView(console);
281     }
282   }
283 
284   public void print(String s)
285   {
286     if(FTPBase.gui) console.append(s);
287     else System.out.print(s);
288   }
289 
290   public void println(String s)
291   {
292     if(FTPBase.gui) console.append(s + "\n");
293     else System.out.println(s);
294   }
295 }