All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alachisoft.ncache.client.internal.command.InitSecondarySocketCommand Maven / Gradle / Ivy

There is a newer version: 5.3.0
Show newest version
package com.alachisoft.ncache.client.internal.command;

import com.alachisoft.ncache.runtime.exceptions.CommandException;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

public class InitSecondarySocketCommand extends InitialCommandBase {
    private String clientId;

    public InitSecondarySocketCommand(String clientID) {
        super();
        clientId = clientID;

    }

    @Override
    public CommandType getCommandType() {
        return CommandType.INIT_SECONDARY;
    }

    @Override
    public RequestType getCommandRequestType() {
        return RequestType.InternalCommand;
    }

    @Override
    protected void createCommand() throws CommandException {
        try {
            constructCommand("InitSecondarySocketCommand \"" + super.getRequestId() + "\"" + clientId + "\"Y\"", new byte[0]);
        } catch (UnsupportedEncodingException e) {
        }
    }
    protected byte[] constructCommand(String cmdString, byte[] data) throws UnsupportedEncodingException
    {

        byte[] command = cmdString.getBytes(StandardCharsets.UTF_8);
        byte[] buffer = new byte[2 * (CommandOptions.COMMAND_SIZE + CommandOptions.DATA_SIZE) + command.length + data.length];

        byte[] commandSize = String.valueOf(command.length).getBytes(StandardCharsets.UTF_8);
        byte[] dataSize = String.valueOf(data.length).getBytes(StandardCharsets.UTF_8);

        System.arraycopy(commandSize, 0, buffer, 0, commandSize.length);
        System.arraycopy(dataSize, 0, buffer, CommandOptions.COMMAND_SIZE,
                dataSize.length);
        // we copy the command size two times to avoid if an corruption occurs.
        System.arraycopy(commandSize, 0, buffer, CommandOptions.TOTAL_SIZE,
                commandSize.length);
        System.arraycopy(dataSize, 0, buffer, CommandOptions.TOTAL_SIZE + CommandOptions.COMMAND_SIZE, dataSize.length);

        System.arraycopy(command, 0, buffer, 2 * CommandOptions.TOTAL_SIZE,
                command.length);
        System.arraycopy(data, 0, buffer, (2 * CommandOptions.TOTAL_SIZE) + command.length, data.length);

        return buffer;
    }



}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy