-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuiClient.java
195 lines (157 loc) · 6.95 KB
/
GuiClient.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
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
/**This program obtains a reference of the remote
* object of server and invokes remote methods.
* This is the GUI version of the Client
* that is more advanced and has additional functionalities
*/
import java.awt.image.BufferedImage;
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
import java.awt.*; // Using AWT container and component classes
import java.awt.event.*; // Using AWT event classes and listener interfaces
import javax.swing.*;
import java.io.*;
import javax.imageio.*;
/******** A GUI program using container java.awt.Frame ************/
public class GuiClient extends JFrame implements ActionListener {
private Label lblInput; // Declare input Label
private Label lblOutput; // Declare output Label
private Label lbIP; // Declare IP Label
private TextField tfIP; // Declare IP TextField
private TextField tfInput; // Declare input TextField
private TextField tfOutput; // Declare output TextField
private Button Butqr; // Declare the Button to generate QR code
private Button Butstr; // Declare the Button to stream
private Button Butdow; // Declare the Button to download
private String link; // Input link
private String vlink; // Output link
private String ipadd; // IP address of the server
public GuiClient() {
setLayout(new FlowLayout());
/**** We define the textfields and buttons*******/
lbIP = new Label("Enter the IP address of the server: "); // Construct Label
add(lbIP);
tfIP = new TextField(110); // Construct TextField
add(tfIP); // "super" Frame adds TextField
tfIP.setText("10.16.31.163"); // Set a default value
tfIP.addActionListener(this);
// Hitting Enter on TextField fires ActionEvent
// tfInput (TextField) registers this instance as ActionEvent listener
lblInput = new Label("Enter a link below: ");
add(lblInput);
tfInput = new TextField(110);
add(tfInput);
tfInput.setText("http://vodlocker.com/budq9rt5wt0e");
tfInput.addActionListener(this);
lblOutput = new Label("The download link is below: ");
add(lblOutput);
tfOutput = new TextField(110);
tfOutput.setEditable(false); // read-only
add(tfOutput);
/**************Stream***************/
Butstr = new Button("Stream"); // construct Button
add(Butstr); // "super" Frame adds Button
Butstr.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
streamvidlink(getvidlink());
tfOutput.setText("The video is being streamed");
}
});
/**************Download***************/
Butdow = new Button("Download");
add(Butdow);
Butdow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
downvidlink(getvidlink());
tfOutput.setText("The video is being downloaded");
}
});
/**************QR CODE***************/
Butqr = new Button("Qr Code");
add(Butqr);
Butqr.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// we load the qrvidlink() function that saves the image locally
qrvidlink();
try {
// we display the image that is saved
Runtime.getRuntime().exec("display qrcode.png");
tfOutput.setText("The Qr code will be generated");}
catch (Exception ex){
System.out.println("Qr Code failed: " + ex);
}
}
});
setTitle("Client"); // "super" Frame sets title
setSize(900, 400); // "super" Frame sets initial window size
setVisible(true); // "super" Frame shows
}
public static void main(String[] args)
{
// Invoke the constructor to setup the GUI, by allocating an anonymous instance
new GuiClient();
}
/*********************************************************************/
/******** functionality when we press enter on the text field *******/
/*******************************************************************/
@Override
public void actionPerformed(ActionEvent evt) {
vlink = getvidlink(); // it gets the video link from the text input
tfOutput.setText(vlink); // and tries to set the direct link to the video
}
/**********************************************************************************/
/************ Here we define the functionality of the buttons ********************/
/********************************************************************************/
// we define the method for getting a video link when pressing enter
public String getvidlink() {
try {
System.setSecurityManager(new SecurityManager());
ipadd= tfIP.getText();
Interface client = (Interface)Naming.lookup("rmi://"+ipadd+"/getvid");
// Get the String entered into the TextField tfInput, convert to int
link = tfInput.getText();
vlink = client.getvid(link);
}catch (Exception e) {
System.out.println("[System] Server failed: " + e);
}
return vlink;
}
// we define the method for getting a streaming link when pressing on the stream button
public void streamvidlink(String msg) {
try {
Runtime.getRuntime().exec("smplayer " +msg); }
catch (Exception e)
{
System.out.println("Streaming with smplayer failed: " + e);
}
}
// we define the method for downloading when pressing on the download button
public void downvidlink(String msg) {
try {
Runtime.getRuntime().exec("wget -c " +msg); }
catch (Exception e)
{
System.out.println("Downloading with wget failed: " + e);
}
}
// we define the method for downloading when pressing on the download button
public void qrvidlink() {
try {
System.setSecurityManager(new SecurityManager());
ipadd= tfIP.getText();
Interface client = (Interface)Naming.lookup("rmi://"+ipadd+"/getvid");
// Get the String entered into the TextField tfInput, convert to int
link = tfInput.getText();
vlink = client.getvid(link);
// here we receive the image serialized into bytes from the server and
// saved it on the client as png image
byte[] bytimg = client.qrvid(vlink);
BufferedImage img = ImageIO.read(new ByteArrayInputStream(bytimg));
File outputfile = new File("qrcode.png");
ImageIO.write(img,"png",outputfile);
//img= new ImageIcon(bytimg.toByteArray());
}catch (Exception e) {
System.out.println("[System] Server failed: " + e);
}
}
}