Пример #1
0
static void s_Repr_Value(CNcbiOstrstream& oss, const CJsonNode& node)
{
    switch (node.GetNodeType()) {
    case CJsonNode::eObject:
        oss << '{';
        s_Repr_Object(oss, node);
        oss << '}';
        break;
    case CJsonNode::eArray:
        oss << '[';
        s_Repr_Array(oss, node);
        oss << ']';
        break;
    case CJsonNode::eString:
        oss << '"' << NStr::PrintableString(node.AsString()) << '"';
        break;
    case CJsonNode::eInteger:
        oss << node.AsInteger();
        break;
    case CJsonNode::eDouble:
        oss << node.AsDouble();
        break;
    case CJsonNode::eBoolean:
        oss << (node.AsBoolean() ? "true" : "false");
        break;
    default: /* case CJsonNode::eNull: */
        oss << "null";
    }
}
Пример #2
0
void g_DetectTypeAndSet(CJsonNode& node,
        const CTempString& key, const CTempString& value)
{
    if (IsInteger(value))
        node.SetNumber(key, NStr::StringToInt8(value));
    else if (NStr::CompareNocase(value, "FALSE") == 0)
        node.SetBoolean(key, false);
    else if (NStr::CompareNocase(value, "TRUE") == 0)
        node.SetBoolean(key, true);
    else if (NStr::CompareNocase(value, "NONE") == 0)
        node.SetNull(key);
    else
        node.SetString(key, UnquoteIfQuoted(value));
}
Пример #3
0
bool SNetCacheService::Call(const string& method,
        CArgArray& arg_array, CJsonNode& reply)
{
    if (method == "get_blob") {
        reply.AppendInteger(m_AutomationProc->AddObject(
                TAutomationObjectRef(new SNetCacheBlob(this,
                        arg_array.NextString(kEmptyStr)))));
    } else if (method == "get_servers") {
        CJsonNode object_ids(CJsonNode::NewArrayNode());
        for (CNetServiceIterator it = m_NetCacheAPI.GetService().Iterate(
                CNetService::eIncludePenalized); it; ++it)
            object_ids.AppendInteger(m_AutomationProc->
                    ReturnNetCacheServerObject(m_NetCacheAPI, *it)->GetID());
        reply.Append(object_ids);
    } else
        return SNetService::Call(method, arg_array, reply);

    return true;
}
Пример #4
0
static void s_Repr_Array(CNcbiOstrstream& oss, const CJsonNode& node)
{
    CJsonIterator it = node.Iterate();
    if (it) {
        s_Repr_Value(oss, *it);
        while (++it) {
            oss << ", ";
            s_Repr_Value(oss, *it);
        }
    }
}
Пример #5
0
static void s_Repr_Object(CNcbiOstrstream& oss, const CJsonNode& node)
{
    CJsonIterator it = node.Iterate();
    if (it) {
        oss << '"' << it.GetKey() << "\": ";
        s_Repr_Value(oss, *it);
        while (++it) {
            oss << ", \"" << it.GetKey() << "\": ";
            s_Repr_Value(oss, *it);
        }
    }
}
Пример #6
0
bool g_FixMisplacedPID(CJsonNode& stat_info, CTempString& executable_path,
        const char* pid_key)
{
    SIZE_TYPE misplaced_pid = NStr::Find(executable_path, "; PID: ");
    if (misplaced_pid == NPOS)
        return false;

    SIZE_TYPE pos = misplaced_pid + sizeof("; PID: ") - 1;
    stat_info.SetInteger(pid_key, NStr::StringToInt8(
            CTempString(executable_path.data() + pos,
                    executable_path.length() - pos)));
    executable_path.erase(misplaced_pid);
    return true;
}
Пример #7
0
void SNetStorageRPC::x_SetStorageFlags(CJsonNode& node, TNetStorageFlags flags)
{
    CJsonNode storage_flags(CJsonNode::NewObjectNode());

    if (flags & fNST_Fast)
        storage_flags.SetBoolean("Fast", true);
    if (flags & fNST_Persistent)
        storage_flags.SetBoolean("Persistent", true);
    if (flags & fNST_NetCache)
        storage_flags.SetBoolean("NetCache", true);
    if (flags & fNST_FileTrack)
        storage_flags.SetBoolean("FileTrack", true);
    if (flags & fNST_Movable)
        storage_flags.SetBoolean("Movable", true);
    if (flags & fNST_Cacheable)
        storage_flags.SetBoolean("Cacheable", true);
    if (flags & fNST_NoMetaData)
        storage_flags.SetBoolean("NoMetaData", true);

    node.SetByKey("StorageFlags", storage_flags);
}
Пример #8
0
bool CJsonOverUTTPWriter::SendNode(const CJsonNode& node)
{
    switch (node.GetNodeType()) {
    case CJsonNode::eObject:
        m_OutputStack.push_back(m_CurrentOutputNode);
        m_CurrentOutputNode.m_Node = node;
        m_CurrentOutputNode.m_ObjectIterator = node.GetObject().begin();
        m_SendHashValue = false;
        if (!m_UTTPWriter.SendControlSymbol('{'))
            return false;
        break;

    case CJsonNode::eArray:
        m_OutputStack.push_back(m_CurrentOutputNode);
        m_CurrentOutputNode.m_Node = node;
        m_CurrentOutputNode.m_ArrayIterator = node.GetArray().begin();
        if (!m_UTTPWriter.SendControlSymbol('['))
            return false;
        break;

    case CJsonNode::eString:
        {
            string str(node.GetString());
            if (!m_UTTPWriter.SendChunk(str.data(), str.length(), false))
                return false;
        }
        break;

    case CJsonNode::eNumber:
        if (!m_UTTPWriter.SendNumber(node.GetNumber()))
            return false;
        break;

    case CJsonNode::eBoolean:
        if (!m_UTTPWriter.SendControlSymbol(node.GetBoolean() ? 'Y' : 'N'))
            return false;
        break;

    case CJsonNode::eNull:
        if (!m_UTTPWriter.SendControlSymbol('U'))
            return false;
    }

    return true;
}
Пример #9
0
bool CJsonOverUTTPWriter::x_SendNode(const CJsonNode& node)
{
    switch (node.GetNodeType()) {
    case CJsonNode::eObject:
        x_PushNode(node);
        m_CurrentOutputNode.m_Iterator = node.Iterate();
        m_SendHashValue = false;
        return m_UTTPWriter.SendControlSymbol('{');

    case CJsonNode::eArray:
        x_PushNode(node);
        m_CurrentOutputNode.m_Iterator = node.Iterate();
        return m_UTTPWriter.SendControlSymbol('[');

    case CJsonNode::eString:
        {
            const string& str(static_cast<const SJsonStringNodeImpl*>(
                (const SJsonNodeImpl*) node)->m_String);
            return m_UTTPWriter.SendChunk(str.data(), str.length(), false);
        }

    case CJsonNode::eInteger:
        return m_UTTPWriter.SendNumber(node.AsInteger());

    case CJsonNode::eDouble:
        m_Double = node.AsDouble();
        if (!m_UTTPWriter.SendControlSymbol(DOUBLE_PREFIX)) {
            x_PushNode(node);
            return false;
        }
        return m_UTTPWriter.SendRawData(&m_Double, sizeof(m_Double));

    case CJsonNode::eBoolean:
        return m_UTTPWriter.SendControlSymbol(node.AsBoolean() ? 'Y' : 'N');

    default: /* case CJsonNode::eNull: */
        return m_UTTPWriter.SendControlSymbol('U');
    }
}
Пример #10
0
 SFlattenIterator(const CJsonNode& container)
 {
     m_CurrentFrame.m_Iterator = container.Iterate();
     m_CurrentFrame.m_Index = container.IsObject() ? (size_t) -1 : 0;
     x_DepthFirstSearchForScalar();
 }
Пример #11
0
static void s_TrapErrors(const CJsonNode& request,
        const CJsonNode& reply, CSocket& sock,
        SNetStorage::SConfig::EErrMode err_mode)
{
    const string server_address(sock.GetPeerAddress());
    CJsonNode issues(reply.GetByKeyOrNull("Warnings"));

    if (issues) {
        for (CJsonIterator it = issues.Iterate(); it; ++it) {
            const SIssue issue(*it);
            LOG_POST(Warning << "NetStorage server " << server_address <<
                    " issued warning " << issue);
        }
    }

    const string status = reply.GetString("Status");
    const bool status_ok = status == "OK";
    issues = reply.GetByKeyOrNull("Errors");

    // Got errors
    if (!status_ok || issues) {
        if (status_ok && err_mode != SNetStorage::SConfig::eThrow) {
            if (err_mode == SNetStorage::SConfig::eLog) {
                for (CJsonIterator it = issues.Iterate(); it; ++it) {
                    const SIssue issue(*it);
                    LOG_POST(Error << "NetStorage server " << server_address <<
                            " issued error " << issue);
                }
            }
        } else {
            Int8 code = CNetStorageServerError::eUnknownError;
            Int8 sub_code = SIssue::kEmptySubCode;
            ostringstream errors;

            if (!issues)
                errors << status;
            else {
                const char* prefix = "error ";

                for (CJsonIterator it = issues.Iterate(); it; ++it) {
                    const SIssue issue(*it);
                    code = issue.code;
                    sub_code = issue.sub_code;
                    errors << prefix << issue;
                    prefix = ", error ";
                }
            }

            string err_msg = FORMAT("Error while executing " <<
                            request.GetString("Type") << " "
                    "on NetStorage server " <<
                            sock.GetPeerAddress() << ". "
                    "Server returned " << errors.str());

            s_ThrowError(code, sub_code, err_msg);
        }
    }

    if (reply.GetInteger("RE") != request.GetInteger("SN")) {
        NCBI_THROW_FMT(CNetStorageException, eServerError,
                "Message serial number mismatch "
                "(NetStorage server: " << sock.GetPeerAddress() << "; "
                "request: " << request.Repr() << "; "
                "reply: " << reply.Repr() << ").");
    }
}
Пример #12
0
bool SNetCacheBlob::Call(const string& method,
        CArgArray& arg_array, CJsonNode& reply)
{
    if (method == "write") {
        string value(arg_array.NextString());
        if (m_Reader.get() != NULL) {
            NCBI_THROW(CAutomationException, eCommandProcessingError,
                    "Cannot write blob while it's being read");
        } else if (m_Writer.get() == NULL) {
            bool return_key = m_BlobKey.empty();
            m_Writer.reset(m_NetCacheObject->m_NetCacheAPI.PutData(&m_BlobKey));
            if (return_key)
                reply.AppendString(m_BlobKey);
        }
        m_Writer->Write(value.data(), value.length());
    } else if (method == "read") {
        if (m_Writer.get() != NULL) {
            NCBI_THROW(CAutomationException, eCommandProcessingError,
                    "Cannot read blob while it's being written");
        } else if (m_Reader.get() == NULL)
            m_Reader.reset(m_NetCacheObject->m_NetCacheAPI.GetReader(
                    m_BlobKey, &m_BlobSize));
        size_t buf_size = (size_t) arg_array.NextInteger(-1);
        if (buf_size > m_BlobSize)
            buf_size = m_BlobSize;
        string buffer(buf_size, 0);
        char* buf_ptr = &buffer[0];
        size_t bytes_read;
        size_t total_bytes_read = 0;

        while (buf_size > 0) {
            ERW_Result rw_res = m_Reader->Read(buf_ptr, buf_size, &bytes_read);
            if (rw_res == eRW_Success) {
                total_bytes_read += bytes_read;
                buf_ptr += bytes_read;
                buf_size -= bytes_read;
            } else if (rw_res == eRW_Eof) {
                break;
            } else {
                NCBI_THROW_FMT(CAutomationException, eCommandProcessingError,
                        "I/O error while reading " << m_BlobKey);
            }
        }
        buffer.resize(total_bytes_read);
        m_BlobSize -= total_bytes_read;

        reply.AppendString(buffer);
    } else if (method == "close") {
        if (m_Reader.get() != NULL)
            m_Reader.reset();
        else if (m_Writer.get() != NULL) {
            m_Writer->Close();
            m_Writer.reset();
        }
    } else if (method == "get_key") {
        reply.AppendString(m_BlobKey);
    } else
        return false;

    return true;
}