<commit_before>/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2005-2007, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// $Id$


package scala.actors.remote


import java.lang.{Thread, SecurityException}
import java.io.{DataInputStream, DataOutputStream, IOException}
import java.net.{InetAddress, ServerSocket, Socket, UnknownHostException}

import compat.Platform

import scala.collection.mutable.HashMap

/* Object TcpService.
 *
 * @version 0.9.8
 * @author <NAME>
 */
object TcpService {
  val random = new java.util.Random(Platform.currentTime)

  def generatePort: int = {
    var portnum = 0
    try {
      portnum = 8000 + random.nextInt(500)
      val socket = new ServerSocket(portnum)
      socket.close()
    }
    catch {
      case ioe: IOException =>
        // this happens when trying to open a socket twice
        // at the same port
        // try again
        generatePort
      case se: SecurityException =>
        // do nothing
    }
    portnum
  }
}

/* Class TcpService.
 *
 * @version 0.9.8
 * @author <NAME>
 */
class TcpService(port: Int) extends Thread with Service {
  val serializer: JavaSerializer = new JavaSerializer(this)

  private val internalNode = new Node(InetAddress.getLocalHost().getHostAddress(), port)
  def node: Node = internalNode

  private val pendingSends = new HashMap[Node, List[Array[byte]]]

  def send(node: Node, data: Array[byte]): unit = synchronized {

    def bufferMsg(t: Throwable) = {
      // buffer message, so that it can be re-sent
      // when remote net kernel comes up
      pendingSends.get(node) match {
        case None =>
          pendingSends += node -> (data :: Nil)
        case Some(msgs) =>
          pendingSends += node -> (data :: msgs)<commit_msg>Fixed possible memory leak (bug 668).<commit_after>/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2005-2007, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// $Id$


package scala.actors.remote


import java.lang.{Thread, SecurityException}
import java.io.{DataInputStream, DataOutputStream, IOException}
import java.net.{InetAddress, ServerSocket, Socket, UnknownHostException}

import compat.Platform

import scala.collection.mutable.HashMap

/* Object TcpService.
 *
 * @version 0.9.8
 * @author <NAME>
 */
object TcpService {
  val random = new java.util.Random(Platform.currentTime)

  def generatePort: int = {
    var portnum = 0
    try {
      portnum = 8000 + random.nextInt(500)
      val socket = new ServerSocket(portnum)
      socket.close()
    }
    catch {
      case ioe: IOException =>
        // this happens when trying to open a socket twice
        // at the same port
        // try again
        generatePort
      case se: SecurityException =>
        // do nothing
    }
    portnum
  }

  var BufSize: int = 65536
}

/* Class TcpService.
 *
 * @version 0.9.8
 * @author <NAME>
 */
class TcpService(port: Int) extends Thread with Service {
  val serializer: JavaSerializer = new JavaSerializer(this)

  private val internalNode = new Node(InetAddress.getLocalHost().getHostAddress(), port)
  def node: Node = internalNode

  private val pendingSends = new HashMap[Node, List[Array[byte]]]

  /**
   * Sends a byte array to another node on the network.
   * If the node is not yet up, up to <code>TcpService.BufSize</code>
   * messages are buffered.
   */
  def send(node: Node, data: Array[byte]): unit = synchronized {

    def bufferMsg(t: Throwable) = {
      // buffer message, so that it can be re-sent
      // when remote net kernel comes up
      pendingSends.get(node) match {
        case None =>
          pendingSends += node -> (data :: Nil)
        case Some(msgs) if msgs.length < TcpService.BufSize =>
          pendingSends += node -> (data :: msgs)
{'fraction_non_alphanumeric': 0.11429195804195805, 'fraction_numerical': 0.011363636363636364, 'mean_word_length': 1.6380403458213257, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5555827', 'n_tokens_mistral': 1333, 'n_tokens_neox': 1261, 'n_words': 336}