Skip to content
Permalink
master
Go to file
 
 
Cannot retrieve contributors at this time
60 lines (54 sloc) 2.24 KB
package de.teamproject16.pbft.Network;
import com.spotify.docker.client.DockerCertificateException;
import com.spotify.docker.client.DockerException;
import de.luckydonald.utils.dockerus.DockerusAuto;
import de.teamproject16.pbft.Messages.Message;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.util.List;
/**
* Created by IngridBoldt on 06.10.16.
*/
public class Sender {
String msg;
/**
* Make the message object to a string and give it to the method broadcast.
* @param msg object from message with specific data.
* @throws UnsupportedEncodingException
* @throws InterruptedException
* @throws DockerException
* @throws DockerCertificateException
*/
public void sendMessage(Message msg) throws UnsupportedEncodingException, InterruptedException, DockerException, DockerCertificateException {
broadcast(msg.toString());
}
/**
* Send broadcast messages in the network of registered nodes.
* @param message String with the specific message (init, void...).
* @throws UnsupportedEncodingException
* @throws DockerCertificateException
* @throws DockerException
* @throws InterruptedException
*/
public void broadcast(String message) throws UnsupportedEncodingException, DockerCertificateException, DockerException, InterruptedException {
List<String> otherHostnames = DockerusAuto.getInstance().getHostnames(true);
message += "\n";
msg = "ANSWER " + message.length() + "\n" + message;
byte[] msgBytes = msg.getBytes("UTF-8");
for (String nodeHost: otherHostnames){
Boolean sent = false;
while(!sent){
try {
Socket socket = new Socket(nodeHost, 4458); //open a socket port 4458, and the nodeHost names of the other hosts
DataOutputStream dataStream = new DataOutputStream(socket.getOutputStream()); //to send messages
dataStream.write(msgBytes); //write the messages at the datastream
sent = true;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
You can’t perform that action at this time.