$search
00001 // Licensed under Apache License version 2.0 00002 // Original license LGPL 00003 00004 // 00005 // This library is free software; you can redistribute it and/or 00006 // modify it under the terms of the GNU Lesser General Public 00007 // License as published by the Free Software Foundation; either 00008 // version 2.1 of the License, or (at your option) any later version. 00009 // 00010 // This library is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 // Lesser General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU Lesser General Public 00016 // License along with this library; if not, write to the Free Software 00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 00019 package ros.zeroconf.jmdns; 00020 00021 import java.awt.BorderLayout; 00022 import java.awt.Color; 00023 import java.awt.Container; 00024 import java.awt.GridLayout; 00025 import java.io.IOException; 00026 import java.net.InetAddress; 00027 import java.util.Enumeration; 00028 00029 import javax.jmdns.JmmDNS; 00030 import javax.jmdns.ServiceEvent; 00031 import javax.jmdns.ServiceInfo; 00032 import javax.jmdns.ServiceListener; 00033 import javax.jmdns.ServiceTypeListener; 00034 import javax.swing.DefaultListModel; 00035 import javax.swing.JFrame; 00036 import javax.swing.JLabel; 00037 import javax.swing.JList; 00038 import javax.swing.JPanel; 00039 import javax.swing.JScrollPane; 00040 import javax.swing.JTextArea; 00041 import javax.swing.ListSelectionModel; 00042 import javax.swing.SwingUtilities; 00043 import javax.swing.border.EmptyBorder; 00044 import javax.swing.event.ListSelectionEvent; 00045 import javax.swing.event.ListSelectionListener; 00046 import javax.swing.table.AbstractTableModel; 00047 00053 public class BrowserOriginal extends JFrame implements ServiceListener, ServiceTypeListener, ListSelectionListener { 00057 private static final long serialVersionUID = 5750114542524415107L; 00058 JmmDNS jmmdns; 00059 // Vector headers; 00060 String type; 00061 DefaultListModel types; 00062 JList typeList; 00063 DefaultListModel services; 00064 JList serviceList; 00065 JTextArea info; 00066 00071 BrowserOriginal(JmmDNS mmDNS) throws IOException { 00072 super("JmDNS Browser"); 00073 this.jmmdns = mmDNS; 00074 00075 Color bg = new Color(230, 230, 230); 00076 EmptyBorder border = new EmptyBorder(5, 5, 5, 5); 00077 Container content = getContentPane(); 00078 content.setLayout(new GridLayout(1, 3)); 00079 00080 types = new DefaultListModel(); 00081 typeList = new JList(types); 00082 typeList.setBorder(border); 00083 typeList.setBackground(bg); 00084 typeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 00085 typeList.addListSelectionListener(this); 00086 00087 JPanel typePanel = new JPanel(); 00088 typePanel.setLayout(new BorderLayout()); 00089 typePanel.add("North", new JLabel("Types")); 00090 typePanel.add("Center", new JScrollPane(typeList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED)); 00091 content.add(typePanel); 00092 00093 services = new DefaultListModel(); 00094 serviceList = new JList(services); 00095 serviceList.setBorder(border); 00096 serviceList.setBackground(bg); 00097 serviceList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 00098 serviceList.addListSelectionListener(this); 00099 00100 JPanel servicePanel = new JPanel(); 00101 servicePanel.setLayout(new BorderLayout()); 00102 servicePanel.add("North", new JLabel("Services")); 00103 servicePanel.add("Center", new JScrollPane(serviceList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED)); 00104 content.add(servicePanel); 00105 00106 info = new JTextArea(); 00107 info.setBorder(border); 00108 info.setBackground(bg); 00109 info.setEditable(false); 00110 info.setLineWrap(true); 00111 00112 JPanel infoPanel = new JPanel(); 00113 infoPanel.setLayout(new BorderLayout()); 00114 infoPanel.add("North", new JLabel("Details")); 00115 infoPanel.add("Center", new JScrollPane(info, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED)); 00116 content.add(infoPanel); 00117 00118 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 00119 setLocation(100, 100); 00120 setSize(600, 400); 00121 00122 this.jmmdns.addServiceTypeListener(this); 00123 00124 this.setVisible(true); 00125 } 00126 00127 /* 00128 * (non-Javadoc) 00129 * @see javax.jmdns.ServiceListener#serviceAdded(javax.jmdns.ServiceEvent) 00130 */ 00131 @Override 00132 public void serviceAdded(ServiceEvent event) { 00133 final String name = event.getName(); 00134 00135 System.out.println("ADD: " + name); 00136 SwingUtilities.invokeLater(new Runnable() { 00137 @Override 00138 public void run() { 00139 insertSorted(services, name); 00140 } 00141 }); 00142 } 00143 00144 /* 00145 * (non-Javadoc) 00146 * @see javax.jmdns.ServiceListener#serviceRemoved(javax.jmdns.ServiceEvent) 00147 */ 00148 @Override 00149 public void serviceRemoved(ServiceEvent event) { 00150 final String name = event.getName(); 00151 00152 System.out.println("REMOVE: " + name); 00153 SwingUtilities.invokeLater(new Runnable() { 00154 @Override 00155 public void run() { 00156 services.removeElement(name); 00157 } 00158 }); 00159 } 00160 00161 @Override 00162 public void serviceResolved(ServiceEvent event) { 00163 final String name = event.getName(); 00164 00165 System.out.println("RESOLVED: " + name); 00166 if (name.equals(serviceList.getSelectedValue())) { 00167 ServiceInfo[] serviceInfos = this.jmmdns.getServiceInfos(type, name); 00168 this.dislayInfo(serviceInfos); 00169 // this.dislayInfo(new ServiceInfo[] { event.getInfo() }); 00170 } 00171 } 00172 00173 /* 00174 * (non-Javadoc) 00175 * @see javax.jmdns.ServiceTypeListener#serviceTypeAdded(javax.jmdns.ServiceEvent) 00176 */ 00177 @Override 00178 public void serviceTypeAdded(ServiceEvent event) { 00179 final String aType = event.getType(); 00180 00181 System.out.println("TYPE: " + aType); 00182 SwingUtilities.invokeLater(new Runnable() { 00183 @Override 00184 public void run() { 00185 insertSorted(types, aType); 00186 } 00187 }); 00188 } 00189 00190 /* 00191 * (non-Javadoc) 00192 * @see javax.jmdns.ServiceTypeListener#subTypeForServiceTypeAdded(javax.jmdns.ServiceEvent) 00193 */ 00194 @Override 00195 public void subTypeForServiceTypeAdded(ServiceEvent event) { 00196 System.out.println("SUBTYPE: " + event.getType()); 00197 } 00198 00199 void insertSorted(DefaultListModel model, String value) { 00200 for (int i = 0, n = model.getSize(); i < n; i++) { 00201 int result = value.compareToIgnoreCase((String) model.elementAt(i)); 00202 if (result == 0) { 00203 return; 00204 } 00205 if (result < 0) { 00206 model.insertElementAt(value, i); 00207 return; 00208 } 00209 } 00210 model.addElement(value); 00211 } 00212 00218 @Override 00219 public void valueChanged(ListSelectionEvent e) { 00220 if (!e.getValueIsAdjusting()) { 00221 if (e.getSource() == typeList) { 00222 type = (String) typeList.getSelectedValue(); 00223 System.out.println("VALUE CHANGED: type: " + type); 00224 this.jmmdns.removeServiceListener(type, this); 00225 services.setSize(0); 00226 info.setText(""); 00227 if (type != null) { 00228 this.jmmdns.addServiceListener(type, this); 00229 } 00230 } else if (e.getSource() == serviceList) { 00231 String name = (String) serviceList.getSelectedValue(); 00232 System.out.println("VALUE CHANGED: type: " + type + " service: " + name); 00233 if (name == null) { 00234 info.setText(""); 00235 } else { 00236 ServiceInfo[] serviceInfos = this.jmmdns.getServiceInfos(type, name); 00237 // This is actually redundant. getServiceInfo will force the resolution of the service and call serviceResolved 00238 this.dislayInfo(serviceInfos); 00239 } 00240 } 00241 } 00242 } 00243 00244 private void dislayInfo(ServiceInfo[] serviceInfos) { 00245 if (serviceInfos.length == 0) { 00246 System.out.println("INFO: null"); 00247 info.setText("service not found\n"); 00248 } else { 00249 StringBuilder buf = new StringBuilder(2048); 00250 System.out.println("INFO: " + serviceInfos.length); 00251 for (ServiceInfo service : serviceInfos) { 00252 System.out.println("INFO: " + service); 00253 buf.append(service.getName()); 00254 buf.append('.'); 00255 buf.append(service.getTypeWithSubtype()); 00256 buf.append('\n'); 00257 buf.append(service.getServer()); 00258 buf.append(':'); 00259 buf.append(service.getPort()); 00260 buf.append('\n'); 00261 for (InetAddress address : service.getInetAddresses()) { 00262 buf.append(address); 00263 buf.append(':'); 00264 buf.append(service.getPort()); 00265 buf.append('\n'); 00266 } 00267 for (Enumeration<String> names = service.getPropertyNames(); names.hasMoreElements();) { 00268 String prop = names.nextElement(); 00269 buf.append(prop); 00270 buf.append('='); 00271 buf.append(service.getPropertyString(prop)); 00272 buf.append('\n'); 00273 } 00274 buf.append("------------------------\n"); 00275 } 00276 this.info.setText(buf.toString()); 00277 } 00278 } 00279 00283 class ServiceTableModel extends AbstractTableModel { 00287 private static final long serialVersionUID = 5607994569609827570L; 00288 00289 @Override 00290 public String getColumnName(int column) { 00291 switch (column) { 00292 case 0: 00293 return "service"; 00294 case 1: 00295 return "address"; 00296 case 2: 00297 return "port"; 00298 case 3: 00299 return "text"; 00300 } 00301 return null; 00302 } 00303 00304 @Override 00305 public int getColumnCount() { 00306 return 1; 00307 } 00308 00309 @Override 00310 public int getRowCount() { 00311 return services.size(); 00312 } 00313 00314 @Override 00315 public Object getValueAt(int row, int col) { 00316 return services.elementAt(row); 00317 } 00318 } 00319 00320 @Override 00321 public String toString() { 00322 return "RVBROWSER"; 00323 } 00324 00331 public static void main(String argv[]) throws IOException { 00332 new BrowserOriginal(JmmDNS.Factory.getInstance()); 00333 } 00334 }