Posted By

kubaitis on 11/01/10


Tagged

Zeroconf bonjour jmdns mdns


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

kubaitis


Register and Monitor mDNS Services : Zeroconf


 / Published in: Java
 

Requires jmdns.jar from http://sourceforge.net/projects/jmdns/

  1. import javax.jmdns.JmDNS;
  2. import javax.jmdns.ServiceInfo;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.Hashtable;
  6. public class mDNSAgent extends Thread {
  7. public mDNSAgent(){
  8. this.mdnsType = "_myservice._tcp.local.";
  9. this.mdnsName = "MyService";
  10. try{jmdns = JmDNS.create();}catch(Exception e){e.printStackTrace();System.exit(1);}
  11. running = false;
  12. paused=false;
  13. waitInterval=500L;
  14. status="initialized";
  15. servers = new ArrayList<ServerInfo>();
  16. }
  17. public mDNSAgent(final String mdnsType,final String mdnsName){
  18. this.mdnsType = mdnsType;
  19. this.mdnsName = mdnsName;
  20. try{jmdns = JmDNS.create();}catch(Exception e){e.printStackTrace();System.exit(1);}
  21. running = false;
  22. paused=false;
  23. waitInterval=500L;
  24. status="initialized";
  25. servers = new ArrayList<ServerInfo>();
  26. }
  27. private boolean running;
  28. private boolean paused;
  29. private JmDNS jmdns;
  30. private long waitInterval;
  31. private String mdnsName;
  32. private String mdnsType;
  33. private String status;
  34. private ArrayList<ServerInfo> servers;
  35. public ArrayList<ServerInfo> getServers(){return servers;}
  36. public boolean isRunning(){return running;}
  37. public boolean isPaused(){return paused;}
  38. public void stopService(){running=false;paused=false;}
  39. public void pauseService(){paused=true;}
  40. public void resumeService(){paused=false;}
  41. public void setWaitInterval(long input){waitInterval=input;}
  42. public void setStatus(String input){status=input;}
  43. public long getWaitInterval(){return waitInterval;}
  44. public String getMdnsName(){return mdnsName;}
  45. public String getMdnsType(){return mdnsType;}
  46. public String getStatus(){return status;}
  47. public static final void sleep(long millis){try{Thread.sleep(millis);}catch(Exception e){}}
  48. public void checkPause(){while(isRunning()&&isPaused()){sleep(waitInterval);}}
  49. public void register(final String hostName,final Integer webServerPort){
  50. Hashtable<String,String> settings = new Hashtable<String,String>();
  51. settings.put("host", hostName);
  52. settings.put("web_port", webServerPort.toString());
  53. settings.put("secure_web_port", webServerPort.toString());
  54. try {
  55. ServiceInfo info = ServiceInfo.create(mdnsType, mdnsName, webServerPort,1,1,settings);
  56. jmdns.registerService(info);
  57. }catch(IOException e){e.printStackTrace();}
  58. }
  59. public void run(){
  60. while (isRunning()) {
  61. checkPause();
  62. ServiceInfo[] infos = jmdns.list(mdnsType);
  63. if(infos!=null&&infos.length>0){
  64. ArrayList<ServerInfo> temp = new ArrayList<ServerInfo>();
  65. for (int i=0; i < infos.length; i++) {
  66. temp.add(new ServerInfo(infos[i].getName(),infos[i].getPropertyString("host"),Integer.valueOf(infos[i].getPropertyString("web_port")),Integer.valueOf(infos[i].getPropertyString("secure_web_port"))));
  67. }
  68. servers = temp;
  69. try {Thread.sleep(waitInterval);}catch(InterruptedException e){break;}
  70. }
  71. }
  72. }
  73. }

Report this snippet  

You need to login to post a comment.