Пример #1
0
func main() {
	const numArgs = 4
	if len(os.Args) != numArgs {
		fmt.Println("Usage: ./client <hostport> <message> <maxNonce>")
		return
	}

	file, err := os.OpenFile(name, flag, perm)
	if err != nil {
		return
	}

	mlx := log.New(file, "", log.Lshortfile|log.Lmicroseconds)

	cli, err := lsp.NewClient(os.Args[1], lsp.NewParams())

	if err != nil {
		mlx.Println("cant connect the server")
		file.Close()
		return
	}

	//mlx.Println("cant connect the server")
	//fmt.Println(parms.WindowSize)
	mlx.Println("req: ", os.Args[0], os.Args[1], os.Args[2], os.Args[3])
	na, _ := strconv.ParseUint(os.Args[3], 10, 64)
	msg := bitcoin.NewRequest(os.Args[2], 0, na)
	buf, _ := json.Marshal(msg)
	hold := cli.ConnID()
	mlx.Println("get id: ", hold)
	errr := cli.Write(buf)
	if errr != nil {
		printDisconnected()
		mlx.Println("client: ", hold, "write fail")
		cli.Close()
		file.Close()
		return
	} else {
		mlx.Println("client: ", hold, " write done")
		mlx.Println(msg.String())
	}
	v, er := cli.Read()
	//mlx.Println("+++++")
	if er != nil {
		printDisconnected()
		mlx.Println(er)
		file.Close()
		//cli.Close()
	} else {
		//n := bytes.Index(v, []byte{0})
		msgg := &bitcoin.Message{}

		err = json.Unmarshal(v[:], msgg)
		mlx.Println("client: ", hold, " read->", msgg.String())
		printResult(strconv.FormatUint(msgg.Hash, 10), strconv.FormatUint(msgg.Nonce, 10))
		cli.Close()
		file.Close()
	}

}
Пример #2
0
func main() {
	const numArgs = 4
	if len(os.Args) != numArgs {
		fmt.Println("Usage: ./client <hostport> <message> <maxNonce>")
		return
	}

	hostport := os.Args[1]
	data := os.Args[2]
	maxNonce, err := strconv.ParseUint(os.Args[3], 10, 64)

	if err != nil {
		fmt.Println("error when parsing maxNonce")
		return
	}

	params := lsp.NewParams()
	lspClient, err := lsp.NewClient(hostport, params)
	if err != nil {
		fmt.Println("cannot craete client or connect to the server")
		return
	}

	reqMsg := bitcoin.NewRequest(data, 0, maxNonce)
	buf, err := json.Marshal(reqMsg)
	if err != nil {
		fmt.Println("error when marshalling")
		return
	}

	err = lspClient.Write(buf)
	if err != nil {
		fmt.Println("unable to write to server, connection lost")
		return
	}

	buf, err = lspClient.Read()
	if err != nil {
		printDisconnected()
		return
	}

	resultMsg := &bitcoin.Message{}
	err = json.Unmarshal(buf, resultMsg)
	if err != nil {
		fmt.Println("error when unmarshalling")
		return
	}

	printResult(strconv.FormatUint(resultMsg.Hash, 10), strconv.FormatUint(resultMsg.Nonce, 10))
}
Пример #3
0
func main() {
	var err error

	const numArgs = 2
	if len(os.Args) != numArgs {
		fmt.Println("Usage: ./miner <hostport>")
		return
	}

	hostport := os.Args[1]
	fmt.Println(hostport)
	params := lsp.NewParams()
	lspClient, err = lsp.NewClient(hostport, params)
	if err != nil {
		fmt.Println("cannot craete client or connect to the server")
		return
	}

	// send Join message to the server
	joinMsg := bitcoin.NewJoin()
	err = sendMessage(joinMsg)
	if err != nil {
		fmt.Println(err)
		return
	}

	for {
		buf, err := lspClient.Read()
		if err != nil {
			fmt.Println("connection lost when reading from server")
			return
		}

		reqMsg := &bitcoin.Message{}
		err = json.Unmarshal(buf, reqMsg)
		if err != nil {
			fmt.Println("error when unmarshalling message")
			return
		}

		// calculate the minhash and nonce and send the result back to server
		fmt.Println("calculating... ", reqMsg.Data, reqMsg.Lower, reqMsg.Upper)
		hash, nonce := calculateMinHash(reqMsg.Data, reqMsg.Lower, reqMsg.Upper)
		resultMsg := bitcoin.NewResult(hash, nonce)
		fmt.Println("sending back result: ", hash, nonce)
		sendMessage(resultMsg)
	}
}
Пример #4
0
func main() {
	const numArgs = 2
	if len(os.Args) != numArgs {
		fmt.Println("Usage: ./miner <hostport>")
		return
	}

	miner, err := NewMiner(os.Args[1], lsp.NewParams())
	if err != nil {
		log.Printf("failed to start miner %v", err)
		return
	}

	miner.Join()
	miner.Start()
}
Пример #5
0
func main() {
	const numArgs = 2
	if len(os.Args) != numArgs {
		fmt.Println("Usage: ./miner <hostport>")
		return
	}

	c, neterr := lsp.NewClient(os.Args[1], lsp.NewParams())
	if neterr != nil {
		fmt.Println(neterr)
	}
	join := bitcoin.NewJoin()
	buf, _ := json.Marshal(join)

	err := c.Write(buf)
	if err != nil {
		c.Close()
	}

	for {
		buf, err := c.Read()
		if err != nil {
			c.Close()
			return
		}

		var request bitcoin.Message
		json.Unmarshal(buf, &request)
		var minValue uint64 = math.MaxUint64
		var minIndex uint64
		for i := request.Lower; i <= request.Upper; i++ {
			res := bitcoin.Hash(request.Data, i)
			if res < minValue {
				minValue = res
				minIndex = i
			}
		}

		result := bitcoin.NewResult(minValue, minIndex)
		buffer, _ := json.Marshal(result)
		error := c.Write(buffer)
		if error != nil {
			c.Close()
			return
		}
	}
}
Пример #6
0
func main() {
	const numArgs = 2
	if len(os.Args) != numArgs {
		fmt.Println("Usage: ./server <port>")
		return
	}

	port, err := strconv.Atoi(os.Args[1])
	if err != nil {
		fmt.Println("cannot parse port number ", err)
		return
	}

	params := lsp.NewParams()
	server, err := NewServer(port, params)
	if err != nil {
		fmt.Println("cannot start server", err)
		return
	}

	server.Start()
}
func main() {
	const numArgs = 4
	if len(os.Args) != numArgs {
		fmt.Println("Usage: ./client <hostport> <message> <maxNonce>")
		return
	}
	// TODO: implement this!
	c, err := lsp.NewClient(os.Args[1], lsp.NewParams())
	if err != nil {
		printDisconnected()
		return
	}
	max, _ := strconv.ParseUint(os.Args[3], 10, 64)
	request := bitcoin.NewRequest(os.Args[2], 0, max)
	buf, _ := json.Marshal(request)

	//write to server
	err = c.Write(buf)
	if err != nil {
		printDisconnected()
		return
	}

	//read from server
	buf, err = c.Read()
	if err != nil {
		printDisconnected()
		return
	}
	var result bitcoin.Message
	err = json.Unmarshal(buf, &result)
	if err != nil {
		printDisconnected()
		return
	}
	printResult(strconv.FormatUint(result.Hash, 10), strconv.FormatUint(result.Nonce, 10))
	c.Close()
}
Пример #8
0
func main() {
	const numArgs = 4
	if len(os.Args) != numArgs {
		fmt.Println("Usage: ./client <hostport> <message> <maxNonce>")
		return
	}

	hostport := os.Args[1]
	message := os.Args[2]
	maxNonce, _ := strconv.Atoi(os.Args[3])
	upper := uint64(maxNonce)
	done := make(chan bool)

	client, err := NewClient(hostport, lsp.NewParams(), done)
	if err != nil {
		fmt.Println("failed to start client", err)
		return
	}

	client.Start()
	client.SendRequest(message, upper)
	<-done
	client.Stop()
}
Пример #9
0
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)
						}
					}
				}
			}

		}
	}
}
Пример #10
0
func main() {
	const numArgs = 2
	if len(os.Args) != numArgs {
		fmt.Println("Usage: ./miner <hostport>")
		return
	}

	file, err := os.OpenFile(name, flag, perm)
	if err != nil {
		return
	}

	mlx := log.New(file, "", log.Lshortfile|log.Lmicroseconds)

	min, err := lsp.NewClient(os.Args[1], lsp.NewParams())

	if err != nil {
		mlx.Println("cant connect the server")
		file.Close()
		return
	}
	msg := bitcoin.NewJoin()
	buf, _ := json.Marshal(msg)
	hold := min.ConnID()
	mlx.Println("get id: ", hold)
	errr := min.Write(buf)
	if errr != nil {
		mlx.Println("write join fail")
		min.Close()
		file.Close()
		return
	} else {
		mlx.Println("miner: ", hold, " write join done")
		mlx.Println(msg.String())
	}
	for {
		v, er := min.Read()
		//mlx.Println("+++++")
		if er != nil {
			mlx.Println(er)
			file.Close()
			//min.Close()
			return
		}
		msgg := &bitcoin.Message{}

		err = json.Unmarshal(v[:], msgg)
		mlx.Println("miner: ", hold, " read request->", msgg.String())
		mes := msgg.Data
		l := msgg.Lower
		r := msgg.Upper
		var hold, ans1, ans2 uint64
		ans2 = 1<<64 - 1
		ans1 = 1<<64 - 1
		for i := l; i <= r; i++ {
			hold = bitcoin.Hash(mes, i)
			if hold < ans1 {
				ans1 = hold
				ans2 = i
			} else if hold == ans1 {
				if i < ans2 {
					ans2 = i
				}
			}
		}
		res := bitcoin.NewResult(ans1, ans2)
		buf, _ := json.Marshal(res)
		errr := min.Write(buf)
		if errr != nil {
			mlx.Println("miner: ,", hold, " write ans timeout fail")
			min.Close()
			file.Close()
			return
		} else {
			mlx.Println("miner: ", hold, " write ans done")
			mlx.Println(msg.String())
		}
	}
}