From e658a25e6cfb1da1948cdcb3af1c84f516d32e89 Mon Sep 17 00:00:00 2001 From: Rob Dawson Date: Fri, 18 Nov 2011 18:05:25 +0000 Subject: [PATCH] Fixing transaction boundaries to avoid locking db when the target host takes a long time to respond. Code tidy --- bmsync/src/bmsync.c | 205 +++++++++++++++++++++++++------------------- 1 file changed, 119 insertions(+), 86 deletions(-) diff --git a/bmsync/src/bmsync.c b/bmsync/src/bmsync.c index e200e1c..544f4ec 100644 --- a/bmsync/src/bmsync.c +++ b/bmsync/src/bmsync.c @@ -59,11 +59,59 @@ Contains the entry-point for the bmsync command-line utility. static struct SyncPrefs prefs = {0, 0, NULL, 0, DEFAULT_PORT, NULL, NULL}; static SOCKET doConnect(char* host, char* alias, int port); -static int parseData(SOCKET fd, char* alias, int*); +static int parseData(SOCKET fd, char* alias, time_t ts, int* rowCount); static struct Data* parseRow(char* row); static int sendRequest(SOCKET fd, long ts, char* host, int port); static time_t getMaxTsForHost(char* alias); +static int syncWithHost(char* host, char* alias, int port){ + resetStatusMsg(); + + time_t maxTsForHost = getMaxTsForHost(alias); + + // Attempt to connect to the host/port specified + SOCKET sockFd = doConnect(host, alias, port); + int status = (sockFd == 0) ? FAIL : SUCCESS; + + if (status == SUCCESS) { + statusMsg(MSG_CONNECTED); + + // We're in - send a request for the data + status = sendRequest(sockFd, maxTsForHost, host, port); + if (status == SUCCESS){ + // Parse the response + int rowCount; + status = parseData(sockFd, alias, maxTsForHost, &rowCount); + + if (status == SUCCESS){ + // It all worked ok + statusMsg("%d new row%s", rowCount, (rowCount == 1 ? "" : "s")); + + } else { + // Something was wrong with the response + logMsg(LOG_ERR, "unable to parse sync response row"); + } + + } else { + // Unable to send the request to the remote host + logMsg(LOG_ERR, "unable to send sync request to %s:%d", host, port); + } + + #ifdef _WIN32 + closesocket(sockFd); + #else + close(sockFd); + #endif + + } else { + // Unable to connect + logMsg(LOG_ERR, "failed to connect to %s:%d", host, port); + } + printf("\n"); + + return status; +} + int main(int argc, char **argv){ printf(COPYRIGHT); fflush(stdout); @@ -93,11 +141,7 @@ int main(int argc, char **argv){ openDb(); dbVersionCheck(); setupDb(); - int i; - SOCKET sockFd; - char* host; - char* alias; int port = prefs.port; #ifdef _WIN32 @@ -105,57 +149,21 @@ int main(int argc, char **argv){ int rc = WSAStartup(MAKEWORD(2,2), &wsaData); if (rc != 0) { logMsg(LOG_ERR, "WSAStartup returned error %d", rc); - exit(1); + status = FAIL; } #endif - - for (i=0; i