Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Correction of some bugs and performance enhancement.
[jaceP2P.git] / src / jaceP2P / JaceSession.java
1 package jaceP2P;
2
3 public class JaceSession {
4
5         // attributes
6         private static JaceSession Instance;
7         private/* static */Task taskObject = null;
8         private/* static */Thread taskThread = null;
9
10         // constructors
11         private JaceSession() {
12         }
13
14         public static JaceSession Instance() {
15                 if (Instance == null) {
16                         Instance = new JaceSession();
17                 }
18                 return Instance;
19         }
20
21         // nettoie les files de comm si pas vides
22         public synchronized void init() {
23                 // JaceBuffer.Instance().purge();
24                 // MsgQueue.Instance().purge();
25                 taskObject = null;
26                 taskThread = null;
27         }
28
29         public synchronized void addTaskObject(Task obj) {
30                 taskObject = obj;
31         }
32
33         public synchronized Task getTaskObject() {
34                 return taskObject;
35         }
36
37         public void addTaskThread(Thread th) {
38                 taskThread = th;
39         }
40
41         public Thread getTaskThread() {
42                 return taskThread;
43         }
44
45         @SuppressWarnings("deprecation")
46         public void deleteTaskThread() {
47                 try {
48                         // not to put this because it crashes !!!!
49                         // taskThread.yield();
50                         // taskThread.interrupt();
51                         System.out.println("I m going to kill the thread");
52                         // taskThread.join();
53                         taskThread.stop();
54                         System.out.println("Thread stopped without problem.");
55                         taskThread = null;
56                 } catch (Exception e) {
57                         System.err.println("ERROR : thread not join : " + e);
58                         System.err.println("I m going to kill the thread");
59                         try {
60                                 taskThread.stop();
61                                 System.out.println("Thread stopped after a problem !");
62                                 taskThread = null;
63                         } catch (Exception e2) {
64                                 System.err.println("ERROR thread not killed : " + e);
65                         }
66
67                 }
68         }
69
70         // clean JaceBuffer, MsgQueue, Register
71         // and delete taskObject and taskThread
72         public synchronized void kill() {
73                 // JaceBuffer.Instance().purge();
74                 MsgQueue.Instance().purge();
75
76                 Register.Instance().purge();
77
78                 taskObject = null;
79                 deleteTaskThread();
80                 System.out.println("I SET MY TASK at NUULL");
81
82                 System.out.println("Size of MsgQueue : "
83                                 + MsgQueue.Instance().getSize());
84
85                 Instance = null;
86         }
87 }