Commit 867a9339 authored by 神楽坂玲奈's avatar 神楽坂玲奈

group

parent a9cbec69
Pipeline #16358 passed with stages
in 2 minutes and 15 seconds
import { GatewayGroup as GatewayGroupData } from '../import/scripts/GatewayGroup';
import _ from 'lodash';
import routers from '../import/data/Router.json';
import gatewayGroups from '../import/data/GatewayGroup.json';
export interface GatewayGroup extends GatewayGroupData {}
export class GatewayGroup {
static all: Record<number, GatewayGroup> = _.keyBy(
gatewayGroups.map((g) => new this(g)),
'id'
);
public routers: number[];
constructor(public data: GatewayGroupData) {
Object.assign(this, data);
this.routers = GatewayGroup.groupRouters(data);
}
static groupRouters(g: GatewayGroupData): number[] {
return _.uniq(
g.locationPrefix
.flatMap((p) => routers.filter((r) => r.location.startsWith(p)))
.concat(routers.filter((r) => g.includeRouters.includes(r.name)))
.filter((r) => !g.excludeRouters.includes(r.name))
.map((r) => r.id)
.concat(gatewayGroups.filter((g1) => g.children.includes(g1.name)).flatMap((c) => this.groupRouters(c)))
);
}
}
import { RemoteInfo, Socket } from 'dgram'; import { RemoteInfo, Socket } from 'dgram';
import { Change, PeerQuality, Report } from '../protocol'; import { Change, PeerQuality, Report } from '../protocol';
import routers from '../import/data/Router.json'; import routers from '../import/data/Router.json';
// import plans from '../config/plans.json';
import assert from 'assert'; import assert from 'assert';
import { Quality } from './Quality'; import { Quality } from './Quality';
import _ from 'lodash'; import _ from 'lodash';
import config from '../config/config.json'; import config from '../config/config.json';
import _connections from '../import/connections.json'; import _connections from '../import/connections.json';
import { GatewayGroup } from '../import/scripts/GatewayGroup'; import { GatewayGroup } from './GatewayGroup';
const connections: Record<number, Record<number, { metric: number; protocol: string }>> = _connections; const connections: Record<number, Record<number, { metric: number; protocol: string }>> = _connections;
export class Router { export class Router {
static all: Router[] = routers.map((s) => new Router(s.id)); static all: Router[] = routers.map((s) => new Router(s.id));
static updating?: { static updating?: {
router: Router; router: Router;
message: Change; message: Change;
}; };
static groups: Record<number, Router[]>;
seq = 0; seq = 0;
peers: Record<number, PeerQuality> = {}; peers: Record<number, PeerQuality> = {};
...@@ -42,9 +39,9 @@ export class Router { ...@@ -42,9 +39,9 @@ export class Router {
this.via.set(router, router); this.via.set(router, router);
} }
if (Router.updating?.router == this) Router.updating = undefined; if (Router.updating?.router == this) Router.updating = undefined;
// for (const plan of plans.filter(plan => !plan.routers.includes(this.id))) { for (const plan of Object.values(GatewayGroup.all).filter((group) => !group.routers.includes(this.id))) {
// this.plan[plan.id] = this.id; this.plan[plan.id] = this.id;
// } }
} }
onMessage(socket: Socket, data: Report) { onMessage(socket: Socket, data: Report) {
...@@ -107,18 +104,17 @@ export class Router { ...@@ -107,18 +104,17 @@ export class Router {
// 计算 route plan // 计算 route plan
// 凡是自己可以作为那个 plan 出口的,是不会计算直接跳过的,所以这里有 plan 到自己的意思是,没有找到任何一个通的可以出的地方,所以只好从自己出了 // 凡是自己可以作为那个 plan 出口的,是不会计算直接跳过的,所以这里有 plan 到自己的意思是,没有找到任何一个通的可以出的地方,所以只好从自己出了
const changedPlan: Record<number, number> = {}; const changedPlan: Record<number, number> = {};
for (const [_groupId, groupRouters] of Object.entries(Router.groups).filter(([_, g]) => !g.includes(this))) { for (const group of Object.values(GatewayGroup.all).filter((group) => !group.routers.includes(this.id))) {
const groupId = parseInt(_groupId); const currentPlan = this.plan[group.id];
const currentPlan = this.plan[groupId];
const currentMetric = currentPlan === this.id ? Infinity : metric[currentPlan]; const currentMetric = currentPlan === this.id ? Infinity : metric[currentPlan];
const items = groupRouters.map((to) => [to.id, metric[to.id]]); const items = group.routers.map((toId) => [toId, metric[toId]]);
const [bestPlan, bestMetric] = _.minBy(items, ([_, m]) => m)!; const [bestPlan, bestMetric] = _.minBy(items, ([_, m]) => m)!;
if (currentPlan !== this.id && bestMetric === Infinity) { if (currentPlan !== this.id && bestMetric === Infinity) {
// 原来通的,现在不通了 // 原来通的,现在不通了
this.plan[groupId] = changedPlan[groupId] = this.id; this.plan[group.id] = changedPlan[group.id] = this.id;
} else if (currentPlan !== bestPlan && bestMetric + config.throttle < currentMetric) { } else if (currentPlan !== bestPlan && bestMetric + config.throttle < currentMetric) {
this.plan[groupId] = changedPlan[groupId] = bestPlan; this.plan[group.id] = changedPlan[group.id] = bestPlan;
} }
} }
...@@ -167,17 +163,3 @@ export class Router { ...@@ -167,17 +163,3 @@ export class Router {
} }
for (const router of Router.all) router.reset(); for (const router of Router.all) router.reset();
function groupRouters(g: GatewayGroup): Router[] {
return _.uniq(
g.locationPrefix
.flatMap((p) => routers.filter((r) => r.location.startsWith(p)))
.concat(routers.filter((r) => g.includeRouters.includes(r.name)))
.filter((r) => !g.excludeRouters.includes(r.name))
.map((r) => Router.all.find((r1) => r1.id === r.id)!)
.concat(gatewayGroups.filter((g1) => g.children.includes(g1.name)).flatMap((c) => groupRouters(c)))
);
}
Router.groups = Object.fromEntries(gatewayGroups.map((g) => [g.id, groupRouters(g)]));
console.log(Router.groups);
...@@ -20,7 +20,7 @@ const socket = dgram ...@@ -20,7 +20,7 @@ const socket = dgram
const address = socket.address(); const address = socket.address();
console.log(`listening ${address.address}:${address.port}`); console.log(`listening ${address.address}:${address.port}`);
}) })
.on('message', function(message, rinfo) { .on('message', function (message, rinfo) {
try { try {
const hello: Report = JSON.parse(message.toString()); const hello: Report = JSON.parse(message.toString());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment