예제 #1
0
파일: client.go 프로젝트: Liamsi/cothority
func RunClient(server string, nmsgs int, name string, rate int) {
	dbg.Lvl4("Starting to run stampclient")
	c := NewClient(name)
	servers := strings.Split(server, ",")

	// connect to all the servers listed
	for _, s := range servers {
		h, p, err := net.SplitHostPort(s)
		if err != nil {
			log.Fatal("improperly formatted host")
		}
		pn, _ := strconv.Atoi(p)
		c.AddServer(s, coconet.NewTCPConn(net.JoinHostPort(h, strconv.Itoa(pn+1))))
	}

	// Check if somebody asks for the old way
	if rate < 0 {
		log.Fatal("Rounds based limiting deprecated")
	}

	// Stream time coll_stamp requests
	// if rate specified send out one message every rate milliseconds
	dbg.Lvl1(name, "starting to stream at rate", rate)
	streamMessgs(c, servers, rate)
	dbg.Lvl4("Finished streaming")
	return
}
예제 #2
0
파일: client.go 프로젝트: LefKok/Coin
func main() {
	Current := new(Node)
	Magic := [4]byte{0xF9, 0xBE, 0xB4, 0xD9}
	Current.IP = net.IPv4(0, 1, 2, 3)
	Current.PublicKey = "my_cool_key"
	Current.Last_Block = "0"
	Parser, _ := BitCoSi.NewParser("/home/lefteris/hi/blocks", Magic)
	server := "localhost:2011"
	//	suite = app.GetSuite("25519")

	dbg.Lvl2("Connecting to", server)
	conn := coconet.NewTCPConn(server)
	err := conn.Connect()
	if err != nil {
		dbg.Fatal("Error when getting the connection to the host:", err)
	}
	dbg.Lvl1("Connected to ", server)
	go wait_for_blocks()

	for i := 0; i < 10; i++ {

		Current.transaction_pool = Parser.Parse(i, 10+i)
		for len(Current.transaction_pool) > 0 {
			msg := &BitCoSi.BitCoSiMessage{
				Type:  BitCoSi.TransactionAnnouncmentType,
				ReqNo: 0,
				Treq:  &BitCoSi.TransactionAnnouncment{Val: Current.transaction_pool[0]}}

			err = conn.PutData(msg)
			Current.transaction_pool = Current.transaction_pool[1:]
			if err != nil {
				dbg.Fatal("Couldn't send hash-message to server: ", err)
			}
			time.Sleep(10 * time.Millisecond)

		}
	}

	wait_for_Key_blocks()
	time.Sleep(900000 * time.Millisecond)

	// Asking to close the connection
	err = conn.PutData(&BitCoSi.BitCoSiMessage{
		ReqNo: 1,
		Type:  BitCoSi.BitCoSiClose,
	})

	conn.Close()
	dbg.Lvl2("Connection closed with server")

}
예제 #3
0
파일: client.go 프로젝트: LefKok/Coin
func wait_for_blocks() {

	server := "localhost:2011"
	suite = app.GetSuite("25519")

	dbg.Lvl2("Connecting to", server)
	conn := coconet.NewTCPConn(server)
	err := conn.Connect()
	if err != nil {
		dbg.Fatal("Error when getting the connection to the host:", err)
	}
	dbg.Lvl1("Connected to ", server)
	for i := 0; i < 1000; i++ {
		time.Sleep(1 * time.Second)
		msg := &BitCoSi.BitCoSiMessage{
			Type:  BitCoSi.BlockRequestType,
			ReqNo: 0,
		}

		err = conn.PutData(msg)
		if err != nil {
			dbg.Fatal("Couldn't send hash-message to server: ", err)
		}
		dbg.Lvl1("Sent signature request")
		// Wait for the signed message

		tsm := new(BitCoSi.BitCoSiMessage)
		tsm.Brep = &BitCoSi.BlockReply{}
		tsm.Brep.SuiteStr = suite.String()
		err = conn.GetData(tsm)
		if err != nil {
			dbg.Fatal("Error while receiving signature:", err)
		}
		//dbg.Lvlf1("Got signature response %+v", tsm.Brep)

		T := new(BitCoSi.TrBlock)
		T.Block = tsm.Brep.Block
		T.Print()
		dbg.Lvlf1("Response %v ", tsm.Brep.Response)
	}
	// Asking to close the connection
	err = conn.PutData(&BitCoSi.BitCoSiMessage{
		ReqNo: 1,
		Type:  BitCoSi.BitCoSiClose,
	})

	conn.Close()

}
예제 #4
0
파일: stamp.go 프로젝트: mlncn/cothority
// Used to connect to server
func (s *Stamp) connect(server string) error {
	// First get a connection. Get a random one if no server provided
	if server == "" {
		serverPort := strings.Split(s.Config.Hosts[rand.Intn(len(s.Config.Hosts))], ":")
		server = serverPort[0]
		port, _ := strconv.Atoi(serverPort[1])
		server += ":" + strconv.Itoa(port+1)
	}
	if !strings.Contains(server, ":") {
		server += ":2000"
	}
	dbg.Lvl2("Connecting to", server)
	s.conn = coconet.NewTCPConn(server)
	err := s.conn.Connect()
	if err != nil {
		return fmt.Errorf("Couldn't get connection to host: %s", err)
	}

	dbg.Lvl3("Connected to", server)
	return nil
}
예제 #5
0
파일: exit.go 프로젝트: mlncn/cothority
// ForceExit connects to the stamp-port of the conode and asks him to exit
func ForceExit(address string) {
	add, err := cliutils.VerifyPort(address, conode.DefaultPort+1)
	if err != nil {
		dbg.Fatal("Couldn't convert", address, "to a IP:PORT")
	}

	conn := coconet.NewTCPConn(add)
	err = conn.Connect()
	if err != nil {
		dbg.Fatal("Error when getting the connection to the host:", err)
	}
	dbg.Lvl1("Connected to", add)
	msg := &conode.TimeStampMessage{
		Type: conode.StampExit,
	}

	dbg.Lvl1("Asking to exit")
	err = conn.PutData(msg)
	if err != nil {
		dbg.Fatal("Couldn't send exit-message to server:", err)
	}
}
예제 #6
0
파일: stamper.go 프로젝트: mlncn/cothority
func RunClient(flags *app.Flags, conf *app.ConfigColl) {
	dbg.Lvl4("Starting to run stampclient")
	c := NewClient(flags.Name)
	servers := strings.Split(flags.Server, ",")
	// take the right percentage of servers
	servers = scaleServers(flags, conf, servers)
	// connect to all the servers listed
	for _, s := range servers {
		h, p, err := net.SplitHostPort(s)
		if err != nil {
			log.Fatal("improperly formatted host")
		}
		pn, _ := strconv.Atoi(p)
		c.AddServer(s, coconet.NewTCPConn(net.JoinHostPort(h, strconv.Itoa(pn+1))))
	}
	// Stream time coll_stamp requests
	// if rate specified send out one message every rate milliseconds
	dbg.Lvl3(flags.Name, "starting to stream at rate", conf.Rate, "with root", flags.AmRoot)
	streamMessgs(c, servers, conf.Rate)
	dbg.Lvl4("Finished streaming", flags.Name)
	return
}
예제 #7
0
// run each host in hostnameSlice with the number of clients given
func RunTimestamper(hc *config.HostConfig, nclients int, hostnameSlice ...string) ([]*Server, []*Client, error) {
	dbg.Lvl3("RunTimestamper on", hc.Hosts)
	hostnames := make(map[string]*sign.Node)
	// make a list of hostnames we want to run
	if hostnameSlice == nil {
		hostnames = hc.Hosts
	} else {
		for _, h := range hostnameSlice {
			sn, ok := hc.Hosts[h]
			if !ok {
				return nil, nil, errors.New("hostname given not in config file:" + h)
			}
			hostnames[h] = sn
		}
	}

	Clients := make([]*Client, 0, len(hostnames)*nclients)
	// for each client in
	stampers := make([]*Server, 0, len(hostnames))
	for _, sn := range hc.SNodes {
		if _, ok := hostnames[sn.Name()]; !ok {
			log.Errorln("signing node not in hostnmaes")
			continue
		}
		stampers = append(stampers, NewServer(sn))
		if hc.Dir == nil {
			dbg.Lvl3(hc.Hosts, "listening for clients")
			stampers[len(stampers)-1].Listen()
		}
	}
	dbg.Lvl3("stampers:", stampers)
	clientsLists := make([][]*Client, len(hc.SNodes[1:]))
	for i, s := range stampers[1:] {
		// cant assume the type of connection
		clients := make([]*Client, nclients)

		h, p, err := net.SplitHostPort(s.Name())
		if hc.Dir != nil {
			h = s.Name()
		} else if err != nil {
			log.Fatal("RunTimestamper: bad Tcp host")
		}
		pn, err := strconv.Atoi(p)
		if hc.Dir != nil {
			pn = 0
		} else if err != nil {
			log.Fatal("port is not valid integer")
		}
		hp := net.JoinHostPort(h, strconv.Itoa(pn+1))
		//dbg.Lvl4("client connecting to:", hp)

		for j := range clients {
			clients[j] = NewClient("client" + strconv.Itoa((i-1)*len(stampers)+j))
			var c coconet.Conn

			// if we are using tcp connections
			if hc.Dir == nil {
				// the timestamp server serves at the old port + 1
				dbg.Lvl4("new tcp conn")
				c = coconet.NewTCPConn(hp)
			} else {
				dbg.Lvl4("new go conn")
				c, _ = coconet.NewGoConn(hc.Dir, clients[j].Name(), s.Name())
				stoc, _ := coconet.NewGoConn(hc.Dir, s.Name(), clients[j].Name())
				s.Clients[clients[j].Name()] = stoc
			}
			// connect to the server from the client
			clients[j].AddServer(s.Name(), c)
			//clients[j].Sns[s.Name()] = c
			//clients[j].Connect()
		}
		Clients = append(Clients, clients...)
		clientsLists[i] = clients
	}

	return stampers, Clients, nil
}