<commit_before>library clean_lock.locker;

import 'dart:io';
import 'dart:async';
import 'package:useful/socket_jsonizer.dart';

class Locker {

  ServerSocket serverSocket;
  List<Socket> clientSockets = [];
  // lockName: [{socket: Socket, requestId: String}]
  Map<String, List<Map> > requestors = {};
  // lockName: {socket: Socket, requestId: String}
  Map<String, Map> currentLock = {};

  Locker.config(this.serverSocket);

  static Future<Locker> bind(url, port) =>
      ServerSocket.bind(url, port)
        .then((ServerSocket sSocket) {
          Locker locker = new Locker.config(sSocket);
          locker.serverSocket.listen(locker.handleClient);
          return locker;
        });

  // Removes given socket from requestors and releases its locks
  _disposeOfSocket(Socket socket) {
    socket.close();
    requestors.forEach((lock, reqList) {
      while (reqList.removeWhere((e) => e["socket"] == socket));
    });
    _removeSocketLocks(socket);
    clientSockets.remove(socket);
    checkLockRequestors();
  }

  _removeSocketLocks(Socket socket) {
    List toRemove = [];
    currentLock.forEach((lock, sct) {
      if (sct["socket"] == socket) toRemove.add(lock);
    });
    toRemove.forEach(currentLock.remove);
  }

  handleClient(Socket socket) {
    clientSockets.add(socket);
    toJsonStream(socket).listen((Map data) {
      if (data["type"] == "lock") handleLockRequest(data["data"], socket);
    }, onDone: () => _disposeOfSocket(socket));
  }

  handleLockRequest(Map req, Socket socket) =>
    (req["action"] == "get" ? _addRequestor : _releaseLock)(req["requestId"], req["lockType"], socket);

  // Adds the socket with additional data to queue for given lockType
  _addRequestor(String requestId, String lockType, Socket socket) {
    if (requestors[lockType] == null) requestors[lockType] = [];
    requestors[lockType].add({"socket" : socket, "requestId": requestId});
    checkLockRequestors();
  }

  _releaseLock(String requestId, String lockType, Socket socket) {
    if (currentLock[lockType]["socket"] == socket) {
      currentLock.remove(lockType);
      writeJSON(socket, {"result":"ok", "action":"release", "requestId":requestId});
      checkLockRequestors();
    } else {<commit_msg>Temporary prints + bug fix.<commit_after>library clean_lock.locker;

import 'dart:io';
import 'dart:async';
import 'package:useful/socket_jsonizer.dart';

class Locker {

  ServerSocket serverSocket;
  List<Socket> clientSockets = [];
  // lockName: [{socket: Socket, requestId: String}]
  Map<String, List<Map> > requestors = {};
  // lockName: {socket: Socket, requestId: String}
  Map<String, Map> currentLock = {};

  Locker.config(this.serverSocket);

  static Future<Locker> bind(url, port) =>
      ServerSocket.bind(url, port)
        .then((ServerSocket sSocket) {
          Locker locker = new Locker.config(sSocket);
          locker.serverSocket.listen(locker.handleClient);
          return locker;
        });

  // Removes given socket from requestors and releases its locks
  _disposeOfSocket(Socket socket) {
    socket.close();
    requestors.forEach((lock, reqList) {
      reqList.removeWhere((e) => e["socket"] == socket);
    });
    _removeSocketLocks(socket);
    clientSockets.remove(socket);
    checkLockRequestors();
  }

  _removeSocketLocks(Socket socket) {
    List toRemove = [];
    currentLock.forEach((lock, sct) {
      if (sct["socket"] == socket) toRemove.add(lock);
    });
    toRemove.forEach(currentLock.remove);
  }

  handleClient(Socket socket) {
    clientSockets.add(socket);
    toJsonStream(socket).listen((Map data) {
      if (data["type"] == "lock") handleLockRequest(data["data"], socket);
    }, onDone: () => _disposeOfSocket(socket));
  }

  handleLockRequest(Map req, Socket socket) =>
    (req["action"] == "get" ? _addRequestor : _releaseLock)(req["requestId"], req["lockType"], socket);

  // Adds the socket with additional data to queue for given lockType
  _addRequestor(String requestId, String lockType, Socket socket) {
    if (requestors[lockType] == null) requestors[lockType] = [];
    print('Adding requestor $requestId for $lockType.');
    requestors[lockType].add({"socket" : socket, "requestId": requestId});
    checkLockRequestors();
  }

  _releaseLock(String requestId, String lockType, Socket socket) {
    if (currentLock[lockType]["socket"] == socket) {
      currentLock.remove(lockType);
      print('Removing requestor $requestId for $lockType');
      writeJSON(socket, {"result":"ok", "action":"release", "requestId":requestId});
      checkLockRequestors();
    } else {
{'fraction_non_alphanumeric': 0.1487693313003703, 'fraction_numerical': 0.0, 'mean_word_length': 4.090909090909091, 'pattern_counts': {'":': 8, '<': 13, '<?xml version=': 0, '>': 21, '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': '755481', 'n_tokens_mistral': 1422, 'n_tokens_neox': 1399, 'n_words': 296}