ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

CSP-22-3 DHCP服务器

2022-06-04 13:04:50  阅读:194  来源: 互联网

标签:22 offer int IP Tmax late DHCP CSP pack


模拟题

原题链接: https://www.acwing.com/problem/content/3416/

分析题意,注意细节。

可以不用STL,如果能自己模拟,就不要用STL。

#include <bits/stdc++.h>
using namespace std;

/*
1. 客户端 --Discover--> 服务器 请求IP地址
2. 服务器 --Offer-->    客户端 分配空闲的IP地址
3. 客户端选择一个IP --Request--> 服务器
4. 服务器分析:
	4.1 选择我发送的报文:再次确认
		4.1.1 服务器 --ACK--> 客户端 确认配置有效
		4.1.2 服务器 --NAK--> 客户端 地址无效
	4.2 没选择我的:解除我设置的占用
5.1 客户端收到ACK,完成网络配置(有效时间)
5.2 客户端收到NAK,回到步骤1.
6. 有效时间结束前延时。
	服务器按照步骤4延长(广播,但是只有分配服务器应答)
	客户端按照步骤5处理。

服务器的参数:
1. 地址池大小N,能分配的IP为1~N
2. 默认过期时间Tdef
3. 过期时间的上限和下限Tmax、Tmin
4. 本机名称 H

*/

const int MAXN = 10010;

int N, Tdef, Tmax, Tmin;
string H;
int n;
struct Package {
	string send;
	string accept;
	string type;
	int IP;
	int late;
};
// 地址池  0表示未占用 1表示待分配 2表示占用 3表示过期
int IP_pool[MAXN];
// IP 占用者列表
string IP_owner[MAXN];
// 对应IP的过期时间
int IP_late[MAXN];

int main() {
	cin >> N >> Tdef >> Tmax >> Tmin >> H;
	cin >> n;
	for (int ct = 1; ct <= n; ct++) {
		// 接收时间和报文内容
		int ti;
		Package pack;
		cin >> ti >> pack.send >> pack.accept >>
		    pack.type >> pack.IP >> pack.late;

		// 更新IP状态
		for (int i = 1; i <= N; i++) {
			if (IP_pool[i] == 1) {
				if (IP_late[i] <= ti) {
					IP_late[i] = 0;
					IP_pool[i] = 0;
					IP_owner[i] = "";
				}
			}
			if (IP_pool[i] == 2) {
				if (IP_late[i] <= ti) {
					IP_late[i] = 0;
					IP_pool[i] = 3;
				}
			}
		}

		//不处理情况:
		if (pack.accept != H && pack.accept != "*") {
			if (pack.type != "REQ")
				continue;
		}
		if (pack.type != "DIS" && pack.type != "REQ") {
			continue;
		}
		if ((pack.accept == "*" && pack.type != "DIS") ||
		        (pack.accept == H && pack.type == "DIS")) {
			continue;
		}

		if (pack.type == "DIS") {
			int flag = 0;
			int min_0 = 0;
			int min_3 = 0;
			Package offer;
			offer.accept = pack.send;
			offer.send = H;
			offer.type = "OFR";
			for (int i = 1; i <= N; i++) {
				if (flag == 0 && IP_owner[i] == pack.send) {
					// 分配IP
					flag = i;
				}
				if (min_0 == 0 && IP_pool[i] == 0) {
					min_0 = i;
				}
				if (min_3 == 0 && IP_pool[i] == 3) {
					min_3 = i;
				}
			}

//			cout << "IP_1: " << IP_pool[1] << endl;
//			cout << "flag: " << flag << endl;
//			cout << "min_0: " << min_0 << endl;
//			cout << "min_3: " << min_3 << endl;

			if (flag != 0) {
				offer.IP = flag;
				// 处理分配后的IP池
				IP_pool[flag] = 1;
				IP_owner[flag] = pack.send;
				if (pack.late == 0) {
					offer.late = ti + Tdef;
				} else {
					int late_time = pack.late - ti;
					if (late_time < Tmin) {
						offer.late = ti + Tmin;
					} else if (late_time > Tmax) {
						offer.late = ti + Tmax;
					} else {
						offer.late = pack.late;
					}
				}
				IP_late[flag] = offer.late;
			} else {
				if (min_0 != 0) {
					offer.IP = min_0;
					// 处理分配后的IP池
					IP_pool[min_0] = 1;
					IP_owner[min_0] = pack.send;
					//cout << IP_owner[1] << endl;
					if (pack.late == 0) {
						offer.late = ti + Tdef;
					} else {
						int late_time = pack.late - ti;
						if (late_time < Tmin) {
							offer.late = ti + Tmin;
						} else if (late_time > Tmax) {
							offer.late = ti + Tmax;
						} else {
							offer.late = pack.late;
						}
					}
					IP_late[min_0] = offer.late;
				} else if (min_3 != 0) {
					offer.IP = min_3;
					// 处理分配后的IP池
					IP_pool[min_3] = 1;
					IP_owner[min_3] = pack.send;
					if (pack.late == 0) {
						offer.late = ti + Tdef;
					} else {
						int late_time = pack.late - ti;
						if (late_time < Tmin) {
							offer.late = ti + Tmin;
						} else if (late_time > Tmax) {
							offer.late = ti + Tmax;
						} else {
							offer.late = pack.late;
						}
					}
					IP_late[min_3] = offer.late;
				} else
					continue;
			}

			// 发送Offer报文
			cout << offer.send << " " << offer.accept << " "
			     << offer.type << " " << offer.IP << " " <<
			     offer.late << endl;
		} else if (pack.type == "REQ") {

			Package offer;
			offer.send = H;
			offer.accept = pack.send;
			if (pack.accept != H) {
				for (int i = 1; i <= N; i++) {
					if (IP_owner[i] == pack.send && IP_pool[i] == 1) {
						IP_owner[i] = "";
						IP_pool[i] = 0;
						// 清零过期时刻?
						IP_late[i] = 0;
					}
				}
				continue;
			}


			if (pack.IP > N || IP_owner[pack.IP] != pack.send) {

				offer.type = "NAK";
				offer.IP = pack.IP;
				offer.late = 0;

				cout << offer.send << " " << offer.accept << " "
				     << offer.type << " " << offer.IP << " " <<
				     offer.late << endl;

				continue;
			}

			IP_pool[pack.IP] = 2;
			offer.IP = pack.IP;
			if (pack.late == 0) {
				offer.late = ti + Tdef;
			} else {
				int late_time = pack.late - ti;
				if (late_time < Tmin) {
					offer.late = ti + Tmin;
				} else if (late_time > Tmax) {
					offer.late = ti + Tmax;
				} else {
					offer.late = pack.late;
				}
			}
			IP_late[pack.IP] = offer.late;

			offer.type = "ACK";
			cout << offer.send << " " << offer.accept << " "
			     << offer.type << " " << offer.IP << " " <<
			     offer.late << endl;
		}

	}

	return 0;
}

标签:22,offer,int,IP,Tmax,late,DHCP,CSP,pack
来源: https://www.cnblogs.com/superPG/p/16341665.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有