Exemplo n.º 1
0
        private void SmsInterceptor_MessageReceived(object sender, MessageInterceptorEventArgs me)
        {
            SmsMessage request = me.Message as SmsMessage;

            if (request != null)
            {
                // Look at the body of the SMS message to determine
                String incomeData = request.Body.Substring(receiveControlPrefix.Length);
                String msgType    = incomeData.Substring(0, 3);
                String msgID      = incomeData.Substring(3, 2);

                switch (msgType)
                {
                case "srv":     // all parts received.
                    unfinishedSentMsgList.Remove(msgID);
                    Console.WriteLine("message received: " + msgID);
                    break;

                case "rty":     // something to resend
                    if (unfinishedSentMsgList.ContainsKey(msgID))
                    {
                        // find the messages to resend
                        String msgsToResendPart = incomeData.Substring(5);

                        String[] unfinishedMsg = unfinishedSentMsgList[msgID].getData();

                        for (int i = 0; i < msgsToResendPart.Length; i++)
                        {
                            char resendPartChar = msgsToResendPart[i];

                            String smsToSend = receiveControlPrefix + unfinishedSentMsgList[msgID].getSendDataType() + unfinishedSentMsgList[msgID].getMsgID()
                                               + unfinishedSentMsgList[msgID].getNumOfMessages() + resendPartChar + "0"
                                               + unfinishedMsg[convertPartNumberCharToInt(resendPartChar) - 1];

                            char crcChar = getCRC86(smsToSend);

                            //insert CRC char
                            smsToSend = smsToSend.Remove(13, 1);
                            String msgContent = smsToSend.Insert(13, crcChar.ToString());

                            // resend message back to the server
                            MessageToSend messageToSend = new MessageToSend(request.From.Address, msgContent);
                            smsSendingQueue.Add(messageToSend);
                        }
                    }
                    else
                    {
                        String msgContent = "SMS-" + receivingProjectCode + receivingProgramCode + "ukn" + msgID;

                        // send a message-unknown 'ukn' back to the server
                        MessageToSend messageToSend = new MessageToSend(request.From.Address, msgContent);
                        smsSendingQueue.Add(messageToSend);
                    }
                    break;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Send a regular sms, DON'T use this to send logs to server
        /// </summary>
        /// <param name="msgRecipient"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public SMSErrorMessage sendRegularSMS(String msgRecipient, String msgContent)
        {
            // check msgRecipient
            if (msgRecipient.Length != 10)
            {
                return(SMSErrorMessage.RecipientNumberFormatIncorrect);
            }

            MessageToSend messageToSend = new MessageToSend(msgRecipient, msgContent);

            smsSendingQueue.Add(messageToSend);

            return(SMSErrorMessage.None);
        }
Exemplo n.º 3
0
        private void smsSendngMonitor()
        {
            while (true)
            {
                while (smsSendingQueue.Count > 0)
                {
                    MessageToSend messageToSend = smsSendingQueue[0];
                    if (Util.SendSMS(messageToSend.getClientNum(), messageToSend.getMsgContent()))
                    {
                        smsSendingQueue.RemoveAt(0);

                        if (enableLogging)
                        {
                            logWriter.WriteLine(DateTime.Now + "," + messageToSend.getClientNum() + "," + messageToSend.getMsgContent() + ", successfully sent.");
                            logWriter.Flush();
                        }
                    }
                    else
                    {
                        if (messageToSend.failedCount >= smsResendRetryTimes)
                        {
                            smsSendingQueue.RemoveAt(0);

                            if (enableLogging)
                            {
                                logWriter.WriteLine(DateTime.Now + "," + messageToSend.getClientNum() + "," + messageToSend.getMsgContent() + ", failed to send too many times. Giving up retrying.");
                                logWriter.Flush();
                            }
                        }
                        else
                        {
                            if (enableLogging)
                            {
                                logWriter.WriteLine(DateTime.Now + "," + messageToSend.getClientNum() + "," + messageToSend.getMsgContent() + ", message failed to send, retrying in " + sendSMSPollingFreq * (messageToSend.failedCount + 1) + " seconds.");
                                logWriter.Flush();
                            }

                            messageToSend.failedCount++;
                            Thread.Sleep(sendSMSPollingFreq * messageToSend.failedCount * 1000);
                        }
                    }
                }

                Thread.Sleep(sendSMSPollingFreq * 1000);
            }
        }
Exemplo n.º 4
0
        private void ResendFailedToSentMsgs(Object myObject, EventArgs myEventArgs)
        {
            int initialCount = unfinishedSentMsgList.Count;

            foreach (KeyValuePair <String, SentMessage> entry in unfinishedSentMsgList)
            {
                // do something with entry.Value or entry.Key
                DateTime messageSentTime = entry.Value.getSentTime();
                DateTime currentTime     = DateTime.Now;
                TimeSpan ts = currentTime.Subtract(messageSentTime);

                // resend message if it's been over an hour since its last been sent.
                if (ts.Hours >= 1)
                {
                    String[] msgContentArray = entry.Value.getData();

                    for (int i = 0; i < msgContentArray.Length; i++)
                    {
                        String smsToSend = receiveControlPrefix + entry.Value.getSendDataType() + entry.Value.getMsgID()
                                           + entry.Value.getNumOfMessages() + convertPartNumberToChar(i + 1) + "0"
                                           + msgContentArray[i];

                        char crcChar = getCRC86(smsToSend);

                        //insert CRC char
                        smsToSend = smsToSend.Remove(13, 1);
                        String msgContent = smsToSend.Insert(13, crcChar.ToString());


                        // resend message back to the server
                        MessageToSend messageToSend = new MessageToSend(entry.Value.getMsgRecipient(), msgContent);
                        smsSendingQueue.Add(messageToSend);
                    }
                }
            }
        }
Exemplo n.º 5
0
        private void SmsInterceptor_MessageReceived(object sender, MessageInterceptorEventArgs me)
        {
            SmsMessage request = me.Message as SmsMessage;

            if (request != null)
            {
                // Look at the body of the SMS message to determine
                String incomeData = request.Body.Substring(receiveControlPrefix.Length);
                String msgType = incomeData.Substring(0, 3);
                String msgID = incomeData.Substring(3, 2);

                switch (msgType)
                {
                    case "srv": // all parts received.
                        unfinishedSentMsgList.Remove(msgID);
                        Console.WriteLine("message received: " + msgID);
                        break;
                    case "rty": // something to resend
                        if (unfinishedSentMsgList.ContainsKey(msgID))
                        {
                            // find the messages to resend
                            String msgsToResendPart = incomeData.Substring(5);

                            String[] unfinishedMsg = unfinishedSentMsgList[msgID].getData();

                            for (int i = 0; i < msgsToResendPart.Length; i++)
                            {
                                char resendPartChar = msgsToResendPart[i];

                                String smsToSend = receiveControlPrefix + unfinishedSentMsgList[msgID].getSendDataType() + unfinishedSentMsgList[msgID].getMsgID()
                                    + unfinishedSentMsgList[msgID].getNumOfMessages() + resendPartChar + "0"
                                    + unfinishedMsg[convertPartNumberCharToInt(resendPartChar) - 1];

                                char crcChar = getCRC86(smsToSend);

                                //insert CRC char
                                smsToSend = smsToSend.Remove(13, 1);
                                String msgContent = smsToSend.Insert(13, crcChar.ToString());

                                // resend message back to the server
                                MessageToSend messageToSend = new MessageToSend(request.From.Address, msgContent);
                                smsSendingQueue.Add(messageToSend);
                            }

                        }
                        else
                        {
                            String msgContent = "SMS-" + receivingProjectCode + receivingProgramCode + "ukn" + msgID;

                            // send a message-unknown 'ukn' back to the server
                            MessageToSend messageToSend = new MessageToSend(request.From.Address, msgContent);
                            smsSendingQueue.Add(messageToSend);
                        }
                        break;
                }

            }
        }
Exemplo n.º 6
0
        private void ResendFailedToSentMsgs(Object myObject, EventArgs myEventArgs)
        {
            int initialCount = unfinishedSentMsgList.Count;

            foreach (KeyValuePair<String, SentMessage> entry in unfinishedSentMsgList)
            {
                // do something with entry.Value or entry.Key
                DateTime messageSentTime = entry.Value.getSentTime();
                DateTime currentTime = DateTime.Now;
                TimeSpan ts = currentTime.Subtract(messageSentTime);

                // resend message if it's been over an hour since its last been sent.
                if (ts.Hours >= 1)
                {
                    String[] msgContentArray = entry.Value.getData();

                    for (int i = 0; i < msgContentArray.Length; i++)
                    {
                        String smsToSend = receiveControlPrefix + entry.Value.getSendDataType() + entry.Value.getMsgID()
                                        + entry.Value.getNumOfMessages() + convertPartNumberToChar(i + 1) + "0"
                                        + msgContentArray[i];

                        char crcChar = getCRC86(smsToSend);

                        //insert CRC char
                        smsToSend = smsToSend.Remove(13, 1);
                        String msgContent = smsToSend.Insert(13, crcChar.ToString());

                        // resend message back to the server
                        MessageToSend messageToSend = new MessageToSend(entry.Value.getMsgRecipient(), msgContent);
                        smsSendingQueue.Add(messageToSend);
                    }
                }

            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Send a regular sms, DON'T use this to send logs to server
        /// </summary>
        /// <param name="msgRecipient"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public SMSErrorMessage sendRegularSMS(String msgRecipient, String msgContent)
        {
            // check msgRecipient
            if (msgRecipient.Length != 10)
                return SMSErrorMessage.RecipientNumberFormatIncorrect;

            MessageToSend messageToSend = new MessageToSend(msgRecipient, msgContent);
            smsSendingQueue.Add(messageToSend);

            return SMSErrorMessage.None;
        }
Exemplo n.º 8
0
        /// <summary>
        /// Send Text Message that wil be controlled by the SMS protocol we defined.
        /// </summary>
        /// <param name="msgRecipient">10 digit phone number for the message recipient</param>
        /// <param name="receivingProjectCode">1 char for the project</param>
        /// <param name="receivingProgramCode">1 char for the program</param>
        /// <param name="msgType">3 chars for the msgType, developer can define it.</param>
        /// <param name="msg"></param>
        /// <param name="mustGoThrough"></param>
        /// <returns></returns>
        public SMSErrorMessage sendControlledSMS(String msgRecipient, Char receivingProjectCode, Char receivingProgramCode, String msgType, String msg, bool mustGoThrough)
        {
            String smsHeaderBeforeCRCPartNum = "";
            String smsHeader = "";

            // check msgRecipient
            if (msgRecipient.Length != 10)
                return SMSErrorMessage.RecipientNumberFormatIncorrect;

            // check message content for invalid chars
            if (!checkValidChars(msg))
                return SMSErrorMessage.InvalidCharInMessage;

            // deal with each parameter seperately.
            // first thing's first. Does the message need to go through?
            if (mustGoThrough)
            {
                smsHeader = "SMS+";
            }
            else
            {
                smsHeader = "SMS-";
            }

            smsHeaderBeforeCRCPartNum += smsHeader;

            String destinationInfo = "";

            // destination information
            // check project code and program code availability.
            if (!arrayContains(usableCharset, receivingProjectCode))
                return SMSErrorMessage.InvalidProjectCode;
            else
                destinationInfo += receivingProjectCode;

            if (!arrayContains(usableCharset, receivingProgramCode))
                return SMSErrorMessage.InvalidProgramCode;
            else
                destinationInfo += receivingProgramCode;

            smsHeaderBeforeCRCPartNum += destinationInfo;

            // msgType 3 chars
            // didn't really check the charactor availability for the msgtypes
            if (msgType.Length != 3)
                return SMSErrorMessage.MessageTypeNotEqualTo3;

            smsHeaderBeforeCRCPartNum += msgType;

            // removing the compression part for the time being because I can't find a way to decompress it in
            // the Java server code.

            // compress the data first and see if it's shorter
            //byte[] compressedStr = CompressString(msg);

            //---convert the compressed text to base 64---
            //String compressedStrBase64 = System.Convert.ToBase64String(compressedStr, 0, compressedStr.Length);

            String msgID = "";
            Char numberOfParts = ',';
            String[] messagesToSend = null;

            //---if compressed text is larger than original text, send original text instead---
            // for the time being, as compression is not used, I am currently letting the system ALWAYS send
            // uncompressed text.
            //if (compressedStrBase64.Length > msg.Length)
            if (true)
            {
                msgID = getMsgID(false);
                messagesToSend = splitMessage(msg);
            }
            else
            {
                //msgID = getMsgID(true);
                //messagesToSend = splitMessage(compressedStrBase64);
            }

            smsHeaderBeforeCRCPartNum += msgID;

            numberOfParts = convertPartNumberToChar(messagesToSend.Length);

            if (numberOfParts == '!')
            {
                return SMSErrorMessage.MessageTooBig;
            }

            smsHeaderBeforeCRCPartNum += numberOfParts;

            // start sending
            for (int i = 0; i < messagesToSend.Length; i++)
            {
                String smsToSend = smsHeaderBeforeCRCPartNum;
                smsToSend += convertPartNumberToChar(i + 1) + ""; //let message number start with 1
                smsToSend += '0'; //initialize CRC char to 0 for CRC checksum
                smsToSend += messagesToSend[i];

                Char crcChar = getCRC86(smsToSend);

                //insert CRC char
                smsToSend = smsToSend.Remove(13, 1);
                String msgContent = smsToSend.Insert(13, crcChar.ToString());

                MessageToSend messageToSend = new MessageToSend(msgRecipient, msgContent);
                smsSendingQueue.Add(messageToSend);
            }

            // record messages sent
            if (mustGoThrough)
                unfinishedSentMsgList.Add(msgID, new SentMessage(msgRecipient, msgID, numberOfParts, msgType, false, messagesToSend, DateTime.Now));

            return SMSErrorMessage.None;
        }
Exemplo n.º 9
0
        /// <summary>
        /// Send Text Message that wil be controlled by the SMS protocol we defined.
        /// </summary>
        /// <param name="msgRecipient">10 digit phone number for the message recipient</param>
        /// <param name="receivingProjectCode">1 char for the project</param>
        /// <param name="receivingProgramCode">1 char for the program</param>
        /// <param name="msgType">3 chars for the msgType, developer can define it.</param>
        /// <param name="msg"></param>
        /// <param name="mustGoThrough"></param>
        /// <returns></returns>
        public SMSErrorMessage sendControlledSMS(String msgRecipient, Char receivingProjectCode, Char receivingProgramCode, String msgType, String msg, bool mustGoThrough)
        {
            String smsHeaderBeforeCRCPartNum = "";
            String smsHeader = "";

            // check msgRecipient
            if (msgRecipient.Length != 10)
            {
                return(SMSErrorMessage.RecipientNumberFormatIncorrect);
            }

            // check message content for invalid chars
            if (!checkValidChars(msg))
            {
                return(SMSErrorMessage.InvalidCharInMessage);
            }

            // deal with each parameter seperately.
            // first thing's first. Does the message need to go through?
            if (mustGoThrough)
            {
                smsHeader = "SMS+";
            }
            else
            {
                smsHeader = "SMS-";
            }

            smsHeaderBeforeCRCPartNum += smsHeader;

            String destinationInfo = "";

            // destination information
            // check project code and program code availability.
            if (!arrayContains(usableCharset, receivingProjectCode))
            {
                return(SMSErrorMessage.InvalidProjectCode);
            }
            else
            {
                destinationInfo += receivingProjectCode;
            }

            if (!arrayContains(usableCharset, receivingProgramCode))
            {
                return(SMSErrorMessage.InvalidProgramCode);
            }
            else
            {
                destinationInfo += receivingProgramCode;
            }

            smsHeaderBeforeCRCPartNum += destinationInfo;

            // msgType 3 chars
            // didn't really check the charactor availability for the msgtypes
            if (msgType.Length != 3)
            {
                return(SMSErrorMessage.MessageTypeNotEqualTo3);
            }

            smsHeaderBeforeCRCPartNum += msgType;

            // removing the compression part for the time being because I can't find a way to decompress it in
            // the Java server code.

            // compress the data first and see if it's shorter
            //byte[] compressedStr = CompressString(msg);

            //---convert the compressed text to base 64---
            //String compressedStrBase64 = System.Convert.ToBase64String(compressedStr, 0, compressedStr.Length);

            String msgID         = "";
            Char   numberOfParts = ',';

            String[] messagesToSend = null;

            //---if compressed text is larger than original text, send original text instead---
            // for the time being, as compression is not used, I am currently letting the system ALWAYS send
            // uncompressed text.
            //if (compressedStrBase64.Length > msg.Length)
            if (true)
            {
                msgID          = getMsgID(false);
                messagesToSend = splitMessage(msg);
            }
            else
            {
                //msgID = getMsgID(true);
                //messagesToSend = splitMessage(compressedStrBase64);
            }

            smsHeaderBeforeCRCPartNum += msgID;

            numberOfParts = convertPartNumberToChar(messagesToSend.Length);

            if (numberOfParts == '!')
            {
                return(SMSErrorMessage.MessageTooBig);
            }

            smsHeaderBeforeCRCPartNum += numberOfParts;

            // start sending
            for (int i = 0; i < messagesToSend.Length; i++)
            {
                String smsToSend = smsHeaderBeforeCRCPartNum;
                smsToSend += convertPartNumberToChar(i + 1) + ""; //let message number start with 1
                smsToSend += '0';                                 //initialize CRC char to 0 for CRC checksum
                smsToSend += messagesToSend[i];

                Char crcChar = getCRC86(smsToSend);

                //insert CRC char
                smsToSend = smsToSend.Remove(13, 1);
                String msgContent = smsToSend.Insert(13, crcChar.ToString());

                MessageToSend messageToSend = new MessageToSend(msgRecipient, msgContent);
                smsSendingQueue.Add(messageToSend);
            }

            // record messages sent
            if (mustGoThrough)
            {
                unfinishedSentMsgList.Add(msgID, new SentMessage(msgRecipient, msgID, numberOfParts, msgType, false, messagesToSend, DateTime.Now));
            }

            return(SMSErrorMessage.None);
        }