All Downloads are FREE. Search and download functionalities are using the official Maven repository.

assovary-server_2.9.3.3.4.0.source-code.CassovaryServer.scala Maven / Gradle / Ivy

The newest version!
/*
* Copyright 2014 Twitter, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

import com.twitter.cassovary.graph.GraphUtils.RandomWalkParams
import com.twitter.cassovary.graph.{TestGraphs, DirectedGraph, GraphUtils}
import com.twitter.finagle.builder.ServerBuilder
import com.twitter.finagle.{http, Service}
import com.twitter.finagle.stats.OstrichStatsReceiver
import com.twitter.io.Charsets.Utf8
import com.twitter.logging.Logger
import com.twitter.ostrich.admin.{RuntimeEnvironment, AdminHttpService}
import com.twitter.util.Future
import java.net.InetSocketAddress
import org.jboss.netty.buffer.ChannelBuffers.copiedBuffer
import org.jboss.netty.handler.codec.http._

/*
 * A simple Http server demonstrating the use of Ostrich stats library.
 * The server runs an admin service on localhost:9999 and another
 * service on localhost:8888 that responds every Http request by
 * a walk on a generated graph.
 */
object CassovaryServer {

  val log = Logger.get("CassovaryServer")

  def walkOn(graph: DirectedGraph) {
    val numSteps = 100L * 100L
    val walkParams = RandomWalkParams(numSteps, 0.1, None, Some(2))
    val graphUtils = new GraphUtils(graph)
    log.info("Now doing a random walk of %s steps on a graph with %d nodes and %s edges...\n",
      numSteps, graph.nodeCount, graph.edgeCount)
    graphUtils.calculatePersonalizedReputation(0, walkParams)
    log.info("Done\n")
  }

  def main(args: Array[String]) {

    val service = new Service[HttpRequest, HttpResponse] {
      def apply(req: HttpRequest): Future[HttpResponse] = Future {
        val graph = TestGraphs.generateRandomGraph(100, 0.1)
        walkOn(graph)
        val content = "Finished walk on graph with %d nodes and %s edges\n".format(graph.nodeCount,
          graph.edgeCount)
        val response =
          new DefaultHttpResponse(req.getProtocolVersion, HttpResponseStatus.OK)
        response.setContent(copiedBuffer(content, Utf8))
        response
      }
    }

    // start ostrich and admin server
    val admin = new AdminHttpService(
      9999 /*port*/,
      123 /*backlog*/,
      RuntimeEnvironment(this, args))
    admin.start()

    // start finagle server
    val server = ServerBuilder()
          .codec(http.Http())
          .bindTo(new InetSocketAddress(8888))
          .name("CassovaryServer")
          .reportTo(new OstrichStatsReceiver)
          .build(service)
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy