Ejemplo n.º 1
0
 /// <summary>
 /// Set socket pools configuration from a JSON text file
 /// </summary>
 /// <param name="midTier">True if calling from a middle tier; Otherwise, false</param>
 /// <param name="jsonConfig">A file path to a JSON configuration text file, which defaults to sp_config.json at current directory</param>
 /// <returns>An instance of CSpConfig</returns>
 public static CSpConfig SetConfig(bool midTier = false, string jsonConfig = null)
 {
     lock (m_cs) {
         if (m_sc != null)
         {
             return(m_sc);
         }
     }
     if (jsonConfig == null || jsonConfig.Length == 0)
     {
         jsonConfig = "sp_config.json";
     }
     using (StreamReader sr = File.OpenText(jsonConfig)) {
         string       json = sr.ReadToEnd();
         MemoryStream ms   = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json));
         DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(CSpConfig), new DataContractJsonSerializerSettings {
             UseSimpleDictionaryFormat = true
         });
         CSpConfig sc = ser.ReadObject(ms) as CSpConfig;
         sc.CheckErrors();
         lock (m_cs) {
             m_sc     = sc;
             m_Middle = midTier;
             return(m_sc);
         }
     }
 }
Ejemplo n.º 2
0
        public static dynamic GetPool(string poolKey)
        {
            if (poolKey == null || poolKey.Length == 0)
            {
                throw new Exception("Pool key cannot be empty");
            }
            CSpConfig sc = SetConfig();

            lock (m_cs) {
                if (sc.m_vPK.IndexOf(poolKey) == -1)
                {
                    throw new Exception("Pool key " + poolKey + " cannot be found from configuaration");
                }
                CPoolConfig pc = null;
                if (sc.m_Pools.ContainsKey(poolKey))
                {
                    pc = sc.m_Pools[poolKey];
                }
                else
                {
                    foreach (var key in sc.m_Pools.Keys)
                    {
                        CPoolConfig p = sc.m_Pools[key];
                        if (p.m_Slaves == null)
                        {
                            continue;
                        }
                        if (p.m_Slaves.ContainsKey(poolKey))
                        {
                            pc = p.m_Slaves[poolKey];
                            break;
                        }
                    }
                }
                if (pc.Pool != null)
                {
                    return(pc.Pool);
                }
                dynamic pool;
                switch (pc.SvsId)
                {
                case CMysql.sidMysql: {
                    CSocketPool <CMysql> mysql;
                    switch (pc.PoolType)
                    {
                    case tagPoolType.Slave:
                        mysql = new CSqlMasterPool <CMysql, CDataSet> .CSlavePool(pc.DefaultDb, pc.RecvTimeout, pc.AutoConn, pc.ConnTimeout);

                        break;

                    case tagPoolType.Master:
                        mysql = new CSqlMasterPool <CMysql, CDataSet>(pc.DefaultDb, m_Middle, pc.RecvTimeout, pc.AutoConn, pc.ConnTimeout);
                        break;

                    default:
                        mysql = new CSocketPool <CMysql>(pc.AutoConn, pc.RecvTimeout, pc.ConnTimeout);
                        break;
                    }
                    mysql.DoSslServerAuthentication += (sender, cs) => { return(m_sc.Verify(cs)); };
                    pool = mysql;
                }
                break;

                case BaseServiceID.sidODBC: {
                    CSocketPool <COdbc> odbc;
                    switch (pc.PoolType)
                    {
                    case tagPoolType.Slave:
                        odbc = new CSqlMasterPool <COdbc, CDataSet> .CSlavePool(pc.DefaultDb, pc.RecvTimeout, pc.AutoConn, pc.ConnTimeout);

                        break;

                    case tagPoolType.Master:
                        odbc = new CSqlMasterPool <COdbc, CDataSet>(pc.DefaultDb, m_Middle, pc.RecvTimeout, pc.AutoConn, pc.ConnTimeout);
                        break;

                    default:
                        odbc = new CSocketPool <COdbc>(pc.AutoConn, pc.RecvTimeout, pc.ConnTimeout);
                        break;
                    }
                    odbc.DoSslServerAuthentication += (sender, cs) => { return(m_sc.Verify(cs)); };
                    pool = odbc;
                }
                break;

                case CSqlite.sidSqlite: {
                    CSocketPool <CSqlite> sqlite;
                    switch (pc.PoolType)
                    {
                    case tagPoolType.Slave:
                        sqlite = new CSqlMasterPool <CSqlite, CDataSet> .CSlavePool(pc.DefaultDb, pc.RecvTimeout, pc.AutoConn, pc.ConnTimeout);

                        break;

                    case tagPoolType.Master:
                        sqlite = new CSqlMasterPool <CSqlite, CDataSet>(pc.DefaultDb, m_Middle, pc.RecvTimeout, pc.AutoConn, pc.ConnTimeout);
                        break;

                    default:
                        sqlite = new CSocketPool <CSqlite>(pc.AutoConn, pc.RecvTimeout, pc.ConnTimeout);
                        break;
                    }
                    sqlite.DoSslServerAuthentication += (sender, cs) => { return(m_sc.Verify(cs)); };
                    pool = sqlite;
                }
                break;

                case BaseServiceID.sidFile: {
                    CSocketPool <CStreamingFile> sf = new CSocketPool <CStreamingFile>(pc.AutoConn, pc.RecvTimeout, pc.ConnTimeout);
                    sf.DoSslServerAuthentication += (sender, cs) => { return(m_sc.Verify(cs)); };
                    pool = sf;
                }
                break;

                case BaseServiceID.sidQueue: {
                    var aq = new CSocketPool <CAsyncQueue>(pc.AutoConn, pc.RecvTimeout, pc.ConnTimeout);
                    aq.DoSslServerAuthentication += (sender, cs) => { return(m_sc.Verify(cs)); };
                    pool = aq;
                }
                break;

                default: {
                    CSocketPool <CCachedBaseHandler> cbh;
                    switch (pc.PoolType)
                    {
                    case tagPoolType.Slave:
                        cbh = new CMasterPool <CCachedBaseHandler, CDataSet> .CSlavePool(pc.DefaultDb, pc.RecvTimeout, pc.AutoConn, pc.ConnTimeout, pc.SvsId);

                        break;

                    case tagPoolType.Master:
                        cbh = new CMasterPool <CCachedBaseHandler, CDataSet>(pc.DefaultDb, m_Middle, pc.RecvTimeout, pc.AutoConn, pc.ConnTimeout, pc.SvsId);
                        break;

                    default:
                        cbh = new CSocketPool <CCachedBaseHandler>(pc.AutoConn, pc.RecvTimeout, pc.ConnTimeout, pc.SvsId);
                        break;
                    }
                    cbh.DoSslServerAuthentication += (sender, cs) => { return(m_sc.Verify(cs)); };
                    pool = cbh;
                }
                break;
                }
                pool.QueueName             = pc.Queue;
                pc.Pool                    = pool;
                CConnectionContext[,] ppCC = new CConnectionContext[pc.Threads, pc.Hosts.Count];
                for (uint i = 0; i < pc.Threads; ++i)
                {
                    for (int j = 0; j < pc.Hosts.Count; ++j)
                    {
                        ppCC[i, j] = sc.m_Hosts[pc.m_Hosts[j]];
                    }
                }
                pool.StartSocketPool(ppCC);
                pool.QueueAutoMerge = pc.AutoMerge;
                return(pool);
            }
        }