<commit_before>package swarm

import (
	"net"
	"time"
	"xd/lib/bittorrent"
	"xd/lib/bittorrent/extensions"
	"xd/lib/common"
	"xd/lib/log"
	"xd/lib/network"
	"xd/lib/storage"
	"xd/lib/tracker"
	"xd/lib/util"
)

// a bittorrent swarm tracking many torrents
type Swarm struct {
	closing  bool
	net      network.Network
	Torrents Holder
	id       common.PeerID
	trackers map[string]tracker.Announcer
}

func (sw *Swarm) Running() bool {
	return !sw.closing
}

// wait until we get a network context
func (sw *Swarm) WaitForNetwork() {
	for sw.net == nil {
		time.Sleep(time.Second)
	}
}

func (sw *Swarm) startTorrent(t *Torrent) {
	sw.WaitForNetwork()
	// give peerid
	t.id = sw.id
	// add open trackers
	for name := range sw.trackers {
		t.Trackers[name] = sw.trackers[name]
	}

	info := t.MetaInfo()
	for _, u := range info.GetAllAnnounceURLS() {
		tr := tracker.FromURL(u)
		if tr != nil {
			name := tr.Name()
			_, ok := t.Trackers[name]
			if !ok {
				t.Trackers[name] = tr
			}
		}
	}

	// start annoucing
	go t.StartAnnouncing()
	// handle messages
	go t.Run()
}

// got inbound connection
func (sw *Swarm) inboundConn(c net.Conn) {
	h := new(bittorrent.Handshake)
	log.Debug("read bittorrent handshake")
	err := h.Recv(c)
	if err != nil {<commit_msg>fix runtime adding of torrent<commit_after>package swarm

import (
	"net"
	"time"
	"xd/lib/bittorrent"
	"xd/lib/bittorrent/extensions"
	"xd/lib/common"
	"xd/lib/log"
	"xd/lib/network"
	"xd/lib/storage"
	"xd/lib/tracker"
	"xd/lib/util"
)

// a bittorrent swarm tracking many torrents
type Swarm struct {
	closing  bool
	net      network.Network
	Torrents Holder
	id       common.PeerID
	trackers map[string]tracker.Announcer
}

func (sw *Swarm) Running() bool {
	return !sw.closing
}

// wait until we get a network context
func (sw *Swarm) WaitForNetwork() {
	for sw.net == nil {
		time.Sleep(time.Second)
	}
}

func (sw *Swarm) startTorrent(t *Torrent) {
	sw.WaitForNetwork()
	t.ObtainedNetwork(sw.net)
	// give peerid
	t.id = sw.id
	// add open trackers
	for name := range sw.trackers {
		t.Trackers[name] = sw.trackers[name]
	}

	info := t.MetaInfo()
	for _, u := range info.GetAllAnnounceURLS() {
		tr := tracker.FromURL(u)
		if tr != nil {
			name := tr.Name()
			_, ok := t.Trackers[name]
			if !ok {
				t.Trackers[name] = tr
			}
		}
	}

	// start annoucing
	go t.StartAnnouncing()
	// handle messages
	go t.Run()
}

// got inbound connection
func (sw *Swarm) inboundConn(c net.Conn) {
	h := new(bittorrent.Handshake)
	log.Debug("read bittorrent handshake")
	err := h.Recv(c)
	if err != nil {
{'fraction_non_alphanumeric': 0.14626515447790378, 'fraction_numerical': 0.0, 'mean_word_length': 3.214168039538715, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '232501', 'n_tokens_mistral': 1089, 'n_tokens_neox': 1033, 'n_words': 241}