-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoffeeCamControl.java
More file actions
228 lines (205 loc) · 7.31 KB
/
CoffeeCamControl.java
File metadata and controls
228 lines (205 loc) · 7.31 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* Coffee Cam Control Client Application
*/
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Properties;
public class CoffeeCamControl extends Frame implements WindowListener,ActionListener {
private final int APP_REVISION = 4;
private final int CCCP_REVISION = 1;
private JFrame mainWindow;
private JTextArea inputBox;
private JTextArea outputBox;
private JButton buttonSend;
private JButton buttonRetrieve;
private Socket camSocket;
// Class Constructor
public CoffeeCamControl() {
try {
Properties config = new Properties();
// Load configuration from jar file
config.load(getClass().getClassLoader().getResourceAsStream("CoffeeCamControl.cfg"));
String ip = config.getProperty("server_ip");
String welcomeMessage = config.getProperty("welcome_message");
if (ip != null) {
setupGUI(welcomeMessage);
openSocket(ip);
}
else {
System.out.println("Missing value in CoffeeCamControl.cfg!");
}
}
catch (Exception e) {
System.out.println("Error parsing file CoffeeCamControl.cfg: "+e.toString());
}
}
private void setupGUI(String welcomeMessage) {
mainWindow = new JFrame("CoffeeCamControl - r" + APP_REVISION);
mainWindow.setMinimumSize(new Dimension(600,200));
inputBox = new JTextArea();
inputBox.setMinimumSize(new Dimension(520, 100));
// Map Ctrl-Enter and Shift-Enter of input Box to send and receive
InputMap input = inputBox.getInputMap();
input.put(KeyStroke.getKeyStroke("shift ENTER"), "sendMessageAction");
input.put(KeyStroke.getKeyStroke("control ENTER"), "retrieveMessageAction");
ActionMap actions = inputBox.getActionMap();
actions.put("sendMessageAction", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
sendText();
}
});
actions.put("retrieveMessageAction", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
retrieveText();
}
});
buttonSend = new JButton("Send");
buttonRetrieve = new JButton("Retrieve");
buttonSend.setMinimumSize(new Dimension(200, 50));
buttonSend.setPreferredSize(new Dimension(200, 50));
buttonSend.setEnabled(false);
buttonRetrieve.setMinimumSize(new Dimension(200, 50));
buttonRetrieve.setPreferredSize(new Dimension(200, 50));
buttonRetrieve.setEnabled(false);
buttonSend.addActionListener(this);
buttonRetrieve.addActionListener(this);
outputBox = new JTextArea();
outputBox.setMinimumSize(new Dimension(600, 100));
outputBox.setEditable(false);
outputBox.setBackground(mainWindow.getBackground());
outputBox.setText(welcomeMessage);
outputBox.setLineWrap(true);
outputBox.setWrapStyleWord(true);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
JPanel buttonPanel = new JPanel(new GridLayout(2,1,4,4));
buttonPanel.setPreferredSize(new Dimension(200,100));
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.LINE_AXIS));
JScrollPane outputPanel = new JScrollPane(outputBox);
outputPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
buttonPanel.add(buttonSend);
buttonPanel.add(buttonRetrieve);
inputPanel.add(inputBox);
inputPanel.add(buttonPanel);
mainPanel.add(inputPanel);
mainPanel.add(outputPanel);
//mainWindow.setResizable(false);
mainWindow.add(mainPanel);
mainWindow.setVisible(true);
mainWindow.addWindowListener(this);
}
private void openSocket(String ip) {
try {
outputBox.append("Trying to connect to " + ip + "...");
camSocket = new Socket(ip, 1236);
// Do handshake
BufferedReader fromServer = new BufferedReader(new InputStreamReader(camSocket.getInputStream()));
String challenge = fromServer.readLine();
if (challenge.equals("Coffee?")) {
PrintStream toServer = new PrintStream(camSocket.getOutputStream(), true, "UTF-8");
toServer.println("o-)");
buttonSend.setEnabled(true);
buttonRetrieve.setEnabled(true);
outputBox.append("connected. Send /cccp for available commands.\n");
outputBox.setCaretPosition(outputBox.getDocument().getLength());
}
else {
camSocket.close();
camSocket = null;
outputBox.append("Cannot find a CoffeeCam server on the specified IP and port.\n");
outputBox.setCaretPosition(outputBox.getDocument().getLength());
}
}
catch (Exception e) {
camSocket = null;
outputBox.append("cannot connect. Please check server IP in CoffeeCamControl.cfg.\n");
outputBox.setCaretPosition(outputBox.getDocument().getLength());
}
}
// Send message and parse response.
// TODO: Currently, the client will hang if nothing is received.
private void send(String text) {
if (camSocket != null) {
try {
if (!text.startsWith("/")) {
// Use /ccsay by default
text = "/ccsay " + text;
}
else {
// when the input is a command, clear the input box
inputBox.setText("");
}
text = text.replaceAll("\\\n","\\$_");
PrintStream camTextWriter = new PrintStream(camSocket.getOutputStream(), true, "UTF-8");
camTextWriter.println(text);
BufferedReader response = new BufferedReader(new InputStreamReader(camSocket.getInputStream()));
String responseText = response.readLine();
if (responseText != null) {
if (responseText.startsWith("[CCRET] ")) {
responseText = responseText.replaceAll("\\$_","\\\n");
inputBox.setText(responseText.substring(8));
}
else if (!responseText.equals("[CCOK]")) {
outputBox.append(responseText+"\n");
outputBox.setCaretPosition(outputBox.getDocument().getLength());
}
}
else {
outputBox.append("No response from server - broken connection? Please restart and try again.");
outputBox.setCaretPosition(outputBox.getDocument().getLength());
buttonSend.setEnabled(false);
buttonRetrieve.setEnabled(false);
}
}
catch (IOException e) {
outputBox.append("Cannot send message: "+e.toString());
outputBox.setCaretPosition(outputBox.getDocument().getLength());
}
}
}
// Action Listeners for the buttons
public void actionPerformed(ActionEvent event) {
if (event.getSource() == buttonSend) {
sendText();
}
else if (event.getSource() == buttonRetrieve) {
retrieveText();
}
}
private void sendText() {
send(inputBox.getText());
}
private void retrieveText() {
send("/ccget");
}
// Action Listeners for window events
public void windowClosing(WindowEvent event) {
try {
if (camSocket != null) {
send("/ccdisconnect");
camSocket.close();
}
}
catch (IOException e) {
}
dispose();
System.exit(0);
}
// These do nothing at the moment
public void windowOpened(WindowEvent event) {}
public void windowActivated(WindowEvent event) {}
public void windowIconified(WindowEvent event) {}
public void windowDeiconified(WindowEvent event) {}
public void windowDeactivated(WindowEvent event) {}
public void windowClosed(WindowEvent event) {}
// Main method
// Just instantiate the application
public static void main(String argv[]) {
CoffeeCamControl ccc = new CoffeeCamControl();
}
}