/Users/lyon/j4p/src/net/multicast/ChatMain.java

1    package net.multicast; 
2     
3    import futils.Futil; 
4    import futils.ReaderUtil; 
5    import gui.JInfoFrame; 
6    import gui.run.RunTextField; 
7     
8    import java.awt.BorderLayout; 
9    import java.awt.Container; 
10   import java.io.File; 
11   import java.io.FileNotFoundException; 
12   import java.io.IOException; 
13    
14   /** 
15    * Copyright DocJava, inc. User: lyon Date: Oct 27, 2004 Time: 4:59:20 AM 
16    */ 
17   public class ChatMain { 
18       private int port = 1234; 
19       private McastUtil mcx = new McastUtil(port); 
20       private JInfoFrame jif = new JInfoFrame(); 
21    
22       public JInfoFrame getChatFrame() { 
23           return jif; 
24       } 
25    
26       public static void main(String[] args) { 
27           ChatMain cm = new ChatMain(); 
28       } 
29    
30       public ChatMain() { 
31           mcx.setLoopBack(true); 
32           processWithMulticastSocket(); 
33       } 
34    
35       public void processWithMulticastSocket() { 
36           // ChatRcvr will handle the incoming 
37           // Data and print it out to STDN output. 
38    
39           new ChatRcvr(mcx, jif); 
40           Container c = jif.getContentPane(); 
41           RunTextField rtf = new RunTextField() { 
42               public void run() { 
43                   try { 
44                       sendALine(getText()); 
45                       setText(""); 
46                   } catch (IOException e) { 
47                       e.printStackTrace(); 
48                   } 
49               } 
50           }; 
51           c.add(rtf, BorderLayout.SOUTH); 
52           jif.setSize(400, 200); 
53           jif.show(); 
54       } 
55    
56       private void sendALine(String strin) 
57               throws IOException { 
58           if (strin.startsWith("open:")) { 
59               sendAFile(); 
60           } 
61           mcx.sendBytes(strin.getBytes()); 
62       } 
63    
64       private void sendAFile() { 
65           System.out.println("SendAFile_"); 
66           File f = Futil.getReadFile("select a text file"); 
67           sendAFile(f); 
68       } 
69    
70       private void sendAFile(File f) { 
71           try { 
72               String sa[] = ReaderUtil.getFile(f); 
73               byte b[] = ReaderUtil.getBytes(sa); 
74               mcx.sendBytes(b); 
75           } catch (FileNotFoundException e) { 
76               e.printStackTrace(); 
77           } catch (IOException e) { 
78               e.printStackTrace(); 
79           } 
80       } 
81   } 
82    
83