Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
4b096c4b
提交
4b096c4b
authored
9月 10, 2016
作者:
Jeromy
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
floodsub: add api for pub/sub
License: MIT Signed-off-by:
Jeromy
<
why@ipfs.io
>
上级
dcb21bd2
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
175 行增加
和
1 行删除
+175
-1
pubsub.go
core/commands/pubsub.go
+162
-0
root.go
core/commands/root.go
+1
-0
core.go
core/core.go
+6
-1
package.json
package.json
+6
-0
没有找到文件。
core/commands/pubsub.go
0 → 100644
浏览文件 @
4b096c4b
package
commands
import
(
"bytes"
"encoding/binary"
"io"
cmds
"github.com/ipfs/go-ipfs/commands"
floodsub
"gx/ipfs/QmQriRMW5cCJyLrzDnXi7fZ5mVbetiEZjPjbqoJhuSL94m/floodsub"
u
"gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
)
var
PubsubCmd
=
&
cmds
.
Command
{
Helptext
:
cmds
.
HelpText
{
Tagline
:
"An experimental publish-subscribe system on ipfs."
,
ShortDescription
:
`
ipfs pubsub allows you to publish messages to a given topic, and also to
subscribe to new messages on a given topic.
This is an experimental feature. It is not intended in its current state
to be used in a production environment.
`
,
},
Subcommands
:
map
[
string
]
*
cmds
.
Command
{
"pub"
:
PubsubPubCmd
,
"sub"
:
PubsubSubCmd
,
},
}
var
PubsubSubCmd
=
&
cmds
.
Command
{
Helptext
:
cmds
.
HelpText
{
Tagline
:
"Subscribe to messages on a given topic."
,
ShortDescription
:
`
ipfs pubsub sub subscribes to messages on a given topic.
This is an experimental feature. It is not intended in its current state
to be used in a production environment.
`
,
},
Arguments
:
[]
cmds
.
Argument
{
cmds
.
StringArg
(
"topic"
,
true
,
false
,
"String name of topic to subscribe to."
),
},
Run
:
func
(
req
cmds
.
Request
,
res
cmds
.
Response
)
{
n
,
err
:=
req
.
InvocContext
()
.
GetNode
()
if
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
}
// Must be online!
if
!
n
.
OnlineMode
()
{
res
.
SetError
(
errNotOnline
,
cmds
.
ErrClient
)
return
}
topic
:=
req
.
Arguments
()[
0
]
msgs
,
err
:=
n
.
Floodsub
.
Subscribe
(
topic
)
if
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
}
out
:=
make
(
chan
interface
{})
res
.
SetOutput
((
<-
chan
interface
{})(
out
))
ctx
:=
req
.
Context
()
go
func
()
{
defer
close
(
out
)
for
{
select
{
case
msg
,
ok
:=
<-
msgs
:
if
!
ok
{
return
}
out
<-
msg
case
<-
ctx
.
Done
()
:
n
.
Floodsub
.
Unsub
(
topic
)
}
}
}()
},
Marshalers
:
cmds
.
MarshalerMap
{
cmds
.
Text
:
getPsMsgMarshaler
(
func
(
m
*
floodsub
.
Message
)
(
io
.
Reader
,
error
)
{
log
.
Error
(
"FROM: "
,
m
.
GetFrom
())
return
bytes
.
NewReader
(
m
.
Data
),
nil
}),
"ndpayload"
:
getPsMsgMarshaler
(
func
(
m
*
floodsub
.
Message
)
(
io
.
Reader
,
error
)
{
m
.
Data
=
append
(
m
.
Data
,
'\n'
)
return
bytes
.
NewReader
(
m
.
Data
),
nil
}),
"lenpayload"
:
getPsMsgMarshaler
(
func
(
m
*
floodsub
.
Message
)
(
io
.
Reader
,
error
)
{
buf
:=
make
([]
byte
,
8
)
n
:=
binary
.
PutUvarint
(
buf
,
uint64
(
len
(
m
.
Data
)))
return
io
.
MultiReader
(
bytes
.
NewReader
(
buf
[
:
n
]),
bytes
.
NewReader
(
m
.
Data
)),
nil
}),
},
Type
:
floodsub
.
Message
{},
}
func
getPsMsgMarshaler
(
f
func
(
m
*
floodsub
.
Message
)
(
io
.
Reader
,
error
))
func
(
cmds
.
Response
)
(
io
.
Reader
,
error
)
{
return
func
(
res
cmds
.
Response
)
(
io
.
Reader
,
error
)
{
outChan
,
ok
:=
res
.
Output
()
.
(
<-
chan
interface
{})
if
!
ok
{
return
nil
,
u
.
ErrCast
()
}
marshal
:=
func
(
v
interface
{})
(
io
.
Reader
,
error
)
{
obj
,
ok
:=
v
.
(
*
floodsub
.
Message
)
if
!
ok
{
return
nil
,
u
.
ErrCast
()
}
return
f
(
obj
)
}
return
&
cmds
.
ChannelMarshaler
{
Channel
:
outChan
,
Marshaler
:
marshal
,
Res
:
res
,
},
nil
}
}
var
PubsubPubCmd
=
&
cmds
.
Command
{
Helptext
:
cmds
.
HelpText
{
Tagline
:
"Publish a message to a given pubsub topic."
,
ShortDescription
:
`
ipfs pubsub pub publishes a message to a specified topic.
This is an experimental feature. It is not intended in its current state
to be used in a production environment.
`
,
},
Arguments
:
[]
cmds
.
Argument
{
cmds
.
StringArg
(
"topic"
,
true
,
false
,
"Topic to publish to."
),
cmds
.
StringArg
(
"data"
,
true
,
true
,
"Payload of message to publish."
)
.
EnableStdin
(),
},
Options
:
[]
cmds
.
Option
{},
Run
:
func
(
req
cmds
.
Request
,
res
cmds
.
Response
)
{
n
,
err
:=
req
.
InvocContext
()
.
GetNode
()
if
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
}
// Must be online!
if
!
n
.
OnlineMode
()
{
res
.
SetError
(
errNotOnline
,
cmds
.
ErrClient
)
return
}
topic
:=
req
.
Arguments
()[
0
]
for
_
,
data
:=
range
req
.
Arguments
()[
1
:
]
{
if
err
:=
n
.
Floodsub
.
Publish
(
topic
,
[]
byte
(
data
));
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
}
}
},
}
core/commands/root.go
浏览文件 @
4b096c4b
...
...
@@ -105,6 +105,7 @@ var rootSubcommands = map[string]*cmds.Command{
"stats"
:
StatsCmd
,
"swarm"
:
SwarmCmd
,
"tar"
:
TarCmd
,
"pubsub"
:
PubsubCmd
,
"tour"
:
tourCmd
,
"file"
:
unixfs
.
UnixFSCmd
,
"update"
:
ExternalBinary
(),
...
...
core/core.go
浏览文件 @
4b096c4b
...
...
@@ -17,7 +17,8 @@ import (
"time"
diag
"github.com/ipfs/go-ipfs/diagnostics"
goprocess
"gx/ipfs/QmQopLATEYMNg7dVqZRNDfeE2S1yKy8zrRh5xnYiuqeZBn/goprocess"
floodsub
"gx/ipfs/QmQriRMW5cCJyLrzDnXi7fZ5mVbetiEZjPjbqoJhuSL94m/floodsub"
goprocess
"gx/ipfs/QmSF8fPo3jgVBAy8fpdjjYqgG87dkJgUprRBHRd2tmfgpP/goprocess"
mamask
"gx/ipfs/QmSMZwvs3n4GBikZ7hKzT17c3bk65FmyZo2JqtJ16swqCv/multiaddr-filter"
logging
"gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
b58
"gx/ipfs/QmT8rehPR3F6bmwL6zjUN8XpiDBFFpMP2myPdC6ApsWfJf/go-base58"
...
...
@@ -112,6 +113,8 @@ type IpfsNode struct {
Reprovider
*
rp
.
Reprovider
// the value reprovider system
IpnsRepub
*
ipnsrp
.
Republisher
Floodsub
*
floodsub
.
PubSub
proc
goprocess
.
Process
ctx
context
.
Context
...
...
@@ -184,6 +187,8 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption Routin
go
n
.
Reprovider
.
ProvideEvery
(
ctx
,
interval
)
}
n
.
Floodsub
=
floodsub
.
NewFloodSub
(
ctx
,
peerhost
)
// setup local discovery
if
do
!=
nil
{
service
,
err
:=
do
(
ctx
,
n
.
PeerHost
)
...
...
package.json
浏览文件 @
4b096c4b
...
...
@@ -263,6 +263,12 @@
"hash"
:
"QmdCL8M8DXJdSRnwhpDhukX5r8ydjxnzPJpaKrFudDA8yn"
,
"name"
:
"hang-fds"
,
"version"
:
"0.0.0"
},
{
"author"
:
"whyrusleeping"
,
"hash"
:
"QmQriRMW5cCJyLrzDnXi7fZ5mVbetiEZjPjbqoJhuSL94m"
,
"name"
:
"floodsub"
,
"version"
:
"0.3.0"
}
],
"gxVersion"
:
"0.4.0"
,
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论