blob: 5ba8afc0f85057d5867391fcf568e163a78c00f8 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ignite.internal.processors.odbc;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.IgniteLogger;
import org.apache.ignite.internal.util.nio.GridNioParser;
import org.apache.ignite.internal.util.nio.GridNioSession;
import org.apache.ignite.internal.util.nio.GridNioSessionMetaKey;
import org.apache.ignite.internal.util.typedef.internal.U;
import org.apache.ignite.plugin.extensions.communication.Message;
/**
* This class implements stream parser based on {@link ClientListenerNioServerBuffer}.
* <p>
* The rule for this parser is that every message sent over the stream is prepended with
* 4-byte integer header containing message size. So, the stream structure is as follows:
* <pre>
* +--+--+--+--+--+--+...+--+--+--+--+--+--+--+...+--+
* | MSG_SIZE | MESSAGE | MSG_SIZE | MESSAGE |
* +--+--+--+--+--+--+...+--+--+--+--+--+--+--+...+--+
* </pre>
*/
public class ClientListenerNioMessageParser implements GridNioParser {
/** Message metadata key. */
static final int MSG_META_KEY = GridNioSessionMetaKey.nextUniqueKey();
/** Reader metadata key. */
static final int READER_META_KEY = GridNioSessionMetaKey.nextUniqueKey();
/** */
private final IgniteLogger log;
/** */
public ClientListenerNioMessageParser(IgniteLogger log) {
this.log = log;
}
/** {@inheritDoc} */
@Override public Object decode(GridNioSession ses, ByteBuffer buf) throws IOException, IgniteCheckedException {
Message msg = ses.removeMeta(MSG_META_KEY);
try {
if (msg == null)
msg = new ClientMessage();
boolean finished = false;
if (buf.hasRemaining())
finished = msg.readFrom(buf, null);
if (finished)
return msg;
else {
ses.addMeta(MSG_META_KEY, msg);
return null;
}
}
catch (Throwable e) {
U.error(log, "Failed to read message [msg=" + msg +
", buf=" + buf + ", ses=" + ses + "]", e);
throw e;
}
}
/** {@inheritDoc} */
@Override public ByteBuffer encode(GridNioSession ses, Object msg) throws IOException, IgniteCheckedException {
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
@Override public String toString() {
return ClientListenerNioMessageParser.class.getSimpleName();
}
}