コード例 #1
0
	void configure(){
		setLoggingLevelEnabled(LOGGING_LEVEL_DEBUG);
		setVdaRunningStatus(TRUE);
		setGlobalHeap( createHeap(1024) );
		setupConfiguration();
		if (!isVdaOfflineMode()) {
			initializeWinsock();
			initializeKssConnection();
		}
		if(isVdaAvailableCache())
			initializeCache();
		initializeDisk();
	}
コード例 #2
0
ファイル: HTTPClient.cpp プロジェクト: ShawnAndrews/DataMiner
/* Connect to a host on a certain port number. Throws HTTPClientException.
webpage: required format "www.google.com/"
port: default 80. port hosting the web server
webpageHtml: new pointer to webpage's html
*/
void HTTPClient::httpRequestWebpage(const char* webpage, int port, char** returnedWebpageHtml)
{
	char* httpGetText = "GET ";
	char* httpHostText = " HTTP/1.1\r\nHost: ";
	char* httpReturnText = "\r\n\r\n";
	char* webpageHost = NULL;
	char* webpageResource = NULL;
	char* httpRequest = NULL;
	char* tempReturnedWebpageHtml = NULL;
	int resourceIndex = -1;
	int htmlDocumentSize = 0;
	int httpRequestSize = 0;

	//Delete previously stored html data
	if (*returnedWebpageHtml)
	{
		delete[] * returnedWebpageHtml;
		*returnedWebpageHtml = NULL;
	}

	//Initialize Winsock 2.2
	if (!initializeWinsock())
	{
		throw HTTPClientException("httpRequestWebpage(const char*, int, char**)", "Failed to initialize Winsock.\n");
	}

	//cout << "Full URL: '" << webpage << "' Len: " << strlen(webpage) << "'.\n";

	//Find first '/' to indicate resource
	for (size_t i = 0; i < strlen(webpage); i++){
		if (webpage[i] == '/'){
			resourceIndex = i;
			//cout << "Found resource index: " << i << endl;
			break;
		}
	}

	//If failed to find resource
	if (resourceIndex == -1)
		throw HTTPClientException("httpRequestWebpage(const char*, int, char**)", "Cannot find /resource in URL.\n");

	//Set host
	webpageHost = new char[resourceIndex + 1];
	strncpy_s(webpageHost, resourceIndex + 1, webpage, resourceIndex);
	//cout << "Host: '" << webpageHost << "' Len: " << strlen(webpageHost) << endl;

	//Set resource
	webpageResource = new char[strlen(webpage) - resourceIndex + 1];
	strncpy_s(webpageResource, strlen(webpage) - resourceIndex + 1, webpage + resourceIndex, strlen(webpage) - resourceIndex);
	//cout << "Resource: '" << webpageResource << "' Len: " << strlen(webpageResource) << endl;

	//Connect to web server
	if (!connectToWebServer(webpageHost, port)){
		cleanup(webpageHost, webpageResource, httpRequest, tempReturnedWebpageHtml);
		throw HTTPClientException("httpRequestWebpage(const char*, int, char**)", "Failed to connect.\n");
	}

	//Build HTTP request
	httpRequestSize = strlen(httpGetText) + strlen(webpageResource) + strlen(httpHostText) + strlen(webpageHost) + strlen(httpReturnText) + 1;
	httpRequest = new char[httpRequestSize];
	strcpy(httpRequest, httpGetText);
	strcpy(httpRequest + strlen(httpGetText), webpageResource);
	strcpy(httpRequest + strlen(httpGetText) + strlen(webpageResource), httpHostText);
	strcpy(httpRequest + strlen(httpGetText) + strlen(webpageResource) + strlen(httpHostText), webpageHost);
	strcpy(httpRequest + strlen(httpGetText) + strlen(webpageResource) + strlen(httpHostText) + strlen(webpageHost), httpReturnText);

	//Send HTTP request
	if (!sendHttpRequest(httpRequest))
	{
		cleanup(webpageHost, webpageResource, httpRequest, tempReturnedWebpageHtml);
		throw HTTPClientException("httpRequestWebpage(const char*, int, char**)", "Failed to send() HTTP request.\n");
	}

	//Allocate memory for html document
	tempReturnedWebpageHtml = new char[MAXIMUM_WEBPAGE_LENGTH];

	cout << "Start receive()...\n";
	//Recieve HTTP response
	if (!receiveHttpResponse(tempReturnedWebpageHtml, htmlDocumentSize)){
		cleanup(webpageHost, webpageResource, httpRequest, tempReturnedWebpageHtml);
		throw HTTPClientException("httpRequestWebpage(const char*, int, char**)", "Failed to receive() HTTP request.\n");
	}
	cout << "End receive()...\n";

	//Copy temporary buffer to returned buffer
	*returnedWebpageHtml = new char[htmlDocumentSize + 2];
	strcpy(*returnedWebpageHtml, tempReturnedWebpageHtml);

	//printf("Document size: %d \n", htmlDocumentSize);

	//Reset server config, necessary for multiple http requests to different hosts from the same object
	resetServerConfig();

	//Delete memory allocated in method
	cleanup(webpageHost, webpageResource, httpRequest, tempReturnedWebpageHtml);
}