[공부중] 802.11 simulation

main.cpp
Usage : 실행파일명 [Node수] [EndTime] [생성할 데이터파일 이름]

#include 
#include 
#include 
#include 

#define ENDTIME 100000000
#define FILENAME "data.txt"

#define M 5 // maximum number of retransmissions
#define CWmin 32 // initial contention window
/* System Parameters */
#define SLOT_TIME 20 // microsecond
#define SIFS 10 // u sec
#define DIFS 50 // u sec
//#define AIFS 30  // u sec
#define MAC_H 72 // MAC header
#define PHY_H 192 // PHY header
#define PAYLOAD 8000// 8000 bits of payload
#define RTS_FRAME (PHY_H + 160) //RTS frame length
#define CTS_FRAME (PHY_H + 112) // 304 us CTS frame length
#define ACK_FRAME (PHY_H + 112) // 304 us ACK frame length
#define T_DATA (PHY_H + MAC_H + PAYLOAD)
#define aPHY_RX_START_Delay 192
#define CTS_TIMEOUT ((SIFS)+(SLOT_TIME)+(aPHY_RX_START_Delay))
#define T_SUC_BASIC (T_DATA + SIFS + ACK_FRAME + DIFS)
#define T_COL_BASIC (T_DATA + SIFS + DIFS)
#define T_SUC_DCF (RTS_FRAME + SIFS + CTS_FRAME + SIFS + T_DATA + SIFS + ACK_FRAME + DIFS)
#define T_COL_DCF (RTS_FRAME + SIFS + CTS_TIMEOUT + EIFS + DIFS)
#define EIFS ((SLOT_TIME)+(DIFS)+(ACK_FRAME))

using namespace ::std;

class Node {
	int collisionCounter;
	int transCounter;
	int backOffCounter;

	int uniform(int min, int max) {
		unsigned int r = (unsigned int) rand();
		unsigned int maxValue = (RAND_MAX+(unsigned int) 1) / (max - min + 1)
				- 1;

		if (r > maxValue)
			return uniform(min, max);
		else
			return (int) r % (max - min + 1);
	}

public:

	Node() {
		collisionCounter = 0;
		transCounter = 0;
		backOffCounter = 0;
	}
	int getBackOffCounter() {
		return backOffCounter;
	}
	void setBackOffCounter() {
		backOffCounter = (int) uniform(0, (int) (pow((double) 2,
				(double) collisionCounter) * 32) - 1);
	}
	void decreaseBackOffCounter() {
		backOffCounter--;
	}
	void setCollisionCounter() {
		if (collisionCounter > M) {
			collisionCounter = 0;
		} else {
			collisionCounter++;
		}
	}
	void resetCollisionCounter() {
		collisionCounter = 0;
		transCounter++;
	}
	double transRate(int term) {
		return transCounter / (double) term;
	}
	void resetTransRate() {
		transCounter = 0;
	}

};

int main(int argc, char* argv[]) {
	int NODES = 0;
	char cNODES[20];
	int END = ENDTIME;
	char cEND[20];
	char* filename = FILENAME;

	switch (argc) {
	case 1:
		cout << "usage : " << argv[0] << " [NODES] [ENDTIME] [FILENAME]"
				<< endl;
		cout << "Number of Node = ";
		cin >> cNODES;
		break;
	case 4:
		filename = argv[3];
	case 3:
		END = atoi(argv[2]);
	case 2:
		strcpy(cNODES, argv[1]);
		break;
	default:
		cout << "usage : " << argv[0] << " [NODES] [ENDTIME] [FILENAME]"
				<< endl;
		return 0;
	}

	/*if (argc == 1){
	 cout<<"usage : "< 1) { //collision
			t += T_COL_DCF;
			collision++;
			for (int i = 0; i < NODES; i++) {
				if (n[i].getBackOffCounter() == 0) {
					n[i].setBackOffCounter();
					n[i].setCollisionCounter();
				}
			}
		} else if (collisionCheck == 1) { //trans
			t += T_SUC_DCF;
			trans++;
			n[collisionIndex].resetCollisionCounter();
			n[collisionIndex].setBackOffCounter();
			fprintf(dataFile, "%dt", collisionIndex);

		} else if (collisionCheck == 0) { //wait
			t += SLOT_TIME;
			wait++;
			for (int i = 0; i < NODES; i++)
				n[i].decreaseBackOffCounter();

		}
		collisionCheck = 0;

	}
	//	printf("n--------------------------------------------------------------------------n");
	cout << "trans = " << trans << "    collision = " << collision
			<< "    wait = " << wait << "	end time = " << END << endl;
	cout << "TransTime/EndTime = " << trans * PAYLOAD / (double) END << endl;
	cout << "Data saved : " << filename << endl;
	fclose(dataFile);

	delete[] n;

	return 0;
}

/*

 */

jainsIndex.cpp
위에서 "%dt"로 저장된 데이터 파일을 불러 1부터 입력한 윈도우 사이즈 까지 JainsIndex공식을 적용하여 F를 추출 한다.

#include 
#include 

using namespace ::std;
double jainsIndex(int nodes, int dataSize, int* datas, int windowSize);

int main(int argc, char*argv[]) {

	int dataSize = 0;
	int itemp = 0;
	int nodes = 0;
	int windowSize = 100;
	char* sourceFileName = "data.txt";
	switch (argc) {
	case 1:
		break;
	case 3:
		sourceFileName = argv[2];
	case 2:
		windowSize = atoi(argv[1]);
		break;
	default:
		cout << "Usage : " << argv[0] << " [EndWindowSize] [SourceDataFile]" << endl;
		return 0;
	}
	FILE* countDataSize = fopen(sourceFileName, "rt");
	FILE* temp = fopen("result.txt", "wt");
	while (1) {
		if (fscanf(countDataSize, "%dt", &itemp) == -1)
			break;
		if (nodes < itemp)
			nodes = itemp;
		fprintf(temp, "%dt", itemp);
		dataSize++;
	}
	fclose(countDataSize);
	fclose(temp);

	int* datas = new int[dataSize];

	FILE* data = fopen("data.txt", "rt");
	for (int i = 0; 1; i++) {
		if (fscanf(data, "%dt", &itemp) == -1)
			break;
		datas[i] = itemp;
	}
	fclose(data);
	temp = fopen("result.txt", "wt");

	for (int i = 2; i <= windowSize; i++) {
		cout << "WindowSize " << i << " = " << (double) jainsIndex(nodes + 1,
				dataSize, datas, i) << endl;
		fprintf(temp, "%dt%lfn", i, (double) jainsIndex(nodes + 1, dataSize,
				datas, i));
	}
	fclose(temp);
	if (argc != 2)
		cout << "Usage : " << argv[0] << " " << "[EndWindowSize]" << endl;
	delete[] datas;

	return 0;
}

double jainsIndex(int nodes, int dataSize, int* datas, int windowSize) {
	double* r = new double[nodes];
	double sum = 0.0, squareSum = 0.0;
	double sumF = 0.0;
	for (int i = 0; i < dataSize; i++) { //데이터를 순회 하면서
		r[datas[i]] += 1 / (double) windowSize; //데이터 값에 해당하는 r[]을 1/윈도우 사이즈 만큼 증가

		if (i % windowSize == windowSize - 1) { //r의 마지막 원소에 기록 하고 난뒤
			for (int j = 0; j < nodes; j++) { //r을 순회 하면
				squareSum += (r[j] * r[j]); //제곱의합
				r[j] = 0; //할일을 하였으니 초기화 하고.
			}
			sumF += 1 / (nodes * squareSum); //F의 합
			sum = 0;
			squareSum = 0;
		}
	}
	delete[] r;

	return sumF / (dataSize / windowSize); //F의 평균을 리턴
}

2 thoughts on “[공부중] 802.11 simulation

  1. C-Master

    fuck~!!

    • ㅋㅋ 수업시간에 이러시면 곤란 합니다 ㅎ

답글 남기기

이메일 주소는 공개되지 않습니다.

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

이 사이트는 스팸을 줄이는 아키스밋을 사용합니다. 댓글이 어떻게 처리되는지 알아보십시오.