Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
ec481b5a
提交
ec481b5a
authored
10月 22, 2014
作者:
Brian Tiger Chow
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor(net/mux) move proto to internal pb package
上级
e3a9a11a
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
26 行增加
和
18 行删除
+26
-18
Makefile
net/mux/internal/pb/Makefile
+0
-0
mux.pb.go
net/mux/internal/pb/mux.pb.go
+0
-0
mux.proto
net/mux/internal/pb/mux.proto
+0
-0
mux.go
net/mux/mux.go
+15
-8
mux_test.go
net/mux/mux_test.go
+11
-10
没有找到文件。
net/mux/Makefile
→
net/mux/
internal/pb/
Makefile
浏览文件 @
ec481b5a
File moved
net/mux/mux.pb.go
→
net/mux/
internal/pb/
mux.pb.go
浏览文件 @
ec481b5a
File moved
net/mux/mux.proto
→
net/mux/
internal/pb/
mux.proto
浏览文件 @
ec481b5a
File moved
net/mux/mux.go
浏览文件 @
ec481b5a
...
...
@@ -5,6 +5,7 @@ import (
"sync"
msg
"github.com/jbenet/go-ipfs/net/message"
pb
"github.com/jbenet/go-ipfs/net/mux/internal/pb"
u
"github.com/jbenet/go-ipfs/util"
context
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
...
...
@@ -13,6 +14,12 @@ import (
var
log
=
u
.
Logger
(
"muxer"
)
var
(
ProtocolID_Routing
=
pb
.
ProtocolID_Routing
ProtocolID_Exchange
=
pb
.
ProtocolID_Exchange
ProtocolID_Diagnostic
=
pb
.
ProtocolID_Diagnostic
)
// Protocol objects produce + consume raw data. They are added to the Muxer
// with a ProtocolID, which is added to outgoing payloads. Muxer properly
// encapsulates and decapsulates when interfacing with its Protocols. The
...
...
@@ -22,7 +29,7 @@ type Protocol interface {
}
// ProtocolMap maps ProtocolIDs to Protocols.
type
ProtocolMap
map
[
ProtocolID
]
Protocol
type
ProtocolMap
map
[
pb
.
ProtocolID
]
Protocol
// Muxer is a simple multiplexor that reads + writes to Incoming and Outgoing
// channels. It multiplexes various protocols, wrapping and unwrapping data
...
...
@@ -107,7 +114,7 @@ func (m *Muxer) Stop() {
}
// AddProtocol adds a Protocol with given ProtocolID to the Muxer.
func
(
m
*
Muxer
)
AddProtocol
(
p
Protocol
,
pid
ProtocolID
)
error
{
func
(
m
*
Muxer
)
AddProtocol
(
p
Protocol
,
pid
pb
.
ProtocolID
)
error
{
if
_
,
found
:=
m
.
Protocols
[
pid
];
found
{
return
errors
.
New
(
"Another protocol already using this ProtocolID"
)
}
...
...
@@ -170,7 +177,7 @@ func (m *Muxer) handleIncomingMessage(m1 msg.NetMessage) {
// handleOutgoingMessages consumes the messages on the proto.Outgoing channel,
// wraps them and sends them out.
func
(
m
*
Muxer
)
handleOutgoingMessages
(
pid
ProtocolID
,
proto
Protocol
)
{
func
(
m
*
Muxer
)
handleOutgoingMessages
(
pid
pb
.
ProtocolID
,
proto
Protocol
)
{
defer
m
.
wg
.
Done
()
for
{
...
...
@@ -188,7 +195,7 @@ func (m *Muxer) handleOutgoingMessages(pid ProtocolID, proto Protocol) {
}
// handleOutgoingMessage wraps out a message and sends it out the
func
(
m
*
Muxer
)
handleOutgoingMessage
(
pid
ProtocolID
,
m1
msg
.
NetMessage
)
{
func
(
m
*
Muxer
)
handleOutgoingMessage
(
pid
pb
.
ProtocolID
,
m1
msg
.
NetMessage
)
{
data
,
err
:=
wrapData
(
m1
.
Data
(),
pid
)
if
err
!=
nil
{
log
.
Error
(
"muxer serializing error: %v"
,
err
)
...
...
@@ -208,9 +215,9 @@ func (m *Muxer) handleOutgoingMessage(pid ProtocolID, m1 msg.NetMessage) {
}
}
func
wrapData
(
data
[]
byte
,
pid
ProtocolID
)
([]
byte
,
error
)
{
func
wrapData
(
data
[]
byte
,
pid
pb
.
ProtocolID
)
([]
byte
,
error
)
{
// Marshal
pbm
:=
new
(
PBProtocolMessage
)
pbm
:=
new
(
pb
.
PBProtocolMessage
)
pbm
.
ProtocolID
=
&
pid
pbm
.
Data
=
data
b
,
err
:=
proto
.
Marshal
(
pbm
)
...
...
@@ -221,9 +228,9 @@ func wrapData(data []byte, pid ProtocolID) ([]byte, error) {
return
b
,
nil
}
func
unwrapData
(
data
[]
byte
)
([]
byte
,
ProtocolID
,
error
)
{
func
unwrapData
(
data
[]
byte
)
([]
byte
,
pb
.
ProtocolID
,
error
)
{
// Unmarshal
pbm
:=
new
(
PBProtocolMessage
)
pbm
:=
new
(
pb
.
PBProtocolMessage
)
err
:=
proto
.
Unmarshal
(
data
,
pbm
)
if
err
!=
nil
{
return
nil
,
0
,
err
...
...
net/mux/mux_test.go
浏览文件 @
ec481b5a
...
...
@@ -8,6 +8,7 @@ import (
mh
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
msg
"github.com/jbenet/go-ipfs/net/message"
pb
"github.com/jbenet/go-ipfs/net/mux/internal/pb"
peer
"github.com/jbenet/go-ipfs/peer"
context
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
...
...
@@ -37,7 +38,7 @@ func testMsg(t *testing.T, m msg.NetMessage, data []byte) {
}
}
func
testWrappedMsg
(
t
*
testing
.
T
,
m
msg
.
NetMessage
,
pid
ProtocolID
,
data
[]
byte
)
{
func
testWrappedMsg
(
t
*
testing
.
T
,
m
msg
.
NetMessage
,
pid
pb
.
ProtocolID
,
data
[]
byte
)
{
data2
,
pid2
,
err
:=
unwrapData
(
m
.
Data
())
if
err
!=
nil
{
t
.
Error
(
err
)
...
...
@@ -57,8 +58,8 @@ func TestSimpleMuxer(t *testing.T) {
// setup
p1
:=
&
TestProtocol
{
Pipe
:
msg
.
NewPipe
(
10
)}
p2
:=
&
TestProtocol
{
Pipe
:
msg
.
NewPipe
(
10
)}
pid1
:=
ProtocolID_Test
pid2
:=
ProtocolID_Routing
pid1
:=
pb
.
ProtocolID_Test
pid2
:=
pb
.
ProtocolID_Routing
mux1
:=
NewMuxer
(
ProtocolMap
{
pid1
:
p1
,
pid2
:
p2
,
...
...
@@ -108,8 +109,8 @@ func TestSimultMuxer(t *testing.T) {
// setup
p1
:=
&
TestProtocol
{
Pipe
:
msg
.
NewPipe
(
10
)}
p2
:=
&
TestProtocol
{
Pipe
:
msg
.
NewPipe
(
10
)}
pid1
:=
ProtocolID_Test
pid2
:=
ProtocolID_Identify
pid1
:=
pb
.
ProtocolID_Test
pid2
:=
pb
.
ProtocolID_Identify
mux1
:=
NewMuxer
(
ProtocolMap
{
pid1
:
p1
,
pid2
:
p2
,
...
...
@@ -127,7 +128,7 @@ func TestSimultMuxer(t *testing.T) {
counts
:=
[
2
][
2
][
2
]
int
{}
// run producers at every end sending incrementing messages
produceOut
:=
func
(
pid
ProtocolID
,
size
int
)
{
produceOut
:=
func
(
pid
pb
.
ProtocolID
,
size
int
)
{
limiter
:=
time
.
Tick
(
speed
)
for
i
:=
0
;
i
<
size
;
i
++
{
<-
limiter
...
...
@@ -139,7 +140,7 @@ func TestSimultMuxer(t *testing.T) {
}
}
produceIn
:=
func
(
pid
ProtocolID
,
size
int
)
{
produceIn
:=
func
(
pid
pb
.
ProtocolID
,
size
int
)
{
limiter
:=
time
.
Tick
(
speed
)
for
i
:=
0
;
i
<
size
;
i
++
{
<-
limiter
...
...
@@ -175,7 +176,7 @@ func TestSimultMuxer(t *testing.T) {
}
}
consumeIn
:=
func
(
pid
ProtocolID
)
{
consumeIn
:=
func
(
pid
pb
.
ProtocolID
)
{
for
{
select
{
case
m
:=
<-
mux1
.
Protocols
[
pid
]
.
GetPipe
()
.
Incoming
:
...
...
@@ -217,8 +218,8 @@ func TestStopping(t *testing.T) {
// setup
p1
:=
&
TestProtocol
{
Pipe
:
msg
.
NewPipe
(
10
)}
p2
:=
&
TestProtocol
{
Pipe
:
msg
.
NewPipe
(
10
)}
pid1
:=
ProtocolID_Test
pid2
:=
ProtocolID_Identify
pid1
:=
pb
.
ProtocolID_Test
pid2
:=
pb
.
ProtocolID_Identify
mux1
:=
NewMuxer
(
ProtocolMap
{
pid1
:
p1
,
pid2
:
p2
,
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论