VisClient/org/hfbk/vis/mcp/VisMCP.java

Go to the documentation of this file.
00001 package org.hfbk.vis.mcp;
00002 
00003 import java.awt.BorderLayout;
00004 import java.awt.Frame;
00005 import java.awt.List;
00006 import java.awt.Panel;
00007 import java.awt.event.WindowAdapter;
00008 import java.awt.event.WindowEvent;
00009 import java.io.IOException;
00010 import java.net.DatagramPacket;
00011 import java.net.DatagramSocket;
00012 import java.net.SocketAddress;
00013 import java.net.SocketTimeoutException;
00014 import java.util.concurrent.CopyOnWriteArrayList;
00015 
00016 import org.hfbk.util.ShellPanel;
00017 import org.hfbk.util.UIUtils;
00018 import org.hfbk.vis.ImageLoader;
00019 
00020 public class VisMCP {
00021 
00022         Frame frame=new Frame("Vis/MCP"); //main window
00023         
00024         
00025         static int clientport = 7778, sendport=7777, TIMEOUT=1000;  
00026         DatagramSocket socket = null;
00027         DatagramPacket paket = null;
00028         
00030         CopyOnWriteArrayList<SocketAddress> clients=new CopyOnWriteArrayList<SocketAddress>();
00031         
00032         
00033         final List clientLister=new List(5); //the UI list
00034         
00036         public VisMCP() {
00037                 frame.setIconImage(ImageLoader.getRessourceImage("icons/vis.png"));                     
00038                 frame.addWindowListener(new WindowAdapter(){
00039                         public void windowClosing(WindowEvent e) {
00040                                 System.exit(0);
00041                         }
00042                 });
00043                 
00044                 //try to open the udp port..
00045                 try {
00046                         paket = new DatagramPacket(new byte[4096], 4096);
00047                         socket = new DatagramSocket(sendport);
00048                         socket.setSoTimeout(TIMEOUT);
00049                 } catch (IOException e) {
00050                         System.err.println("Ausnahmefehler: " + e);
00051                 }               
00052                 
00053                 //build a Pinger to frequently check client availabilty. 
00054                 new Pinger(){
00055                         //we override pinged to maintain the active client list.
00056                         void pinged(java.util.List<SocketAddress> clientsFound) {
00057                                 //we add & remove instead of completely replace
00058                                 //to keep currently UI list selected items
00059                                 
00060                                 //add new found clients
00061                                 for (SocketAddress client: clientsFound)
00062                                         if(!clients.contains(client)){
00063                                                 clients.add(client);
00064                                                 clientLister.add(client.toString());
00065                                                 clientLister.select(clientLister.getItemCount()-1);
00066                                         }                               
00067                                 //remove lost clients
00068                                 for(SocketAddress client: clients){
00069                                         if (!clientsFound.contains(client)){
00070                                                 clients.remove(client);
00071                                                 clientLister.remove(client.toString());
00072                                         }
00073                                 }
00074                         }
00075                 };              
00076                 
00077                 frame.add(buildPanel());        
00078                 frame.pack();
00079                 UIUtils.blackify(frame); //the finest vis look..
00080                 frame.setVisible(true);                 
00081         }
00082         
00090         Panel buildPanel(){
00091                 Panel p=new ShellPanel(){
00092                         public String action(String command) {
00093                                 return dispatch(command);
00094                         }
00095                 };
00096                 
00097                 clientLister.setMultipleMode(true);             
00098                 p.add(clientLister, BorderLayout.NORTH);
00099                                 
00100                 return p;
00101         }
00102         
00106         String dispatch(String command){
00107                 if (clients.size()==0) return "Error: No client visible.";
00108                 
00109                 byte[] data=command.getBytes(); //extract command string as bytes
00110 
00111                 String result="";
00112                 
00113                 for (SocketAddress client: clients)  
00114                         if(clientLister.isIndexSelected(clients.indexOf(client))){ //if a client is UI list selected.. 
00115                                 result+="\n "+client+": ";
00116                                 try {
00117                                         socket.send(new DatagramPacket(data, data.length, client));
00118                                         socket.receive(paket);
00119                                         result+=new String(paket.getData(), 0, paket.getLength());
00120                                 } catch (Exception e) { //timeout if client doesn't respond. 
00121                                         if (e instanceof SocketTimeoutException) result+="No answer.";
00122                                         else throw new RuntimeException (e);
00123                                 }
00124                         }
00125                 return result;
00126         }
00127         
00128         public static void main(String[] args) {
00129                 new VisMCP();
00130         }
00131 }
00132 
00133 

Generated on Tue Apr 7 17:57:20 2009 for visclient by  doxygen 1.5.1