Commit 939e3eb8 authored by 神楽坂玲奈's avatar 神楽坂玲奈

fix

parent 67e8ecaf
Pipeline #16356 passed with stages
in 39 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)))
);
}
}
......@@ -3,6 +3,7 @@ import config from '../config/config.json';
import { parseInt } from 'lodash';
import { Router } from './Router';
import child_process from 'child_process';
import { GatewayGroup } from './GatewayGroup';
export class RouteWriter {
static id = parseInt(process.env.RAILGUN_ID);
......@@ -12,22 +13,19 @@ export class RouteWriter {
static reset() {
console.log('reset');
const self = Router.all.find(r => r.id === this.id);
this.input.push(`route flush table ${config.table} proto ${config.proto}`);
for (const peer of Router.all.filter((r) => r.id !== this.id)) {
for (const peer of Object.values(Router.all)) {
this.setVia(peer.id, peer.id);
}
for (const [_groupId, groupRouters] of Object.entries(Router.groups).filter(([_, g]) => !g.includes(self))) {
const groupId = parseInt(_groupId);
this.setPlan(groupId, this.id);
for (const group of Object.values(GatewayGroup.all).filter((g) => !g.routers.includes(this.id))) {
this.setPlan(group, this.id);
}
}
static setVia(toId: number, viaId: number) {
this.via.set(toId, viaId);
const to = Router.all.find(r => r.id === toId);
const via = Router.all.find(r => r.id === viaId);
const to = Router.all[toId];
const via = Router.all[viaId];
for (const address of [to.address, ...to.subnets]) {
this.input.push(`route replace ${address} via ${via.linkAddress} table ${config.table} proto ${config.proto}`);
}
......@@ -38,14 +36,14 @@ export class RouteWriter {
}
}
static setPlan(table: number, toId: number) {
static setPlan(group: GatewayGroup, toId: number) {
const table = group.destMark + 1000;
this.plan.set(table, toId);
if (toId === this.id) {
this.input.push(`route flush table ${table} proto ${config.proto}`);
} else {
const viaId = this.via.get(toId);
const via = Router.all.find(r => r.id === viaId);
const via = Router.all[viaId];
this.input.push(`route replace default via ${via.linkAddress} table ${table} proto ${config.proto}`);
}
}
......
......@@ -2,13 +2,13 @@ import * as _ from 'lodash';
import config from '../config/config.json';
import { Hello, PeerQuality } from '../protocol';
import routers from '../import/data/Router.json';
import { GatewayGroup } from '../import/scripts/GatewayGroup';
import gatewayGroups from '../import/data/GatewayGroup.json';
import subnets from '../import/data/Subnet.json';
export class Router implements Hello, PeerQuality {
static all: Router[] = routers.filter(r => r.id !== parseInt(process.env.RAILGUN_ID)).map(r => new Router(r.id, r));
static groups: Record<number, Router[]>;
static all: Record<number, Router> = _.keyBy(
routers.filter((r) => r.id !== parseInt(process.env.RAILGUN_ID)).map((r) => new Router(r.id, r)),
'id'
);
address: string;
linkAddress: string;
......@@ -25,7 +25,7 @@ export class Router implements Hello, PeerQuality {
constructor(public id: number, r: any) {
this.address = r.address;
this.linkAddress = `10.200.${id}.${parseInt(process.env.RAILGUN_ID)}`;
this.subnets = subnets.filter(s => r.name).map(s => s.subnet);
this.subnets = subnets.filter((s) => r.name).map((s) => s.subnet);
}
reset() {
......@@ -59,7 +59,7 @@ export class Router implements Hello, PeerQuality {
this.history.push(delay);
this.history.splice(0, this.history.length - config.history);
const history = this.history.filter(s => s !== undefined);
const history = this.history.filter((s) => s !== undefined);
this.reliability = history.length / config.history;
this.delay = _.mean(history) || 0;
......@@ -67,7 +67,7 @@ export class Router implements Hello, PeerQuality {
for (let i = 0; i < history.length - 1; i++) {
jitterSum += Math.abs(history[i] - history[i + 1]);
}
this.jitter = (jitterSum / history.length - 1) || 0;
this.jitter = jitterSum / history.length - 1 || 0;
this.seq = data.seq;
this.time = time;
......@@ -87,21 +87,8 @@ export class Router implements Hello, PeerQuality {
return {
delay: Math.round(this.delay),
jitter: Math.round(this.jitter),
reliability: Math.max(0, this.reliability - lost / config.history)
reliability: Math.max(0, this.reliability - lost / config.history),
};
}
}
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);
......@@ -5,6 +5,8 @@ import config from '../config/config.json';
import { RouteWriter } from './RouteWriter';
import { Router } from './Router';
import { Change, Hello, Report } from '../protocol';
import _ from 'lodash';
import { GatewayGroup } from './GatewayGroup';
export class Server {
ack = 0;
......@@ -23,15 +25,17 @@ export class Server {
for (const [to, via] of Object.entries(message.via)) {
RouteWriter.setVia(parseInt(to), via);
}
if (message.plan) for (const [plan, to] of Object.entries(message.plan)) {
RouteWriter.setVia(parseInt(plan), to);
if (message.plan) {
for (const [groupId, to] of Object.entries(message.plan)) {
RouteWriter.setPlan(GatewayGroup.all[groupId], to);
}
}
RouteWriter.commit();
this.ack++;
const response: Report = {
id: self.id,
ack: this.ack
ack: this.ack,
};
socket.send(JSON.stringify(response), config.server_port, config.server_address);
......@@ -41,7 +45,7 @@ export class Server {
const message: Report = {
id: self.id,
ack: this.ack,
peers: Object.fromEntries(Router.all.map(peer => [peer.id, peer.update(self.time)]))
peers: _.mapValues(Router.all, (peer) => peer.update(self.time)),
};
socket.send(JSON.stringify(message), config.server_port, config.server_address);
}
......
......@@ -24,7 +24,7 @@ const socket = dgram
// from client
const message: Hello = JSON.parse(msg.toString());
assert(message.id);
const peer = Router.all.find(p => p.id === message.id);
const peer = Router.all[message.id];
assert(peer && rinfo.address === peer.linkAddress && rinfo.port === config.port);
peer.onMessage(message);
}
......@@ -38,7 +38,7 @@ socket.bind(config.port);
setInterval(() => {
self.time = Date.now();
const message = JSON.stringify(self);
for (const peer of Router.all) {
for (const peer of Object.values(Router.all)) {
socket.send(message, config.port, peer.linkAddress);
}
server.update(socket, self);
......
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