Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package exercise1;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.rmi.AlreadyBoundException;
- import java.rmi.RemoteException;
- import java.rmi.registry.LocateRegistry;
- import java.rmi.registry.Registry;
- import java.rmi.server.UnicastRemoteObject;
- import java.util.LinkedList;
- import java.util.Scanner;
- public class My_Process implements Runnable, PrInter {
- public static int timestamp = 1;
- public static int process_id;
- public static LinkedList<Message> Received_Messages = new LinkedList<Message>();
- public static LinkedList<Message> fifo;
- public My_Process(){};
- public static void main(String args[]) {
- LinkedList<Message> ls;
- try {
- //String f = "C:/Users/Kostas/workspace/rmi/bin/exercise1/Messages"+args[2]+".txt";
- process_id = Integer.parseInt(args[2]);
- String f = "Messages"+args[2]+".txt";
- File file = new File(f);
- ls = LoadMessages(file,args[2]);
- fifo = ls;
- Thread.sleep(4000);
- }
- catch(IOException ioe) {
- ioe.printStackTrace();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Scanner reader = new Scanner(System.in); // Reading from System.in
- System.out.println("Enter a number: ");
- int n = reader.nextInt(); // Scans the next token of the input as an int.
- //once finished
- reader.close();
- //send implemented in main
- (new Thread(new My_Process())).start();
- for (Message temp : fifo) {
- try {
- Registry registry = LocateRegistry.getRegistry(null);
- for(int i=1;i<3;i++){
- if(process_id!=i){
- PrInter stub = (PrInter) registry.lookup("PrInter"+i);
- String response = stub.Receive_Message(temp);
- System.out.println("response: " + response);
- }
- }
- } catch (Exception e) {
- System.err.println("Client exception: " + e.toString());
- e.printStackTrace();
- }
- }
- while(true);
- }
- @Override
- public void run() {
- //Receive part implemented in thread
- System.out.println("Receiver thread");
- try {
- My_Process obj = new My_Process();
- PrInter stub = (PrInter) UnicastRemoteObject.exportObject(obj, 0);
- // Bind the remote object's stub in the registry
- Registry registry = LocateRegistry.getRegistry();
- registry.rebind("PrInter"+process_id, stub);
- } catch (RemoteException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- @Override
- public String Receive_Message(Message m) throws RemoteException {
- // TODO Auto-generated method stub
- System.out.println("Received msg from: " + m.get_pid() + "with ts: " + m.get_ts());
- if(m.get_is_ack()==false){
- Received_Messages.add(m);
- // System.out.print("Received messages:");
- // for(Message temp : Received_Messages){
- // System.out.println(temp.get_pid());
- // }
- }
- else{
- try {
- Registry registry = LocateRegistry.getRegistry(null);
- for(int i=1;i<3;i++){
- if(m.get_pid()!=i){
- PrInter stub = (PrInter) registry.lookup("PrInter"+i);
- m.set_ack(true);
- String response = stub.Receive_Message(m);
- System.out.println("Elava ack");
- }
- }
- } catch (Exception e) {
- System.err.println("Client exception: " + e.toString());
- e.printStackTrace();
- }
- }
- return "";
- }
- public static LinkedList<Message> LoadMessages(File f,String s) throws IOException {
- int index;
- String ts, Mes;
- int t;
- Message m;
- LinkedList<Message> list = new LinkedList<Message>();
- FileInputStream fis = new FileInputStream(f);
- //Construct BufferedReader from InputStreamReader
- BufferedReader br = new BufferedReader(new InputStreamReader(fis));
- String line = null;
- while ((line = br.readLine()) != null) {
- try{
- index = line.indexOf(',');
- if (index>0){
- ts = line.substring(0,index);
- Mes = line.substring(index+1,line.length());
- t = Integer.parseInt(ts);
- m = new Message(t, Integer.valueOf(s),Mes,false);
- list.add(m);
- }
- }catch (Exception e) {
- System.err.println("Load exception: " + e.toString());
- e.printStackTrace();
- }
- }
- br.close();
- return list;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement