func NewNetworkManager(port int, params *lsp.Params, parser EventParser, eventChan chan<- *Event) (NetworkManager, error) { server, err := lsp.NewServer(port, params) if err != nil { return &networkManager{}, err } manager := &networkManager{ port: port, params: params, server: server, parser: parser, eventChan: eventChan, closed: false, } go manager.start() return manager, nil }
func main() { flag.Parse() if !*showLogs { log.SetOutput(ioutil.Discard) } lspnet.SetServerReadDropPercent(*readDrop) lspnet.SetServerWriteDropPercent(*writeDrop) params := &lsp.Params{ EpochLimit: *epochLimit, EpochMillis: *epochMillis, WindowSize: *windowSize, } fmt.Printf("Starting server on port %d...\n", *port) srv, err := lsp.NewServer(*port, params) if err != nil { fmt.Printf("Failed to start Server on port %d: %s\n", *port, err) return } fmt.Println("Server waiting for clients...") runServer(srv) }
func main() { const numArgs = 2 if len(os.Args) != numArgs { fmt.Println("Usage: ./server <port>") return } file, err := os.OpenFile(name, flag, perm) if err != nil { return } mlx := log.New(file, "", log.Lshortfile|log.Lmicroseconds) port, _ := strconv.Atoi(os.Args[1]) s, err := lsp.NewServer(port, lsp.NewParams()) if err != nil { mlx.Println("cant start server in the port") file.Close() return } yy := NewScheduler() for { select { case <-yy.waitans: if yy.jobgiveup == false { res := bitcoin.NewResult(yy.ans1, yy.ans2) buf, _ := json.Marshal(res) mlx.Println("send ans to client: ", yy.curclient, res.String()) errr := s.Write(yy.curclient, buf) if errr != nil { mlx.Println("server write ans to client timeout") } } yy.run = false yy.currid += 1 if len(yy.pendjobs) != 0 && yy.currid < yy.reqct { mlx.Println("there is jobs left begin to assign", yy.currid) mlx.Println("move pend miner to miner") yy.resize(mlx, s) mlx.Println("-----") yy.assign(mlx, s) } default: id, v, err := s.Read() if err != nil { //mlx.Println(err) _, ok1 := yy.miner[id] if ok1 { if yy.run { mlx.Println("lost miner: ", id, " when running the job") if yy.miner[id] != nil { yy.pendrange[id] = yy.miner[id] yy.minerfail += 1 delete(yy.miner, id) yy.resize(mlx, s) if len(yy.miner) != 0 { yy.reassign(mlx, s) } continue } } else { mlx.Println("lost miner: ", id, " when there is no job and miner wait for request") delete(yy.miner, id) continue } } _, ok2 := yy.pendminer[id] if ok2 { mlx.Println("lost miner: ", id, " before assign job") delete(yy.pendminer, id) continue } if id == yy.curclient { mlx.Println("lost the running client: ", id, " before return results, give up current job") yy.jobgiveup = true continue } /*else { if id < yy.curclient { continue } mlx.Println("lost the unruning client: ", id, "before start its job") pos := 0 for i, v := range yy.pendclients { if pos == 0 { if i == id { pos = i delete(yy.pendclients, i) } } else { if i > pos { delete(yy.pendclients, i) yy.pendclients[i-1] = v } } } for i, v := range yy.pendjobs { if i > pos { delete(yy.pendjobs, i) yy.pendjobs[i-1] = v } } yy.reqct -= 1 }*/ } else { msg := &bitcoin.Message{} err = json.Unmarshal(v[:], msg) mlx.Println(id, msg.String()) switch msg.Type { case bitcoin.Join: if yy.run { yy.pendminer[id] = 0 yy.resize(mlx, s) if len(yy.pendrange) != 0 { yy.reassign(mlx, s) } } else { yy.miner[id] = nil if len(yy.pendrange) != 0 { mlx.Println("request wait for miner for left job, miner come") yy.reassign(mlx, s) continue } if len(yy.pendclients) != 0 { mlx.Println("request wait for miner, miner come") yy.assign(mlx, s) } } case bitcoin.Request: mlx.Println("receive request form client:", id, msg.String()) if yy.run { yy.pendclients[yy.reqct] = id yy.pendjobs[yy.reqct] = msg yy.reqct += 1 } else { if len(yy.miner) != 0 { yy.pendclients[yy.reqct] = id yy.pendjobs[yy.reqct] = msg yy.reqct += 1 mlx.Println("miner wait for request, client come") yy.assign(mlx, s) } else { yy.pendclients[yy.reqct] = id yy.pendjobs[yy.reqct] = msg yy.reqct += 1 } } case bitcoin.Result: mlx.Println("receive ans: ", msg.Hash, msg.Nonce) if yy.run { for i, v := range yy.miner { if v == nil { continue } mlx.Println("check miner: ", i, v.Lower, v.Upper) if v != nil && msg.Nonce >= v.Lower && msg.Nonce <= v.Upper { if msg.Hash < yy.ans1 { yy.ans1 = msg.Hash yy.ans2 = msg.Nonce } else if msg.Hash == yy.ans1 { if msg.Nonce < yy.ans2 { yy.ans2 = msg.Nonce } } yy.miner[i] = nil yy.block -= 1 mlx.Println("left ", yy.block, " answers, now answer: ", yy.ans1, yy.ans2) break } } if yy.block == 0 { //yy.ans1 = msg.Hash //yy.ans2 = msg.Nonce yy.waitans <- 1 continue } if yy.minerfail > 0 { yy.reassign(mlx, s) } } } } } } }