<commit_before>package netlink
//import "log"
import "bufio"
import "os"
import "fmt"
import "sync"

type Handler struct {
  sock *Socket
  recipients map[uint32]chan Message
  next_seq uint32
  lock sync.Mutex
}

// This is not atomic...
func (self *Handler)Seq()(out uint32){
  self.lock.Lock()
  out = self.next_seq
  self.next_seq++
  self.lock.Unlock()
  return
}

func NewHandler(sock *Socket)(*Handler){
  return &Handler{sock:sock, recipients: map[uint32]chan Message{}, next_seq: 1}
}

func (self *Handler)SendQuery(msg Message, ch chan Message)(err os.Error){
  //log.Printf("Writing Message = %+v", msg.Header)
  if msg.Header.MessageSequence() == 0 {
    msg.Header.SetMessageSequence(self.Seq())
  }
  if msg.Header.MessageSequence() == 0 {
    return os.NewError("Failed to set sequence number")
  }
  var ob []byte
  //log.Printf("msg = %v", msg)
  ob, err = msg.MarshalNetlink(4)
  //log.Printf("OB: %X; ERR: %v", ob, err)
  if err == nil {
    self.recipients[msg.Header.MessageSequence()] = ch
    _, err = self.sock.Write(ob)
  }
  return
}

func (self *Handler)Start(echan chan os.Error){
  r := bufio.NewReader(self.sock)
  for {
    msg, err := ReadMessage(r, 4)
    if err == nil {
      if self.recipients[msg.Header.MessageSequence()] == nil {
        echan <- os.NewError(fmt.Sprintf("GoNetlink: No handler found for sequence number: %d", msg.Header.MessageSequence()))
        continue
      } else {
        switch msg.Header.MessageType() {
          case NLMSG_DONE:
            close(self.recipients[msg.Header.MessageSequence()])
            self.recipients[msg.Header.MessageSequence()] = nil, false
          default: self.recipients[msg.Header.MessageSequence()] <- *msg
        }
      }
    } else {
      echan <- err
    }
  }
  return
}
<commit_msg>Modify handler logic to require less intelligence (will break tools/*)<commit_after>package netlink
//import "log"
import "bufio"
import "os"
import "fmt"
import "sync"

type Handler struct {
  sock *Socket
  recipients map[uint32]chan Message
  next_seq uint32
  lock sync.Mutex
}

// This is not atomic...
func (self *Handler)Seq()(out uint32){
  self.lock.Lock()
  out = self.next_seq
  self.next_seq++
  self.lock.Unlock()
  return
}

func NewHandler(sock *Socket)(*Handler){
  return &Handler{sock:sock, recipients: map[uint32]chan Message{}, next_seq: 1}
}

func (self *Handler)SendQuery(msg Message, ch chan Message)(err os.Error){
  //log.Printf("Writing Message = %+v", msg.Header)
  if msg.Header.MessageSequence() == 0 {
    msg.Header.SetMessageSequence(self.Seq())
  }
  if msg.Header.MessageSequence() == 0 {
    return os.NewError("Failed to set sequence number")
  }
  var ob []byte
  //log.Printf("msg = %v", msg)
  ob, err = msg.MarshalNetlink(4)
  //log.Printf("OB: %X; ERR: %v", ob, err)
  if err == nil {
    self.recipients[msg.Header.MessageSequence()] = ch
    _, err = self.sock.Write(ob)
  }
  return
}

func (self *Handler)Start(echan chan os.Error){
  r := bufio.NewReader(self.sock)
  for {
    msg, err := ReadMessage(r, 4)
    if err == nil {
      if self.recipients[msg.Header.MessageSequence()] == nil {
        echan <- os.NewError(fmt.Sprintf("GoNetlink: No handler found for sequence number: %d", msg.Header.MessageSequence()))
        continue
      } else {
        self.recipients[msg.Header.MessageSequence()] <- *msg
        if msg.Header.MessageFlags() & NLM_F_MULTI == 0 {
            close(self.recipients[msg.Header.MessageSequence()])
            self.recipients[msg.Header.MessageSequence()] = nil, false
        }
      }
    } else {
      echan <- err
    }
  }
  return
}

{'fraction_non_alphanumeric': 0.1480763908109604, 'fraction_numerical': 0.00747301411569333, 'mean_word_length': 3.1444954128440368, 'pattern_counts': {'":': 0, '<': 9, '<?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': '5118938', 'n_tokens_mistral': 1246, 'n_tokens_neox': 1217, 'n_words': 290}