void ATextGenerator::generateUniqueId(AOutputBuffer& target, size_t size /* = 32 */) { if (size < 16) ATHROW(NULL, AException::InvalidParameter); ARope rope; size_t x = ATime::getTickCount(); rope.append((const char *)&x, sizeof(size_t)); size_t bytesToAdd = size - sizeof(size_t); while(bytesToAdd >= 4) { x = ARandomNumberGenerator::get(ARandomNumberGenerator::Lecuyer).nextU4(); rope.append((const char *)&x, 4); bytesToAdd -= 4; } while(bytesToAdd > 0) { x = ARandomNumberGenerator::get(ARandomNumberGenerator::Lecuyer).nextU1(); rope.append((const char *)&x, 1); --bytesToAdd; } AASSERT(NULL, !bytesToAdd); AString str(rope.getSize() * 2, 256); ATextConverter::encode64(rope, str); AASSERT(&str, str.getSize() >= size); target.append(str, size); }
AOSContext *AOSContextManager::_newContext(AFile_Socket *pSocket) { AOSContext *pContext = (AOSContext *)m_FreeStore.popFront(); if (!pContext) return new AOSContext(pSocket, m_Services); else { pContext->reset(pSocket); if (pContext->useEventVisitor().isLoggingEvent()) { ARope rope; rope.append(" - ",3); rope.append(pSocket->getSocketInfo().m_address); rope.append(':'); rope.append(AString::fromInt(pSocket->getSocketInfo().m_port)); pContext->useEventVisitor().useName().append(rope); } return pContext; } }
AOSContext::ReturnCode AOSModule_Template::execute(AOSContext& context, const AXmlElement& params) { const AXmlElement *pNode = params.findElement(ASW("template",8)); AAutoPtr<ATemplate> pTemplate(NULL, false); AAutoPtr<AFile> pFile(NULL, false); if (pNode) { //a_Element contains script pTemplate.reset(m_Services.createTemplate(), true); //a_Parse template AFile_AString strfile; pNode->emitContent(strfile); pTemplate->fromAFile(strfile); } else { //a_Filename provided, use the cache pNode = params.findElement(AOS_BaseModules_Constants::FILENAME); if (pNode) { AString relativePath; pNode->emitContent(relativePath); //a_File to be used (may need caching for it, but for now keep it dynamic) AFilename filename(m_Services.useConfiguration().getAosBaseDataDirectory(), true); filename.join(relativePath, false); if (ACacheInterface::NOT_FOUND == m_Services.useCacheManager().getTemplate(context, filename, pTemplate)) { //a_Not found, return error ARope rope; rope.append(getClass()); rope.append(": Unable to find a template file: ",34); rope.append(filename); context.useEventVisitor().startEvent(rope, AEventVisitor::EL_ERROR); return AOSContext::RETURN_ERROR; } } else { context.addError(getClass(), ASWNL("Unable to find module/template nor module/filename, Template module did not execute, params")); return AOSContext::RETURN_ERROR; //a_Did not find either module/template or module/filename } } //a_Process and save output ARope ropeOutput; pTemplate->process(context.useLuaTemplateContext(), ropeOutput); //a_Add template to debug if (context.getDumpContextLevel() > 0) { AString str("debug/",6); str.append(getClass()); str.append("/template",9); AXmlElement& base = context.useModel().addElement(str); pTemplate->emitXml(base); } //a_Insert output into outpath (if any) pNode = params.findElement(AOS_BaseModules_Constants::PATH); if (pNode) { AString xmlpath; pNode->emitContent(xmlpath); if (!xmlpath.isEmpty()) { //a_Add output as CDATA context.useModel().addElement(xmlpath).addData(ropeOutput, AXmlElement::ENC_CDATADIRECT); } } else { context.useEventVisitor().addEvent(ASWNL("Unable to find module/path, output from template discarded"), AEventVisitor::EL_WARN); } return AOSContext::RETURN_OK; }