Skip to content
Snippets Groups Projects
Commit 6ac88bee authored by Nabil Abdennadher's avatar Nabil Abdennadher
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
import java.io.IOException;
import java.net.Socket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Client {
int port;
String ip;
Socket socket;
public Client(String ip,int portNumber){
port = portNumber;
try{
socket = new Socket(ip,portNumber);
}catch (IOException e){
System.out.println(e.toString());
}
}
public void send(String message){
try{
PrintWriter out = new PrintWriter(socket.getOutputStream());
out.println(message);
out.flush();
close();
}catch (IOException e){
System.out.println(e.toString());
}
}
public void close(){
try{
socket.close();
}catch (IOException e){
System.out.println(e.toString());
}
}
}
\ No newline at end of file
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
public class Node {
private boolean finished = false;
public final Integer id;
private int dist[];
private String first[];
private Set<String> seti = new HashSet<String>();
private final List<String> neighbours;
private int neighboursLength;
private Set<String> msg = new HashSet<String>();
private int t = 0;
private boolean init = false;
private final String status;
public final static int networkSize = ????; // Nombre de noeuds dans le réseau.
public Node(Integer port, List<String> neighboursList, String state) {
id = port;
neighbours = neighboursList;
status = state;
neighboursLength = neighbours.size();
dist = new int[] { 10, 10, 10, 10, 10 };
first = new String[networkSize];
}
public void start() {
if (status.equals("INIT")) {
init = true;
dist[id % 10000] = 0;
first[id % 10000] = id.toString();
broadcast(id + "");
}
}
public void onMessageReceived(String message) {
if (!init) {
System.out.println("T="+t);
init = true;
dist[id % 10000] = 0;
first[id % 10000] = id.toString();
broadcast(id + "");
}
if (msg.size() < neighboursLength) {
msg.add(message);
}
if (msg.size() == neighboursLength) {
t++;
System.out.println("T="+t);
seti.clear();
for (String set : msg) {
String[] parts = set.split(":");
String source = parts[0];
String receivedMessage = "";
for (int i = 1; i < parts.length; i++) {
String m = parts[i];
receivedMessage += m + ":";
int idk = Integer.parseInt(m) % 10000;
// Section I : A compléter
}
}
msg.clear();
// Section II: A compléter
}
}
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
System.out.println(e.toString());
}
}
private String setToString(Set<String> seti) {
String message = "";
for (String set : seti) {
message += set + ":";
}
return message;
}
public Boolean hasFinished() {
return finished;
}
private void sendMessage(String neighbour, String message) {
System.out.println("SENDING " + message + " to " + neighbour);
(new Client("127.0.0.1", Integer.parseInt(neighbour))).send(id + ":" + message);
}
private void broadcast(final String message) {
List<Thread> threads = new ArrayList<Thread>();
for (final String neighbour : neighbours) {
Thread t = new Thread() {
public void run() {
sendMessage(neighbour, message);
}
};
threads.add(t);
t.start();
}
for (Thread t : threads) {
try {
t.join();
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
}
Pcc.java 0 → 100644
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Pcc {
public static void main(String args[]) throws IOException{
//Setup part
//Args: 0 = port, 1 = STATUS, 2 = neighbours file name
Integer port = Integer.parseInt(args[0]);
String state = args[1];
String filename = args[2];
List<String> neighbours = getNeighbours(filename);
Node node = new Node(port,neighbours,state);
Server server = new Server(node);
(new Thread(server)).start();
node.start();
}
static List<String> getNeighbours(String filename) throws IOException {
FileReader fileReader = new FileReader(filename);
BufferedReader bufferedReader = new BufferedReader(fileReader);
ArrayList<String> lines = new ArrayList<String>();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
lines.add(line);
}
bufferedReader.close();
return lines;
}
}
\ No newline at end of file
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Server implements Runnable{
ServerSocket serverSocket;
int port;
Node node;
public Server(Node n){
port = n.id;
node = n;
try{
serverSocket = new ServerSocket(port);
}catch (IOException e){
System.out.println(e.toString());
}
}
@Override
public void run(){
System.out.println("Server listening on port "+port);
try{
while(!node.hasFinished()){
Socket clientSocket = serverSocket.accept();
node.onMessageReceived(new BufferedReader(new InputStreamReader(clientSocket.getInputStream())).readLine());
}
System.out.println("Server closed.");
serverSocket.close();
}catch (IOException e){
System.out.println(e.toString());
}
}
}
\ No newline at end of file
makefile 0 → 100644
ifneq ($(filter run all,$(MAKECMDGOALS)),$())
ifndef port
$(error port is not set)
endif
ifndef status
$(error status is not set)
endif
ifndef filename
$(error filename is not set)
endif
endif
.PHONY: info
info:
@echo "Usage:"
@echo "make [build|clean|run port=XXXX status=INIT|WAIT filename=neighbours/neighbours-X.txt]"
@echo "Examples:"
@echo "make build - Builds the java classes"
@echo "make run port=10000 status=INIT - Run the javacode on port 10000 with the status INIT"
@echo "make all port=10000 status=INIT - Clean, build and run the javacode on port 10000 with the status INIT"
build:
@javac -classpath . Pcc.java
clean:
@rm -f *.class
run:
@java Pcc ${port} ${status} ${filename}
all: clean build run
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment