Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
R
railgun-routing-client
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Railgun
railgun-routing-client
Commits
90f65d18
Commit
90f65d18
authored
Aug 28, 2022
by
神楽坂玲奈
Committed by
铃兰
Aug 28, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rename
parent
ff86a96a
Pipeline
#16077
passed with stages
in 54 seconds
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
23 additions
and
23 deletions
+23
-23
protocol.ts
protocol.ts
+8
-8
src/Peer.ts
src/Peer.ts
+3
-3
src/Server.ts
src/Server.ts
+8
-8
src/main.ts
src/main.ts
+4
-4
No files found.
protocol.ts
View file @
90f65d18
...
...
@@ -7,22 +7,22 @@ export interface PeerQuality {
}
// 路由器向中心服务器发送的消息
export
interface
UploadMessage
{
id
:
number
;
// router id
export
interface
Report
{
id
:
number
;
ack
:
number
;
peers
?:
Record
<
number
,
PeerQuality
>
;
}
// 中心服务器向路由器发送的消息
export
interface
DownloadMessa
ge
{
export
interface
Chan
ge
{
seq
:
number
,
via
:
Record
<
number
,
number
>
,
plan
:
Record
<
number
,
number
>
//
plan: Record<number, number>
}
// 路由器向路由器发送的消息
export
interface
PeerMessage
{
id
:
number
;
seq
:
number
;
time
:
number
;
export
interface
Hello
{
id
:
number
;
// 自己的id
seq
:
number
;
// 这个报文的顺序号,每次发送自增
time
:
number
;
// 自己的系统时间,EPOCH 以来的毫秒数
}
src/Peer.ts
View file @
90f65d18
import
*
as
_
from
'
lodash
'
;
import
config
from
'
../config/config.json
'
;
import
{
PeerMessage
,
PeerQuality
}
from
'
../protocol
'
;
import
{
Hello
,
PeerQuality
}
from
'
../protocol
'
;
export
interface
RouterConfig
{
id
:
number
;
...
...
@@ -8,7 +8,7 @@ export interface RouterConfig {
subnets
:
string
[];
}
export
class
Peer
implements
PeerMessage
,
PeerQuality
,
RouterConfig
{
export
class
Peer
implements
Hello
,
PeerQuality
,
RouterConfig
{
id
:
number
;
address
:
string
;
subnets
:
string
[];
...
...
@@ -33,7 +33,7 @@ export class Peer implements PeerMessage, PeerQuality, RouterConfig {
this
.
history
=
[];
}
onMessage
(
data
:
PeerMessage
)
{
onMessage
(
data
:
Hello
)
{
// console.log(data);
if
(
data
.
seq
===
0
||
data
.
seq
<
this
.
seq
-
config
.
timeout
||
data
.
seq
>
this
.
seq
+
config
.
timeout
)
{
// 收到 seq = 0 或 seq 与之前差距较大,就 reset
...
...
src/Server.ts
View file @
90f65d18
...
...
@@ -4,12 +4,12 @@ import config from '../config/config.json';
import
{
RouteWriter
}
from
'
./RouteWriter
'
;
import
{
Peer
}
from
'
./Peer
'
;
import
{
DownloadMessage
,
PeerMessage
,
UploadMessage
}
from
'
../protocol
'
;
import
{
Change
,
Hello
,
Report
}
from
'
../protocol
'
;
export
class
Server
{
ack
=
0
;
onMessage
(
socket
:
Socket
,
message
:
DownloadMessage
,
self
:
PeerMessage
)
{
onMessage
(
socket
:
Socket
,
message
:
Change
,
self
:
Hello
)
{
if
(
message
.
seq
&&
this
.
ack
!==
message
.
seq
)
{
console
.
log
(
'
seq mismatch rejected
'
);
return
;
...
...
@@ -22,13 +22,13 @@ export class Server {
for
(
const
[
to
,
via
]
of
Object
.
entries
(
message
.
via
))
{
RouteWriter
.
setVia
(
parseInt
(
to
),
via
);
}
for
(
const
[
plan
,
to
]
of
Object
.
entries
(
message
.
plan
))
{
RouteWriter
.
setVia
(
parseInt
(
plan
),
to
);
}
//
for (const [plan, to] of Object.entries(message.plan)) {
//
RouteWriter.setVia(parseInt(plan), to);
//
}
RouteWriter
.
commit
();
this
.
ack
=
message
.
seq
+
1
;
const
response
:
UploadMessage
=
{
const
response
:
Report
=
{
id
:
self
.
id
,
ack
:
this
.
ack
};
...
...
@@ -36,8 +36,8 @@ export class Server {
socket
.
send
(
JSON
.
stringify
(
response
),
config
.
server_port
,
config
.
server_address
);
}
update
(
socket
:
Socket
,
self
:
PeerMessage
,
peers
:
Peer
[])
{
const
message
:
UploadMessage
=
{
update
(
socket
:
Socket
,
self
:
Hello
,
peers
:
Peer
[])
{
const
message
:
Report
=
{
id
:
self
.
id
,
ack
:
this
.
ack
,
peers
:
Object
.
fromEntries
(
peers
.
map
(
peer
=>
[
peer
.
id
,
peer
.
update
(
self
.
time
)]))
...
...
src/main.ts
View file @
90f65d18
...
...
@@ -4,9 +4,9 @@ import config from '../config/config.json';
import
routers
from
'
../config/routers.json
'
;
import
{
Server
}
from
'
./Server
'
;
import
{
Peer
}
from
'
./Peer
'
;
import
{
DownloadMessage
,
PeerMessage
}
from
'
../protocol
'
;
import
{
Change
,
Hello
}
from
'
../protocol
'
;
const
self
:
PeerMessage
=
{
id
:
parseInt
(
process
.
env
.
RAILGUN_ID
),
seq
:
0
,
time
:
0
};
const
self
:
Hello
=
{
id
:
parseInt
(
process
.
env
.
RAILGUN_ID
),
seq
:
0
,
time
:
0
};
const
server
=
new
Server
();
const
peers
=
routers
.
filter
(
r
=>
r
.
id
!==
self
.
id
&&
r
.
interface
).
map
(
r
=>
new
Peer
(
r
));
...
...
@@ -20,11 +20,11 @@ const socket = dgram
try
{
if
(
rinfo
.
address
===
config
.
server_address
&&
rinfo
.
port
===
config
.
server_port
)
{
// from server
const
message
:
DownloadMessa
ge
=
JSON
.
parse
(
msg
.
toString
());
const
message
:
Chan
ge
=
JSON
.
parse
(
msg
.
toString
());
server
.
onMessage
(
socket
,
message
,
self
);
}
else
{
// from client
const
message
:
PeerMessage
=
JSON
.
parse
(
msg
.
toString
());
const
message
:
Hello
=
JSON
.
parse
(
msg
.
toString
());
assert
(
message
.
id
);
const
peer
=
peers
.
find
(
p
=>
p
.
id
===
message
.
id
);
// assert(peer && rinfo.address === `10.200.${self.id}.${peer.id}` && rinfo.port === config.port);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment