Exemplo 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()
	}

}
Exemplo n.º 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))
}
Exemplo n.º 3
0
func (yy *Scheduler) assign(mlx *log.Logger, s lsp.Server) {

	yy.jobgiveup = false
	hold := yy.pendjobs[yy.currid]
	yy.curclient = yy.pendclients[yy.currid]
	mlx.Println("begin assign job: ", yy.currid, hold.String(), " for client: ", yy.curclient, " with ", len(yy.miner), " miners")
	delete(yy.pendjobs, yy.currid)
	delete(yy.pendclients, yy.currid)
	var block uint64
	block = (hold.Upper) / uint64(len(yy.miner))
	if hold.Upper%uint64(len(yy.miner)) != 0 {
		//mlx.Println("left ", hold.Upper%uint64(len(yy.miner)))
		block += 1
	}
	yy.ans1 = 1<<64 - 1
	yy.ans2 = hold.Upper
	yy.block = len(yy.miner)
	// 41 5
	var ct uint64
	ct = 0
	fk := block + 1
	//mlx.Println("block size: ", fk)
	limit := hold.Upper
	for i, _ := range yy.miner {

		hold.Lower = ct * fk
		hold.Upper = hold.Lower + block
		if hold.Upper > limit {
			hold.Upper = limit
		}
		temp := bitcoin.NewRequest(hold.Data, hold.Lower, hold.Upper)
		mlx.Println("assign miner", i, hold.Lower, hold.Upper)
		ct += 1
		buf, _ := json.Marshal(hold)
		errr := s.Write(i, buf)
		yy.miner[i] = temp
		if errr != nil {
			mlx.Println("miner: ", i, " fail in the middle")
			yy.minerfail += 1
			yy.pendrange[i] = temp
			delete(yy.miner, i)
		}
	}
	yy.run = true
}
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()
}
Exemplo n.º 5
0
func (c *Client) SendRequest(data string, upper uint64) error {
	req := bitcoin.NewRequest(data, 0, upper)
	bytes, _ := req.ToBytes()
	return c.client.Write(bytes)
}