imd.C File Reference

#include "imd.h"
#include "vmdsock.h"
#include <string.h>
#include <errno.h>
#include <stdlib.h>

Go to the source code of this file.

Classes

struct  IMDheader
union  netint
 structure used to perform byte swapping operations More...

Defines

#define HEADERSIZE   8
#define IMDVERSION   2

Functions

static void swap4 (char *data, int ndata)
static int32 imd_htonl (int32 h)
static int32 imd_ntohl (int32 n)
static void fill_header (IMDheader *header, IMDType type, int32 length)
static void swap_header (IMDheader *header)
static int32 imd_readn (void *s, char *ptr, int32 n)
static int32 imd_writen (void *s, const char *ptr, int32 n)
int imd_disconnect (void *s)
int imd_pause (void *s)
int imd_kill (void *s)
static int imd_go (void *s)
int imd_handshake (void *s)
int imd_trate (void *s, int32 rate)
int imd_send_mdcomm (void *s, int32 n, const int32 *indices, const float *forces)
int imd_send_energies (void *s, const IMDEnergies *energies)
int imd_send_fcoords (void *s, int32 n, const float *coords)
IMDType imd_recv_header_nolengthswap (void *s, int32 *length)
int imd_recv_handshake (void *s)
 Receive header and data.
IMDType imd_recv_header (void *s, int32 *length)
int imd_recv_mdcomm (void *s, int32 n, int32 *indices, float *forces)
int imd_recv_energies (void *s, IMDEnergies *energies)
int imd_recv_fcoords (void *s, int32 n, float *coords)


Define Documentation

#define HEADERSIZE   8

Definition at line 14 of file imd.C.

Referenced by imd_disconnect(), imd_go(), imd_handshake(), imd_kill(), imd_pause(), imd_recv_header(), imd_recv_header_nolengthswap(), imd_send_energies(), imd_send_fcoords(), imd_send_mdcomm(), and imd_trate().

#define IMDVERSION   2

Definition at line 15 of file imd.C.

Referenced by imd_handshake(), and imd_recv_handshake().


Function Documentation

static void fill_header ( IMDheader header,
IMDType  type,
int32  length 
) [static]

Definition at line 60 of file imd.C.

References imd_htonl(), IMDheader::length, and IMDheader::type.

Referenced by imd_disconnect(), imd_go(), imd_handshake(), imd_kill(), imd_pause(), imd_send_energies(), imd_send_fcoords(), imd_send_mdcomm(), and imd_trate().

00060                                                                        {
00061   header->type = imd_htonl((int32)type);
00062   header->length = imd_htonl(length);
00063 }

int imd_disconnect ( void *  s  ) 

Definition at line 108 of file imd.C.

References fill_header(), HEADERSIZE, IMD_DISCONNECT, and imd_writen().

00108                             {
00109   IMDheader header;
00110   fill_header(&header, IMD_DISCONNECT, 0);
00111   return (imd_writen(s, (char *)&header, HEADERSIZE) != HEADERSIZE);
00112 }

static int imd_go ( void *  s  )  [static]

Definition at line 126 of file imd.C.

References fill_header(), HEADERSIZE, IMD_GO, and imd_writen().

Referenced by imd_recv_handshake().

00126                            {
00127   IMDheader header;
00128   fill_header(&header, IMD_GO, 0);
00129   return (imd_writen(s, (char *)&header, HEADERSIZE) != HEADERSIZE);
00130 }

int imd_handshake ( void *  s  ) 

Definition at line 133 of file imd.C.

References fill_header(), HEADERSIZE, IMD_HANDSHAKE, imd_writen(), IMDVERSION, and IMDheader::length.

Referenced by my_imd_connect().

00133                            {
00134   IMDheader header;
00135   fill_header(&header, IMD_HANDSHAKE, 1);
00136   header.length = IMDVERSION;   // Not byteswapped!
00137   return (imd_writen(s, (char *)&header, HEADERSIZE) != HEADERSIZE);
00138 }

static int32 imd_htonl ( int32  h  )  [static]

Definition at line 45 of file imd.C.

References netint::b, netint::high, netint::highest, netint::i, netint::low, and netint::lowest.

Referenced by fill_header().

00045                                 {
00046   netint n;
00047   n.b.highest = h >> 24;
00048   n.b.high    = h >> 16;
00049   n.b.low     = h >> 8;
00050   n.b.lowest  = h;
00051   return n.i;
00052 }

int imd_kill ( void *  s  ) 

Definition at line 120 of file imd.C.

References fill_header(), HEADERSIZE, IMD_KILL, and imd_writen().

00120                       {
00121   IMDheader header;
00122   fill_header(&header, IMD_KILL, 0);
00123   return (imd_writen(s, (char *)&header, HEADERSIZE) != HEADERSIZE);
00124 }

static int32 imd_ntohl ( int32  n  )  [static]

Definition at line 54 of file imd.C.

References netint::b, netint::high, netint::highest, netint::i, netint::low, and netint::lowest.

Referenced by swap_header().

00054                                 {
00055   netint u;
00056   u.i = n;
00057   return (u.b.highest << 24 | u.b.high << 16 | u.b.low << 8 | u.b.lowest);
00058 }

int imd_pause ( void *  s  ) 

Definition at line 114 of file imd.C.

References fill_header(), HEADERSIZE, IMD_PAUSE, and imd_writen().

00114                        {
00115   IMDheader header;
00116   fill_header(&header, IMD_PAUSE, 0);
00117   return (imd_writen(s, (char *)&header, HEADERSIZE) != HEADERSIZE);
00118 }

static int32 imd_readn ( void *  s,
char *  ptr,
int32  n 
) [static]

Definition at line 70 of file imd.C.

References vmdsock_read().

Referenced by imd_recv_energies(), imd_recv_fcoords(), imd_recv_header(), imd_recv_header_nolengthswap(), and imd_recv_mdcomm().

00070                                                     {
00071   int32 nleft;
00072   int32 nread;
00073  
00074   nleft = n;
00075   while (nleft > 0) {
00076     if ((nread = vmdsock_read(s, ptr, nleft)) < 0) {
00077       if (errno == EINTR)
00078         nread = 0;         /* and call read() again */
00079       else
00080         return -1;
00081     } else if (nread == 0)
00082       break;               /* EOF */
00083     nleft -= nread;
00084     ptr += nread;
00085   }
00086   return n-nleft;
00087 }

int imd_recv_energies ( void *  s,
IMDEnergies energies 
)

Definition at line 231 of file imd.C.

References imd_readn().

Referenced by GlobalMasterIMD::get_vmd_forces().

00231                                                       {
00232   return (imd_readn(s, (char *)energies, sizeof(IMDEnergies))
00233           != sizeof(IMDEnergies));
00234 }

int imd_recv_fcoords ( void *  s,
int32  n,
float *  coords 
)

Definition at line 236 of file imd.C.

References imd_readn().

Referenced by GlobalMasterIMD::get_vmd_forces().

00236                                                       {
00237   return (imd_readn(s, (char *)coords, 12*n) != 12*n);
00238 }

int imd_recv_handshake ( void *  s  ) 

Receive header and data.

Definition at line 191 of file imd.C.

References imd_go(), IMD_HANDSHAKE, imd_recv_header_nolengthswap(), IMDVERSION, swap4(), and vmdsock_selread().

00191                                 {
00192   // Wait 5 seconds for the handshake to come
00193   if (vmdsock_selread(s, 5) != 1) return -1;
00194 
00195   // Check to see that a valid handshake was received
00196   int32 buf;
00197   IMDType type = imd_recv_header_nolengthswap(s, &buf);
00198   if (type != IMD_HANDSHAKE) return -1;
00199 
00200   // Check its endianness, as well as the IMD version.
00201   if (buf == IMDVERSION) {
00202     if (!imd_go(s)) return 0;
00203     return -1;
00204   }
00205   swap4((char *)&buf, 4);
00206   if (buf == IMDVERSION) {
00207     if (!imd_go(s)) return 1;
00208   }
00209   
00210   // We failed to determine endianness.
00211   return -1; 
00212 }

IMDType imd_recv_header ( void *  s,
int32 length 
)

Definition at line 214 of file imd.C.

References HEADERSIZE, IMD_IOERROR, imd_readn(), and swap_header().

Referenced by GlobalMasterIMD::get_vmd_forces(), and my_imd_connect().

00214                                                 {
00215   IMDheader header;
00216   if (imd_readn(s, (char *)&header, HEADERSIZE) != HEADERSIZE)
00217     return IMD_IOERROR;
00218   int i;
00219   char *ch = (char*)(&header);
00220   swap_header(&header);
00221   *length = header.length;
00222   return IMDType(header.type); 
00223 }

IMDType imd_recv_header_nolengthswap ( void *  s,
int32 length 
)

Definition at line 182 of file imd.C.

References HEADERSIZE, IMD_IOERROR, imd_readn(), and swap_header().

Referenced by imd_recv_handshake().

00182                                                              {
00183   IMDheader header;
00184   if (imd_readn(s, (char *)&header, HEADERSIZE) != HEADERSIZE)
00185     return IMD_IOERROR;
00186   *length = header.length;
00187   swap_header(&header);
00188   return IMDType(header.type);
00189 }

int imd_recv_mdcomm ( void *  s,
int32  n,
int32 indices,
float *  forces 
)

Definition at line 225 of file imd.C.

References imd_readn().

Referenced by GlobalMasterIMD::get_vmd_forces().

00225                                                                      {
00226   if (imd_readn(s, (char *)indices, 4*n) != 4*n) return 1;
00227   if (imd_readn(s, (char *)forces, 12*n) != 12*n) return 1;
00228   return 0;
00229 }

int imd_send_energies ( void *  s,
const IMDEnergies energies 
)

Definition at line 159 of file imd.C.

References fill_header(), HEADERSIZE, IMD_ENERGIES, and imd_writen().

Referenced by GlobalMasterIMD::send_energies().

00159                                                             {
00160   int32 size = HEADERSIZE+sizeof(IMDEnergies);
00161   char *buf = new char[size];
00162   fill_header((IMDheader *)buf, IMD_ENERGIES, 1);
00163   memcpy((void *)(buf+HEADERSIZE), (const void *)energies, sizeof(IMDEnergies));
00164   int rc = (imd_writen(s, buf, size) != size);
00165   delete [] buf;
00166   return rc;
00167 }

int imd_send_fcoords ( void *  s,
int32  n,
const float *  coords 
)

Definition at line 169 of file imd.C.

References fill_header(), HEADERSIZE, IMD_FCOORDS, and imd_writen().

Referenced by GlobalMasterIMD::send_fcoords().

00169                                                             {
00170   int32 size = HEADERSIZE+12*n;
00171   char *buf = new char[size];
00172   fill_header((IMDheader *)buf, IMD_FCOORDS, n);
00173   memcpy((void *)(buf+HEADERSIZE), (const void *)coords, 12*n);
00174   int rc = (imd_writen(s, buf, size) != size);
00175   delete [] buf;
00176   return rc;
00177 }

int imd_send_mdcomm ( void *  s,
int32  n,
const int32 indices,
const float *  forces 
)

Definition at line 148 of file imd.C.

References fill_header(), HEADERSIZE, IMD_MDCOMM, and imd_writen().

00148                                                                               {
00149   int32 size = HEADERSIZE+16*n;
00150   char *buf = new char[size]; 
00151   fill_header((IMDheader *)buf, IMD_MDCOMM, n);
00152   memcpy((void *)(buf+HEADERSIZE), (const void *)indices, 4*n);
00153   memcpy((void *)(buf+HEADERSIZE+4*n), (const void *)forces, 12*n);
00154   int rc = (imd_writen(s, buf, size) != size);
00155   delete [] buf;
00156   return rc;
00157 }

int imd_trate ( void *  s,
int32  rate 
)

Definition at line 140 of file imd.C.

References fill_header(), HEADERSIZE, IMD_TRATE, and imd_writen().

00140                                    {
00141   IMDheader header;
00142   fill_header(&header, IMD_TRATE, rate);
00143   return (imd_writen(s, (char *)&header, HEADERSIZE) != HEADERSIZE);
00144 }

static int32 imd_writen ( void *  s,
const char *  ptr,
int32  n 
) [static]

Definition at line 89 of file imd.C.

References vmdsock_write().

Referenced by imd_disconnect(), imd_go(), imd_handshake(), imd_kill(), imd_pause(), imd_send_energies(), imd_send_fcoords(), imd_send_mdcomm(), and imd_trate().

00089                                                            {
00090   int32 nleft;
00091   int32 nwritten;
00092 
00093   nleft = n;
00094   while (nleft > 0) {
00095     if ((nwritten = vmdsock_write(s, ptr, nleft)) <= 0) {
00096       if (errno == EINTR)
00097         nwritten = 0;
00098       else
00099         return -1;
00100     }
00101     nleft -= nwritten;
00102     ptr += nwritten;
00103   }
00104   return n;
00105 }

static void swap4 ( char *  data,
int  ndata 
) [static]

Definition at line 17 of file imd.C.

Referenced by imd_recv_handshake().

00017                                          {
00018   int i;
00019   char *dataptr;
00020   char b0, b1;
00021 
00022   dataptr = data;
00023   for (i=0; i<ndata; i+=4) {
00024     b0 = dataptr[0];
00025     b1 = dataptr[1];
00026     dataptr[0] = dataptr[3];
00027     dataptr[1] = dataptr[2];
00028     dataptr[2] = b1;
00029     dataptr[3] = b0;
00030     dataptr += 4;
00031   }
00032 }

static void swap_header ( IMDheader header  )  [static]

Definition at line 65 of file imd.C.

References imd_ntohl(), IMDheader::length, and IMDheader::type.

Referenced by imd_recv_header(), and imd_recv_header_nolengthswap().

00065                                            {
00066   header->type = imd_ntohl(header->type);
00067   header->length= imd_ntohl(header->length);
00068 }


Generated on Fri Sep 22 01:17:16 2017 for NAMD by  doxygen 1.4.7