1 21 22 package org.objectweb.jonas.discovery; 23 24 import java.io.ByteArrayOutputStream ; 25 import java.io.IOException ; 26 import java.io.ObjectOutputStream ; 27 import java.io.Serializable ; 28 import java.net.DatagramPacket ; 29 30 34 public class DiscMessage implements Serializable { 35 36 39 public static final String DISCOVERY_PROTOCOL_VERSION = "1.0"; 40 41 42 45 private String sourceAddress; 46 49 private int sourcePort; 50 51 54 private String version; 55 56 57 58 59 67 public DiscMessage(String sourceAddress, int sourcePort) { 68 this.sourceAddress = sourceAddress; 69 this.sourcePort = sourcePort; 70 this.version = DISCOVERY_PROTOCOL_VERSION; 71 } 72 73 78 public String getSourceAddress() { 79 return sourceAddress; 80 } 81 82 87 public int getSourcePort() { 88 return sourcePort; 89 } 90 91 96 public void setSourceAddress(String sourceAddress) { 97 this.sourceAddress = sourceAddress; 98 } 99 100 105 public void setSourcePort(int sourcePort) { 106 this.sourcePort = sourcePort; 107 } 108 109 116 public static byte[] objectToBytes(Serializable obj) { 117 if (obj == null) { 118 return null; 119 } 120 try { 121 ByteArrayOutputStream byteStream = new ByteArrayOutputStream (); 122 new ObjectOutputStream (byteStream).writeObject(obj); 123 return byteStream.toByteArray(); 124 } catch (IOException ex) { 125 throw new IllegalArgumentException (ex.toString()); 126 } 127 } 128 129 134 private static DatagramPacket getDatagram(Serializable o) { 135 byte[] content = objectToBytes(o); 136 if (content == null) { 137 return null; 138 } 139 DatagramPacket dp = new DatagramPacket (content, content.length); 140 return dp; 141 } 142 143 146 public String toString() { 147 String messageString = null; 148 messageString = sourceAddress + ":" + sourcePort; 149 return messageString; 150 } 151 152 153 public String getVersion() { 154 return version; 155 } 156 157 } | Popular Tags |