use of org.apache.ignite.internal.binary.BinaryReaderExImpl in project ignite by apache.
the class OdbcMessageParser method decode.
/**
* {@inheritDoc}
*/
@Override
public ClientListenerRequest decode(byte[] msg) {
assert msg != null;
BinaryInputStream stream = new BinaryHeapInputStream(msg);
BinaryReaderExImpl reader = new BinaryReaderExImpl(marsh.context(), stream, ctx.config().getClassLoader(), true);
byte cmd = reader.readByte();
ClientListenerRequest res;
switch(cmd) {
case OdbcRequest.QRY_EXEC:
{
String schema = reader.readString();
String sql = reader.readString();
int paramNum = reader.readInt();
Object[] params = readParameterRow(reader, paramNum);
int timeout = 0;
if (ver.compareTo(OdbcConnectionContext.VER_2_3_2) >= 0)
timeout = reader.readInt();
res = new OdbcQueryExecuteRequest(schema, sql, params, timeout);
break;
}
case OdbcRequest.QRY_EXEC_BATCH:
{
String schema = reader.readString();
String sql = reader.readString();
int paramRowLen = reader.readInt();
int rowNum = reader.readInt();
boolean last = reader.readBoolean();
Object[][] params = new Object[rowNum][];
for (int i = 0; i < rowNum; ++i) params[i] = readParameterRow(reader, paramRowLen);
int timeout = 0;
if (ver.compareTo(OdbcConnectionContext.VER_2_3_2) >= 0)
timeout = reader.readInt();
res = new OdbcQueryExecuteBatchRequest(schema, sql, last, params, timeout);
break;
}
case OdbcRequest.QRY_FETCH:
{
long queryId = reader.readLong();
int pageSize = reader.readInt();
res = new OdbcQueryFetchRequest(queryId, pageSize);
break;
}
case OdbcRequest.QRY_CLOSE:
{
long queryId = reader.readLong();
res = new OdbcQueryCloseRequest(queryId);
break;
}
case OdbcRequest.META_COLS:
{
String schema = reader.readString();
String table = reader.readString();
String column = reader.readString();
res = new OdbcQueryGetColumnsMetaRequest(schema, table, column);
break;
}
case OdbcRequest.META_TBLS:
{
String catalog = reader.readString();
String schema = reader.readString();
String table = reader.readString();
String tableType = reader.readString();
res = new OdbcQueryGetTablesMetaRequest(catalog, schema, table, tableType);
break;
}
case OdbcRequest.META_PARAMS:
{
String schema = reader.readString();
String sqlQuery = reader.readString();
res = new OdbcQueryGetParamsMetaRequest(schema, sqlQuery);
break;
}
case OdbcRequest.MORE_RESULTS:
{
long queryId = reader.readLong();
int pageSize = reader.readInt();
res = new OdbcQueryMoreResultsRequest(queryId, pageSize);
break;
}
default:
throw new IgniteException("Unknown ODBC command: [cmd=" + cmd + ']');
}
return res;
}
use of org.apache.ignite.internal.binary.BinaryReaderExImpl in project ignite by apache.
the class TcpClientChannel method handshakeRes.
/**
* Receive and handle handshake response.
*/
private void handshakeRes(ByteBuffer buf, ProtocolVersion proposedVer, String user, String pwd, Map<String, String> userAttrs) throws ClientConnectionException, ClientAuthenticationException, ClientProtocolError {
BinaryInputStream res = BinaryByteBufferInputStream.create(buf);
try (BinaryReaderExImpl reader = ClientUtils.createBinaryReader(null, res)) {
boolean success = res.readBoolean();
if (success) {
byte[] features = EMPTY_BYTES;
if (ProtocolContext.isFeatureSupported(proposedVer, BITMAP_FEATURES))
features = reader.readByteArray();
protocolCtx = new ProtocolContext(proposedVer, ProtocolBitmaskFeature.enumSet(features));
if (protocolCtx.isFeatureSupported(PARTITION_AWARENESS)) {
// Reading server UUID
srvNodeId = reader.readUuid();
}
} else {
ProtocolVersion srvVer = new ProtocolVersion(res.readShort(), res.readShort(), res.readShort());
String err = reader.readString();
int errCode = ClientStatus.FAILED;
if (res.remaining() > 0)
errCode = reader.readInt();
if (errCode == ClientStatus.AUTH_FAILED)
throw new ClientAuthenticationException(err);
else if (proposedVer.equals(srvVer))
throw new ClientProtocolError(err);
else if (!supportedVers.contains(srvVer) || (!ProtocolContext.isFeatureSupported(srvVer, AUTHORIZATION) && !F.isEmpty(user)))
// authentication and authentication is required.
throw new ClientProtocolError(String.format("Protocol version mismatch: client %s / server %s. Server details: %s", proposedVer, srvVer, err));
else {
// Retry with server version.
handshake(srvVer, user, pwd, userAttrs);
}
}
} catch (IOException e) {
throw handleIOError(e);
}
}
use of org.apache.ignite.internal.binary.BinaryReaderExImpl in project ignite by apache.
the class ClientListenerNioListener method onHandshake.
/**
* Perform handshake.
*
* @param ses Session.
* @param msg Message bytes.
*/
private void onHandshake(GridNioSession ses, ClientMessage msg) {
BinaryContext ctx = new BinaryContext(BinaryCachingMetadataHandler.create(), new IgniteConfiguration(), null);
BinaryMarshaller marsh = new BinaryMarshaller();
marsh.setContext(new MarshallerContextImpl(null, null));
ctx.configure(marsh);
BinaryReaderExImpl reader = new BinaryReaderExImpl(ctx, new BinaryHeapInputStream(msg.payload()), null, true);
byte cmd = reader.readByte();
if (cmd != ClientListenerRequest.HANDSHAKE) {
U.warn(log, "Unexpected client request (will close session): " + ses.remoteAddress());
ses.close();
return;
}
short verMajor = reader.readShort();
short verMinor = reader.readShort();
short verMaintenance = reader.readShort();
ClientListenerProtocolVersion ver = ClientListenerProtocolVersion.create(verMajor, verMinor, verMaintenance);
BinaryWriterExImpl writer = new BinaryWriterExImpl(null, new BinaryHeapOutputStream(8), null, null);
byte clientType = reader.readByte();
ClientListenerConnectionContext connCtx = null;
try {
connCtx = prepareContext(clientType, ses);
ensureClientPermissions(clientType);
if (connCtx.isVersionSupported(ver)) {
connCtx.initializeFromHandshake(ses, ver, reader);
ses.addMeta(CONN_CTX_META_KEY, connCtx);
} else
throw new IgniteCheckedException("Unsupported version: " + ver.asString());
cancelHandshakeTimeout(ses);
connCtx.handler().writeHandshake(writer);
metrics.onHandshakeAccept(clientType);
} catch (IgniteAccessControlException authEx) {
metrics.onFailedAuth();
writer.writeBoolean(false);
writer.writeShort((short) 0);
writer.writeShort((short) 0);
writer.writeShort((short) 0);
writer.doWriteString(authEx.getMessage());
if (ver.compareTo(ClientConnectionContext.VER_1_1_0) >= 0)
writer.writeInt(ClientStatus.AUTH_FAILED);
} catch (IgniteCheckedException e) {
U.warn(log, "Error during handshake [rmtAddr=" + ses.remoteAddress() + ", msg=" + e.getMessage() + ']');
metrics.onGeneralReject();
ClientListenerProtocolVersion currVer;
if (connCtx == null)
currVer = ClientListenerProtocolVersion.create(0, 0, 0);
else
currVer = connCtx.defaultVersion();
writer.writeBoolean(false);
writer.writeShort(currVer.major());
writer.writeShort(currVer.minor());
writer.writeShort(currVer.maintenance());
writer.doWriteString(e.getMessage());
if (ver.compareTo(ClientConnectionContext.VER_1_1_0) >= 0)
writer.writeInt(ClientStatus.FAILED);
}
ses.send(new ClientMessage(writer.array()));
}
use of org.apache.ignite.internal.binary.BinaryReaderExImpl in project ignite by apache.
the class ClientCacheAffinityMapping method readResponse.
/**
* Reads caches affinity response from the input channel and creates {@code ClientCacheAffinityMapping} instance
* from this response.
*
* @param ch Input channel.
*/
public static ClientCacheAffinityMapping readResponse(PayloadInputChannel ch) {
try (BinaryReaderExImpl in = ClientUtils.createBinaryReader(null, ch.in())) {
long topVer = in.readLong();
int minorTopVer = in.readInt();
ClientCacheAffinityMapping aff = new ClientCacheAffinityMapping(new AffinityTopologyVersion(topVer, minorTopVer));
int mappingsCnt = in.readInt();
for (int i = 0; i < mappingsCnt; i++) {
boolean applicable = in.readBoolean();
int cachesCnt = in.readInt();
if (applicable) {
// Partition awareness is applicable for this caches.
Map<Integer, Map<Integer, Integer>> cacheKeyCfg = U.newHashMap(cachesCnt);
for (int j = 0; j < cachesCnt; j++) cacheKeyCfg.put(in.readInt(), readCacheKeyConfiguration(in));
UUID[] partToNode = readNodePartitions(in);
for (Map.Entry<Integer, Map<Integer, Integer>> keyCfg : cacheKeyCfg.entrySet()) aff.cacheAffinity.put(keyCfg.getKey(), new CacheAffinityInfo(keyCfg.getValue(), partToNode));
} else {
// Partition awareness is not applicable for this caches.
for (int j = 0; j < cachesCnt; j++) aff.cacheAffinity.put(in.readInt(), NOT_APPLICABLE_CACHE_AFFINITY_INFO);
}
}
return aff;
} catch (IOException e) {
throw new ClientError(e);
}
}
use of org.apache.ignite.internal.binary.BinaryReaderExImpl in project ignite by apache.
the class ClientUtils method binaryMetadata.
/**
* Deserialize binary type metadata from stream.
*/
BinaryMetadata binaryMetadata(BinaryInputStream in) throws IOException {
try (BinaryReaderExImpl reader = createBinaryReader(in)) {
int typeId = reader.readInt();
String typeName = reader.readString();
String affKeyFieldName = reader.readString();
Map<String, BinaryFieldMetadata> fields = ClientUtils.map(in, unused -> reader.readString(), unused2 -> new BinaryFieldMetadata(reader.readInt(), reader.readInt()));
boolean isEnum = reader.readBoolean();
Map<String, Integer> enumValues = isEnum ? ClientUtils.map(in, unsed -> reader.readString(), unsed2 -> reader.readInt()) : null;
Collection<BinarySchema> schemas = ClientUtils.collection(in, unused -> new BinarySchema(reader.readInt(), new ArrayList<>(ClientUtils.collection(in, unused2 -> reader.readInt()))));
return new BinaryMetadata(typeId, typeName, fields, affKeyFieldName, schemas, isEnum, enumValues);
}
}
Aggregations