Exemple #1
0
func (t *Tracker) Request(uploaded int, downloaded int, left int, status string) (peers *list.List, err os.Error) {
	err = nil
	url := fmt.Sprint(t.url,
		"?",
		"info_hash=", http.URLEscape(t.infoHash),
		"&peer_id=", http.URLEscape(t.peerId),
		"&port=", http.URLEscape(t.port),
		"&uploaded=", strconv.Itoa(uploaded),
		"&downloaded=", strconv.Itoa(downloaded),
		"&left=", strconv.Itoa(left),
		"&status=", http.URLEscape(status))
	println(url)
	response, _, err := http.Get(url)
	if err != nil {
		return
	}

	if response.StatusCode != http.StatusOK {
		err = os.NewError("http error")
	}
	buf := new(bencode.BeString)
	resReader := bufio.NewReader(response.Body)
	be, err := buf.Decode(resReader)
	if err != nil {
		return
	}
	if be.Betype != bencode.Bedict {
		err = os.NewError("unexpected response from tracker")
		return
	}
	if failure, ok := be.Bedict["failure reason"]; !ok {
		err = os.NewError("unexpected response from tracker")
		return
	} else {
		print(failure)
		return
	}
	if interval, ok := be.Bedict["interval"]; !ok {
		err = os.NewError("unexpected response from tracker")
		return
	} else {
		t.interval, err = strconv.Atoi(interval.Bestr)
	}
	if _, ok := be.Bedict["peers"]; !ok {
		err = os.NewError("unexpected response from tracker")
		return
	}
	list := list.New()
	for i := range be.Bedict["peers"].Belist.Iter() {
		if i.(*bencode.BeNode).Betype == bencode.Bedict {
			list.PushFront(i.(*bencode.BeNode).Bedict)
		} else {
			err = os.NewError("unexpected response from tracker")
			return
		}
	}
	peers = list
	return
}
Exemple #2
0
func daythirteen() {
	file, _ := os.Open("input-day13")
	list := list.New()
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		text := scanner.Text()
		list.PushFront(text)
	}
	arr := make([]string, list.Len())
	count := 0
	for e := list.Front(); e != nil; e = e.Next() {
		arr[count] = e.Value.(string)
		count++
	}
	fmt.Printf("Best = %d\n", ProcSeatingArray(arr, 0))
	fmt.Printf("Best = %d\n", ProcSeatingArray(arr, 1))
}
Exemple #3
0
func reconstructPath(came_from map[Coord]Coord, current_node Coord) []*Move {
	list := list.New()
	for present := true; present; _, present = came_from[current_node] {
		next_node := came_from[current_node]
		list.PushFront(findMove(current_node, next_node))

		current_node = next_node
	}

	// Convert to a list and return
	rtn := make([]*Move, list.Len())
	i := 0
	for e := list.Front(); e != nil; e = e.Next() {
		move := e.Value.(Move)
		rtn[i] = &move
		i++
	}
	return rtn
}
Exemple #4
0
func Map(value string) *list.List {
	list := list.New()

	lines := strings.Split(value, "\n")

	for _, l := range lines {
		// ommit empty lines
		if l == "" {
			continue
		}

		numbers := strings.Split(strings.TrimSpace(l), ",")

		for _, n := range numbers {
			list.PushFront(mapreduce.KeyValue{Key: n, Value: ""})
		}
	}

	return list
}
func Test_SourceServer(t *testing.T) {

	hp := config.HostPort{Host: "localhost", Port: 44444}
	err, poollink := pool.NewFlumePoolLink(hp)
	if nil != err {
		t.Fail()
	}

	list := list.New()
	list.PushFront(poollink)
	sourceserver := newSourceServer("location", list)

	go func() { sourceserver.start() }()

	for i := 0; i < 100; i++ {
		_, message := decodeCommand([]byte(LOG))
		sourceserver.buffChannel <- message
	}
	time.Sleep(10 * time.Second)
	sourceserver.stop()
}
Exemple #6
0
func (stack *Stack) Push(data []byte) {
	list := (*list.List)(stack)
	list.PushFront(data)
}