-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRMI_BioAPI_Demo.java
More file actions
202 lines (170 loc) · 6.68 KB
/
Copy pathRMI_BioAPI_Demo.java
File metadata and controls
202 lines (170 loc) · 6.68 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.rmi.RemoteException;
import java.util.Scanner;
public class RMI_BioAPI_Demo {
// The client ip address that the socket listener listens at
private String socket_listener_ip;
// *** This section is for multi-threading initialization
private final class RequestThread extends Thread {
// To decide which thread to create
private String option;
// The ip address of the rmi server
private String AsteriskJava_IP;
// Useless, need to get rid of
private String Service_UID;
// Name of remote file on server side
private String remote_AsteriskSrcFilename;
// The client ip address that the socket listener listens at (variable
// for thread)
private String socket_listener_ip;
// Port of client socket
private int socket_port;
// Name of local file (file will create one if not available)
private String local_fileName;
RequestThread(final String option, final String AsteriskJava_IP, final String Service_UID,
final String remote_AsteriskSrcFilename, final String socket_listener_ip, final int socket_port,
final String local_fileName) {
this.option = option;
this.AsteriskJava_IP = AsteriskJava_IP;
this.Service_UID = Service_UID;
this.remote_AsteriskSrcFilename = remote_AsteriskSrcFilename;
this.socket_listener_ip = socket_listener_ip;
this.socket_port = socket_port;
this.local_fileName = local_fileName;
this.start();
}
public void run() {
// If socket, create a socket thread
if (option.equals("socket")) {
try {
initialize_socket(socket_port, local_fileName);
} catch (Exception e) {
System.out.println(e.getMessage() + " Error on initializing socket server");
}
}
// If AsteriskJava, create new client instance
if (option.equals("AsteriskJava")) {
new RMI_BioAPI_AsteriskJava_Client(AsteriskJava_IP, Service_UID, remote_AsteriskSrcFilename,
socket_listener_ip, socket_port, local_fileName);
}
// Runs the above operations simultaneously as multi-threads
}
}// Request Thread end
// --------------------------------------------------------------------------------------
private PrintWriter pw;
private BufferedReader br;
private Socket socket;
private ServerSocket serverSocket = null;
public void initialize_socket(int port, String local_fileName) {
try {
// Create client side server socket bound to specified port
serverSocket = new ServerSocket(port);
// Display ip and port - for testing purposes
String addr = serverSocket.getInetAddress().toString();
String addrr = serverSocket.getLocalSocketAddress().toString();
System.out.println("IP address of client socket: " + addr);
System.out.println("Client socket address(IP address: port): " + addrr);
initialize_socket_stream_buffer();
socket_listener(local_fileName);
serverSocket.close();
} catch (IOException e) {
System.err.println("Could not listen on port: " + port + e.getMessage());
}
}
public void initialize_socket_stream_buffer() {
try {
// Create socket and enable listening using serverSocket
socket = serverSocket.accept();
System.out.println("Server socket (Client) is ready to recieve response from server");
// Init reader and writer
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(socket.getOutputStream(), true);
} catch (IOException e) {
e.printStackTrace();
}
}
public void socket_listener(String local_fileName) {
System.out.println("Recieving data from server: ");
String inputln;
PrintWriter outStream = null;
// Find/create file and create printwriter
try {
// For displaying where the file ended up
// Need to designate a location to find the client files ?
File f = new File("local_fileName");
System.out.println(local_fileName + " found " + f.getAbsolutePath());
outStream = new PrintWriter(new FileOutputStream(local_fileName));
System.out.println("Local file found in this location: " + outStream.toString());
} catch (FileNotFoundException e1) {
System.out.println("Failed to open file to write to. File name was: " + local_fileName);
e1.printStackTrace();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
try {
while ((inputln = br.readLine()) != "Done" && inputln != null) {
System.out.println(inputln);
// Write lines received to local file
outStream.println(inputln);
}
} catch (IOException e) {
e.printStackTrace();
}
outStream.close();
socket_stream_buffer_close();
}
public void socket_stream_buffer_close() {
pw.close();
try {
br.close();
Scanner reader = new Scanner(System.in); // Reading from System.in
System.out.println("Enter any string to continue.. ");
String temp = reader.next();
if (temp != null)
reader.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/** RMI_BioAPI_Demo constructor **/
public RMI_BioAPI_Demo(String local_fileName, int client_socket_port, String AsteriskJava_IP, String Service_UID,
String remote_AsteriskSrcFilename) throws RemoteException {
// Get system ip address
try {
socket_listener_ip = InetAddress.getLocalHost().getHostAddress();
System.out.println("Socket listener ip is: " + socket_listener_ip);
} catch (UnknownHostException e) {
e.printStackTrace();
}
// Create new threads, socket, and RMI_BioAPI_Asterisk_Client ( looks up remote object and invokes method on rm)
new RequestThread("socket", "N/A", "N/A", "N/A", socket_listener_ip, client_socket_port, local_fileName);
new RequestThread("AsteriskJava", AsteriskJava_IP, Service_UID, remote_AsteriskSrcFilename, socket_listener_ip,
client_socket_port, local_fileName);
System.out.println("RMI_BioAPI_Demo instance is created. Local file name: " + local_fileName
+ ". Remote file name: " + remote_AsteriskSrcFilename);
}
public static void main(String[] args) throws Exception {
if (args.length != 5) {
System.out.println(
"Syntax - java RMI_BioAPI_Demo <local_Filename> <host_port> <Remote_AsteriskJava_IP> <service_UID> <remote_source_Filename>");
System.exit(1);
}
// Create an instance of our service server ...
System.out.println("This is the arg[0]: " + args[0]);
RMI_BioAPI_Demo demo_instance = new RMI_BioAPI_Demo(args[0], Integer.parseInt(args[1]), args[2], args[3],
args[4]);
}
//java -Djava.security.manager -Djava.security.policy=rmi.policy
}