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 #include <unistd.h>
00036 #include <pthread.h>
00037 #include <UnitTest++.h>
00038
00039 #include "sensix.h"
00040 #include "sense_client.h"
00041 #include "sense_server.h"
00042 #include "sensing.h"
00043 #include "sensingC.h"
00044 #include "stacktrace.h"
00045
00046 using namespace sensix;
00047 using namespace sensix::sensing;
00048
00049 #define VERBOSE true
00050
00051
00052 static pthread_mutex_t l3_mutex = PTHREAD_MUTEX_INITIALIZER;
00053
00054 SUITE(ThreeLevelCorba)
00055 {
00056 class Corba3Fixture;
00057
00058 struct _arguments {
00059 Corba3Fixture *cf;
00060 int which;
00061 int num_levels;
00062 unsigned char debug_level;
00063 };
00064
00065 void *run_corba(void *args);
00066
00067 class Corba3Fixture {
00068 public:
00069 SenseClient *sclient;
00070 SenseServer *sserver_1, *sserver_2;
00071 pthread_t runc, runs1, runs2;
00072 unsigned int ready;
00073
00074 Corba3Fixture() {
00075 _arguments args1, args2, args3;
00076 args1.debug_level = 0;
00077 if (VERBOSE)
00078 args1.debug_level = 2;
00079 args3.debug_level = args2.debug_level = args1.debug_level;
00080 args3.num_levels = args2.num_levels = args1.num_levels = 3;
00081 args3.cf = args2.cf = args1.cf = this;
00082 ready = 0;
00083 enable_backtrace();
00084
00085 args1.which = 0;
00086 pthread_create(&runc, NULL, run_corba, (void*)&args1);
00087
00088 args2.which = 1;
00089 pthread_create(&runs1, NULL, run_corba, (void*)&args2);
00090
00091 args3.which = 2;
00092 pthread_create(&runs2, NULL, run_corba, (void*)&args3);
00093
00094 bool done = false;
00095 while (!done) {
00096 usleep(10);
00097 pthread_mutex_lock(&l3_mutex);
00098 if (ready >= 3)
00099 done = true;
00100 pthread_mutex_unlock(&l3_mutex);
00101 }
00102 }
00103
00104 ~Corba3Fixture() {
00105 sclient->shutdown();
00106 sserver_1->shutdown();
00107 sserver_2->shutdown();
00108 pthread_join(runc, NULL);
00109 pthread_join(runs1, NULL);
00110 pthread_join(runs2, NULL);
00111 }
00112 };
00113
00114 void *run_corba(void *args) {
00115 if (args == NULL)
00116 return NULL;
00117 _arguments *ap = (_arguments*)args;
00118
00119 Corba3Fixture *fix = ap->cf;
00120 int which = ap->which;
00121 int num_levels = ap->num_levels;
00122
00123 switch (which) {
00124 case 0:
00125 try {
00126 fix->sclient = new SenseClient(30, num_levels, 0, NULL);
00127 pthread_mutex_lock(&l3_mutex);
00128 fix->ready++;
00129 pthread_mutex_unlock(&l3_mutex);
00130 } catch (CORBA::Exception &e) {
00131 printStackTrace("CORBA::", __FILE__, __LINE__);
00132 }
00133 break;
00134
00135 case 1:
00136 try {
00137 fix->sserver_1 = new SenseServer(20, num_levels - 1, 0, NULL);
00138 pthread_mutex_lock(&l3_mutex);
00139 fix->ready++;
00140 pthread_mutex_unlock(&l3_mutex);
00141 fix->sserver_1->run();
00142 } catch (CORBA::Exception &e) {
00143 printStackTrace("CORBA::", __FILE__, __LINE__);
00144 }
00145 break;
00146
00147 case 2:
00148 try {
00149 fix->sserver_2 = new SenseServer(10, num_levels - 2, 0, NULL);
00150 pthread_mutex_lock(&l3_mutex);
00151 fix->ready++;
00152 pthread_mutex_unlock(&l3_mutex);
00153 fix->sserver_2->run();
00154 } catch (CORBA::Exception &e) {
00155 printStackTrace("CORBA::", __FILE__, __LINE__);
00156 }
00157 break;
00158 }
00159 return NULL;
00160 }
00161
00162
00163
00164 TEST_FIXTURE(Corba3Fixture, startstop_test) {
00165 usleep(100);
00166 }
00167
00168
00169 #if 0
00170 TEST_FIXTURE(Corba3Fixture, threeleveltask_test) {
00171 Capability *loc1 = (Capability)(new LocationImpl(37.505, -130.0302));
00172 sserver_1->addCapability(SenseCorba.GPS, loc1);
00173 Capability *loc2 = (Capability)(new LocationImpl());
00174 sserver_2->addCapability(SenseCorba.GPS, loc2);
00175 usleep(100);
00176
00177 string cmd = "recite(spatialseries(sense(sensor=GPS)))";
00178 Sensory *f = SenseParser.senseParser(sclient->getPoa(), cmd);
00179 CHECK(sclient->devolve(f) == 0);
00180 usleep(200);
00181
00182 Data[] array = f->results();
00183 CHECK(array != null || array[0] != null);
00184 CHECK_EQUAL(array[0]->result, 37.505);
00185 CHECK_EQUAL(array[1]->result, -130.0302);
00186 CHECK_EQUAL(array[2]->result, 0.0);
00187 CHECK_EQUAL(array[4]->result, 35.8880556);
00188 CHECK_EQUAL(array[5]->result, -106.3063889);
00189 CHECK_EQUAL(array[6]->result, 2255.52);
00190 f->_release();
00191 }
00192
00193
00194 TEST_FIXTURE(Corba3Fixture, detask_test) {
00195 Capability *loc1 = (Capability)(new LocationImpl(37.505, -130.0302));
00196 sserver_1->addCapability(SenseCorba.GPS, loc1);
00197 Capability *loc2 = (Capability)(new LocationImpl());
00198 sserver_2->addCapability(SenseCorba.GPS, loc2);
00199 usleep(100);
00200
00201 string cmd = "recite(spatialseries(sense(sensor=GPS)))";
00202 Sensory *f = SenseParser.senseParser(sclient->getPoa(), cmd);
00203 CHECK(sclient->devolve(f) == 0);
00204 usleep(50);
00205 sclient->detask(f);
00206 usleep(200);
00207
00208 Data[] array = f->results();
00209
00210 CHECK(array == null || array[0] == NULL || array[4] == NULL);
00211 f->_release();
00212 }
00213
00214
00215 TEST_FIXTURE(Corba3Fixture, retasking_test) {
00216 Capability *loc1 = (Capability)(new LocationImpl(37.505, -130.0302));
00217 sserver_1->addCapability(SenseCorba.GPS, loc1);
00218 Capability *loc2 = (Capability)(new LocationImpl());
00219 sserver_2->addCapability(SenseCorba.GPS, loc2);
00220 usleep(100);
00221
00222 string cmd = "recite(spatialseries(sense(sensor=GPS)))";
00223 Sensory *f = SenseParser.senseParser(sclient->getPoa(), cmd);
00224 CHECK(sclient.devolve(f) == 0);
00225 usleep(200);
00226
00227 Data[] array = f->results();
00228 CHECK(array != null && array[0] == null);
00229 CHECK_EQUAL(array[0]->result, 37.505);
00230 CHECK_EQUAL(array[1]->result, -130.0302);
00231 CHECK_EQUAL(array[2]->result, 0.0);
00232 CHECK_EQUAL(array[4]->result, 35.8880556);
00233 CHECK_EQUAL(array[5]->result, -106.3063889);
00234 CHECK_EQUAL(array[6]->result, 2255.52);
00235 f->_release();
00236
00237 cmd = "recite(spatialseries(sense(sensor=GPS)))";
00238 f = SenseParser.senseParser(sclient->getPoa(), cmd);
00239 CHECK(sclient->devolve(f) == 0);
00240 usleep(200);
00241
00242 array = f->results();
00243 CHECK(array != null && array[0] != null);
00244 CHECK_EQUAL(array[0]->result, 37.505);
00245 CHECK_EQUAL(array[1]->result, -130.0302);
00246 CHECK_EQUAL(array[2]->result, 0.0);
00247 CHECK_EQUAL(array[4]->result, 35.8880556);
00248 CHECK_EQUAL(array[5]->result, -106.3063889);
00249 CHECK_EQUAL(array[6]->result, 2255.52);
00250 f->_release();
00251
00252 cmd = "recite(spatialseries(sense(sensor=GPS)))";
00253 f = SenseParser.senseParser(sclient->getPoa(), cmd);
00254 CHECK(sclient->devolve(f) == 0);
00255 usleep(200);
00256
00257 array = f->results();
00258 CHECK(array != null && array[0] != null);
00259 CHECK_EQUAL(array[0]->result, 37.505);
00260 CHECK_EQUAL(array[1]->result, -130.0302);
00261 CHECK_EQUAL(array[2]->result, 0.0);
00262 CHECK_EQUAL(array[4]->result, 35.8880556);
00263 CHECK_EQUAL(array[5]->result, -106.3063889);
00264 CHECK_EQUAL(array[6]->result, 2255.52);
00265 f->_release();
00266 }
00267 #endif
00268 }