private void SetLastQueuedSlno() { SharedClass.Logger.Info("Getting Last Queued Slno's"); SqlCommand sqlCmd = (SqlCommand)null; SqlConnection sqlCon = (SqlConnection)null; try { sqlCon = new SqlConnection(SharedClass.GetConnectionString(Environment.PRODUCTION)); sqlCmd = new SqlCommand("SELECT Id,ISNULL(HPQLastSlno,0) as HPSlno, ISNULL(MPQLastSlno,0) as MPSlno, ISNULL(LPQLastSlno,0) as LPSlno From VoiceGateways WITH(NOLOCK)", sqlCon); sqlCon.Open(); SqlDataReader reader = sqlCmd.ExecuteReader(); while (reader.Read()) { CallsQueueSlno.SetSlno(Convert.ToInt32(reader["Id"]), Environment.PRODUCTION, Priority.PriorityMode.High, Convert.ToInt64(reader["HPSlno"])); CallsQueueSlno.SetSlno(Convert.ToInt32(reader["Id"]), Environment.PRODUCTION, Priority.PriorityMode.Medium, Convert.ToInt64(reader["MPSlno"])); CallsQueueSlno.SetSlno(Convert.ToInt32(reader["Id"]), Environment.PRODUCTION, Priority.PriorityMode.Low, Convert.ToInt64(reader["LPSlno"])); } sqlCon.Close(); } catch (Exception ex) { SharedClass.Logger.Error("Exception in Getting Last Queued Slno Reason : " + ex.ToString()); } if (SharedClass.PollStaging) { try { sqlCon = new SqlConnection(SharedClass.GetConnectionString(Environment.STAGING)); sqlCmd = new SqlCommand("SELECT Id,ISNULL(HPQLastSlno,0) as HPSlno, ISNULL(MPQLastSlno,0) as MPSlno, ISNULL(LPQLastSlno,0) as LPSlno From VoiceGateways WITH(NOLOCK)", sqlCon); sqlCon.Open(); SqlDataReader reader = sqlCmd.ExecuteReader(); while (reader.Read()) { CallsQueueSlno.SetSlno(Convert.ToInt32(reader["Id"]), Environment.STAGING, Priority.PriorityMode.High, Convert.ToInt64(reader["HPSlno"])); CallsQueueSlno.SetSlno(Convert.ToInt32(reader["Id"]), Environment.STAGING, Priority.PriorityMode.Medium, Convert.ToInt64(reader["MPSlno"])); CallsQueueSlno.SetSlno(Convert.ToInt32(reader["Id"]), Environment.STAGING, Priority.PriorityMode.Low, Convert.ToInt64(reader["LPSlno"])); } sqlCon.Close(); } catch (Exception ex) { SharedClass.Logger.Error("Exception in Getting Last Queued Slno Of Staging Reason : " + ex.ToString()); } } }
//private void UpdateLastSlno(Priority.PriorityMode mode, long slno) //{ // switch (mode) // { // case Priority.PriorityMode.Urgent: // this._urgentPriorityQueueLastSlno = slno; // break; // case Priority.PriorityMode.High: // this.HighPriorityQueueLastSlno = slno; // break; // case Priority.PriorityMode.Medium: // this.MediumPriorityQueueLastSlno = slno; // break; // default: // this.LowPriorityQueueLastSlno = slno; // break; // } //} //private long GetLastSlno(Priority.PriorityMode mode) //{ // long num; // switch (mode) // { // case Priority.PriorityMode.Urgent: // num = this._urgentPriorityQueueLastSlno; // break; // case Priority.PriorityMode.High: // num = this._highPriorityQueueLastSlno; // break; // case Priority.PriorityMode.Medium: // num = this._mediumPriorityQueueLastSlno; // break; // default: // num = this._lowPriorityQueueLastSlno; // break; // } // return num; //} private void UpdateLastProcessedSlno() { SharedClass.Logger.Info("Updating Last Slnos"); SqlConnection sqlCon = (SqlConnection)null; SqlCommand sqlCmd = (SqlCommand)null; try { sqlCon = new SqlConnection(SharedClass.GetConnectionString(Environment.PRODUCTION)); sqlCmd = new SqlCommand("Update VoiceGateways With(Rowlock) Set UPQLastSlno = @UPQLastSlno, HPQLastSlno = @HPQLastSlno, MPQLastSlno = @MPQLastSlno , LPQLastSlno = @LPQLastSlno Where ID = @GatewayId", sqlCon); sqlCmd.Parameters.Add("@UPQLastSlno", SqlDbType.BigInt).Value = CallsQueueSlno.GetSlno(this.Id, Environment.PRODUCTION, Priority.PriorityMode.Urgent); sqlCmd.Parameters.Add("@HPQLastSlno", SqlDbType.BigInt).Value = CallsQueueSlno.GetSlno(this.Id, Environment.PRODUCTION, Priority.PriorityMode.High); sqlCmd.Parameters.Add("@MPQLastSlno", SqlDbType.BigInt).Value = CallsQueueSlno.GetSlno(this.Id, Environment.PRODUCTION, Priority.PriorityMode.Medium); sqlCmd.Parameters.Add("@LPQLastSlno", SqlDbType.BigInt).Value = CallsQueueSlno.GetSlno(this.Id, Environment.PRODUCTION, Priority.PriorityMode.Low); sqlCmd.Parameters.Add("@GatewayId", SqlDbType.Int).Value = this.Id; sqlCon.Open(); sqlCmd.ExecuteNonQuery(); sqlCon.Close(); SharedClass.Logger.Info("Updating Last Slnos Done"); } catch (Exception ex) { SharedClass.Logger.Error("Exception in Updating Last Processed Slno's Reason : " + ex.ToString()); } if (SharedClass.PollStaging) { try { sqlCon = new SqlConnection(SharedClass.GetConnectionString(Environment.STAGING)); sqlCmd = new SqlCommand("Update VoiceGateways With(Rowlock) Set UPQLastSlno = @UPQLastSlno, HPQLastSlno = @HPQLastSlno, MPQLastSlno = @MPQLastSlno , LPQLastSlno = @LPQLastSlno Where ID = @GatewayId", sqlCon); sqlCmd.Parameters.Add("@HPQLastSlno", SqlDbType.BigInt).Value = CallsQueueSlno.GetSlno(this.Id, Environment.STAGING, Priority.PriorityMode.High); sqlCmd.Parameters.Add("@MPQLastSlno", SqlDbType.BigInt).Value = CallsQueueSlno.GetSlno(this.Id, Environment.STAGING, Priority.PriorityMode.Medium); sqlCmd.Parameters.Add("@LPQLastSlno", SqlDbType.BigInt).Value = CallsQueueSlno.GetSlno(this.Id, Environment.STAGING, Priority.PriorityMode.Low); sqlCmd.Parameters.Add("@UPQLastSlno", SqlDbType.BigInt).Value = CallsQueueSlno.GetSlno(this.Id, Environment.STAGING, Priority.PriorityMode.Urgent); sqlCmd.Parameters.Add("@GatewayId", SqlDbType.Int).Value = this.Id; sqlCon.Open(); sqlCmd.ExecuteNonQuery(); sqlCon.Close(); } catch (Exception ex) { SharedClass.Logger.Error("Exception in Updating Last Processed Staging Slno's Reason : " + ex.ToString()); } } }
private void GetPendingGroupCallsFromDataBase(object input) { PollingInput pollingInput = (PollingInput)input; SqlConnection connection = new SqlConnection(SharedClass.GetConnectionString(pollingInput.Environment)); SqlCommand sqlCommand = new SqlCommand("VC_Get_PendingGroupCalls", connection); sqlCommand.CommandType = CommandType.StoredProcedure; SqlDataAdapter da = null; DataSet ds = null; Call call = null; while (!this._pushThreadMutex.WaitOne()) { Thread.Sleep(100); } ++this._pollThreadsRunning; this._pushThreadMutex.ReleaseMutex(); SharedClass.Logger.Info("Started"); while (this._shouldIPoll && !SharedClass.HasStopSignal) { try { sqlCommand.Parameters.Clear(); sqlCommand.Parameters.Add("@GatewayId", SqlDbType.Int).Value = this._id; sqlCommand.Parameters.Add("@LastSlno", SqlDbType.BigInt).Value = CallsQueueSlno.GetSlno(this._id, pollingInput.Environment, pollingInput.PriorityMode); if (connection.State != ConnectionState.Open) { connection.Open(); } da = new SqlDataAdapter(); da.SelectCommand = sqlCommand; ds = new DataSet(); da.Fill(ds); if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { foreach (DataRow dataRow in ds.Tables[0].Rows) { try { call = new Call(); call.Environment = pollingInput.Environment; call.QueueTableSlno = Convert.ToInt64(dataRow["Id"]); CallsQueueSlno.SetSlno(this.Id, pollingInput.Environment, pollingInput.PriorityMode, Convert.ToInt64(dataRow["Id"])); call.CallId = Convert.ToInt64(dataRow["CallId"]); call.AccountId = Convert.ToInt64(dataRow["AccountId"]); call.UUID = dataRow["UUID"].ToString(); call.CallerId = dataRow["CallerId"].ToString(); call.Destination = dataRow["Destination"].ToString(); call.Xml = dataRow["Xml"].ToString(); call.RingUrl = dataRow["RingUrl"].ToString(); call.AnswerUrl = dataRow["AnswerUrl"].ToString(); call.HangupUrl = dataRow["HangupUrl"].ToString(); call.Pulse = Convert.ToByte(dataRow["Pulse"]); call.PricePerPulse = float.Parse(dataRow["PricePerPulse"].ToString()); call.PriorityValue = Convert.ToByte(dataRow["Priority"]); call.IsGroupCall = true; call.IsSipUser = Convert.ToBoolean(dataRow["IsSipUser"].ToString()); this._callsQueue.EnQueue(call, pollingInput.PriorityMode); } catch (Exception e) { SharedClass.Logger.Error("Error In Constructing Call Object : " + e.ToString()); try { PropertyInfo[] properties = call.GetType().GetProperties(); foreach (PropertyInfo propertyInfo in properties) { if (propertyInfo.CanRead) { if (propertyInfo.GetValue(call) == DBNull.Value) { SharedClass.DumpLogger.Error(propertyInfo.Name + " : NULL"); } else { SharedClass.DumpLogger.Error(propertyInfo.Name + " : " + propertyInfo.GetValue(call).ToString()); } } } } catch (Exception e1) { } finally { } } } //this.UpdateLastSlno(priorityMode, call.QueueTableSlno); CallsQueueSlno.SetSlno(this._id, pollingInput.Environment, pollingInput.PriorityMode, call.QueueTableSlno); } else { try { Thread.Sleep(2000); } catch (ThreadAbortException) { } } } catch (Exception ex) { SharedClass.Logger.Error(("Error In Polling, " + ex.ToString())); } finally { da = null; ds = null; } } SharedClass.Logger.Info("Stopped Polling, ShouldIPoll : " + this._shouldIPoll + ", Has Stop Signal : " + SharedClass.HasStopSignal); while (!this._pushThreadMutex.WaitOne()) { Thread.Sleep(100); SharedClass.Logger.Info("Waiting for pushThreadMutex to decrease pollThreadsRunning count"); } --this._pollThreadsRunning; this._pushThreadMutex.ReleaseMutex(); if (SharedClass.HasStopSignal) { return; } string str = this.GetDisplayString() + " "; string text; text = str + "UP Poller Stopped"; lock (SharedClass.Notifier) { SharedClass.Notifier.SendSms(text); } }
public void LoadGateways() { SharedClass.Logger.Info("Fetching Gateways List From Database"); SqlCommand sqlCommand = null; try { sqlCommand = new SqlCommand("Get_VoiceGateways", new SqlConnection(SharedClass.GetConnectionString(Environment.PRODUCTION))); sqlCommand.CommandType = CommandType.StoredProcedure; SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); DataSet dataSet = new DataSet(); sqlDataAdapter.SelectCommand = sqlCommand; sqlDataAdapter.Fill(dataSet); if (dataSet.Tables.Count > 0 && dataSet.Tables[0].Rows.Count > 0) { foreach (DataRow dataRow in dataSet.Tables[0].Rows) { Gateway gateway = new Gateway(); Thread gatewayThread = null; try { gateway.Id = Convert.ToInt16(dataRow["Id"]); gateway.Name = dataRow["Code"].ToString().Replace(" ", ""); gateway.ConnectUrl = dataRow["ConnectUrl"].ToString(); gateway.Ip = dataRow["Ip"].ToString(); gateway.Port = Convert.ToInt32(dataRow["Port"]); gateway.MaximumConcurrency = Convert.ToInt32(dataRow["MaximumConcurrency"]); gateway.CurrenctConcurrency = Convert.ToInt32(dataRow["CurrenctConcurrency"]); gateway.OriginationUrl = dataRow["OriginationUrl"].ToString(); if (!gateway.OriginationUrl.EndsWith("/")) { gateway.OriginationUrl += "/"; } if (dataRow["ExtraDialString"] != DBNull.Value) { gateway.ExtraDialString = dataRow["ExtraDialString"].ToString(); } gateway.CountryPrefix = dataRow["CountryPrefix"].ToString(); gateway.IsCountryPrefixAllowed = Convert.ToBoolean(dataRow["IsCountryPrefixAllowed"].ToString()); gateway.DialPrefix = dataRow["DialPrefix"].ToString(); if (dataRow.Table.Columns.Contains("StartingHour") && !dataRow["StartingHour"].IsDBNull()) { byte tempByte = 0; if (byte.TryParse(dataRow["StartingHour"].ToString(), out tempByte)) { gateway.StartingHour = tempByte; } } if (dataRow.Table.Columns.Contains("StoppingHour") && !dataRow["StoppingHour"].IsDBNull()) { byte tempByte = 23; if (byte.TryParse(dataRow["StoppingHour"].ToString(), out tempByte)) { gateway.StoppingHour = tempByte; } } if (dataRow.Table.Columns.Contains("UrgentPriorityQueueLastSlno") && !dataRow["UrgentPriorityQueueLastSlno"].IsDBNull()) { long tempLong = 0; if (long.TryParse(dataRow["UrgentPriorityQueueLastSlno"].ToString(), out tempLong)) { //gateway.UrgentPriorityQueueLastSlno = tempLong; CallsQueueSlno.SetSlno(gateway.Id, Environment.PRODUCTION, Priority.PriorityMode.Urgent, tempLong); } } //gateway.HighPriorityQueueLastSlno = Convert.ToInt64(dataRow["HighPriorityQueueLastSlno"]); CallsQueueSlno.SetSlno(gateway.Id, Environment.PRODUCTION, Priority.PriorityMode.High, Convert.ToInt64(dataRow["HighPriorityQueueLastSlno"])); //gateway.MediumPriorityQueueLastSlno = Convert.ToInt64(dataRow["MediumPriorityQueueLastSlno"]); CallsQueueSlno.SetSlno(gateway.Id, Environment.PRODUCTION, Priority.PriorityMode.Medium, Convert.ToInt64(dataRow["MediumPriorityQueueLastSlno"])); //gateway.LowPriorityQueueLastSlno = Convert.ToInt64(dataRow["LowPriorityQueueLastSlno"]); CallsQueueSlno.SetSlno(gateway.Id, Environment.PRODUCTION, Priority.PriorityMode.Low, Convert.ToInt64(dataRow["LowPriorityQueueLastSlno"])); gateway.NumberOfPushThreads = Convert.ToByte(dataRow["PushThreadsTotal"]); gatewayThread = new Thread(new ThreadStart(gateway.Start)); gatewayThread.Name = gateway.Name.Replace(" ", ""); SharedClass.Logger.Info("Starting Gateway " + gateway.Name); gatewayThread.Start(); } catch (Exception ex) { SharedClass.Logger.Error("Error Starting Gateway, " + ex.ToString()); SharedClass.DumpLogger.Error(dataRow.ToString()); } } } else { SharedClass.Logger.Error("No Gateways Returned From DataBase"); } } catch (Exception ex) { SharedClass.Logger.Error("Error Getting Gateways List, Reason : " + ex.ToString()); } }