<commit_before>import dgram from 'dgram';
import shortid from 'shortid';

import * as udpProtocol from './udp-protocol';

class Session{
  constructor(sessionId, caller, callee) {
    this.sessionId = sessionId;
    this.caller = caller;
    this.callee = callee;
  }
}

export default class SessionManager {
  constructor() {
    this.sessionList = {};
    this.serverAddress = {
      address: '',
      port: 0,
    };
    this.udpSocket = dgram.createSocket('udp4');
  }

  init() {
    this.udpSocket.on('message', this.onMessage.bind(this));
    this.udpSocket.on('error', this.onError.bind(this));
    this.udpSocket.bind({
      exclusive: true
    }, () => {
      this.serverAddress.address = this.udpSocket.address().address;
      this.serverAddress.port = this.udpSocket.address().port;
    });
  }

  createSession(caller, callee) {
    const sessionId = shortid();
    callee.sessionId = sessionId;
    caller.sessionId = sessionId;
    this.sessionList[sessionId] = new Session(sessionId, caller, callee);
    return sessionId;
  }

  onMessage(msg, rinfo) {
    const { address, port } = remoteInfo;
    console.log('udp message from ', address, port, message.toString());

    const messageData = udpProtocol.parsePacket(message);
    if (!messageData.valid) {
      console.log('invalid UDP packet received');
      return;
    }

    const sessionId = messageData.payload.sessionId;
    const session = sessionList[sessionId];

    if (!sessionId || !session) {
      console.log('invalid SessionId field');
      return;
    }

    switch (messageData.type) {
      case udpProtocol.packetType.U_SRV_ADDR:
        session.callerInfo = {};
        break;
      default:
        console.log("unknown message type");  
    }
  }

  onError(err) {
    console.log('Session socket error:', err);
  }
}<commit_msg>implement server udp handler<commit_after>import dgram from 'dgram';
import shortid from 'shortid';

import * as udpProtocol from './udp-protocol';

class Session{
  constructor(sessionId, caller, callee) {
    this.sessionId = sessionId;
    this.caller = caller;
    this.callerInfo = {};
    this.callee = callee;
    this.calleeInfo = {};
  }

  updateAddress(connectId, address, port) {
    if (this.caller.id === connectId) {
      this.callerInfo.address = address;
      this.callerInfo.port = port;
    } else if (this.callee.id === connectId){
      this.calleeInfo.address = address;
      this.calleeInfo.port = port;
    } else {
      console.log("unknown connect id");
    }
  }
}

export default class SessionManager {
  constructor() {
    this.sessionList = {};
    this.serverAddress = {
      address: '',
      port: 0,
    };
    this.udpSocket = dgram.createSocket('udp4');
  }

  init() {
    this.udpSocket.on('message', this.onMessage.bind(this));
    this.udpSocket.on('error', this.onError.bind(this));
    this.udpSocket.bind({
      exclusive: true
    }, () => {
      this.serverAddress.address = this.udpSocket.address().address;
      this.serverAddress.port = this.udpSocket.address().port;
    });
  }

  createSession(caller, callee) {
    const sessionId = shortid();
    callee.sessionId = sessionId;
    caller.sessionId = sessionId;
    this.sessionList[sessionId] = new Session(sessionId, caller, callee);
    return sessionId;
  }

  onMessage(msg, rinfo) {
    const { address, port } = remoteInfo;
    console.log('udp message from ', address, port, message.toString());

    const messageData = udpProtocol.parsePacket(message);
    if (!messageData.valid) {
      console.log('invalid UDP packet received');
      return;
    }

    const sessionId = messageData.payload.sessionId;
    const connectId = messageData.payload.connectId;
    const session = sessionList[sessionId];

    if (!sessionId || !session) {
      console.log('invalid SessionId field');
      return;
    }

    switch (messageData.type) {
      case udpProtocol.packetType.U_SRV_ADDR:
        session.updateAddress(connectId, address, port);
        break;
      default:
        console.log("unknown message type");  
    }
  }

  onError(err) {
    console.log('Session socket error:', err);
  }
}
{'fraction_non_alphanumeric': 0.13328512894673417, 'fraction_numerical': 0.0009640877319836105, 'mean_word_length': 2.9865513928914504, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6519525', 'n_tokens_mistral': 1310, 'n_tokens_neox': 1292, 'n_words': 260}