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

2018-05-23  本文已影响0人  fairy冉冉

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

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

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

上一篇 下一篇

猜你喜欢

热点阅读