00001
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 package sensix.test;
00036
00037 import junit.framework.*;
00038 import sensix.*;
00039 import sensix.discovery.*;
00040 import sensix.sensing.*;
00041
00042
00043 public class TwoLevelTest extends TestCase
00044 {
00045 private class Runner extends Thread
00046 {
00047 private TwoLevelTest parent;
00048 private int which;
00049 private int num_levels;
00050 private byte debug_level;
00051 private boolean ready;
00052
00053 public Runner(TwoLevelTest test, int what, int lvls, byte dl) {
00054 parent = test;
00055 which = what;
00056 num_levels = lvls;
00057 debug_level = dl;
00058 ready = false;
00059 }
00060
00061 public void run() {
00062 try {
00063 switch (which) {
00064 case 0:
00065 parent.sclient = new SenseClient(30, num_levels,
00066 "ClientTest_2." + num_levels,
00067 debug_level);
00068 ready = true;
00069 break;
00070
00071 case 1:
00072 parent.sserver = new SenseServer(20, num_levels - 1,
00073 "ServerTest_2." + (num_levels - 1),
00074 debug_level, "");
00075 ready = true;
00076 parent.sserver.run();
00077 break;
00078 }
00079 } catch (DiscoveryException de) {
00080 System.err.println("Discovery exception: " + de.description);
00081 de.printStackTrace(System.err);
00082 fail("Exception starting SENSIX Discovery service");
00083 } catch (Exception e) {
00084 System.err.println("Miscellaneous exception");
00085 e.printStackTrace(System.err);
00086 if (which == 0)
00087 fail("Exception starting SENSIX client: " + e.toString());
00088 else
00089 fail("Exception starting SENSIX server: " + e.toString());
00090 }
00091 }
00092
00093 public void check_ready() {
00094 while (!ready) {
00095 try {
00096 Thread.sleep(10);
00097 } catch (Exception e) {}
00098 }
00099 }
00100 }
00101
00102 public SenseClient sclient;
00103 public SenseServer sserver;
00104 protected Runner runc, runs;
00105
00106 public static boolean VERBOSE = false;
00107
00108
00109 protected void setUp() {
00110 byte dl = 0;
00111 if (VERBOSE)
00112 dl = 2;
00113
00114 runc = new Runner(this, 0, 2, dl);
00115 runc.start();
00116 runs = new Runner(this, 1, 2, dl);
00117 runs.start();
00118
00119 runc.check_ready();
00120 runs.check_ready();
00121 }
00122
00123
00124 protected void tearDown() {
00125 sclient.shutdown();
00126 sserver.shutdown();
00127 try {
00128 runc.join();
00129 runs.join();
00130 } catch (InterruptedException e) {}
00131 }
00132
00133
00134
00135
00136 public void testStartStop() {
00137 try {
00138 Thread.sleep(100);
00139 } catch (InterruptedException e) {
00140 System.err.println("Interruption");
00141 }
00142 }
00143
00144
00145 public void testAddCapability() {
00146 try {
00147 Capability loc = (Capability)(new Location());
00148 sserver.addCapability(Sensix.GPS, loc);
00149
00150 Thread.sleep(100);
00151 } catch (InterruptedException e) {
00152 System.err.println("Interruption");
00153 }
00154 }
00155
00156
00157 public void testTwoLevelTask() {
00158 try {
00159 Capability loc = (Capability)(new Location());
00160 sserver.addCapability(Sensix.GPS, loc);
00161
00162 Thread.sleep(100);
00163
00164 String cmd = "recite(spatialseries(sense(sensor=GPS)))";
00165 Sensory f = SenseParser.senseParser(cmd);
00166 if (sclient.devolve(f) < 0)
00167 fail("Unexpected Discovery exception");
00168
00169 Thread.sleep(100);
00170
00171 Data[] array = f.results();
00172 if (array == null || array.length == 0 || array[0] == null)
00173 fail("NULL data");
00174 assertEquals(array[0].fresult(), 35.8880556);
00175 assertEquals(array[1].fresult(), -106.3063889);
00176 assertEquals(array[2].fresult(), 2255.52);
00177 } catch (sensix.sensing.ParseException e) {
00178 System.err.println("Parse Exception in test: ");
00179 e.printStackTrace(System.err);
00180 } catch (java.io.IOException e) {
00181 System.err.println("I/O Exception in test: ");
00182 e.printStackTrace(System.err);
00183 } catch (InterruptedException e) {
00184 System.err.println("Interruption");
00185 }
00186 }
00187 }