summaryrefslogtreecommitdiff
path: root/orkbasecxx/OrkClient.cpp
diff options
context:
space:
mode:
authorHenri Herscher <henri@oreka.org>2005-10-20 13:40:58 +0000
committerHenri Herscher <henri@oreka.org>2005-10-20 13:40:58 +0000
commit7e1d63dd9fd149e4934bf77095c8610fac786b04 (patch)
tree5fe486a1b0300c3b84fb559107a868e5cc2c95da /orkbasecxx/OrkClient.cpp
parent467768fc956fc3e5a253373f26c71c681b31b6b8 (diff)
First checkin
git-svn-id: https://oreka.svn.sourceforge.net/svnroot/oreka/trunk@2 09dcff7a-b715-0410-9601-b79a96267cd0
Diffstat (limited to 'orkbasecxx/OrkClient.cpp')
-rw-r--r--orkbasecxx/OrkClient.cpp132
1 files changed, 132 insertions, 0 deletions
diff --git a/orkbasecxx/OrkClient.cpp b/orkbasecxx/OrkClient.cpp
new file mode 100644
index 0000000..900656d
--- /dev/null
+++ b/orkbasecxx/OrkClient.cpp
@@ -0,0 +1,132 @@
+/*
+ * Oreka -- A media capture and retrieval platform
+ *
+ * Copyright (C) 2005, orecx LLC
+ *
+ * http://www.orecx.com
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License.
+ * Please refer to http://www.gnu.org/copyleft/gpl.html
+ *
+ */
+
+#include "Utils.h"
+#include "OrkClient.h"
+#include "ace/INET_Addr.h"
+#include "ace/SOCK_Connector.h"
+#include "ace/SOCK_Stream.h"
+
+#ifdef WIN32
+ #define IOV_TYPE char*
+#else
+ #define IOV_TYPE void*
+#endif
+
+bool OrkHttpClient::ExecuteUrl(CStdString& request, CStdString& response, CStdString& hostname, int tcpPort, int timeout)
+{
+ ACE_SOCK_Connector connector;
+ ACE_SOCK_Stream peer;
+ ACE_INET_Addr peer_addr;
+ ACE_Time_Value aceTimeout (timeout);
+
+ char szTcpPort[10];
+ sprintf(szTcpPort, "%d", tcpPort);
+ iovec iov[8];
+ iov[0].iov_base = (IOV_TYPE)"GET ";
+ iov[0].iov_len = 4; // Length of "GET ".
+ iov[1].iov_base = (PSTR)(PCSTR)request;
+ iov[1].iov_len = request.size();
+ iov[2].iov_base = (IOV_TYPE)" HTTP/1.0\r\n";
+ iov[2].iov_len = 11;
+ iov[3].iov_base = (IOV_TYPE)"Host: ";
+ iov[3].iov_len = 6;
+ iov[4].iov_base = (PSTR)(PCSTR)hostname;
+ iov[4].iov_len = hostname.size();
+ iov[5].iov_base = (IOV_TYPE)":";
+ iov[5].iov_len = 1;
+ iov[6].iov_base = szTcpPort;
+ iov[6].iov_len = strlen(szTcpPort);
+ iov[7].iov_base = (IOV_TYPE)"\r\n\r\n";
+ iov[7].iov_len = 4;
+
+ if (peer_addr.set (tcpPort, (PCSTR)hostname) == -1)
+ {
+ return false;
+ }
+ else if (connector.connect (peer, peer_addr, &aceTimeout) == -1)
+ {
+ if (errno == ETIME)
+ {
+ }
+ return false;
+ }
+ else if (peer.sendv_n (iov, 8, &aceTimeout) == -1)
+ {
+ return false;
+ }
+
+ ssize_t numReceived = 0;
+#define BUFSIZE 4096
+ char buf [BUFSIZE];
+
+ CStdString header;
+ bool gotHeader = false;
+ while ( (numReceived = peer.recv (buf, BUFSIZE, &aceTimeout)) > 0 )
+ {
+ for(int i=0; i<numReceived; i++)
+ {
+ if(!gotHeader)
+ {
+ // extract header (delimited by CR-LF-CR-LF)
+ header += buf[i];
+ size_t headerSize = header.size();
+ if (headerSize > 4 &&
+ header.GetAt(headerSize-1) == '\n' &&
+ header.GetAt(headerSize-2) == '\r' &&
+ header.GetAt(headerSize-3) == '\n' &&
+ header.GetAt(headerSize-4) == '\r' )
+ {
+ gotHeader = true;
+ }
+ }
+ else
+ {
+ // extract content
+ response += buf[i];
+ }
+ }
+ }
+ if(numReceived < 0)
+ {
+ return false;
+ }
+ if(header.size() <= 0 || response.size() <= 0)
+ {
+ return false;
+ }
+ if( header.GetAt(10) != '2' &&
+ header.GetAt(11) != '0' &&
+ header.GetAt(12) != '0' &&
+ header.GetAt(13) != ' ' &&
+ header.GetAt(14) != 'O' &&
+ header.GetAt(15) != 'K' )
+ {
+ return false;
+ }
+ return true;
+}
+
+bool OrkHttpSingleLineClient::Execute(SyncMessage& request, AsyncMessage& response, CStdString& hostname, int tcpPort, CStdString& serviceName, int timeout)
+{
+ CStdString requestString = "/" + serviceName + "/command?";
+ requestString += request.SerializeUrl();
+ CStdString responseString;
+ if (ExecuteUrl(requestString, responseString, hostname, tcpPort, timeout))
+ {
+ response.DeSerializeSingleLine(responseString);
+ return true;
+ }
+ return false;
+}
+