Module vertx

[hide private]
[frames] | no frames]

# Source Code for Module vertx

      1  # Copyright 2011 the original author or authors.
      2  #
      3  # Licensed under the Apache License, Version 2.0 (the "License");
      4  # you may not use this file except in compliance with the License.
      5  # You may obtain a copy of the License at
      6  #
      7  #      http://www.apache.org/licenses/LICENSE-2.0
      8  #
      9  # Unless required by applicable law or agreed to in writing, software
     10  # distributed under the License is distributed on an "AS IS" BASIS,
     11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12  # See the License for the specific language governing permissions and
     13  # limitations under the License.
     14
     15  """
     16  This module provides the entry point to the vert.x platform
     17  """
     18
     19  import org.vertx.java.deploy.impl.VertxLocator
     20  import org.vertx.java.core.json
     21  import org.vertx.java.deploy.impl
     22
     23  from core.http import HttpServer, HttpClient
     24  from core.net import NetServer, NetClient
     25  from core.sock_js import SockJSServer
     26  from core.handlers import TimerHandler, DoneHandler
     27  from core.javautils import map_to_java, map_from_java
     28
     29  __author__ = "Scott Horn"
     30  __email__ = "scott@hornmicro.com"
     31  __credits__ = "Based entirely on work by Tim Fox http://tfox.org"
     32
     33 -class Vertx(object):
     34      config = None
     35
     36 -def create_http_server(**kwargs):
     37      """ Return a HttpServer """
     38      return HttpServer(**kwargs)
     39
     40 -def create_http_client(**kwargs):
     41      """ Return a HttpClient """
     42      return HttpClient(**kwargs)
     43
     44 -def create_net_server(**kwargs):
     45      """ Return a NetServer """
     46      return NetServer(**kwargs)
     47
     48 -def create_net_client(**kwargs):
     49      """ Return a NetClient """
     50      return NetClient(**kwargs)
     51
     52 -def create_sockjs_server(http_server):
     53      """ Return a SockJSServer """
     54      return SockJSServer(http_server)
     55
     56 -def get_logger():
     57      """ Get the logger for the verticle"""
     58      return org.vertx.java.deploy.impl.VertxLocator.container.getLogger()
     59
     60 -def deploy_verticle(main, config=None, instances=1, handler=None):
     61      """Deploy a verticle. The actual deploy happens asynchronously
     62
     63      Keyword arguments:
     64      @param main: the main of the verticle to deploy
     65      @param config: dict configuration for the verticle
     66      @param instances: number of instances to deploy
     67      @param handler: will be executed when deploy has completed
     68
     69      """
     70      if config != None:
     71          config = org.vertx.java.core.json.JsonObject(map_to_java(config))
     72
     73      org.vertx.java.deploy.impl.VertxLocator.container.deployVerticle(main, config, instances, DoneHandler(handler))
     74
     75 -def deploy_worker_verticle(main, config=None, instances=1, handler=None):
     76      """Deploy a worker verticle. The actual deploy happens asynchronously
     77
     78      Keyword arguments:
     79      @param main: the main of the verticle to deploy
     80      @param config: dict configuration for the verticle
     81      @param instances: the number of instances to deploy
     82      @param handler: handler will be executed when deploy has completed
     83      """
     84      if config != None:
     85          config = org.vertx.java.core.json.JsonObject(map_to_java(config))
     86      org.vertx.java.deploy.impl.VertxLocator.container.deployWorkerVerticle(main, config, instances, DoneHandler(handler))
     87
     88
     89 -def deploy_module(module_name, config=None, instances=1, handler=None):
     90      """Deploy a module. The actual deploy happens asynchronously
     91
     92      Keyword arguments:
     93      @param module_name: The name of the module to deploy
     94      @param config: dict configuration for the module
     95      @param instances: Number of instances to deploy
     96      @param handler: handler will be executed when deploy has completed
     97      """
     98      if config != None:
     99          config = org.vertx.java.core.json.JsonObject(map_to_java(config))
    100      org.vertx.java.deploy.impl.VertxLocator.container.deployModule(module_name, config, instances, DoneHandler(handler))
    101
    102 -def undeploy_verticle(id):
    103      """Undeploy a verticle
    104
    105      Keyword arguments:
    106      @param id: the unique id of the deployment
    107      """
    108      org.vertx.java.deploy.impl.VertxLocator.container.undeployVerticle(id)
    109
    110 -def undeploy_module(id):
    111      """Undeploy a module
    112
    113      Keyword arguments:
    114      @param id: the unique id of the module
    115      """
    116      org.vertx.java.deploy.impl.VertxLocator.container.undeployModule(id)
    117
    118 -def config():
    119      """Get config for the verticle
    120      @return: dict config for the verticle
    121      """
    122      if Vertx.config is None:
    123          Vertx.config = map_from_java(org.vertx.java.deploy.impl.VertxLocator.container.getConfig().toMap())
    124      return Vertx.config
    125
    126 -def java_vertx():
    127      return org.vertx.java.deploy.impl.VertxLocator.vertx
    128
    129 -def set_timer(delay, handler):
    130      """Sets a one-shot timer that will fire after a certain delay.
    131      This method will accept either a Proc or a block.
    132
    133      Keyword arguments:
    134      @param delay: the delay, in milliseconds
    135      @param handler: a block representing the code that will be run after the delay the unique id of the timer
    136      """
    137      java_vertx().setTimer(delay, TimerHandler(handler))
    138
    139 -def set_periodic(delay, handler):
    140      """Sets a periodic timer.
    141
    142      Keyword arguments:
    143      @param delay: the period of the timer, in milliseconds
    144      @param handler: a block representing the code that will be when the timer fires the unique id of the timer
    145      """
    146      java_vertx().setPeriodic(delay, TimerHandler(handler))
    147
    148
    149 -def cancel_timer(id):
    150      """Cancels a timer.
    151
    152      Keyword arguments:
    153      @param id: the id of the timer, as returned from set_timer or set_periodic
    154      @return: true if the timer was cancelled, false if it wasn't found.
    155      """
    156      java_vertx().cancelTimer(id)
    157
    158
    159 -def run_on_loop(handler):
    160      """Put the handler on the event queue for this loop so it will be run asynchronously
    161      ASAP after this event has been processed
    162
    163      Keyword arguments:
    164      @param handler: a block representing the code that will be run ASAP
    165      """
    166      java_vertx().runOnLoop(DoneHandler(handler))
    167