2 Created on Mar 19, 2014 6 @link: http://hierarchical-cluster-engine.com/ 7 @copyright: Copyright © 2013-2014 IOIX Ukraine 8 @license: http://hierarchical-cluster-engine.com/license/ 15 from DTMCObjectsSerializator
import DTMCObjectsSerializator
16 from cement.core
import foundation
25 import Constants
as CONSTANTS
33 logger = logging.getLogger(APP_CONSTS.LOGGER_NAME)
39 class DTMC(foundation.CementApp):
42 label = CONSTANTS.APP_NAME
48 foundation.CementApp.__init__(self)
49 self.
config = ConfigParser.ConfigParser()
62 def fillError(self, errorStr, errorCode, isLogging=True):
74 host = str(self.
config.get(CONSTANTS.APP_NAME, CONSTANTS.DTM_HOST))
75 if len(host) > 1
and host[0] ==
"\"":
79 port = str(self.
config.get(CONSTANTS.APP_NAME, CONSTANTS.DTM_PORT))
80 addr = host +
":" + port
82 transport.Consts.TCP_TYPE)
91 ffile = open(fileName,
"r") 94 jsonData = json.loads(data) 97 if task == CONSTANTS.TASKS[0]:
100 eventObj.id = task_id
101 elif task == CONSTANTS.TASKS[1]:
103 elif task == CONSTANTS.TASKS[2]:
105 elif task == CONSTANTS.TASKS[3]:
107 elif task == CONSTANTS.TASKS[4]:
109 elif task == CONSTANTS.TASKS[5]:
111 elif task == CONSTANTS.TASKS[6]:
124 elif task == CONSTANTS.TASKS[0]:
126 elif task == CONSTANTS.TASKS[1]:
128 elif task == CONSTANTS.TASKS[2]:
130 elif task == CONSTANTS.TASKS[3]:
132 elif task == CONSTANTS.TASKS[4]:
134 elif task == CONSTANTS.TASKS[5]:
136 elif task == CONSTANTS.TASKS[6]:
140 self.
errorCode = CONSTANTS.ERROR_UNKNOWN_TASK
141 self.
errorStr = CONSTANTS.ERROR_STR14
143 jsonString = json.dumps(obj)
147 jsonString = obj.toJSON()
157 eventObjClassName = eventObj.__class__.__name__
158 if task == CONSTANTS.TASKS[0]:
160 raise Exceptions.WrongEventObjectTypeException(CONSTANTS.ERROR_STR8.format(task,
"GeneralResponse"))
161 elif task == CONSTANTS.TASKS[1]:
163 raise Exceptions.WrongEventObjectTypeException(CONSTANTS.ERROR_STR8.format(task,
"EEResponseData"))
164 elif task == CONSTANTS.TASKS[2]:
166 raise Exceptions.WrongEventObjectTypeException(CONSTANTS.ERROR_STR8.format(task,
"GeneralResponse"))
167 elif task == CONSTANTS.TASKS[3]:
169 raise Exceptions.WrongEventObjectTypeException(CONSTANTS.ERROR_STR8.format(task,
"EEResponseData"))
170 elif task == CONSTANTS.TASKS[4]:
171 if eventObjClassName != [].__class__.__name__:
172 raise Exceptions.WrongEventObjectTypeException(CONSTANTS.ERROR_STR8.format(task,
"list[]"))
174 for listElement
in eventObj:
176 eventObjClassName = listElement.__class__.__name__
179 raise Exceptions.WrongEventObjectTypeException(CONSTANTS.ERROR_STR8.format(task,
180 "list(TaskManagerFields)[" + str(i) +
"]"))
181 elif task == CONSTANTS.TASKS[5]:
183 raise Exceptions.WrongEventObjectTypeException(CONSTANTS.ERROR_STR8.format(task,
"GeneralResponse"))
184 elif task == CONSTANTS.TASKS[6]:
186 raise Exceptions.WrongEventObjectTypeException(CONSTANTS.ERROR_STR8.format(task,
"AvailableTaskIds"))
188 jsonString = json.dumps(eventObj, default=JsonSerializable.json_serial, sort_keys=
True, indent=4)
191 jsonString = eventObj.toJSON()
192 except UnicodeDecodeError, err:
193 ExceptionLog.handler(logger, err,
"<-------------- DECODE Error ---------------->", (eventObj))
207 timeout = self.
config.get(CONSTANTS.APP_NAME, CONSTANTS.TCP_TIMEOUT_CONFIG_NAME)
208 except ConfigParser.NoSectionError:
209 timeout = CONSTANTS.TCP_TIMEOUT
210 except ConfigParser.NoOptionError:
211 timeout = CONSTANTS.TCP_TIMEOUT
213 if task == CONSTANTS.TASKS[0]:
214 eventType = EVENT_TYPES.NEW_TASK
215 elif task == CONSTANTS.TASKS[1]:
216 eventType = EVENT_TYPES.CHECK_TASK_STATE
217 elif task == CONSTANTS.TASKS[2]:
218 eventType = EVENT_TYPES.DELETE_TASK
219 elif task == CONSTANTS.TASKS[3]:
220 eventType = EVENT_TYPES.FETCH_TASK_RESULTS
221 elif task == CONSTANTS.TASKS[4]:
222 eventType = EVENT_TYPES.GET_TASK_STATUS
223 elif task == CONSTANTS.TASKS[5]:
224 eventType = EVENT_TYPES.DELETE_TASK_RESULTS
225 elif task == CONSTANTS.TASKS[6]:
226 eventType = EVENT_TYPES.FETCH_AVAILABLE_TASK_IDS
231 self.
fillError(CONSTANTS.ERROR_STR7.format(str(timeout)), CONSTANTS.ERROR_NETWORK)
235 ret = retEvent.eventObj
243 if self.pargs.config ==
None:
244 configReadList = self.
config.read(CONSTANTS.DEFAULT_CONFIG_NAME1)
245 if len(configReadList) == 0:
246 configReadList = self.
config.read(CONSTANTS.DEFAULT_CONFIG_NAME2)
248 configReadList = self.
config.read(self.pargs.config)
249 return len(configReadList)
258 logIniFileName = self.
config.get(CONSTANTS.LOG_CONFIG_SECTION_NAME, CONSTANTS.LOG_CONFIG_OPTION_NAME)
259 if logIniFileName !=
None:
260 logging.config.fileConfig(logIniFileName)
261 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
262 self.
errorStr = CONSTANTS.ERROR_STR10
263 self.
errorCode = CONSTANTS.ERROR_LOG_SECTION_ERROR
264 except Exception, err:
265 self.
errorStr = CONSTANTS.ERROR_STR11 +
': ' + str(err)
266 self.
errorCode = CONSTANTS.ERROR_LOG_INIT
273 foundation.CementApp.setup(self)
279 foundation.CementApp.run(self)
281 if '-h' in self.argv
or '--help' in self.argv:
290 if self.
errorCode == CONSTANTS.ERROR_NOERROR:
293 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
294 self.
fillError(CONSTANTS.ERROR_STR13, CONSTANTS.ERROR_CONFIG_SECTION)
296 self.
fillError(CONSTANTS.ERROR_STR12, CONSTANTS.ERROR_CONNECTION)
299 if isHelpArg ==
False and self.pargs.task ==
None:
300 self.
fillError(CONSTANTS.ERROR_STR1, CONSTANTS.ERROR_ARGS1)
301 elif self.pargs.task !=
None:
302 if self.pargs.task
in CONSTANTS.TASKS:
303 if self.pargs.file !=
None:
307 self.
fillError(CONSTANTS.ERROR_STR4, CONSTANTS.ERROR_BAD_FILE_NAME)
309 self.
fillError(CONSTANTS.ERROR_STR5, CONSTANTS.ERROR_BAD_JSON)
310 except Exceptions.DeserilizeException
as excp:
311 self.
fillError(CONSTANTS.ERROR_STR6.format(excp.message), CONSTANTS.ERROR_DTMC)
314 if retEventObj !=
None:
317 except Exceptions.WrongEventObjectTypeException
as excp:
318 self.
fillError(excp.message, CONSTANTS.ERROR_WRONG_RESPONSE)
320 self.
fillError(CONSTANTS.ERROR_STR2, CONSTANTS.ERROR_ARGS2)
322 self.
fillError(CONSTANTS.ERROR_STR3, CONSTANTS.ERROR_BAD_TASK)
324 self.
fillError(CONSTANTS.ERROR_STR12, CONSTANTS.ERROR_CONNECTION)
326 self.
fillError(CONSTANTS.ERROR_STR9, CONSTANTS.ERROR_NO_CONFIG,
False)
330 sys.stdout.write(jsonBuf)
332 logger.info(APP_CONSTS.LOGGER_DELIMITER_LINE)
338 foundation.CementApp.close(self)
def generateEmptyResponse(self, task)
generateEmptyResponse method If here was some critical error, we generate empty response here ...
def __init__(self, connectionBuilder=None)
constructor initialise all class variable and recieve connectionBuilder as param(not mandatory) ...
def setup(self)
setup method Method calls before run application
def loadLogConfigFile(self)
load logging load logging configuration (log file, log level, filters)
GeneralResponse event object, represents general state response for multipurpose usage.
AvailableTaskIds event object, for return all available task id.
def connectionInit(self)
connectionInit method initializes internal variables that containts network connections/communication...
DTMCObjectsSerializator Class contents serialize/deserialize methods for incoming "DTMC" commands...
def configReader(self)
configReader method Method try to read config file by prereared paths, return count of readed configs...
def taskProcessingDeserialize(self, task, fileName, task_id)
taskProcessingDeserialize method Reads task from file and deserializes it.
DTMC Class contents main functional of DTMC application, class inherits from foundation.CementApp.
Class hides routines of bulding connection objects.
def close(self)
close method Method calls after application run
TaskManagerFields event object, for return task fields values.
def transportCommunications(self, task, eventObj)
taskProcessingDeserialize method Method serializes incoming task to the JSON string task - task arg e...
def run(self)
run method Method contains main application functionality
EEResponseData event object, store task results data, returned from EE.
def fillError(self, errorStr, errorCode, isLogging=True)
fillError method calls from error-code point from main processing (...from event handlers) errorStr -...
def taskProcessingSerialize(self, task, eventObj)
taskProcessingDeserialize method Method serializes incoming task to the JSON string task - task arg e...