Browse Source

Worker tries to use helper

Thomas Buck 12 years ago
parent
commit
4a94862d6d
4 changed files with 215 additions and 6 deletions
  1. 210
    3
      Cube Control/cubeWorker.java
  2. 1
    1
      Cube Control/helper/winSerial.c
  3. 2
    2
      Cube Control/makefile
  4. 2
    0
      Cube Control/serialHelper.c

+ 210
- 3
Cube Control/cubeWorker.java View File

@@ -34,6 +34,24 @@ import java.io.FileWriter;
34 34
 import java.io.File;
35 35
 import java.io.IOException;
36 36
 import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
37
+import java.io.Closeable;
38
+import java.io.FileNotFoundException;
39
+import java.io.FileOutputStream;
40
+import java.io.IOException;
41
+import java.io.InputStream;
42
+import java.io.InputStreamReader;
43
+import java.io.OutputStream;
44
+import java.io.BufferedReader;
45
+import java.net.URI;
46
+import java.net.URISyntaxException;
47
+import java.net.URL;
48
+import java.security.CodeSource;
49
+import java.security.ProtectionDomain;
50
+import java.util.zip.ZipEntry;
51
+import java.util.zip.ZipException;
52
+import java.util.zip.ZipFile;
53
+import java.lang.Process;
54
+import java.util.StringTokenizer;
37 55
 
38 56
 public class cubeWorker {
39 57
 
@@ -262,15 +280,204 @@ public class cubeWorker {
262 280
     }
263 281
 
264 282
     public String[] getSerialPorts() {
265
-
266
-           String[] sPorts = { "Select serial port..." }; // Has to be the first entry
267
-           return sPorts;
283
+		String[] ports = {"Select serial port..."};
284
+		String helperName;
285
+		if ((System.getProperty("os.name").toLowerCase()).indexOf("win") >= 0) {
286
+			helperName = "serialHelper.exe";
287
+		} else {
288
+			helperName = "serialHelper";
289
+		}
290
+		String[] arg = {"p"};
291
+		String portLines = HelperUtility.runHelper(arg);
292
+		System.out.println("Output: " + portLines);
293
+		if (portLines == null) {
294
+			return ports;
295
+		}
296
+		StringTokenizer sT = new StringTokenizer(portLines, "\n");
297
+		int size = sT.countTokens() + 1;
298
+		ports = new String[size];
299
+		ports[0] = "Select serial port...";
300
+		for (int i = 1; i < size; i++) {
301
+			ports[i] = sT.nextToken();
302
+		}
303
+		return ports;
268 304
     }
269 305
 
270 306
 // --------------------
271 307
 
272 308
 }
273 309
 
310
+class HelperUtility {
311
+
312
+	public static String runHelper(String[] args) {
313
+		String[] helperName = new String[args.length + 1];
314
+		boolean windows = false;
315
+		if ((System.getProperty("os.name").toLowerCase()).indexOf("win") >= 0) {
316
+			helperName[0] = "serialHelper.exe";
317
+			windows = true;
318
+		} else {
319
+			helperName[0] = "serialHelper";
320
+		}
321
+		for (int i = 0; i < args.length; i++) {
322
+			helperName[i + 1] = args[i];
323
+		}
324
+		String ret = "";
325
+		try {
326
+			File helper = new File(getFile(getJarURI(), helperName[0]));
327
+			helperName[0] = helper.getAbsolutePath();
328
+			if (!windows) {
329
+				Process execute = Runtime.getRuntime().exec("chmod a+x " + helper.getAbsolutePath());
330
+				execute.waitFor();
331
+				if (execute.exitValue() != 0) {
332
+					System.out.println("Could not set helper as executeable (" + execute.exitValue()+ ")");
333
+					return null;
334
+				}
335
+			}
336
+			Process p = Runtime.getRuntime().exec(helperName);
337
+			BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
338
+			String line;
339
+			boolean fin = false;
340
+			
341
+			do { // Wait for process to finish... Doesn't work...?
342
+				fin = false;
343
+				try {
344
+					p.waitFor();
345
+				} catch (Exception e) {
346
+					fin = true;
347
+				}
348
+
349
+				// Read output in same loop... Should work...!
350
+				line = br.readLine();
351
+				if (line != null) {
352
+					ret = ret + line + "\n";
353
+					fin = true;
354
+				}
355
+			} while (fin);
356
+
357
+			br.close();
358
+			if (ret.length() == 0) {
359
+				ret = "g"; // We have added a last \n... We will remove it, so add garbage to be removed...
360
+			}
361
+			ret = ret.substring(0, ret.length() - 1);
362
+			return ret;
363
+		} catch(Exception e) {
364
+			e.printStackTrace();
365
+		}
366
+
367
+		return null;
368
+	}
369
+
370
+	// From http://stackoverflow.com/questions/600146/run-exe-which-is-packaged-inside-jar
371
+	private static URI getJarURI()
372
+        throws URISyntaxException
373
+    {
374
+        final ProtectionDomain domain;
375
+        final CodeSource       source;
376
+        final URL              url;
377
+        final URI              uri;
378
+
379
+        domain = cubeWorker.class.getProtectionDomain();
380
+        source = domain.getCodeSource();
381
+        url    = source.getLocation();
382
+        uri    = url.toURI();
383
+
384
+        return (uri);
385
+    }
386
+
387
+    private static URI getFile(final URI    where,
388
+                               final String fileName)
389
+        throws ZipException,
390
+               IOException
391
+    {
392
+        final File location;
393
+        final URI  fileURI;
394
+
395
+        location = new File(where);
396
+
397
+        // not in a JAR, just return the path on disk
398
+        if(location.isDirectory())
399
+        {
400
+            fileURI = URI.create(where.toString() + fileName);
401
+        }
402
+        else
403
+        {
404
+            final ZipFile zipFile;
405
+
406
+            zipFile = new ZipFile(location);
407
+
408
+            try
409
+            {
410
+                fileURI = extract(zipFile, fileName);
411
+            }
412
+            finally
413
+            {
414
+                zipFile.close();
415
+            }
416
+        }
417
+
418
+        return (fileURI);
419
+    }
420
+
421
+    private static URI extract(final ZipFile zipFile,
422
+                               final String  fileName)
423
+        throws IOException
424
+    {
425
+        final File         tempFile;
426
+        final ZipEntry     entry;
427
+        final InputStream  zipStream;
428
+        OutputStream       fileStream;
429
+
430
+        tempFile = File.createTempFile(fileName, Long.toString(System.currentTimeMillis()));
431
+        tempFile.deleteOnExit();
432
+        entry    = zipFile.getEntry(fileName);
433
+
434
+        if(entry == null)
435
+        {
436
+            throw new FileNotFoundException("cannot find file: " + fileName + " in archive: " + zipFile.getName());
437
+        }
438
+
439
+        zipStream  = zipFile.getInputStream(entry);
440
+        fileStream = null;
441
+
442
+        try
443
+        {
444
+            final byte[] buf;
445
+            int          i;
446
+
447
+            fileStream = new FileOutputStream(tempFile);
448
+            buf        = new byte[1024];
449
+            i          = 0;
450
+
451
+            while((i = zipStream.read(buf)) != -1)
452
+            {
453
+                fileStream.write(buf, 0, i);
454
+            }
455
+        }
456
+        finally
457
+        {
458
+            close(zipStream);
459
+            close(fileStream);
460
+        }
461
+
462
+        return (tempFile.toURI());
463
+    }
464
+
465
+    private static void close(final Closeable stream)
466
+    {
467
+        if(stream != null)
468
+        {
469
+            try
470
+            {
471
+                stream.close();
472
+            }
473
+            catch(final IOException ex)
474
+            {
475
+                ex.printStackTrace();
476
+            }
477
+        }
478
+    }
479
+}
480
+
274 481
 class AnimationUtility {
275 482
   private static String lastError = null;
276 483
 

+ 1
- 1
Cube Control/helper/winSerial.c View File

@@ -113,7 +113,7 @@ char** getSerialPorts(void) {
113 113
 	 ports = (LPTSTR)malloc(100 * sizeof(CHAR));
114 114
 #endif
115 115
 
116
-	num = QueryDosDevice(ports, 100);	
116
+	num = QueryDosDevice(NULL, ports, 100);	
117 117
 	files = (char **)malloc(num * sizeof(char *));
118 118
 	
119 119
 	for (i = 0; i < num; i++) {

+ 2
- 2
Cube Control/makefile View File

@@ -6,9 +6,9 @@ TARGET = unix
6 6
 JAVAFILES = cubeWorker.java layerEditFrame.java frame.java
7 7
 
8 8
 ifeq ($(TARGET),win)
9
-CLASSES = cubeWorker.class layerEditFrame.class layerEditFrame$$1.class layerEditFrame$$2.class layerEditFrame$$3.class layerEditFrame$$4.class frame.class frame$$1.class frame$$2.class frame$$3.class frame$$4.class frame$$5.class frame$$6.class frame$$7.class frame$$8.class frame$$9.class frame$$10.class frame$$11.class frame$$12.class frame$$13.class frame$$14.class frame$$15.class frame$$16.class frame$$17.class frame$$18.class frame$$19.class frame$$20.class frame$$21.class frame$$22.class frame$$23.class frame$$24.class AFrame.class Animation.class AnimationUtility.class LEDoff.png LEDon.png
9
+CLASSES = cubeWorker.class layerEditFrame.class layerEditFrame$$1.class layerEditFrame$$2.class layerEditFrame$$3.class layerEditFrame$$4.class frame.class frame$$1.class frame$$2.class frame$$3.class frame$$4.class frame$$5.class frame$$6.class frame$$7.class frame$$8.class frame$$9.class frame$$10.class frame$$11.class frame$$12.class frame$$13.class frame$$14.class frame$$15.class frame$$16.class frame$$17.class frame$$18.class frame$$19.class frame$$20.class frame$$21.class frame$$22.class frame$$23.class frame$$24.class AFrame.class Animation.class AnimationUtility.class HelperUtility.class LEDoff.png LEDon.png
10 10
 else
11
-CLASSES = 'cubeWorker.class' 'layerEditFrame.class' 'layerEditFrame$$1.class' 'layerEditFrame$$2.class' 'layerEditFrame$$3.class' 'layerEditFrame$$4.class' 'frame.class' 'frame$$1.class' 'frame$$2.class' 'frame$$3.class' 'frame$$4.class' 'frame$$5.class' 'frame$$6.class' 'frame$$7.class' 'frame$$8.class' 'frame$$9.class' 'frame$$10.class' 'frame$$11.class' 'frame$$12.class' 'frame$$13.class' 'frame$$14.class' 'frame$$15.class' 'frame$$16.class' 'frame$$17.class' 'frame$$18.class' 'frame$$19.class' 'frame$$20.class' 'frame$$21.class' 'frame$$22.class' 'frame$$23.class' 'frame$$24.class' 'AFrame.class' 'Animation.class' 'AnimationUtility.class' 'LEDoff.png' 'LEDon.png'
11
+CLASSES = 'cubeWorker.class' 'layerEditFrame.class' 'layerEditFrame$$1.class' 'layerEditFrame$$2.class' 'layerEditFrame$$3.class' 'layerEditFrame$$4.class' 'frame.class' 'frame$$1.class' 'frame$$2.class' 'frame$$3.class' 'frame$$4.class' 'frame$$5.class' 'frame$$6.class' 'frame$$7.class' 'frame$$8.class' 'frame$$9.class' 'frame$$10.class' 'frame$$11.class' 'frame$$12.class' 'frame$$13.class' 'frame$$14.class' 'frame$$15.class' 'frame$$16.class' 'frame$$17.class' 'frame$$18.class' 'frame$$19.class' 'frame$$20.class' 'frame$$21.class' 'frame$$22.class' 'frame$$23.class' 'frame$$24.class' 'AFrame.class' 'Animation.class' 'AnimationUtility.class' 'HelperUtility.class' 'LEDoff.png' 'LEDon.png'
12 12
 endif
13 13
 
14 14
 all: build clean

+ 2
- 0
Cube Control/serialHelper.c View File

@@ -51,6 +51,8 @@ Return values:
51 51
 int main(int argc, char *argv[]) {
52 52
 	size_t length, written;
53 53
 
54
+		printf("Debugging Worker... Ignore me!\n");
55
+
54 56
 		if (argc < 2) {
55 57
 			usage(argv[0]);
56 58
 			return 1;

Loading…
Cancel
Save