Example #1
0
func RunServer(listenaddr string) {
	server.UsersByNick = make(map[string]*User)
	server.UsersByUsername = make(map[string]*User)
	server.Connections = make(map[*IrcConn]*User)
	server.Channels = treap.NewTree(StrLess)
	server.Hostname = "localhost"
	server.Version = "piRC 0.0.1-alpha1"
	server.StartedAt = time.Now()
	ln, err := net.Listen("tcp", listenaddr)
	if err != nil {
		log.Println(err)
		log.Panicf("Unable to start a server on %v!", listenaddr)
	}

	// Connection handler loop
	for {
		conn, err := ln.Accept()
		if err != nil {
			log.Println("Error opening connection.")
			log.Println(err)
			continue
		} else {
			log.Println("Connection opened")
		}

		// Connection processor loop
		// No select(2) -- it's a beautiful thing :D
		// conn.Read will block in this goroutine until data is received
		go func(conn net.Conn) {
			remote_addr := conn.RemoteAddr().String()
			remote_addr = remote_addr[0:strings.IndexAny(remote_addr, ":")]
			c := IrcConn{conn, remote_addr, server, make([]byte, 2048)}

			for {
				bytes_read, err := conn.Read(c.body[0:])
				if err != nil {
					if err.Error() == "EOF" {
						log.Printf("Connection from %v closed.", remote_addr)
					} else {
						log.Println("Error receiving data.")
						log.Println(err)
					}
					server.UnregisterConnection(&c)
					break
				} else {
					parser := new(CmdParser)
					parser.Client = &c
					err := parser.Parse(c.body[0:bytes_read])
					if err != nil {
						log.Printf("Command parse error: %v", err.Error())
					} else {
						server.Dispatch(parser)
					}
				}
				c.body = make([]byte, 2048)
			}
		}(conn)
	}
}
Example #2
0
func BenchmarkStatHatTreap(b *testing.B) {
	m := stathat.NewTree(func(i, j interface{}) bool {
		return i.(int) < j.(int)
	})
	for i := 0; i < b.N; i++ {
		k := rand.Int()
		m.Insert(k, i)
		j := m.Get(k)
		if j != i {
			b.Error("should be same value")
		}
	}
}
Example #3
0
// Creates a new Distributed tree frontend, with num many backends
func NewDistTree(num int, lessfn treap.LessFunc) *Tree {
	tree := &Tree{
		numBackends: num,
	}

	// Create the initial collection of tree backends
	for i := 0; i < num; i++ {
		t := treap.NewTree(lessfn)
		backend := treeBackend{tree: t}
		tree.backends = append(tree.backends, backend)
	}

	return tree
}