ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/experimental/MNote/uk/org/iscream/mnote/MNoteSocketHandler.java
Revision: 1.1
Committed: Thu Jun 14 09:16:08 2001 UTC (23 years, 5 months ago) by ajm
Branch: MAIN
CVS Tags: HEAD
Log Message:
initial checkin of the Multicast Notification System

File Contents

# Content
1 package uk.org.iscream.mnote;
2
3 import java.net.MulticastSocket;
4 import java.net.UnknownHostException;
5 import java.net.InetAddress;
6 import java.net.DatagramPacket;
7 import java.io.IOException;
8
9 class MNoteSocketHandler extends Thread implements SendListener {
10
11 private static final int BUFFER_SIZE = 50;
12
13 public MNoteSocketHandler(int port, String groupName, MNoteProtocolHandler protocolHandler) throws UnknownHostException, IOException{
14 _port = port;
15 _mgroup = InetAddress.getByName(groupName);
16 _protocolHandler = protocolHandler;
17 _msocket = new MulticastSocket(_port);
18 protocolHandler.addSendListener(this);
19 this.start();
20 }
21
22 public void run() {
23 String inBound = null;
24 byte[] buffer = new byte[BUFFER_SIZE];
25 DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
26 try {
27 _msocket.joinGroup(_mgroup);
28 while (_running) {
29 _msocket.receive(packet);
30 inBound = new String(buffer).trim();
31 _protocolHandler.processMessage(inBound);
32 }
33 } catch (IOException e) {
34 System.err.println("IO ERROR: " + e);
35 }
36 }
37
38 public void acceptSendEvent(SendEvent se) {
39 sendMessage(se.getMessage());
40 }
41
42 public void shutdown() {
43 _running = false;
44 try {
45 _msocket.leaveGroup(_mgroup);
46 } catch (IOException e) {
47 System.err.println("IO ERROR: " + e);
48 }
49 }
50
51 private synchronized void sendMessage(String outBound) {
52 DatagramPacket packet = new DatagramPacket(outBound.getBytes(), outBound.length(), _mgroup, _port);
53 try {
54 _msocket.send(packet);
55 } catch (IOException e) {
56 System.err.println("IO ERROR: " + e);
57 }
58 }
59
60 private MNoteProtocolHandler _protocolHandler = null;
61 private MulticastSocket _msocket = null;
62 private InetAddress _mgroup = null;
63 private int _port = 0;
64 private boolean _running = true;
65 }