<commit_before>(function(exports){
  
  function LookupService(log, peerCons, handleConFn, id, peerJSopts){
    if(!(this instanceof LookupService))
      return new LookupService(log, peerCons, handleConFn, id, peerJSopts);
    this.log = log;
    this.peerCons = peerCons;
    this.connections = {};
    this.gossipMsgsToSend = {};
    this.waitingPathFor = {};
    this.discoveredPaths = {};
    this.handleConnection = handleConFn;
    //Peer.id (local ID)
    this.id = id;
    this.opts = peerJSopts;
    this.iceCandidateSent = {};
    //called like that for being compatible with PeerJS
    this.socket = {};
    this.socket.send = this.send;
    this.targetReachedMsgs = {}
  }
  
  LookupService.prototype.apply = function(msg){
    var target = msg.receiver;
    this.iceCandidateSent[target] = false;
    this.waitingPathFor[target] = true;
    if(!this.gossipMsgsToSend[target]){ this.gossipMsgsToSend[target] = []; }
    this.gossipMsgsToSend[target].push(msg);
    var dc = new DataConnection(target, this);
    this.connections[target] = dc;
    
    var self = this;
    dc.on('open', function(){
      var gossipMsg = self.gossipMsgsToSend[dc.peer].pop();
      if(gossipMsg){
        self.log.info('Sending gossip msg: ' + JSON.stringify(gossipMsg));
        dc.send(gossipMsg);
      }else
        self.log.error('Void entry at LookupService.gossipMsgsToSend for peer: ' + dc.peer);
    });
    
    dc.on('error', function(e){
      self.log.error('In connection between ' + self.id + ' and ' + dc.peer);
    });
  };
  
  LookupService.prototype.dispatch = function(msg){
    var con;
    switch(msg.type){
      case 'REQ':
        msg.path.push(this.id);
        msg.steps++;
        if(msg.target === this.id){
          if(!this.targetReachedMsgs[msg.target]){ this.targetReachedMsgs[msg.target] = []; }
          this.targetReachedMsgs[msg.target].push({ path: msg.path, steps: msg.steps });
          this.inOfferReception(msg);
        }else
          this.broadcast(msg);
        break;
      case 'ANSW':
        msg.steps--;
        if(msg.emitter === id){
          if(this.waitingPathFor[msg.target]){
            delete this.waitingPathFor[msg.target];
            this.discoveredPaths[msg.target] = msg.path;
            this.doNegotiation(msg.target);
          }
          //here the request is igonered because the target was already removed from
          //this.waitingPathFor. This is actually the end of the lookup.
          //TODO maintaining different paths for the same target could be better
          //due to peers dinamicity; in the near future LookupService will could into
          //a DHT or another P2P data structure
        }else{
          con = this.getConnection(msg.path[msg.steps]);
          if(con)
            con.send(msg);
          else
            this.log.error('Lookup, ANSW transmition to emitter is lost. Msg: ' + JSON.stringify(msg));
        }
        break;
      default:
        this.log.warn('Lookup msg is not recognized. Msg: ' + JSON.stringify(msg));
        break;
    }
  };
  
  LookupService.prototype.inOfferReception = function(msg){
    var payload = msg.payload;
    var dc = new DataConnection(msg.emitter, this, {
      connectionId: payload.connectionId,
      _payload: payload,
      metadata: payload.metadata,
      label: payload.label,
      serialization: payload.serialization,
      reliable: payload.reliable
    });
    this.connections[msg.emitter] = dc;
    this.iceCandidateSent[msg.emitter] = false;
    this.handleConnection(dc);
  };
  
  LookupService.prototype.send = function(msg){
    this.log.info('Provider.sed was called with msg: ' + JSON.stringify(msg));
    var outMsg = {};
    switch(msg.type){
      case 'OFFER':
        outMsg.header = 'LOOKUP';
        outMsg.type = 'REQ';
        outMsg.path = [this.id];
        outMsg.steps = 0;
        outMsg.target = msg.dst;
        outMsg.emitter = this.id;
        outMsg.payload = msg.payload;
        this.log.info('LookupService.broadcast with msg: ' + JSON.stringify(outMsg));
        this.broadcast(outMsg);
        break;
      case 'ANSWER':
        //var loUpMsg = this.inOfferReception 
        //got from peerjs._makeAnswer
        //type: 'ANSWER',
        //payload: {
        //  sdp: answer,
        //  type: connection.type,
        //  connectionId: connection.id,
        //  browser: util.browser
        //},
        //dst: connection.peer
        //TODO stock outgoing msgs for later
       outMsg.type = 'ANSW';
          msg.steps--;
          con = this.getConnection(msg.path[msg.steps]);
          if(con)
            con.send({});
          else
            this.log.error('LookUp, first ANSW to emitter is lost. Msg: ' + JSON.stringify(msg)); 
        break;
      default:
        break;
    }
  };
  
  LookupService.prototype.getConnection = function(peerId){<commit_msg>WIP, NonStable: taking rid of signaling server<commit_after>(function(exports){
  
  function LookupService(log, peerCons, handleConFn, id, peerJSopts){
    if(!(this instanceof LookupService))
      return new LookupService(log, peerCons, handleConFn, id, peerJSopts);
    this.log = log;
    this.peerCons = peerCons;
    this.connections = {};
    this.gossipMsgsToSend = {};
    this.waitingPathFor = {};
    this.discoveredPaths = {};
    this.handleConnection = handleConFn;
    //Peer.id (local ID)
    this.id = id;
    this.opts = peerJSopts;
    this.iceCandidateSent = {};
    //called like that for being compatible with PeerJS
    this.socket = {};
    this.socket.send = this.send;
    this.targetReachedMsgs = {}
  }
  
  LookupService.prototype.apply = function(msg){
    var target = msg.receiver;
    this.iceCandidateSent[target] = false;
    this.waitingPathFor[target] = true;
    if(!this.gossipMsgsToSend[target]){ this.gossipMsgsToSend[target] = []; }
    this.gossipMsgsToSend[target].push(msg);
    var dc = new DataConnection(target, this);
    this.connections[target] = dc;
    
    var self = this;
    dc.on('open', function(){
      var gossipMsg = self.gossipMsgsToSend[dc.peer].pop();
      if(gossipMsg){
        self.log.info('Sending gossip msg: ' + JSON.stringify(gossipMsg));
        dc.send(gossipMsg);
      }else
        self.log.error('Void entry at LookupService.gossipMsgsToSend for peer: ' + dc.peer);
    });
    
    dc.on('error', function(e){
      self.log.error('In connection between ' + self.id + ' and ' + dc.peer);
    });
  };
  
  LookupService.prototype.dispatch = function(msg){
    var con;
    switch(msg.type){
      case 'REQ':
        msg.path.push(this.id);
        msg.steps++;
        if(msg.target === this.id){
          msg.steps--;
          if(!this.targetReachedMsgs[msg.emitter]){ this.targetReachedMsgs[msg.emitter] = []; }
          this.targetReachedMsgs[msg.emitter].push({ path: msg.path, steps: msg.steps });
          this.inOfferReception(msg);
        }else
          this.broadcast(msg);
        break;
      case 'ANSW':
        msg.steps--;
        if(msg.emitter === id){
          if(this.waitingPathFor[msg.target]){
            delete this.waitingPathFor[msg.target];
            this.discoveredPaths[msg.target] = msg.path;
            this.doNegotiation(msg.target);
          }
          //here the request is igonered because the target was already removed from
          //this.waitingPathFor. This is actually the end of the lookup.
          //TODO maintaining different paths for the same target could be better
          //due to peers dinamicity; in the near future LookupService will could into
          //a DHT or another P2P data structure
        }else{
          con = this.getConnection(msg.path[msg.steps]);
          if(con)
            con.send(msg);
          else
            this.log.error('Lookup, ANSW transmition to emitter is lost. Msg: ' + JSON.stringify(msg));
        }
        break;
      default:
        this.log.warn('Lookup msg is not recognized. Msg: ' + JSON.stringify(msg));
        break;
    }
  };
  
  LookupService.prototype.inOfferReception = function(msg){
    var payload = msg.payload;
    var dc = new DataConnection(msg.emitter, this, {
      connectionId: payload.connectionId,
      _payload: payload,
      metadata: payload.metadata,
      label: payload.label,
      serialization: payload.serialization,
      reliable: payload.reliable
    });
    this.connections[msg.emitter] = dc;
    this.iceCandidateSent[msg.emitter] = false;
    this.handleConnection(dc);
  };
 
  LookupService.prototype.createLoUpMsg = function(type, path, steps, target, payload){
    return{
      header: 'LOOKUP', 'type': type, 'path': path,
      'steps': steps, 'target': target, emitter: this.id,
      'payload': payload
    };
  };
  
  LookupService.prototype.send = function(msg){
    this.log.info('Provider.sed was called with msg: ' + JSON.stringify(msg));
    var outMsg;
    switch(msg.type){
      case 'OFFER':
        outMsg = this.createLoUpMsg('REQ', [this.id], 0, msg.dst, msg.payload);
        this.log.info('LookupService.broadcast with msg: ' + JSON.stringify(outMsg));
        this.broadcast(outMsg);
        break;
      case 'ANSWER':
        var loUpMsg = this.targetReachedMsgs[msg.dst].pop();
        if(loUpMsg){
          outMsg = this.createLoUpMsg('ANSW', loUpMsg.path, loUpMsg.steps, msg.dst, msg.payload);
          var connection = this.getConnection( loUpMsg.path[loUpMsg.steps] );
          if(connection)
            connection.send(outMsg);
          else{
            this.log.error('Link between ' + this.id + ' and ' + loUpMsg.path[loUpMsg.steps]+
              'is broken. Answer is lost.');
          }
        }else
          this.log.error('Entry for emitter: ' + msg.dst + ' is lost, why?');
        break;
      default:
        break;
    }
  };
  
  LookupService.prototype.getConnection = function(peerId){
{'fraction_non_alphanumeric': 0.13007305194805194, 'fraction_numerical': 0.00040584415584415587, 'mean_word_length': 2.3653123933082965, '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': '6582619', 'n_tokens_mistral': 3067, 'n_tokens_neox': 3019, 'n_words': 640}