Converted imd module to C++.
authorRossen Apostolov <rossen@kth.se>
Mon, 8 Sep 2014 12:05:54 +0000 (14:05 +0200)
committerGerrit Code Review <gerrit@gerrit.gromacs.org>
Mon, 7 Sep 2015 10:50:14 +0000 (12:50 +0200)
Removed unused variables.
Implemented our own byte swapping routines to prevent
deprecated-register warnings for imd.cpp and imdsocket.cpp.

Change-Id: Iaa61299f2d4dad61e8a0a0122fb402184d4d1f62

src/gromacs/imd/CMakeLists.txt
src/gromacs/imd/imd.cpp [moved from src/gromacs/imd/imd.c with 98% similarity]
src/gromacs/imd/imdsocket.cpp [moved from src/gromacs/imd/imdsocket.c with 90% similarity]

index 6714e7bcb1671837a2dd80e5d73ac2ad04f66d8a..0b47f378724845a9cd171351ca54eb332c6fae50 100644 (file)
@@ -1,7 +1,7 @@
 #
 # This file is part of the GROMACS molecular simulation package.
 #
-# Copyright (c) 2014, by the GROMACS development team, led by
+# Copyright (c) 2014,2015, by the GROMACS development team, led by
 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
 # and including many others, as listed in the AUTHORS file in the
 # top-level source directory and at http://www.gromacs.org.
@@ -32,5 +32,5 @@
 # To help us fund GROMACS development, we humbly ask that you cite
 # the research papers on the package. Check out http://www.gromacs.org.
 
-file(GLOB IMD_SOURCES *.cpp *.c)
+file(GLOB IMD_SOURCES *.cpp)
 set(LIBGROMACS_SOURCES ${LIBGROMACS_SOURCES} ${IMD_SOURCES} PARENT_SCOPE)
similarity index 98%
rename from src/gromacs/imd/imd.c
rename to src/gromacs/imd/imd.cpp
index a21df8058428b0880ec43ab0e049247ab04764a7..873e0e85caa94a5dfefff0ee32e1aa4ed84eddce 100644 (file)
@@ -239,12 +239,40 @@ const char *eIMDType_names[IMD_NR + 1] = {
 
 #ifdef GMX_IMD
 
+/*! \brief Byte swap in case we are little-endian */
+static gmx_int32_t gmx_htonl(gmx_int32_t src)
+{
+    int num = 1;
+
+    if (*(char *)&num == 1)
+    {
+        return src;
+    }
+    else
+    {
+        gmx_int32_t dest = 0;
+
+        dest |= (src & 0xFF000000) >> 24;
+        dest |= (src & 0x00FF0000) >> 8;
+        dest |= (src & 0x0000FF00) << 8;
+        dest |= (src & 0x000000FF) << 24;
+
+        return dest;
+    }
+}
+
+/*! \brief Byte-unswap 32 bit word in case we are little-endian */
+static gmx_int32_t gmx_ntohl(gmx_int32_t src)
+{
+    return gmx_htonl(src);
+}
+
 /*! \brief Fills the header with message and the length argument. */
 static void fill_header(IMDHeader *header, IMDMessageType type, gmx_int32_t length)
 {
     /* We (ab-)use htonl network function for the correct endianness */
-    header->type   = htonl((gmx_int32_t) type);
-    header->length = htonl(length);
+    header->type   = gmx_htonl((gmx_int32_t) type);
+    header->length = gmx_htonl(length);
 }
 
 
@@ -252,8 +280,8 @@ static void fill_header(IMDHeader *header, IMDMessageType type, gmx_int32_t leng
 static void swap_header(IMDHeader *header)
 {
     /* and vice versa... */
-    header->type   = ntohl(header->type);
-    header->length = ntohl(header->length);
+    header->type   = gmx_ntohl(header->type);
+    header->length = gmx_ntohl(header->length);
 }
 
 
@@ -617,7 +645,7 @@ static gmx_bool imd_tryconnect(t_gmx_IMD_setup *IMDsetup)
 static void imd_blockconnect(t_gmx_IMD_setup *IMDsetup)
 {
     /* do not wait for connection, when e.g. ctrl+c is pressed and we will terminate anyways. */
-    if (gmx_get_stop_condition() != gmx_stop_cond_none)
+    if (!((int) gmx_get_stop_condition() == gmx_stop_cond_none))
     {
         return;
     }
@@ -811,7 +839,6 @@ static void imd_sync_nodes(t_inputrec *ir, t_commrec *cr, double t)
 {
     int              new_nforces = 0;
     t_gmx_IMD_setup *IMDsetup;
-    int              start, end, i;
 
 
     IMDsetup = ir->imd->setup;
similarity index 90%
rename from src/gromacs/imd/imdsocket.c
rename to src/gromacs/imd/imdsocket.cpp
index ef70405d931d6e5f5219c3c5a5bda7ec8462f012..6dd6e83466dd420ff36c2c6c9ceb18cc4933fca3 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2014, by the GROMACS development team, led by
+ * Copyright (c) 2014,2015, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -96,7 +96,7 @@ extern int imdsock_winsockinit()
 
 
 /*! \brief Print a nice error message on UNIX systems, using errno.h. */
-static void print_IMD_error(char *file, int line, char *msg)
+static void print_IMD_error(const char *file, int line, char *msg)
 {
     fprintf(stderr, "%s Error in file %s on line %d.\n", IMDstr, file, line);
 
@@ -106,6 +106,32 @@ static void print_IMD_error(char *file, int line, char *msg)
     }
 }
 
+/*! \brief Byte swap in case we are little-endian */
+static uint16_t gmx_htons(uint16_t src)
+{
+    uint16_t num = 1;
+
+    if (*(char *)&num == 1)
+    {
+        return src;
+    }
+    else
+    {
+        uint16_t dest = 0;
+
+        dest |= (src & 0x0000FF00) >> 8;
+        dest |= (src & 0x000000FF) << 8;
+
+        return dest;
+    }
+}
+
+/*! \brief Byte-unswap 16 bit word in case we are little-endian */
+static uint16_t gmx_ntohs(uint16_t src)
+{
+    return gmx_htons(src);
+}
+
 
 extern IMDSocket* imdsock_create()
 {
@@ -138,7 +164,7 @@ extern int imdsock_bind(IMDSocket *sock, int port)
 #ifdef GMX_IMD
     memset(&(sock->address), 0, sizeof(sock->address));
     sock->address.sin_family = PF_INET;
-    sock->address.sin_port   = htons(port);
+    sock->address.sin_port   = gmx_htons(port);
 
     /* Try to bind to address and port ...*/
     ret = bind(sock->sockfd, (struct sockaddr *) &sock->address, sizeof(sock->address));
@@ -212,11 +238,10 @@ extern int imdsock_getport(IMDSocket *sock, int *port)
 {
     int                ret;
 #ifdef GMX_IMD
-    struct sockaddr_in sin;
     socklen_t          len;
 
 
-    len = sizeof(sin);
+    len = sizeof(struct sockaddr_in);
     ret = getsockname(sock->sockfd, (struct sockaddr *) &(sock->address), &len);
     if (ret)
     {
@@ -225,10 +250,11 @@ extern int imdsock_getport(IMDSocket *sock, int *port)
     }
     else
     {
-        *port = ntohs(sock->address.sin_port);
+        *port = gmx_ntohs(sock->address.sin_port);
     }
 #else
     gmx_incons("imdsock_getport called without IMD support.");
+    ret = -1;
 #endif
 
     return ret;