-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcRRrun.java
201 lines (165 loc) · 7.3 KB
/
cRRrun.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
196
197
198
199
200
201
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
public class cRRrun extends JFrame {
private JFrame frame;
private JTextField userIDField;
private JTextField jobIDField;
private JTextField durationField;
private JTextField deadlineField;
private Socket socket;
public cRRrun() {
super("Computation Resource Requester");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
createWelcomePanel();
setLocationRelativeTo(null);
setVisible(true);
initializeSocket();
}
private void createWelcomePanel() {
JPanel welcomePanel = new JPanel(new BorderLayout());
getContentPane().removeAll();
add(welcomePanel);
JTextArea summaryTextArea = new JTextArea("This is the Computational Resource Requester page where you can request a job.");
summaryTextArea.setEditable(false);
summaryTextArea.setWrapStyleWord(true);
summaryTextArea.setLineWrap(true);
summaryTextArea.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
welcomePanel.add(summaryTextArea, BorderLayout.NORTH);
JPanel inputPanel = new JPanel(new GridBagLayout());
userIDField = new JTextField(10);
jobIDField = new JTextField(10);
durationField = new JTextField(10);
deadlineField = new JTextField(10);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(0, 0, 5, 5);
gbc.anchor = GridBagConstraints.WEST;
inputPanel.add(new JLabel(" User ID (Number):"), gbc);
gbc.gridx++;
gbc.insets = new Insets(0, 5, 5, 0);
gbc.anchor = GridBagConstraints.WEST;
inputPanel.add(userIDField, gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.insets = new Insets(5, 0, 0, 5);
gbc.anchor = GridBagConstraints.WEST;
inputPanel.add(new JLabel(" Job ID (Number):"), gbc);
gbc.gridx++;
gbc.insets = new Insets(5, 5, 0, 0);
gbc.anchor = GridBagConstraints.WEST;
inputPanel.add(jobIDField, gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.insets = new Insets(5, 0, 0, 5);
gbc.anchor = GridBagConstraints.WEST;
inputPanel.add(new JLabel(" Job Duration (Hours):"), gbc);
gbc.gridx++;
gbc.insets = new Insets(5, 5, 0, 0);
gbc.anchor = GridBagConstraints.WEST;
inputPanel.add(durationField, gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.insets = new Insets(5, 0, 0, 5);
gbc.anchor = GridBagConstraints.WEST;
inputPanel.add(new JLabel(" Deadline (mm/dd/yyyy):"), gbc);
gbc.gridx++;
gbc.insets = new Insets(5, 5, 0, 0);
gbc.anchor = GridBagConstraints.WEST;
inputPanel.add(deadlineField, gbc);
JPanel buttonPanel = new JPanel();
buttonPanel.add(Box.createHorizontalStrut(85));
JButton submitJobButton = new JButton("Submit");
submitJobButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
submitJob();
}
});
buttonPanel.add(submitJobButton);
JPanel centeringPanel = new JPanel(new GridBagLayout());
gbc.gridx = 0;
gbc.gridy = 0;
centeringPanel.add(inputPanel, gbc);
gbc.gridy++;
centeringPanel.add(buttonPanel, gbc);
welcomePanel.add(centeringPanel, BorderLayout.CENTER);
revalidate();
repaint();
}
private void initializeSocket() {
try {
// Replace "localhost" and 8080 with the actual server address and port
socket = new Socket("localhost", 8080);
System.out.println("Connected to Server!");
//OutputStream outputStream = socket.getOutputStream();
//objectOutputStream = new ObjectOutputStream(outputStream);
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(frame, "Error connecting to the server: " + e.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}
}
private void sendInfo(String info) {
try {
// Send the information to the server
OutputStream outputStream = socket.getOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeUTF(info);
objectOutputStream.flush();
// Wait for a response from the server
ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
String serverResponse = (String) objectInputStream.readObject();
// Show the appropriate success or failure message based on the server's response
if (serverResponse.equals("accepted")) {
JOptionPane.showMessageDialog(frame, "The server has accepted the request!\nThank you for using our computation services",
"Success", JOptionPane.INFORMATION_MESSAGE);
} else if (serverResponse.equals("rejected")) {
JOptionPane.showMessageDialog(frame, "The server has rejected the request.\nPlease call the lot's office.",
"Error", JOptionPane.ERROR_MESSAGE);
} else {
JOptionPane.showMessageDialog(frame, "Unexpected server response: " + serverResponse,
"Error", JOptionPane.ERROR_MESSAGE);
}
socket.close();
initializeSocket();
} catch (IOException e) {
JOptionPane.showMessageDialog(frame, "Error sending information to server: " + e.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(frame, "Error sending information to server: " + e.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}
}
private void submitJob() {
try {
int userID = Integer.parseInt(userIDField.getText());
int jobID = Integer.parseInt(jobIDField.getText());
int duration = Integer.parseInt(durationField.getText());
String deadline = deadlineField.getText();
// Construct the information string
String info = "Job Information: " + userID + " " + jobID + " " + duration + " " + deadline;
// Send the information to the server
sendInfo(info);
userIDField.setText("");
jobIDField.setText("");
durationField.setText("");
deadlineField.setText("");
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Invalid input. Please enter valid numbers.");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new cRRrun();
});
}
}