




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第Go語(yǔ)言學(xué)習(xí)otns示例分析目錄學(xué)習(xí)過(guò)程proto文件visualize/grpc/replay目錄下的文件cmd/otns-replay目錄下的文件grpc_Service(包含pb)otns_replay(包含pb)cmd/otns/otns.go文件simulation目錄下的文件type.gosimulationController.gosimulation_config.gosimulation.gocli目錄ast.goCmdRunner.go總結(jié)
學(xué)習(xí)過(guò)程
由于在用虛擬機(jī)體驗(yàn)過(guò)程中出現(xiàn)了未知的錯(cuò)誤之后,打算使用wsl又遇到了安裝錯(cuò)誤,各種辦法解決無(wú)果,于是我打算跳過(guò)體驗(yàn)的這一部分,直接先進(jìn)行這個(gè)例子中的grpc調(diào)用部分的梳理分析,等有空了再去解決一下wsl安裝不了的問(wèn)題。
proto文件
這個(gè)例子中只有一個(gè)proto文件,位于ot-ns-main/visualize/grpc/pb下,里面的service也只定義了兩個(gè)rpc方法:
serviceVisualizeGrpcService{
//rpcEcho(EchoRequest)returns(EchoResponse);
rpcVisualize(VisualizeRequest)returns(streamVisualizeEvent);
rpcCommand(CommandRequest)returns(CommandResponse);
Visualize(VisualizeRequest)returns(streamVisualizeEvent)
這個(gè)方法接受一個(gè)VisualizeRequest,返回VisualizeEvent流。兩個(gè)消息定義如下:
messageVisualizeRequest{
messageVisualizeEvent{
oneoftype{
AddNodeEventadd_node=1;
DeleteNodeEventdelete_node=2;
SetNodeRloc16Eventset_node_rloc16=3;
SetNodeRoleEventset_node_role=4;
SetNodePosEventset_node_pos=5;
SetNodePartitionIdEventset_node_partition_id=6;
OnNodeFailEventon_node_fail=7;
OnNodeRecoverEventon_node_recover=8;
SetParentEventset_parent=9;
CountDownEventcount_down=10;
ShowDemoLegendEventshow_demo_legend=11;
AdvanceTimeEventadvance_time=12;
AddRouterTableEventadd_router_table=13;
RemoveRouterTableEventremove_router_table=14;
AddChildTableEventadd_child_table=15;
RemoveChildTableEventremove_child_table=16;
SendEventsend=17;
SetSpeedEventset_speed=18;
HeartbeatEventheartbeat=19;
OnExtAddrChangeEventon_ext_addr_change=20;
SetTitleEventset_title=21;
SetNodeModeEventset_node_mode=22;
SetNetworkInfoEventset_network_info=23;
請(qǐng)求為空,而VisualizeEvent里面使用oneof關(guān)鍵字包含了很多的消息體,每個(gè)消息體封裝了一個(gè)事件。
Command(CommandRequest)returns(CommandResponse)
這個(gè)方法接受CommandRequest并返回CommandResponse,兩個(gè)消息體定義如下:
messageCommandRequest{
stringcommand=1;
messageCommandResponse{
repeatedstringoutput=1;
CommandResponse中的output在go中會(huì)聲明為string[]
visualize/grpc/replay目錄下的文件
grpcField(未包含pb)
定義了一個(gè)結(jié)構(gòu)grpcField,里面包含了節(jié)點(diǎn)信息、當(dāng)前時(shí)間與速度、標(biāo)題信息、網(wǎng)絡(luò)信息、及其設(shè)置。
typegrpcFieldstruct{
nodesmap[NodeId]*grpcNode
curTimeuint64
curSpeedfloat64
speedfloat64
titleInfovisualize.TitleInfo
networkInfovisualize.NetworkInfo
grpcNode(未包含pb)
定義了節(jié)點(diǎn)結(jié)構(gòu)grpcNode,包含各種信息,還有一個(gè)new這個(gè)結(jié)構(gòu)的函數(shù)
typegrpcNodestruct{
nodeidNodeId
extaddruint64
xint
yint
radioRangeint
modeNodeMode
rloc16uint16
roleOtDeviceRole
partitionIduint32
failedbool
parentuint64
routerTablemap[uint64]struct{}
childTablemap[uint64]struct{}
grpcServer(包含pb)
自定義了一個(gè)grpcServer,包含信息如下
typegrpcServerstruct{
vis*grpcVisualizer
server*grpc.Server
addressstring
visualizingStreamsmap[*grpcStream]struct{}
同時(shí)按照接口要求實(shí)現(xiàn)了Visualize()和Command()方法,還自定義了其他的方法如run、stop、prepareStream等等,看名字就容易知道是什么用途
grpcStream(包含pb)
里面自定義了一個(gè)結(jié)構(gòu)grpcStream,使用這個(gè)文件中的newGrpcStream可以將Visualize函數(shù)的服務(wù)端流賦到這個(gè)結(jié)構(gòu)中
grpcVisualizer(包含pb)
其中自定義了一個(gè)結(jié)構(gòu):
typegrpcVisualizerstruct{
simctrlvisualize.SimulationController
server*grpcServer
f*grpcField
showDemoLegendEvent*pb.VisualizeEvent
replay*replay.Replay
sync.Mutex
需要注意的是這個(gè)結(jié)構(gòu)繼承了互斥鎖sync.Mutex,并且包含了上面的grpcServer、grpcServer結(jié)構(gòu),這個(gè)文件里面的函數(shù)大概都是添加、刪除節(jié)點(diǎn)或者修改什么信息之類的,基本是調(diào)用了grpcField和grpcServer文件里面的函數(shù),但是在調(diào)用之前加了鎖。這個(gè)結(jié)構(gòu)實(shí)現(xiàn)了visualize/types.go中的Visualizer接口并且,這個(gè)結(jié)構(gòu)中包含了visualize.SimulationController接口的字段,而visualize.SimulationController定義如下:
typeSimulationControllerinterface{
Command(cmdstring)([]string,error)
大概就是命令的入口。
cmd/otns-replay目錄下的文件
grpc_Service(包含pb)
定義了grpcService結(jié)構(gòu),并且實(shí)現(xiàn)了Visualize和Command兩個(gè)方法
typegrpcServicestruct{
replayFilestring
2.grpcService結(jié)構(gòu)下的visualizeStream()函數(shù)
將grpcService的replay文件檢驗(yàn)并打開,并且逐行讀取內(nèi)容,并解析到varentrypb.ReplayEntry中,再通過(guò)stream將entry.Event發(fā)送到服務(wù)的客戶端
實(shí)現(xiàn)的Visualize方法:
啟動(dòng)visualizeStream()協(xié)程,創(chuàng)建一個(gè)心跳事件,每隔一秒心跳一下,直到上面的visualizeStream()讀取完成
otns_replay(包含pb)
main()函數(shù)
一系列的校驗(yàn)和配置參數(shù)之后,用上面的grpcService結(jié)構(gòu)注冊(cè)服務(wù)端,在本機(jī)地址8999端口監(jiān)聽。然后就是配置和打開網(wǎng)頁(yè)
cmd/otns/otns.go文件
調(diào)用了otns_main/otns_main.go下的Main()函數(shù):
首先依然是解析和配置參數(shù)和環(huán)境:
parseArgs()
simplelogger.SetLevel(simplelogger.ParseLevel(args.LogLevel))
parseListenAddr()
rand.Seed(time.Now().UnixNano())
//runconsoleinthemaingoroutine
ctx.Defer(func(){
_=os.Stdin.Close()
handleSignals(ctx)
然后是打開replay文件并創(chuàng)建visualizer實(shí)例:
varvisvisualize.Visualizer
ifvisualizerCreator!=nil{
vis=visualizerCreator(ctx,amp;args)
visGrpcServerAddr:=fmt.Sprintf("%s:%d",args.DispatcherHost,args.DispatcherPort-1)
replayFn:=""
if!args.NoReplay{
replayFn=fmt.Sprintf("otns_%s.replay",os.Getenv("PORT_OFFSET"))
ifvis!=nil{
vis=visualizeMulti.NewMultiVisualizer(
vis,
visualizeGrpc.NewGrpcVisualizer(visGrpcServerAddr,replayFn),
}else{
vis=visualizeGrpc.NewGrpcVisualizer(visGrpcServerAddr,replayFn)
創(chuàng)建一個(gè)新模擬,并設(shè)置CmdRunner和Visualizer:
sim:=createSimulation(ctx)
rt:=cli.NewCmdRunner(ctx,sim)
sim.SetVisualizer(vis)
啟動(dòng)一個(gè)協(xié)程運(yùn)行模擬:
gosim.Run()
啟動(dòng)客戶命令行協(xié)程:
gofunc(){
err:=cli.Run(rt,cliOptions)
ctx.Cancel(errors.Wrapf(err,"consoleexit"))
設(shè)置并打開網(wǎng)頁(yè):
gofunc(){
siteAddr:=fmt.Sprintf("%s:%d",args.DispatcherHost,args.DispatcherPort-3)
err:=webSite.Serve(siteAddr)
iferr!=nil{
simplelogger.Errorf("sitequited:%+v,OTNS-Webwon'tbeavailable!",err)
ifargs.AutoGo{
goautoGo(ctx,sim)
web.ConfigWeb(args.DispatcherHost,args.DispatcherPort-2,args.DispatcherPort-1,args.DispatcherPort-3)
simplelogger.Debugf("openweb:%v",args.OpenWeb)
ifargs.OpenWeb{
_=web.OpenWeb(ctx)
Visualizer啟動(dòng):
vis.Run()//visualizemustruninthemainthread
simulation目錄下的文件
simulation是grpcVisualizer和cmdRunner通信的橋梁。
type.go
定義了CmdRunner接口:
typeCmdRunnerinterface{
RunCommand(cmdstring,outputio.Writer)error
simulationController.go
定義了simulationController類,這個(gè)類實(shí)現(xiàn)了visualize.SimulationController接口,也就是grpcVisualizer里有的字段:
typesimulationControllerstruct{
sim*Simulation
func(sc*simulationController)Command(cmdstring)([]string,error){
varoutputBuilderstrings.Builder
sim:=sc.sim
err:=sim.cmdRunner.RunCommand(cmd,amp;outputBuilder)
iferr!=nil{
returnnil,err
output:=strings.Split(outputBuilder.String(),"\n")
ifoutput[len(output)-1]==""{
output=output[:len(output)-1]
returnoutput,nil
還定義了同樣實(shí)現(xiàn)了visualize.SimulationController接口的只讀類,這里不展開說(shuō)了。還有一個(gè)NewSimulationController(sim*Simulation)函數(shù)產(chǎn)生simulationControllersimulationController應(yīng)該是一個(gè)介于Command和Simulation之間的中介,接收Command并操作CmdRunner更改Simulation,并且輸出信息。
simulation_config.go
定義了配置和默認(rèn)配置
simulation.go
simulation結(jié)構(gòu)定義:
typeSimulationstruct{
ctx*progctx.ProgCtx
cfg*Config
nodesmap[NodeId]*Node
d*dispatcher.Dispatcher
visvisualize.Visualizer
cmdRunnerCmdRunner
rawModebool
networkInfovisualize.NetworkInfo
有一個(gè)new產(chǎn)生simulation結(jié)構(gòu)的函數(shù)各種增刪改查操作,都是通過(guò)simulation結(jié)構(gòu)中的vis
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025山西航空產(chǎn)業(yè)集團(tuán)有限公司校園招聘(第一批)43人筆試參考題庫(kù)附帶答案詳解
- 2025四川雅安市寶興縣興綠林業(yè)投資有限公司招聘(第二批)2人筆試參考題庫(kù)附帶答案詳解
- 紡織行業(yè)考生的學(xué)習(xí)信念試題及答案
- 紡織工程師項(xiàng)目執(zhí)行規(guī)范試題及答案
- 客房入股合同協(xié)議書
- 推拿合同協(xié)議書
- 施工安全合同協(xié)議書
- 店員處罰合同協(xié)議書
- 企業(yè)合同協(xié)議書
- 工程合同終止協(xié)議書
- 2025年4月新高考語(yǔ)文全國(guó)Ⅰ卷各地??荚囶}匯編之語(yǔ)用
- 山東省聊城市2025年高考模擬試題(二)數(shù)學(xué)+答案
- 小學(xué)數(shù)學(xué)西師大版(2024)三年級(jí)下冊(cè)旋轉(zhuǎn)與平移現(xiàn)象教學(xué)設(shè)計(jì)
- 田園綜合體可行性研究報(bào)告
- 沈陽(yáng)市東北大學(xué)非教師崗位招聘考試真題2024
- 2025年中考語(yǔ)文二輪復(fù)習(xí):散文閱讀 專題練習(xí)題(含答案)
- 高校宿管培訓(xùn)
- 2025屆新高考教學(xué)教研聯(lián)盟高三第二次聯(lián)考政治試題及答案
- 2025年03月國(guó)家衛(wèi)生健康委統(tǒng)計(jì)信息中心公開招聘人才派遣1人筆試歷年典型考題(歷年真題考點(diǎn))解題思路附帶答案詳解
- 行政管理本科畢業(yè)論文-數(shù)字政府建設(shè)背景下的行政文化轉(zhuǎn)型探析
- 賭博酒駕警示教育
評(píng)論
0/150
提交評(píng)論