/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * Licensed 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 com.alibaba.nacos.naming.push;

import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.Version;
import org.codehaus.jackson.util.VersionUtil;

/**
 * Client info.
 *
 * @author nacos
 */
public class ClientInfo {
    
    public Version version = Version.unknownVersion();
    
    public ClientType type = ClientType.UNKNOWN;
    
    public ClientInfo(String userAgent) {
        String versionStr = StringUtils.isEmpty(userAgent) ? StringUtils.EMPTY : userAgent;
        
        if (versionStr.startsWith(ClientTypeDescription.JAVA_CLIENT)) {
            type = ClientType.JAVA;
            
            versionStr = versionStr.substring(versionStr.indexOf(":v") + 2, versionStr.length());
            version = VersionUtil.parseVersion(versionStr);
            
            return;
        }
        
        if (versionStr.startsWith(ClientTypeDescription.DNSF_CLIENT)) {
            type = ClientType.DNS;
            
            versionStr = versionStr.substring(versionStr.indexOf(":v") + 2, versionStr.length());
            version = VersionUtil.parseVersion(versionStr);
            
            return;
        }
        
        if (versionStr.startsWith(ClientTypeDescription.C_CLIENT)) {
            type = ClientType.C;
            
            versionStr = versionStr.substring(versionStr.indexOf(":v") + 2, versionStr.length());
            version = VersionUtil.parseVersion(versionStr);
            
            return;
        }
        
        if (versionStr.startsWith(ClientTypeDescription.SDK_CLIENT)) {
            type = ClientType.JAVA_SDK;
            
            versionStr = versionStr.substring(versionStr.indexOf(":v") + 2, versionStr.length());
            version = VersionUtil.parseVersion(versionStr);
            
            return;
        }
        
        if (versionStr.startsWith(UtilsAndCommons.NACOS_SERVER_HEADER)) {
            type = ClientType.NACOS_SERVER;
            
            versionStr = versionStr.substring(versionStr.indexOf(":v") + 2, versionStr.length());
            version = VersionUtil.parseVersion(versionStr);
            
            return;
        }
        
        if (versionStr.startsWith(ClientTypeDescription.NGINX_CLIENT)) {
            type = ClientType.TENGINE;
            
            versionStr = versionStr.substring(versionStr.indexOf(":v") + 2, versionStr.length());
            version = VersionUtil.parseVersion(versionStr);
            
            return;
        }
        
        if (versionStr.startsWith(ClientTypeDescription.CPP_CLIENT)) {
            type = ClientType.C;
            
            versionStr = versionStr.substring(versionStr.indexOf(":v") + 2, versionStr.length());
            version = VersionUtil.parseVersion(versionStr);
            
            return;
        }
        
        if (versionStr.startsWith(ClientTypeDescription.GO_CLIENT)) {
            type = ClientType.GO;
            
            versionStr = versionStr.substring(versionStr.indexOf(":v") + 2, versionStr.length());
            version = VersionUtil.parseVersion(versionStr);
            
            return;
        }
        
        //we're not eager to implement other type yet
        this.type = ClientType.UNKNOWN;
        this.version = Version.unknownVersion();
    }
    
    public enum ClientType {
        /**
         * Go client type.
         */
        GO,
        /**
         * Java client type.
         */
        JAVA,
        /**
         * C client type.
         */
        C,
        /**
         * php client type.
         */
        PHP,
        /**
         * dns-f client type.
         */
        DNS,
        /**
         * nginx client type.
         */
        TENGINE,
        /**
         * sdk client type.
         */
        JAVA_SDK,
        /**
         * Server notify each other.
         */
        NACOS_SERVER,
        /**
         * Unknown client type.
         */
        UNKNOWN
    }
    
    public static class ClientTypeDescription {
        
        public static final String JAVA_CLIENT = "Nacos-Java-Client";
        
        public static final String DNSF_CLIENT = "Nacos-DNS";
        
        public static final String C_CLIENT = "Nacos-C-Client";
        
        public static final String SDK_CLIENT = "Nacos-SDK-Java";
        
        public static final String NGINX_CLIENT = "unit-nginx";
        
        public static final String CPP_CLIENT = "vip-client4cpp";
        
        public static final String GO_CLIENT = "Nacos-Go-Client";
        
    }
    
}

@@ -3797,32 +3797,335 @@
 eturn;%0A        %7D
+%0A%0A        if (versionStr.startsWith(ClientTypeDescription.CSHARP_CLIENT)) %7B%0A            type = ClientType.CSHARP;%0A%0A            versionStr = versionStr.substring(versionStr.indexOf(%22:v%22) + 2, versionStr.length());%0A            version = VersionUtil.parseVersion(versionStr);%0A%0A            return;%0A        %7D
 %0A        %0A      
@@ -4496,32 +4496,103 @@
  C,%0A        /**%0A
+         * CSharp client type.%0A         */%0A        CSHARP,%0A        /**%0A
          * php c
@@ -5562,32 +5562,107 @@
 acos-Go-Client%22;
+%0A%0A        public static final String CSHARP_CLIENT = %22Nacos-CSharp-Client%22;
 %0A        %0A    %7D%0A

Add CSharp Client