360
|
1 package alice.daemon;
|
|
2
|
|
3 import java.io.IOException;
|
|
4 import java.net.SocketAddress;
|
|
5 import java.nio.ByteBuffer;
|
|
6 import java.nio.channels.DatagramChannel;
|
|
7
|
|
8 import alice.codesegment.SingletonMessage;
|
|
9 import alice.datasegment.Command;
|
|
10
|
|
11 public class MulticastConnection extends Connection {
|
419
|
12 private DatagramChannel dc;
|
|
13 private SocketAddress sAddr;
|
360
|
14
|
419
|
15 public MulticastConnection(DatagramChannel d, SocketAddress s) {
|
|
16 dc = d;
|
|
17 sAddr = s;
|
|
18 }
|
360
|
19
|
419
|
20 // may need to add infomation who send on ds.
|
|
21 @Override
|
|
22 public synchronized void write(Command cmd){
|
|
23 CommandMessage cmdMsg = cmd.convert();
|
|
24 ByteBuffer buffer;
|
|
25 try {
|
|
26 buffer = ByteBuffer.wrap(SingletonMessage.getInstance().write(cmdMsg));
|
|
27 while (buffer.hasRemaining()){
|
|
28 dc.send(buffer, sAddr);
|
|
29 }
|
|
30 } catch (IOException e) {
|
|
31 e.printStackTrace();
|
|
32 }
|
360
|
33
|
419
|
34 }
|
360
|
35
|
419
|
36 @Override
|
|
37 public void close(){
|
|
38 try {
|
|
39 dc.close();
|
|
40 } catch (IOException e) {
|
|
41 e.printStackTrace();
|
|
42 }
|
|
43 }
|
|
44
|
|
45 public void receive(ByteBuffer receiveData){
|
|
46 try {
|
|
47 dc.receive(receiveData);
|
|
48 } catch (IOException e) {
|
|
49 e.printStackTrace();
|
|
50 }
|
|
51 }
|
360
|
52
|
|
53 }
|