Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. package exercise1;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.rmi.AlreadyBoundException;
  9. import java.rmi.RemoteException;
  10. import java.rmi.registry.LocateRegistry;
  11. import java.rmi.registry.Registry;
  12. import java.rmi.server.UnicastRemoteObject;
  13. import java.util.LinkedList;
  14. import java.util.Scanner;
  15.  
  16. public class My_Process implements Runnable, PrInter {
  17. public static int timestamp = 1;
  18. public static int process_id;
  19. public static LinkedList<Message> Received_Messages = new LinkedList<Message>();
  20. public static LinkedList<Message> fifo;
  21.  
  22. public My_Process(){};
  23.  
  24. public static void main(String args[]) {
  25.  
  26. LinkedList<Message> ls;
  27.  
  28. try {
  29.  
  30. //String f = "C:/Users/Kostas/workspace/rmi/bin/exercise1/Messages"+args[2]+".txt";
  31.  
  32. process_id = Integer.parseInt(args[2]);
  33.  
  34. String f = "Messages"+args[2]+".txt";
  35. File file = new File(f);
  36. ls = LoadMessages(file,args[2]);
  37. fifo = ls;
  38.  
  39. Thread.sleep(4000);
  40.  
  41. }
  42. catch(IOException ioe) {
  43. ioe.printStackTrace();
  44. } catch (InterruptedException e) {
  45. // TODO Auto-generated catch block
  46. e.printStackTrace();
  47. }
  48. Scanner reader = new Scanner(System.in); // Reading from System.in
  49. System.out.println("Enter a number: ");
  50. int n = reader.nextInt(); // Scans the next token of the input as an int.
  51. //once finished
  52. reader.close();
  53. //send implemented in main
  54. (new Thread(new My_Process())).start();
  55. for (Message temp : fifo) {
  56. try {
  57. Registry registry = LocateRegistry.getRegistry(null);
  58. for(int i=1;i<3;i++){
  59. if(process_id!=i){
  60. PrInter stub = (PrInter) registry.lookup("PrInter"+i);
  61. String response = stub.Receive_Message(temp);
  62. System.out.println("response: " + response);
  63. }
  64. }
  65.  
  66. } catch (Exception e) {
  67. System.err.println("Client exception: " + e.toString());
  68. e.printStackTrace();
  69. }
  70. }
  71.  
  72. while(true);
  73. }
  74.  
  75. @Override
  76. public void run() {
  77. //Receive part implemented in thread
  78. System.out.println("Receiver thread");
  79.  
  80. try {
  81. My_Process obj = new My_Process();
  82. PrInter stub = (PrInter) UnicastRemoteObject.exportObject(obj, 0);
  83.  
  84. // Bind the remote object's stub in the registry
  85. Registry registry = LocateRegistry.getRegistry();
  86. registry.rebind("PrInter"+process_id, stub);
  87.  
  88. } catch (RemoteException e) {
  89. // TODO Auto-generated catch block
  90. e.printStackTrace();
  91. }
  92.  
  93. }
  94.  
  95. @Override
  96. public String Receive_Message(Message m) throws RemoteException {
  97. // TODO Auto-generated method stub
  98. System.out.println("Received msg from: " + m.get_pid() + "with ts: " + m.get_ts());
  99. if(m.get_is_ack()==false){
  100. Received_Messages.add(m);
  101. // System.out.print("Received messages:");
  102. // for(Message temp : Received_Messages){
  103. // System.out.println(temp.get_pid());
  104. // }
  105. }
  106. else{
  107. try {
  108. Registry registry = LocateRegistry.getRegistry(null);
  109. for(int i=1;i<3;i++){
  110. if(m.get_pid()!=i){
  111. PrInter stub = (PrInter) registry.lookup("PrInter"+i);
  112. m.set_ack(true);
  113. String response = stub.Receive_Message(m);
  114. System.out.println("Elava ack");
  115. }
  116. }
  117.  
  118. } catch (Exception e) {
  119. System.err.println("Client exception: " + e.toString());
  120. e.printStackTrace();
  121. }
  122. }
  123.  
  124. return "";
  125. }
  126.  
  127. public static LinkedList<Message> LoadMessages(File f,String s) throws IOException {
  128. int index;
  129. String ts, Mes;
  130. int t;
  131. Message m;
  132. LinkedList<Message> list = new LinkedList<Message>();
  133. FileInputStream fis = new FileInputStream(f);
  134.  
  135. //Construct BufferedReader from InputStreamReader
  136. BufferedReader br = new BufferedReader(new InputStreamReader(fis));
  137.  
  138. String line = null;
  139. while ((line = br.readLine()) != null) {
  140. try{
  141.  
  142. index = line.indexOf(',');
  143. if (index>0){
  144. ts = line.substring(0,index);
  145. Mes = line.substring(index+1,line.length());
  146.  
  147. t = Integer.parseInt(ts);
  148.  
  149.  
  150. m = new Message(t, Integer.valueOf(s),Mes,false);
  151. list.add(m);
  152. }
  153. }catch (Exception e) {
  154. System.err.println("Load exception: " + e.toString());
  155. e.printStackTrace();
  156. }
  157. }
  158.  
  159. br.close();
  160.  
  161. return list;
  162. }
  163.  
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement