Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Лабы / 2 / lab.04.by mice / hail / main

.cpp
Скачиваний:
10
Добавлен:
17.04.2013
Размер:
2.84 Кб
Скачать
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>

#include "cdm_addr_info.h"
#include "cdm_hailer.h"


int usage();
int parse_cmd_line(const int argc, const char *const *const args, cdm_hailer *const hailer, int *const num);


int main(int argc, char **args) {
	WSADATA wsa_data;
	if (0 != WSAStartup(MAKEWORD(2, 0), &wsa_data)) {
		printf("Can't init WSA\n");
		return -1;
	}

	cdm_hailer hailer;
	int num = 4;
	if (0 != parse_cmd_line(argc, args, &hailer, &num)) goto exit_label;

	if (0 != hailer.link()) {
		printf("Can't creare socket and associate it with remote address\n");
		goto exit_label;
	}

	printf("\n[Stage 0]\n\n");
	for (int i = num; 0 < i; --i) {
		printf("Sending request...");
		if (0 != hailer.hail_it(i)) {
			printf(" failed.\n");
			continue;
		}

		printf(" ok.\nWaiting reply...");
		if (0 != hailer.listen_it(1000)) {
			printf(" time out.\n");
			continue;
		}
		double t = (double)hailer.hail_reply()->rtime / (double)CLOCKS_PER_SEC;
		printf(" ok.\trt: %.3f\tttl: %i\n", t, hailer.hail_reply()->ttl);
	}

	printf("\n\n[Stage 1]\n\n");
	printf("Sending %i requests...", num);
	int c = 0;
	for (int i = num; 0 < i; --i) {
		if (0 == hailer.hail_it(i)) ++c;
	}
	printf(" %i done.\n", c);
	
	for (int i = c; 0 < i; --i) {
		printf("Waiting reply...");
		if (0 != hailer.listen_it(1000)) {
			printf(" time out.\n");
			continue;
		}
		double t = (double)hailer.hail_reply()->rtime / (double)CLOCKS_PER_SEC;
		printf(" ok.\trt: %.3f\tttl: %i\n", t, hailer.hail_reply()->ttl);
	}

exit_label:
	return WSACleanup();
}


int usage() {
	printf("\n");
	printf("Usage is:\n");
	printf("hail [host=localhost] [packet_size=32] [count=4]\n");
	printf("You can freely share this program with friends.\n");
	printf("\n");

	return 0;
}


int parse_cmd_line(const int argc, const char *const *const args, cdm_hailer *const hailer, int *const num) {
	if (0 != hailer->iaddr()->set_to_local(0)) {
		printf("Very bad error - you don't have localhost. Gi, man - lol.");
		return -1;
	}

	if (1 >= argc) {
		printf("Whom to hail? localhost? Ok, i'll try.  But don't promise anything...\n");
		return usage();
	}

	if (0 != hailer->target(args[1])) {
		printf("Can't resolve host %s\n", args[1]);
		return -1;
	}

	if (2 < argc) {
		int sz = atoi(args[2]);
		if (0 < sz && sz < hailer->mx_pckt_sz()) {
			hailer->set_pckt_sz(sz);
		} else {
			printf("Invalid packet size. It must be in (0, %i).\n", hailer->mx_pckt_sz());
		}
	}

	if (3 < argc) {
		int n = atoi(args[3]);
		if (0 < n && n < 10000) *num = n;
		else *num = 4;
	}

	printf("Will hail %s [%s] with %i packets of size %i bytes\n", hailer->iaddr()->host(), hailer->iaddr()->ip_str(), *num, hailer->pckt_sz());

	return 0;
}
Соседние файлы в папке hail
  • #
    17.04.2013414 б10check_sum.h
  • #
    17.04.201344.03 Кб10hail.ncb
  • #
    17.04.2013897 б10hail.sln
  • #
    17.04.201310.24 Кб10hail.suo
  • #
    17.04.20133.78 Кб10hail.vcproj
  • #
    17.04.20132.84 Кб10main.cpp