pacs_client.h

Go to the documentation of this file.
00001 
00007 /* Copyright, 2001 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
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                 // No connexion
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                 // Checks
00100                 nlassert (_Server);
00101 
00102                 // Close the message
00103                 bool nlFalse=false;
00104                 _Message.serial (nlFalse);
00105 
00106                 // Send the message
00107                 _Server->send (_Message);
00108         }
00109 
00113         void    update ()
00114         {
00115                 // Checks
00116                 nlassert (_Server);
00117 
00118                 _Server->update ();
00119         }
00120 
00122 
00132         void rayTest (double p0, double p1, uint32 testId)
00133         {
00134                 // Append to the current message
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                 // Append to the current message
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                 // Append to the current message
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                 // Append to the current message
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                 // Append to the current message
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                 // Append to the current message
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                 // Append to the current message
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                 // Append to the current message
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                 // Append to the current message
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                 // Append to the current message
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                 // Append to the current message
00296                 std::string name="OT";
00297                 bool nlTrue=true;
00298                 _Message.serial (nlTrue, name, mask);
00299         }
00300         
00308         void setObstacle (bool obstacle)
00309         {
00310                 // Append to the current message
00311                 std::string name="OB";
00312                 bool nlTrue=true;
00313                 _Message.serial (nlTrue, name, obstacle);
00314         }
00315         
00323         void setOrientation (double orientation)
00324         {
00325                 // Append to the current message
00326                 std::string name="OR";
00327                 bool nlTrue=true;
00328                 _Message.serial (nlTrue, name, orientation);
00329         }
00330         
00338         void setAbsorption (float absorption)
00339         {
00340                 // Append to the current message
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                 // Append to the current message
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                 // Append to the current message
00372                 std::string name="HE";
00373                 bool nlTrue=true;
00374                 _Message.serial (nlTrue, name, height);
00375         }
00376         
00384         void setRadius (float radius)
00385         {
00386                 // Append to the current message
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                 // Append to the current message
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                 // Append to the current message
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                 // Append to the current message
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 // Callback to listen to the server
00495 static void cbPacsAnswer (CMessage &msgin, TSockId from, CCallbackNetBase &netbase)
00496 {
00497         // Get the client pointer
00498         CPacsClient *client=(CPacsClient*)(uint)from->appId ();
00499 
00500         // Check stream
00501         msgin.serialCheck ((uint32)NLNET_PACS_PROTOCOL_VERSION);
00502 
00503         // Message callback
00504         client->messageCallback ();
00505 
00506         bool again;
00507         msgin.serial (again);
00508         
00509         while (again)
00510         {
00511                 // Read the message sub string
00512                 std::string subMessage;
00513                 msgin.serial (subMessage);
00514 
00515                 // This code can work only if sizeof (uint) == sizeof (void*)
00516                 nlassert (sizeof (uint)==sizeof (void*));
00517 
00518                 // Raytrace callback ?
00519                 if (subMessage=="RY")
00520                 {
00521                         // Read test id and test result
00522                         uint32 testId;
00523                         bool testResult;
00524                         msgin.serial (testId, testResult);
00525 
00526                         // Call the callback
00527                         client->rayTestCallback (testId, testResult);
00528                 }
00529                 // Trigger callback ?
00530                 else if (subMessage=="TR")
00531                 {
00532                         // Read eval id and trigger info
00533                         uint32 evalId;
00534                         std::vector<NLPACS::UTriggerInfo> triggerInfo;
00535                         msgin.serial (evalId);
00536                         msgin.serialCont (triggerInfo);
00537 
00538                         // Call the callback
00539                         client->triggerCallback (evalId, triggerInfo);
00540                 }
00541                 // Test move callback ?
00542                 else if (subMessage=="TM")
00543                 {
00544                         // Read the primitive id and test result
00545                         NLPACS::UMovePrimitive::TUserData id;
00546                         bool testResult;
00547                         msgin.serial (id, testResult);
00548                         
00549                         // Call the callback    
00550                         client->testMoveCallback (id, testResult);
00551                 }
00552                 // Test move callback ?
00553                 else if (subMessage=="PS")
00554                 {
00555                         // Read the primitive id and test result
00556                         NLPACS::UMovePrimitive::TUserData id;
00557                         NLMISC::CVectorD position;
00558                         NLMISC::CVectorD speed;
00559                         msgin.serial (id, position, speed);
00560                         
00561                         // Call the callback    
00562                         client->getPositionSpeedCallback (id, position, speed);
00563                 }
00564                 else
00565                         NLMISC::nlError ("Pacs client: unkown sub message string");
00566 
00567                 // Next message ?
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         // Create a connexion
00580         _Server = new CCallbackClient;
00581 
00582         // Look up for PACS service
00583         CNamingClient::lookupAndConnect ("PS", *_Server);
00584         if (_Server->connected())
00585         {
00586                 // Add callback array
00587                 _Server->addCallbackArray (PacsCallbackArray, sizeof (PacsCallbackArray) / sizeof (PacsCallbackArray[0]));
00588 
00589                 // This code can work only if sizeof (uint) == sizeof (void*)
00590                 nlassert (sizeof (uint)==sizeof (void*));
00591                 _Server->id ()->setAppId ((uint64)(uint)this);
00592 
00593                 // Return ok
00594                 return true;
00595         }
00596         else
00597         {
00598                 return false;
00599         }
00600 }
00601 
00602 } // NLNET
00603 
00604 
00605 #endif // NL_PACS_CLIENT_H
00606 
00607 /* End of pacs_client.h */

Generated on Tue Mar 16 06:30:38 2004 for NeL by doxygen 1.3.6