<commit_before>package dns

// A concurrent client implementation. 
// Client sends query to a channel which
// will then handle the query. Returned replys
// are return on another channel. Ready for handling --- same
// setup for server - a HANDLER function that gets run
// when the query returns.

// This completely mirrors server.go impl.
import (
	"os"
	"net"
)

type QueryHandler interface {
	QueryDNS(w RequestWriter, q *Msg)
}

// A RequestWriter interface is used by an DNS query handler to
// construct an DNS request.
type RequestWriter interface {
	RemoteAddr() string     // moet het channel zijn...!

	Write([]byte) (int, os.Error)
}

type qconn struct {
	remoteAddr net.Addr  // address of remote side (sans port)
	port       int       // port of the remote side, needed TODO(mg)
	handler    Handler   // request handler
	request    []byte    //  the request
	w          chan *Msg //
	hijacked   bool      // connection has been hijacked by hander TODO(mg)
}

type reply struct {
	conn *qconn
	req  *Msg
}

// QueryMux is an DNS request multiplexer. It matches the
// zone name of each incoming request against a list of 
// registered patterns add calls the handler for the pattern
// that most closely matches the zone name.
type QueryMux struct {
	m map[string]QueryHandler
}

// NewQueryMux allocates and returns a new QueryMux.
func NewQueryMux() *QueryMux { return &QueryMux{make(map[string]QueryHandler)} }

// DefaultQueryMux is the default QueryMux used by Query.
var DefaultQueryMux = NewQueryMux()

func newQueryChan() chan *Msg { return make(chan *Msg) }

// Default channel to use for the resolver
var DefaultQueryChan = newQueryChan()

// The HandlerQueryFunc type is an adapter to allow the use of
// ordinary functions as DNS query handlers.  If f is a function
// with the appropriate signature, HandlerQueryFunc(f) is a
// QeuryHandler object that calls f.
type HandlerQueryFunc func(RequestWriter, *Msg)

// QueryDNS calls f(w, reg)
func (f HandlerQueryFunc) QueryDNS(w RequestWriter, r *Msg) {
	f(w, r)
}

// Helper handlers
// Todo

// reusing zoneMatch from server.go
func (mux *QueryMux) match(zone string) QueryHandler {
	var h QueryHandler
	var n = 0
	for k, v := range mux.m {
		if !zoneMatch(k, zone) {
			continue
		}
		if h == nil || len(k) > n {
			n = len(k)
			h = v
		}
	}
	return h
}

func (mux *QueryMux) Handle(pattern string, handler QueryHandler) {
	if pattern == "" {
		panic("dns: invalid pattern " + pattern)
	}
	if pattern[len(pattern)-1] != '.' { // no ending .
		mux.m[pattern+"."] = handler
	} else {
		mux.m[pattern] = handler
	}
}

func (mux *QueryMux) HandleQueryFunc(pattern string, handler func(RequestWriter, *Msg)) {
	mux.Handle(pattern, HandlerQueryFunc(handler))
}

func (mux *QueryMux) QueryDNS(w RequestWriter, request *Msg) {
	h := mux.match(request.Question[0].Name)
	if h == nil {
		//                h = RefusedHandler()
		// something else
	}
	h.QueryDNS(w, request)
}

type Client struct {
	Network      string       // if "tcp" a TCP query will be initiated, otherwise an UDP one
	Attempts     int          // number of attempts
	Retry        bool         // retry with TCP
	Handler      QueryHandler // handler to invoke, dns.DefaultQueryMux if nil
	ReadTimeout  int64        // the net.Conn.SetReadTimeout value for new connections
	WriteTimeout int64        // the net.Conn.SetWriteTimeout value for new connections
}


// Query accepts incoming DNS request,
// Write to in
// creating a new service thread for each.  The service threads
// read requests and then call handler to reply to them.
// Handler is typically nil, in which case the DefaultServeMux is used.
func Query(w chan *Msg, handler QueryHandler) os.Error {
	clnt := &Client{Handler: handler}
	return clnt.Query(w)
}

func (clnt *Client) Query(w chan *Msg) os.Error {
	handler := clnt.Handler
	if handler == nil {
		handler = DefaultQueryMux
	}
	return nil
}

func (clnt *Client) ListenAndQuery(w chan *Msg) os.Error {
	/* ... */
	return nil
}<commit_msg>Compiles again - still not sure about the impl. and API<commit_after>package dns

// A concurrent client implementation. 
// Client sends query to a channel which
// will then handle the query. Returned replys
// are return on another channel. Ready for handling --- same
// setup for server - a HANDLER function that gets run
// when the query returns.

// This completely mirrors server.go impl.
import (
	"os"
	//	"net"
)

type QueryHandler interface {
	QueryDNS(w RequestWriter, q *Msg)
}

// A RequestWriter interface is used by an DNS query handler to
// construct an DNS request.
type RequestWriter interface {
	Write(*Msg)
}

// hijacked connections...?
type reply struct {
	Client *Client
	req    *Msg
}

// QueryMux is an DNS request multiplexer. It matches the
// zone name of each incoming request against a list of 
// registered patterns add calls the handler for the pattern
// that most closely matches the zone name.
type QueryMux struct {
	m map[string]QueryHandler
}

// NewQueryMux allocates and returns a new QueryMux.
func NewQueryMux() *QueryMux { return &QueryMux{make(map[string]QueryHandler)} }

// DefaultQueryMux is the default QueryMux used by Query.
var DefaultQueryMux = NewQueryMux()

func newQueryChan() chan *Msg { return make(chan *Msg) }

// Default channel to use for the resolver
var DefaultReplyChan = newQueryChan()
var DefaultQueryChan = newQueryChan()

// The HandlerQueryFunc type is an adapter to allow the use of
// ordinary functions as DNS query handlers.  If f is a function
// with the appropriate signature, HandlerQueryFunc(f) is a
// QueryHandler object that calls f.
type HandlerQueryFunc func(RequestWriter, *Msg)

// QueryDNS calls f(w, reg)
func (f HandlerQueryFunc) QueryDNS(w RequestWriter, r *Msg) {
	go f(w, r)
}

func HandleQueryFunc(pattern string, handler func(RequestWriter, *Msg)) {
	DefaultQueryMux.HandleQueryFunc(pattern, handler)
}

// Helper handlers
// Todo

// reusing zoneMatch from server.go
func (mux *QueryMux) match(zone string) QueryHandler {
	var h QueryHandler
	var n = 0
	for k, v := range mux.m {
		if !zoneMatch(k, zone) {
			continue
		}
		if h == nil || len(k) > n {
			n = len(k)
			h = v
		}
	}
	return h
}

func (mux *QueryMux) Handle(pattern string, handler QueryHandler) {
	if pattern == "" {
		panic("dns: invalid pattern " + pattern)
	}
	if pattern[len(pattern)-1] != '.' { // no ending .
		mux.m[pattern+"."] = handler
	} else {
		mux.m[pattern] = handler
	}
}

func (mux *QueryMux) HandleQueryFunc(pattern string, handler func(RequestWriter, *Msg)) {
	mux.Handle(pattern, HandlerQueryFunc(handler))
}

func (mux *QueryMux) QueryDNS(w RequestWriter, request *Msg) {
	h := mux.match(request.Question[0].Name)
	if h == nil {
		//                h = RefusedHandler()
		// something else
	}
	h.QueryDNS(w, request)
}

type Client struct {
	Network      string       // if "tcp" a TCP query will be initiated, otherwise an UDP one
	Attempts     int          // number of attempts
	Retry        bool         // retry with TCP
	ChannelQuery chan *Msg    // read DNS request from this channel
	ChannelReply chan *Msg    // read DNS request from this channel
	Handler      QueryHandler // handler to invoke, dns.DefaultQueryMux if nil
	ReadTimeout  int64        // the net.Conn.SetReadTimeout value for new connections
	WriteTimeout int64        // the net.Conn.SetWriteTimeout value for new connections
}


// Query accepts incoming DNS request,
// Write to in
// creating a new service thread for each.  The service threads
// read requests and then call handler to reply to them.
// Handler is typically nil, in which case the DefaultServeMux is used.
func Query(c chan *Msg, handler QueryHandler) os.Error {
	client := &Client{ChannelQuery: c, Handler: handler}
	return client.Query()
}

func (c *Client) Query() os.Error {
	handler := c.Handler
	if handler == nil {
		handler = DefaultQueryMux
	}
forever:
	for {
		select {
		case in := <-c.ChannelQuery:
			w := new(reply)
			w.Client = c
			w.req = in
			handler.QueryDNS(w, w.req)
		}
	}
	return nil
}

func (c *Client) ListenAndQuery() os.Error {
	if c.ChannelQuery == nil {
		c.ChannelQuery = DefaultQueryChan
	}
	if c.ChannelReply == nil {
		c.ChannelReply = DefaultReplyChan
	}
	return c.Query()
}

func (c *Client) Do(m *Msg, addr string) {
	// addr !!!
	if c.ChannelQuery == nil {
		DefaultQueryChan <- m
	}
}

func ListenAndQuery(c chan *Msg, handler QueryHandler) {
	client := &Client{ChannelQuery: c, Handler: handler}
	go client.ListenAndQuery()
}

func (w *reply) Write(m *Msg) {
	// Write to the channel
	w.Client.ChannelReply <- m
}
{'fraction_non_alphanumeric': 0.10221187427240978, 'fraction_numerical': 0.001629802095459837, 'mean_word_length': 3.252970297029703, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, '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': '4058000', 'n_tokens_mistral': 2778, 'n_tokens_neox': 2653, 'n_words': 1008}