VmdConnection.h

Go to the documentation of this file.
00001 #ifndef SimTK_MOLMODEL_VMDCONNECTION_H_
00002 #define SimTK_MOLMODEL_VMDCONNECTION_H_
00003 
00004 extern "C" {
00005 #include "vmdsock.h"
00006 }
00007 #include "imd.h"
00008 #include <iostream>
00009 #include <vector>
00010 
00011 // Microsoft build environment
00012 #ifdef _MSC_VER
00013 #include <Windows.h>
00014 #define sleep(x) Sleep(1000*x)
00015 #endif
00016  
00017 using namespace std;
00018 
00019 static bool debugVmdConnection = true;
00020 
00021 class VmdFloat3 
00022 {
00023 public:
00024     VmdFloat3(float x, float y, float z)
00025     {
00026         d[0] = x;
00027         d[1] = y;
00028         d[2] = z;
00029     }
00030         
00031     float& operator[](int i) {return d[i];}
00032     const float& operator[](int i) const {return d[i];}
00033 
00034 private:
00035     float d[3];
00036 };
00037 
00038 class VmdConnection
00039 {
00040 public:
00041     
00042     VmdConnection(int portNumber)
00043     :   socket(NULL),
00044         clientSocket(NULL),
00045         socketNumber(portNumber)
00046     {
00047         if (debugVmdConnection)
00048             cerr << "Opening socket connection to VMD on port " << portNumber << "..." << endl;
00049         
00050         vmdsock_init();
00051         socket = vmdsock_create(); // uses malloc...
00052         vmdsock_bind(socket, portNumber);
00053         vmdsock_listen(socket);
00054     }
00055     
00056     ~VmdConnection()
00057     {
00058         if (debugVmdConnection)
00059             cerr << "Closing socket connection to VMD..." << endl;
00060         
00061         if ( clientIsConnected() )
00062         {
00063             closeClientConnection();
00064         }
00065         
00066         if (socket != NULL)
00067         {
00068             vmdsock_shutdown(socket);
00069             vmdsock_destroy(socket);
00070         }
00071     }
00072     
00073     bool checkForConnection() 
00074     {
00075         if ( ! clientIsConnected() )
00076         {
00077             if (socket == NULL) 
00078             {
00079                 cerr << "ERROR: socket is NULL" << endl;
00080                 return false;
00081             }
00082             if (vmdsock_selread(socket, 0) > 0)
00083             {
00084                 clientSocket = vmdsock_accept(socket);
00085                 if ( imd_handshake(clientSocket) )
00086                 {
00087                     clientSocket = NULL;
00088                 }
00089             }
00090         }
00091 
00092         if ( clientIsConnected() )
00093         {
00094             sleep(1); // wait a second for VMD to respond
00095 
00096             int length;
00097             if ( (vmdsock_selread(clientSocket, 0) != 1) ||
00098                  (imd_recv_header(clientSocket, &length) != IMD_GO) ) 
00099             {
00100                 closeClientConnection();
00101             }
00102         }
00103         
00104         if ( clientIsConnected() ) 
00105             return true;
00106         else
00107             return false;
00108     }
00109     
00110     bool clientIsConnected() const 
00111     {
00112         return (NULL != clientSocket);
00113     }
00114     
00115     void sendCoordinates(std::vector<VmdFloat3> coords)
00116     {
00117         if ( imd_send_fcoords(clientSocket, coords.size(), &coords[0][0]) )
00118         {
00119             closeClientConnection();
00120         }
00121     }
00122 
00123     void closeClientConnection() 
00124     {
00125         imd_disconnect(clientSocket);
00126         vmdsock_shutdown(clientSocket);
00127         vmdsock_destroy(clientSocket);
00128         clientSocket = NULL;
00129     }
00130 
00131     int getSocketNumber() const {return socketNumber;}
00132 
00133 private:
00134     int socketNumber;
00135     void *socket;
00136     void *clientSocket;
00137 };
00138 
00139 
00140 #endif // SimTK_MOLMODEL_VMDCONNECTION_H_

Generated on Fri Sep 26 07:44:19 2008 for SimTKcore by  doxygen 1.5.6