Ejemplo n.º 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()
	}

}
Ejemplo n.º 2
0
func NewMiner(hostport string, params *lsp.Params) (*Miner, error) {
	client, err := lsp.NewClient(hostport, params)
	if err != nil {
		return nil, fmt.Errorf("cannot create miner :%v", err)
	}

	return &Miner{
		client: client,
	}, nil
}
Ejemplo n.º 3
0
func NewClient(hostport string, params *lsp.Params, done chan bool) (*Client, error) {
	client, err := lsp.NewClient(hostport, params)
	if err != nil {
		return nil, err
	}

	c := &Client{
		client: client,
		done:   done,
	}

	return c, nil
}
Ejemplo n.º 4
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))
}
Ejemplo n.º 5
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)
	}
}
Ejemplo n.º 6
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
		}
	}
}
Ejemplo n.º 7
0
func main() {
	flag.Parse()
	if !*showLogs {
		log.SetOutput(ioutil.Discard)
	}
	lspnet.SetClientReadDropPercent(*readDrop)
	lspnet.SetClientWriteDropPercent(*writeDrop)
	params := &lsp.Params{
		EpochLimit:  *epochLimit,
		EpochMillis: *epochMillis,
		WindowSize:  *windowSize,
	}
	hostport := lspnet.JoinHostPort(*host, strconv.Itoa(*port))
	fmt.Printf("Connecting to server at '%s'...\n", hostport)
	cli, err := lsp.NewClient(hostport, params)
	if err != nil {
		fmt.Printf("Failed to connect to server at %s: %s\n", hostport, err)
		return
	}
	runClient(cli)
}
Ejemplo n.º 8
0
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()
}
Ejemplo n.º 9
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())
		}
	}
}