alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (ServerChannel.scala)

This example Scala source code file (ServerChannel.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

abstractserverchannel, abstractserverchannel, channel, channel, int, int, listening, net, network, server, serverchannel, serversocket, socket, socket, string, t

The Scala ServerChannel.scala source code

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

// $Id: ServerChannel.scala 18365 2009-07-21 11:00:42Z michelou $

package scala.remoting

import java.net.{ServerSocket, Socket}

/** <p>
 *    Creates a server channel and binds its associated socket to the
 *    specified port number.<br/>
 *    Example:
 *  </p>
 *  <b>class ComputeChannel(s: Socket) extends Channel(s) {
 *    <b>def receiveFunc = receive[Int => Int]
 *  }
 *  <b>class ComputeServer(p: Int)
 *  <b>extends AbstractServerChannel[ComputeChannel](p) {
 *     <b>def newChannel(s: Socket) = new ComputeChannel(s)
 *  }</pre>
 *
 *  @author Stephane Micheloud
 *  @version 1.0
 */
class ServerChannel(p: Int) extends AbstractServerChannel[Channel](p) {
  def newChannel(s: Socket) = new Channel(s)
}

abstract class AbstractServerChannel[T <: Channel](_port: Int) {

  /** Creates an input channel and binds its associated socket to any
   *  free port.
   */
  def this() = this(0)

  // The maximum queue length for incoming requests to connect is set to 50.
  private val serverSocket = new ServerSocket(_port)

  /** Returns the local address of this channel. */
  val host = serverSocket.getInetAddress.getHostAddress

  /** Returns the port on which this channel is listening. */
  val port = serverSocket.getLocalPort
  info("Listening on port "+port)

  protected def newChannel(socket: Socket): T

  def accept: T = {
    System.gc() // required!
    newChannel(serverSocket.accept)
  }

  def close() {
    try { serverSocket.close() }
    catch { case e: java.io.IOException => }
    info("Server socket "+host+":"+port+" closed")
  }

  protected def info(msg: String) {
    runtime.remoting.Debug.info("[ServerChannel] "+msg)
  }
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala ServerChannel.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.