<commit_before>part of distributed_dart;

/**
  * Sendport Proxy
  * Reference to a specific [LocalIsolate].
  * To send a [SendPort] to a remote host, it must be wrapped in a
  * [LocalIsolate] and sent as a [RemoteSendPort]
  */
class RemoteSendPort {
  final IsolateId id;
  final IsolateNode node;

  RemoteSendPort(this.id): node = IsolateNode.localhost;

  RemoteSendPort.fromMap(Map m):
    id = new IsolateId.fromMap(m['id']),
    node = new IsolateNode.fromMap(m['node']);

  void send(dynamic data, reply){
    var request = null; // TODO: new IsolateDataRequest(data)
    new Network.fromNode(node).send(request);
  }
  
  Future call(dynamic data){
    var c = new Completer();
    //new Network.fromNode(node).send(new IsolateDataRequest());
    return c.future;
  }

  /// stream interface
  void add(dynamic data){
    send(data,null);
  }
}

/**
  * unique ID to distinguish isolates
  */
class IsolateId {
  static int nextid = 0;

  final int id;
  final int timestamp;
  final int port;
  final String host;

  IsolateId():
   id = nextid++,
   timestamp = new DateTime.now().millisecondsSinceEpoch,
   port = IsolateNode.localhost.port,
   host = IsolateNode.localhost.host;

  IsolateId.fromMap(Map m):
    id = m['id'], 
    timestamp = m['timestamp'], 
    port = m['port'],
    host = m['host'];
  
  Map toJson(){
   var obj = { 
     'id' : id,
     'timestamp': timestamp,
     'port': port, 
     'host' : host 
   }; 
   return obj;
  }

  String toString() => "$host:$id:$timestamp";
}

/**
  * Lookup table, contains reference to all physical isolates spawned on the
  * current Node.
  * 
  * The table contains a mapping between an [IsolateId] and a [SendPort]
  * [SendPort]'s can be created manually, and bound to a [LocalIsolate]
  * object, or it can be created directly in via a [LocalIsolate] constructor.
  */
class LocalIsolate{
  final IsolateId id = new IsolateId();
  final SendPort sendport;
  
  /// [LocalIsolate] lookup table, key is (string) IsolateId
  static Map<String, LocalIsolate> _isolatemap = {};
  RemoteSendPort toRemoteSendPort() => new RemoteSendPort(id);

  /** 
    * Search for [LocalIsolate] instance
    * returns null if isolate does not exist.
    */
  static LocalIsolate Lookup(IsolateId id){
    var key = id.toString();
    if( _isolatemap.containsKey(key))
      return _isolatemap[key];
  }

  /// spawn a new isolate
  factory LocalIsolate.Spawn(String uri){
    var sp = spawnUri(uri);
    return new LocalIsolate.BindSendPort(sp); 
  }

  /// bind a new sendport to the [LocalIsolate] lookup table
  LocalIsolate.BindSendPort(this.sendport){
    _isolatemap[id.toString()] = this; 
  }
}

/**
  * Mapping between [IsolateId] and [RemoteSendPort]
  */
class RemoteIsolate {
  /// map: (string) IsolateId -> RemoteSendPort
  static Map<String,RemoteSendPort> _remoteports = {};

  static RemoteSendPort Lookup(IsolateId id) {
    if( _remoteports.containsKey(id.toString()))
        return _remoteports[id.toString()];
  }

  static Future<IsolateId> Spawn(String uri){
    Future<DartCode> dc = DartCode.resolve(uri);
    return new Future.value(new IsolateId()); // TODO: get from remote 
  }
}


// PUBLIC API //////////////////////////////////////////////////////////////////////
/**<commit_msg>rename method<commit_after>part of distributed_dart;

/**
  * Sendport Proxy
  * Reference to a specific [LocalIsolate].
  * To send a [SendPort] to a remote host, it must be wrapped in a
  * [LocalIsolate] and sent as a [RemoteSendPort]
  */
class RemoteSendPort {
  final IsolateId id;
  final IsolateNode node;

  RemoteSendPort(this.id): node = IsolateNode.localhost;

  RemoteSendPort.fromMap(Map m):
    id = new IsolateId.fromMap(m['id']),
    node = new IsolateNode.fromMap(m['node']);

  void send(dynamic data, reply){
    var request = null; // TODO: new IsolateDataRequest(data)
    new Network.fromNode(node).send(request);
  }
  
  Future call(dynamic data){
    var c = new Completer();
    //new Network.fromNode(node).send(new IsolateDataRequest());
    return c.future;
  }

  /// stream interface
  void add(dynamic data){
    send(data,null);
  }
}

/**
  * unique ID to distinguish isolates
  */
class IsolateId {
  static int nextid = 0;

  final int id;
  final int timestamp;
  final int port;
  final String host;

  IsolateId():
   id = nextid++,
   timestamp = new DateTime.now().millisecondsSinceEpoch,
   port = IsolateNode.localhost.port,
   host = IsolateNode.localhost.host;

  IsolateId.fromMap(Map m):
    id = m['id'], 
    timestamp = m['timestamp'], 
    port = m['port'],
    host = m['host'];
  
  Map toJson(){
   var obj = { 
     'id' : id,
     'timestamp': timestamp,
     'port': port, 
     'host' : host 
   }; 
   return obj;
  }

  String toString() => "$host:$id:$timestamp";
}

/**
  * Lookup table, contains reference to all physical isolates spawned on the
  * current Node.
  * 
  * The table contains a mapping between an [IsolateId] and a [SendPort]
  * [SendPort]'s can be created manually, and bound to a [LocalIsolate]
  * object, or it can be created directly in via a [LocalIsolate] constructor.
  */
class LocalIsolate{
  final IsolateId id = new IsolateId();
  final SendPort sendport;
  
  /// [LocalIsolate] lookup table, key is (string) IsolateId
  static Map<String, LocalIsolate> _isolatemap = {};
  RemoteSendPort toRemoteSendPort() => new RemoteSendPort(id);

  /** 
    * Search for [LocalIsolate] instance
    * returns null if isolate does not exist.
    */
  static LocalIsolate Lookup(IsolateId id){
    var key = id.toString();
    if( _isolatemap.containsKey(key))
      return _isolatemap[key];
  }

  /// spawn a new isolate
  factory LocalIsolate.spawn(String uri){
    var sp = spawnUri(uri);
    return new LocalIsolate.fromSendPort(sp); 
  }

  /// bind a new sendport to the [LocalIsolate] lookup table
  LocalIsolate.fromSendPort(this.sendport){
    _isolatemap[id.toString()] = this; 
  }
}

/**
  * Mapping between [IsolateId] and [RemoteSendPort]
  */
class RemoteIsolate {
  /// map: (string) IsolateId -> RemoteSendPort
  static Map<String,RemoteSendPort> _remoteports = {};

  static RemoteSendPort Lookup(IsolateId id) {
    if( _remoteports.containsKey(id.toString()))
        return _remoteports[id.toString()];
  }

  static Future<IsolateId> Spawn(String uri){
    Future<DartCode> dc = DartCode.resolve(uri);
    return new Future.value(new IsolateId()); // TODO: get from remote 
  }
}


// PUBLIC API //////////////////////////////////////////////////////////////////////
/**
{'fraction_non_alphanumeric': 0.14202810018326206, 'fraction_numerical': 0.00030543677458766036, 'mean_word_length': 3.0128676470588234, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, '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': '1440029', 'n_tokens_mistral': 2134, 'n_tokens_neox': 2071, 'n_words': 593}