Beispiel #1
0
func main() {

	id, _ := strconv.Atoi(os.Args[1])
	fmt.Println("Received here:", id, " 2:", os.Args)
	number_of_servers := 5
	size_of_in_chan := 20
	size_of_out_chan := 20

	parent_dir := "" + os.Args[3]

	fnm := parent_dir + "/Raft/configuration" + strconv.Itoa(id) + ".xml"

	r = Raft.NewRaft(id, size_of_in_chan, size_of_out_chan, 100*time.Millisecond, fnm, parent_dir)

	r.RecomputeParameters()

	wg := new(sync.WaitGroup)
	wg.Add(2)
	go r.S.Send(wg)
	go r.S.Receive(number_of_servers, wg)
	//go r.Recv_From_Inbox()
	go r.RaftController()
	//go raft.Close()
	fmt.Println("\nIn main")

	//go r.Start_Handling_Request_From_Clients();

	//go start_send_requests_client(r)
	//go start_recv_responses_client(r)

	raft_rpc := new(Raft.RaftRPC)
	rpc.Register(raft_rpc)

	l, e := net.Listen("tcp", ":"+os.Args[2])

	if e != nil {
		//log.Fatal("listen error:", e)
		fmt.Printf("\nError in listening")
	}

	go func() {
		for {
			conn, errconn := l.Accept()
			if errconn != nil {
				fmt.Printf("\nConnection error")
			}
			go rpc.ServeConn(conn)
		}
	}()

	wg.Wait()
	fmt.Println("Exiting")

}
func main() {

	//exit_main=make(chan *bool)

	id, _ := strconv.Atoi(os.Args[1])

	number_of_servers := 5
	size_of_in_chan := 20
	size_of_out_chan := 20

	parent_dir := "" + os.Args[3]
	fnm := parent_dir + "/Raft/configuration" + strconv.Itoa(id) + ".xml"

	//fmt.Println(""+parent_dir+":"+fnm)

	r = Raft.NewRaft(id, size_of_in_chan, size_of_out_chan, 100*time.Millisecond, fnm, parent_dir)

	wg := new(sync.WaitGroup)
	wg.Add(2)
	go r.S.Send(number_of_servers, wg)
	go r.S.Receive(number_of_servers, wg)
	go r.Recv_From_Inbox()
	go r.RaftController()
	//go raft.Close()
	fmt.Println("\nIn main")

	raft_rpc := new(Raft.RaftRPC)
	rpc.Register(raft_rpc)
	///rpc.HandleHTTP()

	l, e := net.Listen("tcp", ":"+os.Args[2])

	if e != nil {
		//log.Fatal("listen error:", e)
		fmt.Printf("\nError in listening")
	}
	//go http.Serve(l, nil)
	//fmt.Println("Waiting")
	go func() {
		for {
			conn, errconn := l.Accept()
			if errconn != nil {
				fmt.Printf("\nConnection error")
			}
			go rpc.ServeConn(conn)
		}
	}()

	wg.Wait()
	fmt.Println("Exiting")

}
Beispiel #3
0
func main() {
	flag.Parse()
	if *id == 0 || *dbgport == 0 {
		fmt.Println("Can't run because proper arguments not given.Use -h or --help to get info about arguments")
		os.Exit(2)
	}

	rpc.Register(&Sender)
	litsner := connect()
	go handler(&litsner)
	raftVal = *raft.NewRaft(*id, "C:\\CloudSrc\\src\\raftconfig.json")
	db, _ := leveldb.OpenFile("db_"+strconv.Itoa(*id)+".db", nil)

	if *dbgmsg > 0 {
		raftVal.DebugOn()
	}

	go func() {
		i := 0
		for i < 100 {

			if raftVal.IsLeader() {
				var op []byte
				switch i % 2 {
				case 0:
					op = store(put{Key: random(40), Value: random(40), Wo: nil})
					log.Println("Doing PUT", i)
				case 1:
					op = store(delete{Key: random(40), Wo: nil})
					log.Println("Doing DEL", i)
				}
				i++

				raftVal.Inbox() <- &op
			}
			time.Sleep(time.Millisecond * 500)
		}
	}()
	go func() {
		for {

			select {
			case y := <-raftVal.Outbox():
				var command cluster.LogValue
				command = *y
				var operation interface{}
				operation = load(command.Operands)
				switch operation.(type) {
				case put:
					db.Put(operation.(put).Key, operation.(put).Value, operation.(put).Wo)
					log.Println("PUT", string(operation.(put).Key), operation.(put).Value)
				case delete:
					db.Delete(operation.(delete).Key, operation.(delete).Wo)
					log.Println("delete", operation.(delete).Key)

				}

			}
		}
	}()

	time.Sleep(time.Hour)
}