00001 00022 package org.objectweb.jonas.discovery; 00023 00024 import java.io.ByteArrayOutputStream; 00025 import java.io.IOException; 00026 import java.io.ObjectOutputStream; 00027 import java.io.Serializable; 00028 import java.net.DatagramPacket; 00029 00034 public class DiscMessage implements Serializable { 00038 private String sourceAddress; 00042 private int sourcePort; 00043 00053 public DiscMessage(String sourceAddress, int sourcePort) { 00054 this.sourceAddress = sourceAddress; 00055 this.sourcePort = sourcePort; 00056 } 00057 00063 public String getSourceAddress() { 00064 return sourceAddress; 00065 } 00066 00072 public int getSourcePort() { 00073 return sourcePort; 00074 } 00075 00081 public void setSourceAddress(String sourceAddress) { 00082 this.sourceAddress = sourceAddress; 00083 } 00084 00090 public void setSourcePort(int sourcePort) { 00091 this.sourcePort = sourcePort; 00092 } 00093 00101 public static byte[] objectToBytes(Serializable obj) { 00102 if (obj == null) { 00103 return null; 00104 } 00105 try { 00106 ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); 00107 new ObjectOutputStream(byteStream).writeObject(obj); 00108 return byteStream.toByteArray(); 00109 } catch (IOException ex) { 00110 throw new IllegalArgumentException(ex.toString()); 00111 } 00112 } 00113 00119 private static DatagramPacket getDatagram(Serializable o) { 00120 byte[] content = objectToBytes(o); 00121 if (content == null) { 00122 return null; 00123 } 00124 DatagramPacket dp = new DatagramPacket(content, content.length); 00125 return dp; 00126 } 00127 00131 public String toString() { 00132 String messageString = null; 00133 messageString = sourceAddress + ":" + sourcePort; 00134 return messageString; 00135 } 00136 00137 }