Project

General

Profile

Statistics
| Revision:

root / src / messages / SendMessage.java

History | View | Annotate | Download (1.07 KB)

1
package messages;
2

    
3
import peer.Peer;
4

    
5
import javax.net.ssl.SSLSocket;
6
import javax.net.ssl.SSLSocketFactory;
7
import java.io.ObjectOutputStream;
8
import java.net.InetAddress;
9

    
10
public class SendMessage implements Runnable{
11

    
12
    Message message;
13

    
14
    SendMessage(Message message)
15
    {
16
        this.message = message;
17
    }
18

    
19

    
20
    @Override
21
    public void run() {
22
        System.out.println("Sending " + message + " to :  " + message.getIpAddress() + message.getPort());
23

    
24
        if(message.getPort() == Peer.port)
25
            return;
26

    
27
        SSLSocketFactory socketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
28
        SSLSocket clientSocket;
29
        try {
30
            clientSocket = (SSLSocket) socketFactory.createSocket(InetAddress.getByName(message.getIpAddress()), message.getPort());
31
            clientSocket.startHandshake();
32

    
33
            ObjectOutputStream outToServer = new ObjectOutputStream(clientSocket.getOutputStream());
34
            outToServer.writeObject(message);
35
        } catch (Exception e) {
36
            System.out.println("User disconnected");
37
        }
38
    }
39
}