00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef NL_PACS_CLIENT_H
00027 #define NL_PACS_CLIENT_H
00028
00029 #include "stdnet.h"
00030
00031 #include "nel/net/inet_address.h"
00032 #include "nel/net/callback_client.h"
00033 #include "nel/net/naming_client.h"
00034
00035 #include "nel/pacs/u_move_primitive.h"
00036 #include "nel/pacs/u_collision_desc.h"
00037
00038 #define NLNET_PACS_PROTOCOL_VERSION 1
00039
00040 namespace NLNET
00041 {
00042
00043 TCallbackItem PacsCallbackArray[];
00044
00052 class CPacsClient
00053 {
00054 friend void cbPacsAnswer (CMessage &msgin, TSockId from, CCallbackNetBase &netbase);
00055 public:
00056
00058 CPacsClient()
00059 {
00060
00061 _Server=NULL;
00062 }
00063
00064 ~CPacsClient()
00065 {
00066 disconnect ();
00067 }
00068
00069 bool connect ();
00070
00071 void disconnect ()
00072 {
00073 if (_Server)
00074 {
00075 _Server->disconnect ();
00076 delete _Server;
00077 }
00078 }
00079
00085 void initMessage ()
00086 {
00087 _Message.clear ();
00088 _Message.setType ("PACS");
00089 _Message.serialCheck ((uint32)NLNET_PACS_PROTOCOL_VERSION);
00090 }
00091
00097 void sendMessage ()
00098 {
00099
00100 nlassert (_Server);
00101
00102
00103 bool nlFalse=false;
00104 _Message.serial (nlFalse);
00105
00106
00107 _Server->send (_Message);
00108 }
00109
00113 void update ()
00114 {
00115
00116 nlassert (_Server);
00117
00118 _Server->update ();
00119 }
00120
00122
00132 void rayTest (double p0, double p1, uint32 testId)
00133 {
00134
00135 std::string name="RY";
00136 bool nlTrue=true;
00137 _Message.serial (nlTrue, name, p0, p1, testId);
00138 }
00139
00141
00149 void addPrimitive (NLPACS::UMovePrimitive::TUserData id)
00150 {
00151
00152 std::string name="AD";
00153 bool nlTrue=true;
00154 _Message.serial (nlTrue, name, id);
00155 }
00156
00164 void removePrimitive (NLPACS::UMovePrimitive::TUserData id)
00165 {
00166
00167 std::string name="RV";
00168 bool nlTrue=true;
00169 _Message.serial (nlTrue, name, id);
00170 }
00171
00180 void evalCollision (uint32 evalId, double deltaTime)
00181 {
00182
00183 std::string name="EV";
00184 bool nlTrue=true;
00185 _Message.serial (nlTrue, name, evalId, deltaTime);
00186 }
00187
00197 void testMove (NLPACS::UMovePrimitive::TUserData id, const NLMISC::CVectorD& speed, double deltaTime)
00198 {
00199
00200 std::string name="TS";
00201 bool nlTrue=true;
00202 _Message.serial (nlTrue, name, id, const_cast<NLMISC::CVectorD&> (speed), deltaTime);
00203 }
00204
00206
00215 void setCurrentPrimitive (NLPACS::UMovePrimitive::TUserData id)
00216 {
00217
00218 std::string name="CU";
00219 bool nlTrue=true;
00220 _Message.serial (nlTrue, name, id);
00221 }
00222
00230 void setPrimitiveType (NLPACS::UMovePrimitive::TType type)
00231 {
00232
00233 std::string name="TY";
00234 uint32 t=(uint32)type;
00235 bool nlTrue=true;
00236 _Message.serial (nlTrue, name, t);
00237 }
00238
00246 void setReactionType (NLPACS::UMovePrimitive::TReaction type)
00247 {
00248
00249 std::string name="RT";
00250 uint32 t=(uint32)type;
00251 bool nlTrue=true;
00252 _Message.serial (nlTrue, name, t);
00253 }
00254
00262 void setTriggerType (NLPACS::UMovePrimitive::TTrigger type)
00263 {
00264
00265 std::string name="TT";
00266 uint32 t=(uint32)type;
00267 bool nlTrue=true;
00268 _Message.serial (nlTrue, name, t);
00269 }
00270
00278 void setCollisionMask (NLPACS::UMovePrimitive::TCollisionMask mask)
00279 {
00280
00281 std::string name="CT";
00282 bool nlTrue=true;
00283 _Message.serial (nlTrue, name, mask);
00284 }
00285
00293 void setOcclusionMask (NLPACS::UMovePrimitive::TCollisionMask mask)
00294 {
00295
00296 std::string name="OT";
00297 bool nlTrue=true;
00298 _Message.serial (nlTrue, name, mask);
00299 }
00300
00308 void setObstacle (bool obstacle)
00309 {
00310
00311 std::string name="OB";
00312 bool nlTrue=true;
00313 _Message.serial (nlTrue, name, obstacle);
00314 }
00315
00323 void setOrientation (double orientation)
00324 {
00325
00326 std::string name="OR";
00327 bool nlTrue=true;
00328 _Message.serial (nlTrue, name, orientation);
00329 }
00330
00338 void setAbsorption (float absorption)
00339 {
00340
00341 std::string name="AB";
00342 bool nlTrue=true;
00343 _Message.serial (nlTrue, name, absorption);
00344 }
00345
00354 void setSize (float width, float depth)
00355 {
00356
00357 std::string name="SZ";
00358 bool nlTrue=true;
00359 _Message.serial (nlTrue, name, width, depth);
00360 }
00361
00369 void setHeight (float height)
00370 {
00371
00372 std::string name="HE";
00373 bool nlTrue=true;
00374 _Message.serial (nlTrue, name, height);
00375 }
00376
00384 void setRadius (float radius)
00385 {
00386
00387 std::string name="RD";
00388 bool nlTrue=true;
00389 _Message.serial (nlTrue, name, radius);
00390 }
00391
00400 void globalMove (const NLMISC::CVectorD& position)
00401 {
00402
00403 std::string name="GM";
00404 bool nlTrue=true;
00405 _Message.serial (nlTrue, name, const_cast<NLMISC::CVectorD&> (position));
00406 }
00407
00418 void relativeMove (const NLMISC::CVectorD& speed)
00419 {
00420
00421 std::string name="RM";
00422 bool nlTrue=true;
00423 _Message.serial (nlTrue, name, const_cast<NLMISC::CVectorD&> (speed));
00424 }
00425
00433 void getPositionSpeed (NLPACS::UMovePrimitive::TUserData id)
00434 {
00435
00436 std::string name="PS";
00437 bool nlTrue=true;
00438 _Message.serial (nlTrue, name, id);
00439 }
00440
00441 protected:
00442
00444
00448 virtual void messageCallback ()
00449 {}
00450
00457 virtual void rayTestCallback (uint32 testId, bool testResult)
00458 {}
00459
00467 virtual void triggerCallback (uint32 evalId, const std::vector<NLPACS::UTriggerInfo>& triggerInfo)
00468 {}
00469
00476 virtual void testMoveCallback (NLPACS::UMovePrimitive::TUserData id, bool testResult)
00477 {}
00478
00486 virtual void getPositionSpeedCallback (NLPACS::UMovePrimitive::TUserData id, const NLMISC::CVectorD &position, const NLMISC::CVectorD &speed)
00487 {}
00488
00489 private:
00490 CCallbackClient *_Server;
00491 CMessage _Message;
00492 };
00493
00494
00495 static void cbPacsAnswer (CMessage &msgin, TSockId from, CCallbackNetBase &netbase)
00496 {
00497
00498 CPacsClient *client=(CPacsClient*)(uint)from->appId ();
00499
00500
00501 msgin.serialCheck ((uint32)NLNET_PACS_PROTOCOL_VERSION);
00502
00503
00504 client->messageCallback ();
00505
00506 bool again;
00507 msgin.serial (again);
00508
00509 while (again)
00510 {
00511
00512 std::string subMessage;
00513 msgin.serial (subMessage);
00514
00515
00516 nlassert (sizeof (uint)==sizeof (void*));
00517
00518
00519 if (subMessage=="RY")
00520 {
00521
00522 uint32 testId;
00523 bool testResult;
00524 msgin.serial (testId, testResult);
00525
00526
00527 client->rayTestCallback (testId, testResult);
00528 }
00529
00530 else if (subMessage=="TR")
00531 {
00532
00533 uint32 evalId;
00534 std::vector<NLPACS::UTriggerInfo> triggerInfo;
00535 msgin.serial (evalId);
00536 msgin.serialCont (triggerInfo);
00537
00538
00539 client->triggerCallback (evalId, triggerInfo);
00540 }
00541
00542 else if (subMessage=="TM")
00543 {
00544
00545 NLPACS::UMovePrimitive::TUserData id;
00546 bool testResult;
00547 msgin.serial (id, testResult);
00548
00549
00550 client->testMoveCallback (id, testResult);
00551 }
00552
00553 else if (subMessage=="PS")
00554 {
00555
00556 NLPACS::UMovePrimitive::TUserData id;
00557 NLMISC::CVectorD position;
00558 NLMISC::CVectorD speed;
00559 msgin.serial (id, position, speed);
00560
00561
00562 client->getPositionSpeedCallback (id, position, speed);
00563 }
00564 else
00565 NLMISC::nlError ("Pacs client: unkown sub message string");
00566
00567
00568 msgin.serial (again);
00569 }
00570 }
00571
00572 static TCallbackItem PacsCallbackArray[] =
00573 {
00574 { "PACS_ASW", cbPacsAnswer }
00575 };
00576
00577 inline bool CPacsClient::connect ()
00578 {
00579
00580 _Server = new CCallbackClient;
00581
00582
00583 CNamingClient::lookupAndConnect ("PS", *_Server);
00584 if (_Server->connected())
00585 {
00586
00587 _Server->addCallbackArray (PacsCallbackArray, sizeof (PacsCallbackArray) / sizeof (PacsCallbackArray[0]));
00588
00589
00590 nlassert (sizeof (uint)==sizeof (void*));
00591 _Server->id ()->setAppId ((uint64)(uint)this);
00592
00593
00594 return true;
00595 }
00596 else
00597 {
00598 return false;
00599 }
00600 }
00601
00602 }
00603
00604
00605 #endif // NL_PACS_CLIENT_H
00606
00607