美文网首页
2018-05-23字节点也可以广播

2018-05-23字节点也可以广播

作者: fairy冉冉 | 来源:发表于2018-05-23 15:31 被阅读0次

/主节点广播后建立字节点的广播

package main

import (
    "os"
    "fmt"
    "net/http"
    "io"
)

type nodeINfo struct {
    // 节点名称
    id string
    //节点路径
    path  string
    // http 相应
    write http.ResponseWriter
}
// 创建map,存储各个国家的ip地址
var nodeTable  = make(map[string]string)


func main()  {
    // 接收终端参数
    userId := os.Args[1]
    fmt.Println(userId)
    // 存储四个国家的ip地址
    nodeTable = map[string]string{
        "Apple" :"localhost:1111",
        "MS" :"localhost:1112",
        "Google" :"localhost:1113",
        "IBM" :"localhost:1114",

    }

    // 创建国家对象
    node := nodeINfo{id:userId,path:nodeTable[userId]}
// http协议的回掉函数
// http://localhost:1111/req?warTime =1111
http.HandleFunc("/req",node.request)
http.HandleFunc("/prePrepare",node.prePrepare)
http.HandleFunc("/prepare",node.prepare)
    http.HandleFunc("/commit",node.commit)

    if err:=http.ListenAndServe(node.path,nil);err!=nil {
        fmt.Println(err)
    }

}
// http服务器,接收到网络请求并且。req则回掉request
func (node *nodeINfo)request(writer http.ResponseWriter,request *http.Request)  {
   // 该命令允许request请求参数
    request.ParseForm()
    if (len(request.Form["warTime"])>0) {
        node.write =writer
        fmt.Println("主节点接收到的参数信息为",request.Form["warTime"][0])
        
        // 激活主节点后向其他的节点发送广播
        node.broadcast(request.Form["warTime"][0],"/prePrepare")

    }
}
// 节点发送广播的方法
func (node *nodeINfo)broadcast(msg string,path string)  {
    fmt.Println("广播",path)
    // 遍历所有的节点
    for nodeId,url :=range nodeTable  {
        if nodeId== node.id{
            continue

        }
        // 使当前节点外的节点作出相应
        http.Get("http://"+url+path+"?warTime="+msg+"&nodeId="+node.id)


    }

}
//处理广播后接收到的数据
func (node *nodeINfo)prePrepare(writer http.ResponseWriter,request *http.Request)  {
    request.ParseForm()
    fmt.Println("接收到的广播为",request.Form["warTime"][0])
    if len(request.Form["warTime"])>0 {
        node.broadcast(request.Form["warTime"][0],"/prepare")
    }

}

//接收子节点的广播
func (node *nodeINfo)prepare(writer http.ResponseWriter,request *http.Request){
    request.ParseForm()
    //打印
    fmt.Println("接收到子节点的广播",request.Form["warTime"][0])
    //校验
    if len(request.Form["warTime"])>0 {
        node.authentication(request)
    }

}


var authenticationNOdeMap  =make(map[string]string)
var  authenticationSuceess =false
// 校验拜占庭
func (node *nodeINfo)authentication(request *http.Request )  {
    if !authenticationSuceess {
        if len(request.Form["nodeId"])>0 {

          authenticationNOdeMap[request.Form["nodeId"][0]]="OK"
          // 如果由两个国家节点成功正确返回了结果,则成功
            if len(authenticationNOdeMap)>len(nodeTable)/3 {
                authenticationSuceess = true
                node.broadcast(request.Form["warTime"][0],"/commit")

                
            }
        }

    }


}
// 返回成功相应
func(node *nodeINfo)commit(writer http.ResponseWriter,request *http.Request)  {
    if writer !=nil {
        fmt.Println("拜占庭校验成功哈哈哈哈哈哈哈")
        io.WriteString(node.write,"ok")

    }

}

/*运行起来主节点,一次运行子节点,然后运行网页端的http://localhost:1111/req?warTime=1111
然后看图
*/,
主节点广播的图:

image.png
字节点1:
image.png
字节点2:
[图片上传中...(image.png-5b30e8-1527060651051-0)]
字节点3
image.png

这样一个字节点的广播服务算是成功了

相关文章

  • 2018-05-23字节点也可以广播

    /主节点广播后建立字节点的广播 /*运行起来主节点,一次运行子节点,然后运行网页端的http://localhos...

  • 树莓派开发-蓝牙广播(Bluez)

    一个Linux下作为中心节点广播低功耗蓝牙的库。 可以在Raspberry Pi上使用 实现功能 广播蓝牙 设置蓝...

  • Spark—广播变量

    广播变量 Spark有两种共享变量——累加器、广播变量。广播变量可以让程序高效地向所有工作节点发送一个较大的只读值...

  • VLAN技术及应用、Trunk、网络层解析

    广播域 广播域指接收同样广播消息的节点的集合,如:在该集合中的任何一个节点传输一个广播帧,则所有其他能收到这个帧的...

  • 自定义发送广播

    广播主要分为两种类型:标准广播(完全异步执行的广播)和有序广播(完全同步执行的广播)。我们也可以根据广播是否可以跨...

  • Spark 广播join 与 Hive map join

    Sprak 广播变量 广播变量(Broadcast Variables)允许开发人员在每个节点(Worker or...

  • Broadcast广播的基本使用

    全局广播:在整个Android系统中应用不但可以接受自己的广播,也可以接受别的应用的广播,或则系统的广播(开机广播...

  • Zigbee网络通信之广播

    Zigbee广播通信就是网络内的任何一个节点设备发送出广播数据,网络中的任何设备都可以接收到所发送的数据。 1.广...

  • 十、广播变量

    简介 广播变量理解为是一个公共的共享变量,我们可以把一个dataset 数据集广播出去,然后不同的task在节点上...

  • Delivery order in broadcast prot

    1.FIFO广播协议,在 上广播m1,m2.那么接收顺序也必须是m1,m2 2.因果广播协议,不要求在同一节点,如...

网友评论

      本文标题:2018-05-23字节点也可以广播

      本文链接:https://www.haomeiwen.com/subject/tfmrjftx.html