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

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

There is a newer version: 5.3.0
Show newest version
/*
 * InitCommand.java
 *
 * Created on September 11, 2006, 2:53 PM
 *
 * Copyright 2005 Alachisoft, Inc. All rights reserved.
 * ALACHISOFT PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.alachisoft.ncache.client.internal.command;

import com.alachisoft.ncache.client.internal.util.HelperUtil;
import com.alachisoft.ncache.licensing.LicenseInfo;
import com.alachisoft.ncache.client.ClientInfo;
import com.alachisoft.ncache.client.internal.communication.AddressUtil;
import com.alachisoft.ncache.client.internal.communication.ClientLicenseType;
import com.alachisoft.ncache.common.protobuf.ClientInfoProtocol;
import com.alachisoft.ncache.common.protobuf.CommandProtocol;
import com.alachisoft.ncache.common.protobuf.InitCommandProtocol;
import com.alachisoft.ncache.common.protobuf.ProductVersionProtocol;
import com.alachisoft.ncache.licensing.LicenseManager;
import com.alachisoft.ncache.runtime.exceptions.CommandException;
import com.google.protobuf.ByteString;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;

/**
 * @author Administrator
 * @version 1.0
 */
public final class InitCommand extends InitialCommandBase {

    private final boolean _isDotnetClient;
    private final boolean _secureConnectionEnabled;
    private String cacheId;
    private String _clientId;
    private String _licenseCode = "";
    private String _clientEditionId;
    private ArrayList macAddresses;
    private String _licenseInfoString;
    private LicenseInfo _LicenseInfo;
    private boolean _isBridgeClient;
    private Alachisoft.NCache.Common.ProductVersion _currentVersion;
    private ClientInfo _clientInfo;
    private int _operationTimeout;
    int _clientVersion;
    boolean _FromClientCache;


    public InitCommand(String clientId, String id, byte[] userName, byte[] password,
                       String licenseCode, LicenseInfo info, boolean LicenseIsNull, String clientLocalIP, InetAddress requestedServerAddress, ClientInfo clientInfo, ClientLicenseType clientType, boolean secureConnectionEnabled, int operationTimeout) {

        switch (LicenseManager.getNCacheBuildType())
        {
            case PROFO:
            case PCNTO:
                super.name = "InitProfessionalCommand";

                switch (clientType)
                {
                    case CommunityPaidClient:
                        _clientEditionId = "INITPROF";
                        break;
                    case CommunityFreeClient:
                        _clientEditionId = "INITPROF_NEW";
                        break;
                }
                break;
            case PDEV:
                super.name = "InitCommand";
                _clientEditionId = "INITPROF_DEV";
                break;
        }
        verifyRemoteServer(requestedServerAddress);
        _currentVersion = Alachisoft.NCache.Common.ProductVersion.getProductInfo();

         cacheId = id;
        _clientId = clientId;
        _licenseCode = licenseCode;
        _isDotnetClient = false;
        _clientInfo = new ClientInfo();
        _clientInfo.setClientID(clientInfo.getClientID());
        _clientInfo.setMachineName(clientInfo.getMachineName());
        _clientInfo.setProcessID(clientInfo.getProcessID());
        _clientInfo.setAppName(clientInfo.getAppName());
        _clientInfo.setClientVersion(clientInfo.getClientVersion());
        _clientInfo.setMacAddress(info!=null ? info.getStringMacInfo() : null);
        _clientInfo.setCores(info!=null ? info.getCores() : null);
        _LicenseInfo= info;
        _licenseInfoString = !(LicenseIsNull)? info.serialize() : "";

        _secureConnectionEnabled = secureConnectionEnabled;
        _operationTimeout = operationTimeout;
        _clientVersion = 5000;
    }
    @Override
    public void createCommand() throws CommandException {
        try {
            GetMacAddresses();
        } catch (SocketException ex) {
            throw new CommandException(ex.getMessage());
        }

        ProductVersionProtocol.ProductVersion.Builder productVersionBuilder= ProductVersionProtocol.ProductVersion.newBuilder();
        productVersionBuilder.setAddiotionalData(ByteString.copyFrom(_currentVersion.getAdditionalData()))
                .setEditionID(_currentVersion.getEditionID())
                .setMajorVersion1(parseToByteString(_currentVersion.getMajorVersion1()))
                .setMajorVersion2(parseToByteString(_currentVersion.getMajorVersion2()))
                .setMinorVersion1(parseToByteString(_currentVersion.getMinorVersion1()))
                .setMinorVersion2(parseToByteString(_currentVersion.getMinorVersion2()))
                .setProductName(_currentVersion.getProductName());

        ClientInfoProtocol.ClientInfo.Builder clientInfoBuilder= ClientInfoProtocol.ClientInfo.newBuilder();
        clientInfoBuilder.setMachineName(_clientInfo.getMachineName() !=null ?_clientInfo.getMachineName() : "")
                .setProcessId(_clientInfo.getProcessID())
                .setAppName(_clientInfo.getAppName() !=null ? _clientInfo.getAppName() : "")
                .setClientId(_clientInfo.getClientID() !=null ?_clientInfo.getClientID() : "")

                .setMacAddress(_LicenseInfo !=null ?_LicenseInfo.getStringMacInfo(): "")
                .setCores(_LicenseInfo !=null ?_LicenseInfo.getCores(): -1);

        InitCommandProtocol.InitCommand.Builder initCoomandBuilder = InitCommandProtocol.InitCommand.newBuilder();

        initCoomandBuilder.setCacheId(cacheId)
                .setRequestId(this.getRequestId())
                .setClientEditionId(_clientEditionId)
                .setClientId(_clientId)
                .setIsDotnetClient(_isDotnetClient)
                .setIsBridgeClient(false)
                .setLicenceCode(_licenseCode)
                .setLicenceInfo(_licenseInfoString)
                .setSecureConnectionEnabled(_secureConnectionEnabled)
                .setOperationTimeout(_operationTimeout)
                .setClientVersion(_clientVersion)
                .setFromClientCache(_FromClientCache)
                .setProductVersion(productVersionBuilder)
                .setClientInfo(clientInfoBuilder);

            CommandProtocol.Command.Builder commandBuilder= CommandProtocol.Command.newBuilder();

            commandBuilder = commandBuilder.setInitCommand(initCoomandBuilder)
                    .setRequestID(this.getRequestId())
                    .setType(CommandProtocol.Command.Type.INIT);
        _command = commandBuilder.build();
    }

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

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

    @Override
    public boolean getSupportsAacknowledgement() {
        return false;
    }

    private void GetMacAddresses() throws SocketException {
        try {

            String mac1 = "";
            String mac2 = "";
            String mac3 = "";
            String mac4 = "";

            // get all network interfaces of the current system
            Enumeration networkInterface = NetworkInterface.getNetworkInterfaces();

            int counter = 1;
            // iterate over all interfaces
            while (networkInterface.hasMoreElements()) {
                // get an interface
                NetworkInterface network = networkInterface.nextElement();

                // get its hardware or mac address
                byte[] macAddressBytes = network.getHardwareAddress();

                if (macAddressBytes != null) {

                    // initialize a string builder to hold mac address
                    StringBuilder macAddressStr = new StringBuilder();

                    // iterate over the bytes of mac address
                    for (int i = 0; i < macAddressBytes.length; i++) {
                        // convert byte to string in hexadecimal form
                        macAddressStr.append(String.format("%02X", macAddressBytes[i]));

                        // check if there are more bytes, then add a "-" to make it more readable
                        //  if (i < macAddressBytes.length - 1) {
                        //      macAddressStr.append("-");
                        //  }
                    }
                    if (counter == 1) {
                        mac1 = macAddressStr.toString().toLowerCase();
                    }
                    if (counter == 2) {
                        mac2 = macAddressStr.toString().toLowerCase();
                    }
                    if (counter == 3) {
                        mac3 = macAddressStr.toString().toLowerCase();
                    }
                    if (counter == 4) {
                        mac4 = macAddressStr.toString().toLowerCase();
                    }
                    counter++;
                }
            }
            macAddresses = new ArrayList();
            macAddresses.add(mac1);
            macAddresses.add(mac2);
            macAddresses.add(mac3);
            macAddresses.add(mac4);

            //Sort the list
            Collections.sort(macAddresses);

            //     for(int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy