HCE Project Python language Distributed Tasks Manager Application, Distributed Crawler Application and client API bindings.  2.0.0-chaika
Hierarchical Cluster Engine Python language binding
ConnectionBuilder.py
Go to the documentation of this file.
1 '''
2 Created on Feb 4, 2014
3 
4 @author: igor
5 '''
6 
7 
8 import zmq
9 from Connection import Connection
10 import Consts as consts
11 import logging
12 import app.Consts as APP_CONSTS
13 
14 
15 # Logger initialization
16 logger = logging.getLogger(APP_CONSTS.LOGGER_NAME)
17 
18 
19 
21 class ConnectionBuilder(object):
22  '''
23  Factory is used to create various connection objects
24  '''
25 
26 
30  def __init__(self, id_generator):
31  '''
32  Init global variable
33  '''
34  self.zmq_context = zmq.Context() # pylint: disable=E1101
35  self.zmq_poller = zmq.Poller() # pylint: disable-msg=E1101
36  self.id_generator = id_generator
37  self.builders = dict({consts.ADMIN_CONNECT_TYPE : self.__admin_connection,
38  consts.DATA_CONNECT_TYPE : self.__data_connection})
39 
40 
41 
48  def build(self, connect_type, connect_params, endPointType=consts.CLIENT_CONNECT):
49  sock = self.builders[connect_type](connect_params, endPointType)
50  return Connection(sock, self.zmq_poller, endPointType)
51 
52 
53 
59  def __admin_connection(self, connect_params, endPointType):
60  sock = self.__data_connection(connect_params, endPointType)
61  #@todo need set extra params
62  return sock
63 
64 
65 
71  def __data_connection(self, connect_params, endPointType):
72  sock = None
73  addr = "tcp://" + connect_params.host + ":" + str(connect_params.port)
74  if endPointType == consts.CLIENT_CONNECT:
75  sock = self.zmq_context.socket(zmq.DEALER) # pylint: disable=E0602,E1101
76  sock.setsockopt(zmq.IDENTITY, self.id_generator.get_connection_uid()) # pylint: disable=E0602,E1101
77  sock.connect(addr)
78 
79  if endPointType == consts.SERVER_CONNECT:
80  sock = self.zmq_context.socket(zmq.ROUTER) # pylint: disable=E0602,E1101
81  sock.bind(addr)
82 
83  return sock
def build(self, connect_type, connect_params, endPointType=consts.CLIENT_CONNECT)
build a connection
def __init__(self, id_generator)
constructor
def __admin_connection(self, connect_params, endPointType)
helper function
The builder is used to encapsulation routine of creation various type of connections.
def __data_connection(self, connect_params, endPointType)
helper function