marc fleury wrote:
> Then don't tease us and can you GPL-spare that "multicast socket" code?
> the idea is that a "new born" JNP client should be able to find his
> provider by crying on the network "where is my MOM"?. AFAIK if the
> naming is bootp'd then we are all set.
Ok, ok, here ya go! Enjoy! Basically, all I'm doing is creating
an extremely simple Message class. I serialize this and send it
out the multicast socket. Both client and server do this, so it
doesn't matter which comes up first (the client resends its packet
when it receives a broadcast from a server).
The server receives a broadcast message from a client and looks up
the senders address in a config file. If found, all my app needs
to do is connect to the client via RMI. Hope that isn't too
confusing. The "server" here is a monitor/controller and the
"client" is the source of data, the RMI server.
public class Message implements Serializable
{
public Message(String source, String content)
{
this.source = source;
this.content = content;
}
public String content;
public String source;
}
public interface BroadcastConstants
{
static final String GROUP = "239.2.3.9";
static final int PORT = 3333;
static final int TTL = 1;
}
public class BroadcastClient implements Runnable, BroadcastConstants
{
public BroadcastClient()
{
try
{
group = InetAddress.getByName(GROUP);
sock = new MulticastSocket(PORT);
sock.setTimeToLive(TTL);
sock.joinGroup(group);
// set a receive timeout to periodically check for shutdown
sock.setSoTimeout(200);
}
catch ( Exception e )
{
e.printStackTrace();
}
}
/**
* Broadcast a packet announcing our presence
*/
private void announce() throws IOException
{
Message pkg = new Message("client", announce);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(pkg);
byte[] msg = baos.toByteArray();
DatagramPacket pkt = new DatagramPacket(msg, msg.length, group, PORT);
sock.send(pkt);
}
/**
* Listen for server announcements
*/
private void listen() throws IOException
{
byte[] recvBuffer = new byte[5000];
DatagramPacket recv = new DatagramPacket(recvBuffer, recvBuffer.length);
try
{
sock.receive(recv);
}
catch ( InterruptedIOException ie )
{
return;
}
byte[] buf = new byte[5000];
System.arraycopy(recvBuffer, 0, buf, 0, recv.getLength());
try
{
ObjectInputStream ois = new ObjectInputStream( new
ByteArrayInputStream(buf) );
Message msg = (Message) ois.readObject();
// a server just came online -- resend my announcement
if ( msg.source.equals("server") )
{
announce();
}
}
catch (ClassNotFoundException cnf) { }
}
public void run()
{
try
{
announce();
}
catch (IOException e)
{
e.printStackTrace();
return;
}
while (! stopping)
{
try
{
listen();
}
catch (IOException ioe)
{
ioe.printStackTrace();
if (sock.getPort() == -1)
{ // connection lost
System.exit(1);
}
}
}
}
public void shutdown()
{
stopping = true;
}
private boolean stopping = false;
private InetAddress group;
private MulticastSocket sock;
private static final String announce = "Client announce";
}
public class BroadcastServer implements Runnable, BroadcastConstants
{
public BroadcastServer(ServiceHandler handler)
{
try
{
group = InetAddress.getByName(GROUP);
sock = new MulticastSocket(PORT);
sock.setTimeToLive(TTL);
sock.joinGroup(group);
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
this.handler = handler;
}
public void run()
{
// first, send out an announcement that I'm here
// in case there are clients waiting
Message msg = new Message("server", announce);
try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(msg);
oos.flush();
byte[] msgData = baos.toByteArray();
DatagramPacket pkt = new DatagramPacket(msgData, msgData.length,
group, PORT);
sock.send(pkt);
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
// now, wait for service requests from clients
byte[] recvBuffer = new byte[5000];
while ( true )
{
try
{
DatagramPacket recv = new DatagramPacket(recvBuffer,
recvBuffer.length);
sock.receive(recv);
InetAddress recvAddr = recv.getAddress();
byte[] buf = new byte[5000];
System.arraycopy(recvBuffer, 0, buf, 0, recv.getLength());
ObjectInputStream ois = new ObjectInputStream( new
ByteArrayInputStream(buf) );
Message m = (Message) ois.readObject();
if ( m.source.equals("client") )
{
handler.service(recvAddr.getHostName());
}
}
catch ( Exception e )
{
e.printStackTrace();
}
}
}
private ServiceHandler handler;
private static InetAddress group;
private static MulticastSocket sock;
private static final String announce = "Server announce";
}
public interface ServiceHandler
{
/**
* Service an announcement from a client at address
*/
public void service(String address);
}
--
--------------------------------------------------------------
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
Problems?: [EMAIL PROTECTED]