-
Notifications
You must be signed in to change notification settings - Fork 0
/
CTCPLinkClient.java
104 lines (98 loc) · 3.38 KB
/
CTCPLinkClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.Semaphore;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* This class is in charge of sending messages.
*
*/
public class CTCPLinkClient {
private Socket m_socket;
private String m_strRemoteIP;
private int m_nRemotePort;
private int m_nSourceProcessId;
private int m_nDestProcessId;
private ObjectOutputStream m_obj_out_stream;
//private DataOutputStream m_data_out_stream;
private Semaphore m_mutex;
public CTCPLinkClient(String x_strIP,int x_nPort,int x_nSourceProcessId,int x_nDestProcessId)
{
m_strRemoteIP = x_strIP;
m_nRemotePort = x_nPort;
m_nSourceProcessId = x_nSourceProcessId;
m_nDestProcessId = x_nDestProcessId;
m_mutex = new Semaphore(1);
}
public boolean Initialize()
{
boolean bRet = false;
try
{
m_socket = new Socket(m_strRemoteIP,m_nRemotePort);
m_obj_out_stream = new ObjectOutputStream(m_socket.getOutputStream());
//m_data_out_stream = new DataOutputStream(m_socket.getOutputStream());
bRet = true;
}
catch (IOException ex)
{
//ex.printStackTrace();
}
return bRet;
}
public void Stop()
{
try
{
m_socket.close();
}
catch (IOException ex)
{
}
}
//Sends a packet with Message type MT_NOTHING. This message is used for initialization handshake to make node 0 sure that all the
// other nodes have created their connections and are ready to start entering the critical section
public void Send_Identification_Packet()
{
try
{
CInterProcessMessage msg = new CInterProcessMessage(m_nSourceProcessId, m_nDestProcessId, new CTimeStamp(-1,-1), CInterProcessMessage.EMessageType.MT_NOTHING);
m_mutex.acquire();
//m_data_out_stream.writeBytes(msg.ToString()+"\n");
m_obj_out_stream.writeObject(msg);
m_mutex.release();
}
catch (IOException ex) {
ex.printStackTrace();
} catch (InterruptedException ex) {
Logger.getLogger(CTCPLinkClient.class.getName()).log(Level.SEVERE, null, ex);
}
}
// This method is used for REQ and REP messages.
public void Send(CInterProcessMessage x_msg)
{
try
{
m_mutex.acquire();
m_obj_out_stream.writeObject(x_msg);
//m_data_out_stream.writeBytes(x_msg.ToString()+"\n");
m_mutex.release();
}
catch(IOException ex)
{
ex.printStackTrace();
} catch (InterruptedException ex) {
Logger.getLogger(CTCPLinkClient.class.getName()).log(Level.SEVERE, null, ex);
}
}
}