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: an handler that will be called 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: an handler that will be called 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: an handler that will be called 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, handler=None):
103 """Undeploy a verticle 104 105 Keyword arguments: 106 @param id: the unique id of the deployment 107 @param handler: an handler that will be called when undeploy has completed 108 """ 109 org.vertx.java.deploy.impl.VertxLocator.container.undeployVerticle(id, DoneHandler(handler))
110
111 -def undeploy_module(id, handler=None):
112 """Undeploy a module 113 114 Keyword arguments: 115 @param id: the unique id of the module 116 @param handler: an handler that will be called when undeploy has completed 117 """ 118 org.vertx.java.deploy.impl.VertxLocator.container.undeployModule(id, DoneHandler(handler))
119
120 -def config():
121 """Get config for the verticle 122 @return: dict config for the verticle 123 """ 124 if Vertx.config is None: 125 Vertx.config = map_from_java(org.vertx.java.deploy.impl.VertxLocator.container.getConfig().toMap()) 126 return Vertx.config
127
128 -def java_vertx():
129 return org.vertx.java.deploy.impl.VertxLocator.vertx
130
131 -def set_timer(delay, handler):
132 """Sets a one-shot timer that will fire after a certain delay. 133 134 Keyword arguments: 135 @param delay: the delay, in milliseconds 136 @param handler: an handler that will be called when the timer fires 137 @return: the unique id of the timer 138 """ 139 return java_vertx().setTimer(delay, TimerHandler(handler))
140
141 -def set_periodic(delay, handler):
142 """Sets a periodic timer. 143 144 Keyword arguments: 145 @param delay: the period of the timer, in milliseconds 146 @param handler: an handler that will be called each time the timer fires 147 @return: the unique id of the timer 148 """ 149 return java_vertx().setPeriodic(delay, TimerHandler(handler))
150
151 -def cancel_timer(id):
152 """Cancels a timer. 153 154 Keyword arguments: 155 @param id: the id of the timer, as returned from set_timer or set_periodic 156 @return: true if the timer was cancelled, false if it wasn't found. 157 """ 158 return java_vertx().cancelTimer(id)
159
160 -def run_on_loop(handler):
161 """Put the handler on the event queue for this loop so it will be run asynchronously 162 ASAP after this event has been processed 163 164 Keyword arguments: 165 @param handler: an handler representing the code that will be run ASAP 166 """ 167 java_vertx().runOnLoop(DoneHandler(handler))
168
169 -def exit():
170 """ Cause the container to exit """ 171 org.vertx.java.deploy.impl.VertxLocator.container.exit()
172