Project

General

Profile

Statistics
| Revision:

sdis1819-t7g02 / channels / Channel.java @ 1

History | View | Annotate | Download (2.65 KB)

1
package channels;
2

    
3
import java.io.IOException;
4
import java.net.DatagramPacket;
5
import java.net.InetAddress;
6
import java.net.MulticastSocket;
7
import java.util.Random;
8

    
9
import service.Cloud;
10
import service.Constants;
11

    
12
abstract class Channel extends Thread {
13

    
14
        protected MulticastSocket socket;
15
        protected InetAddress address;
16
        protected int port;
17
        protected DatagramPacket rcv;
18
        protected DatagramPacket send;
19
        protected int serverId;
20
        protected Cloud father;
21
        
22
        protected Channel(String addr, String p, int sId, Cloud f) throws IOException {
23
                
24
                address = InetAddress.getByName(addr);
25
                port = Integer.parseInt(p);
26
                socket = new MulticastSocket(port);
27
                serverId = sId;
28
                father = f; 
29
        }
30
        
31
        protected MulticastSocket getSocket() {
32
                return socket;
33
        }
34

    
35
        protected void setSocket(MulticastSocket socket) {
36
                this.socket = socket;
37
        }
38

    
39
        protected InetAddress getAddress() {
40
                return address;
41
        }
42

    
43
        protected void setAddress(InetAddress address) {
44
                this.address = address;
45
        }
46

    
47
        protected int getPort() {
48
                return port;
49
        }
50

    
51
        protected void setPort(int port) {
52
                this.port = port;
53
        }
54
        
55
        protected int getServerId() {
56
                return serverId;
57
        }
58
        
59
        protected void resetMyPacket(byte[] data) {
60
                rcv = new DatagramPacket(data, Constants.MAX_MESSAGE_SIZE);
61
        }
62
        
63
        public void waitRandomTime() throws InterruptedException {
64
                Random r = new Random();
65
                int low = 1;
66
                int high = 401;
67
                int result = r.nextInt(high-low) + low;
68
                
69
                Thread.sleep(result);
70
        }
71
        
72
        protected Message captureData() throws IOException {
73
                byte[] data = new byte[Constants.MAX_MESSAGE_SIZE];
74
                
75
                Message m;
76
                
77
                resetMyPacket(data);
78
                
79
                socket.receive(rcv);
80
                
81
                byte[] filtredData = new byte[Constants.MAX_MESSAGE_SIZE];
82
                
83
                System.arraycopy(rcv.getData(), rcv.getOffset(), filtredData, 0, rcv.getLength());
84
                
85
                try {
86
                        m = new Message(filtredData, rcv.getLength());
87
                } catch(IllegalArgumentException e) {
88
                        System.out.println("Error receiving message: " + e.getMessage());
89
                        return null;
90
                }
91
                
92
                return m;
93
        }
94
        
95
        public synchronized boolean sendMessage(Message m) {
96
                send = new DatagramPacket(m.toBytes(), m.toBytes().length, address, port);
97
                
98
                try {
99
                        socket.send(send);
100
            }
101
                catch (IOException e) {
102
                        return false;
103
            }
104
                
105
            return true;
106
        }
107
        
108
        public synchronized boolean sendHeader(MessageHeader h) {
109
                send = new DatagramPacket(h.headerToBytes(), h.headerToBytes().length, father.controlRoom.address, father.controlRoom.port);
110
                
111
                try {
112
                        socket.send(send);
113
            }
114
                catch (IOException e) {
115
                        return false;
116
            }
117
                
118
            return true;
119
        }
120
        
121
        protected void joinChannelGroup() throws IOException {
122
                socket.joinGroup(address);
123
        }
124
        
125
        protected void leaveGroupChannel() throws IOException {
126
                socket.leaveGroup(address);
127
        }
128
}