Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Correction of some bugs and performance enhancement.
[jaceP2P.git] / src / jaceP2P / Sender.java
1 package jaceP2P;
2
3 public class Sender extends Thread {
4
5         // attributes
6         static Sender Instance = null;
7         protected JaceBuffer buffer;
8         Message msg = null;
9         boolean msgIsSent; // attribute used in the JaceSender to know if msg has
10                                                 // been sent
11         JaceInterface stub = null;
12         boolean running;
13
14         // constructors
15         public static Sender Instance() {
16                 if (Instance == null) {
17                         Instance = new Sender();
18                 }
19                 return Instance;
20         }
21
22         public Sender() {
23                 setPriority(MAX_PRIORITY);
24                 running = true;
25                 // buffer=new JaceBuffer();
26                 Instance = this;
27         }
28
29         public static void setInstance(SenderRmi s) {
30                 Instance = s;
31         }
32
33         public static void setInstance(SenderSocket s) {
34                 Instance = s;
35         }
36
37         // methods
38         public void kill() {
39                 Instance = null;
40         }
41
42         public JaceBuffer getBuffer() {
43                 return buffer;
44         }
45
46         public void run() {
47                 msg = null;
48                 while (running) {
49                         // consumme the msg to send (doing a wait, so the msg is processed
50                         // immediately)
51                         // System.out.println("the sender thread is alive");
52                         msg = buffer.get();
53
54                         if (msg.getReceiver() == null) {
55                                 System.err.println("In jaceSend recv = null !!!!!");
56                                 // System.out.println("msg.getReceiver() : " +
57                                 // msg.getReceiver());
58
59                                 System.err.println("Rkecv is null in msg of SENDER "
60                                                 + LocalHost.Instance().getName());
61
62                         }
63
64                         // if no destinatory, I send nothing
65                         if (msg.getReceiver().getHostStub() == null) {
66                                 System.out.println("SENDER : the dest is null so I send nothing, the msg is simply lost");
67                                 yield();
68                         }
69
70                         // if there is a destinatory,
71                         else {
72                                 msgIsSent = false;
73                                 System.out.println(msg.getReceiver().getHostName()) ;
74                                 System.out.println(msg.getSender().getHostName()) ;
75                                 
76                                 // sending of the message !
77                                 // stub =
78                                 // RemoteStubs.Instance().lookUp(msg.getReceiver().getHostIP()).getStub();
79                                 stub = msg.getReceiver().getHostStub();
80                                 // if (stub == null) {
81                                 // System.out.println("SENDER : stub nuuuuuuuuuuuuuuuuuuuuuuuuuuull");
82                                 // }
83                                 try {
84                                         // System.out.println("tryin to send to " +
85                                         // msg.getReceiver().getRank() + "(" +
86                                         // Register.Instance().getListeOfTasks().getTaskIdOfRank(msg.getReceiver().getRank()).getHostName()
87                                         // + ")");
88                                         stub.iSendYou(msg);
89                                         
90                                         
91
92                                         // System.out.println("ENVOI succesful to " +
93                                         // msg.getReceiver().getRank() + "(" +
94                                         // Register.Instance().getListeOfTasks().getTaskIdOfRank(msg.getReceiver().getRank()).getHostName()
95                                         // + ")");
96                                         // System.out.println("envoi succesful to " +
97                                         // msg.getReceiver().getHostIP());
98                                         msgIsSent = true;
99                                         yield();
100                                 } catch (Exception e) {
101                                         System.out.println(e);
102
103                                         // System.out.println("SENDER : the node of task " +
104                                         // msg.getReceiver().getHostIP() + " is dead !!!! " + e);
105                                         // System.out.println("SENDER : the dest is dead so I send nothing, message lost");
106                                         yield();
107                                 }
108                         }
109                 }
110         }
111 }