11 package com.google.scrollview;
16 import org.piccolo2d.nodes.PImage;
18 import java.io.BufferedReader;
19 import java.io.IOException;
20 import java.io.InputStreamReader;
21 import java.io.PrintStream;
22 import java.net.ServerSocket;
23 import java.net.Socket;
24 import java.util.ArrayList;
25 import java.util.regex.Pattern;
42 private static Socket socket;
43 private static PrintStream out;
44 public static BufferedReader
in;
49 private static ArrayList<SVWindow> windows;
50 private static Pattern intPattern;
51 private static Pattern floatPattern;
54 static int nrInputLines = 0;
57 static boolean debugViewNetworkTraffic =
false;
61 if (debugViewNetworkTraffic) {
62 System.out.println(
"(S->c) " + e.
toString());
67 byte [] utf8 = str.getBytes(
"UTF8");
68 out.write(utf8, 0, utf8.length);
69 }
catch (java.io.UnsupportedEncodingException ex) {
70 System.out.println(
"Oops... can't encode to UTF8... Exiting");
75 boolean error = out.checkError();
77 System.out.println(
"Connection error. Quitting ScrollView Server...");
91 private static void IOLoop() {
95 while (!socket.isClosed() && !socket.isInputShutdown() &&
96 !socket.isOutputShutdown() &&
97 socket.isConnected() && socket.isBound()) {
100 if (debugViewNetworkTraffic) {
101 System.out.println(
"(c->S," + nrInputLines +
")" + inputLine);
104 if (polylineSize > polylineScanned) {
107 boolean first =
true;
108 for (String coordStr : inputLine.split(
",")) {
109 int coord = Integer.parseInt(coordStr);
113 polylineYCoords[polylineScanned++] = coord;
120 processInput(inputLine);
125 catch (IOException e) {
126 System.out.println(
"Connection error. Quitting ScrollView Server...");
141 private static void parseArguments(String argList,
142 ArrayList<Integer> intList,
143 ArrayList<Float> floatList,
144 ArrayList<String> stringList,
145 ArrayList<Boolean> boolList) {
152 for (String argStr : argList.split(
",")) {
156 int length = str.length() + 1 + argStr.length();
157 StringBuilder appended =
new StringBuilder(length);
158 appended.append(str);
159 appended.append(
",");
160 appended.append(argStr);
161 str = appended.toString();
162 }
else if (argStr.length() == 0) {
165 char quote = argStr.charAt(0);
168 if (quote ==
'\'' || quote ==
'"') {
174 assert str.charAt(0) ==
'\'' || str.charAt(0) ==
'"';
175 int len = str.length();
176 if (len > 1 && str.charAt(len - 1) == str.charAt(0)) {
180 while (slash > 0 && str.charAt(slash - 1) ==
'\\')
182 if ((len - 1 - slash) % 2 == 0) {
185 stringList.add(str.substring(1, len - 1));
193 }
else if (floatPattern.matcher(argStr).matches()) {
195 floatList.add(Float.parseFloat(argStr));
196 }
else if (argStr.equals(
"true")) {
198 }
else if (argStr.equals(
"false")) {
200 }
else if (intPattern.matcher(argStr).matches()) {
202 intList.add(Integer.parseInt(argStr));
211 private static void processInput(String inputLine) {
212 if (inputLine == null) {
216 if (inputLine.charAt(0) ==
'w') {
218 String noWLine = inputLine.substring(1);
219 String[] idStrs = noWLine.split(
"[ :]", 2);
220 int windowID = Integer.parseInt(idStrs[0]);
222 int start = inputLine.indexOf(
'(');
223 int end = inputLine.lastIndexOf(
')');
225 ArrayList<Integer> intList =
new ArrayList<Integer>(4);
226 ArrayList<Float> floatList =
new ArrayList<Float>(2);
227 ArrayList<String> stringList =
new ArrayList<String>(4);
228 ArrayList<Boolean> boolList =
new ArrayList<Boolean>(3);
229 parseArguments(inputLine.substring(start + 1, end),
230 intList, floatList, stringList, boolList);
231 int colon = inputLine.indexOf(
':');
232 if (colon > 1 && colon < start) {
234 String func = inputLine.substring(colon + 1, start);
235 if (func.equals(
"drawLine")) {
236 windows.get(windowID).drawLine(intList.get(0), intList.get(1),
237 intList.get(2), intList.get(3));
238 }
else if (func.equals(
"createPolyline")) {
239 windows.get(windowID).createPolyline(intList.get(0));
240 }
else if (func.equals(
"drawPolyline")) {
241 windows.get(windowID).drawPolyline();
242 }
else if (func.equals(
"drawRectangle")) {
243 windows.get(windowID).drawRectangle(intList.get(0), intList.get(1),
244 intList.get(2), intList.get(3));
245 }
else if (func.equals(
"setVisible")) {
246 windows.get(windowID).setVisible(boolList.get(0));
247 }
else if (func.equals(
"setAlwaysOnTop")) {
248 windows.get(windowID).setAlwaysOnTop(boolList.get(0));
249 }
else if (func.equals(
"addMessage")) {
250 windows.get(windowID).addMessage(stringList.get(0));
251 }
else if (func.equals(
"addMessageBox")) {
252 windows.get(windowID).addMessageBox();
253 }
else if (func.equals(
"clear")) {
254 windows.get(windowID).clear();
255 }
else if (func.equals(
"setStrokeWidth")) {
256 windows.get(windowID).setStrokeWidth(floatList.get(0));
257 }
else if (func.equals(
"drawEllipse")) {
258 windows.get(windowID).drawEllipse(intList.get(0), intList.get(1),
259 intList.get(2), intList.get(3));
260 }
else if (func.equals(
"pen")) {
261 if (intList.size() == 4) {
262 windows.get(windowID).pen(intList.get(0), intList.get(1),
263 intList.get(2), intList.get(3));
265 windows.get(windowID).pen(intList.get(0), intList.get(1),
268 }
else if (func.equals(
"brush")) {
269 if (intList.size() == 4) {
270 windows.get(windowID).brush(intList.get(0), intList.get(1),
271 intList.get(2), intList.get(3));
273 windows.get(windowID).brush(intList.get(0), intList.get(1),
276 }
else if (func.equals(
"textAttributes")) {
277 windows.get(windowID).textAttributes(stringList.get(0),
282 }
else if (func.equals(
"drawText")) {
283 windows.get(windowID).drawText(intList.get(0), intList.get(1),
285 }
else if (func.equals(
"addMenuBarItem")) {
286 if (boolList.size() > 0) {
287 windows.get(windowID).addMenuBarItem(stringList.get(0),
291 }
else if (intList.size() > 0) {
292 windows.get(windowID).addMenuBarItem(stringList.get(0),
296 windows.get(windowID).addMenuBarItem(stringList.get(0),
299 }
else if (func.equals(
"addPopupMenuItem")) {
300 if (stringList.size() == 4) {
301 windows.get(windowID).addPopupMenuItem(stringList.get(0),
307 windows.get(windowID).addPopupMenuItem(stringList.get(0),
310 }
else if (func.equals(
"update")) {
311 windows.get(windowID).update();
312 }
else if (func.equals(
"showInputDialog")) {
313 windows.get(windowID).showInputDialog(stringList.get(0));
314 }
else if (func.equals(
"showYesNoDialog")) {
315 windows.get(windowID).showYesNoDialog(stringList.get(0));
316 }
else if (func.equals(
"zoomRectangle")) {
317 windows.get(windowID).zoomRectangle(intList.get(0), intList.get(1),
318 intList.get(2), intList.get(3));
319 }
else if (func.equals(
"readImage")) {
320 PImage image = SVImageHandler.readImage(intList.get(2),
in);
321 windows.get(windowID).drawImage(image, intList.get(0), intList.get(1));
322 }
else if (func.equals(
"drawImage")) {
323 PImage image =
new PImage(stringList.get(0));
324 windows.get(windowID).drawImage(image, intList.get(0), intList.get(1));
325 }
else if (func.equals(
"destroy")) {
326 windows.get(windowID).destroy();
332 if (idStrs[1].startsWith(
"= luajava.newInstance")) {
333 while (windows.size() <= windowID) {
336 windows.set(windowID,
new SVWindow(stringList.get(1),
337 intList.get(0), intList.get(1),
338 intList.get(2), intList.get(3),
339 intList.get(4), intList.get(5),
345 }
else if (inputLine.startsWith(
"svmain")) {
347 if (inputLine.startsWith(
"svmain:exit")) {
366 public static void main(String[] args) {
367 if (args.length > 0) {
368 SERVER_PORT = Integer.parseInt(args[0]);
370 windows =
new ArrayList<SVWindow>(100);
371 intPattern = Pattern.compile(
"[0-9-][0-9]*");
372 floatPattern = Pattern.compile(
"[0-9-][0-9]*\\.[0-9]*");
376 ServerSocket serverSocket =
new ServerSocket(SERVER_PORT);
377 System.out.println(
"Socket started on port " + SERVER_PORT);
380 socket = serverSocket.accept();
381 System.out.println(
"Client connected");
384 out =
new PrintStream(socket.getOutputStream(),
true);
386 new BufferedReader(
new InputStreamReader(socket.getInputStream(),
388 }
catch (IOException e) {
static void main(String[] args)
static String receiveMessage()
static int polylineScanned
static void addMessage(SVEvent e)
static float polylineYCoords[]
static float polylineXCoords[]