示例#1
0
        internal NMessage GetMessage(NMessage message)
        {
            NMessage tmpMessage = null;

            try
            {
                using (StreamReader sr = new StreamReader(message.LocalFileName))
                {
                    if (File.Exists(message.LocalFileName))
                    {
                        tmpMessage = new NMessage();
                        XmlSerializer des = new XmlSerializer(typeof(NMessage));
                        tmpMessage = (NMessage)des.Deserialize(new System.Xml.XmlTextReader(sr));
                        sr.Close();
                    }
                }
            }
            catch (Exception)
            {
#if (DEBUG)
                throw;
#endif
            }
            return(tmpMessage);
        }
示例#2
0
        public void DeleteMessage(NMessage message)
        {
            NMessage tmpMessage = this.GetMessage(message);

            if (tmpMessage != null)
            {
                tmpMessage.Deleted = true;
                try
                {
                    if (File.Exists(message.LocalFileName))
                    {
                        File.Delete(message.LocalFileName);
                    }
                    String fileName = ArchivePath + GenerateId() + ".xml";
                    tmpMessage.LocalFileName = fileName;
                    using (StreamWriter sw = new StreamWriter(fileName))
                    {
                        XmlSerializer ser = new XmlSerializer(typeof(NMessage));
                        ser.Serialize(sw, tmpMessage);
                        sw.Close();
                    }
                }
                catch (Exception)
                {
#if (DEBUG)
                    throw;
#endif
                }
            }
            this.UpdateIndex(NStorageFolder.Inbox);
            this.UpdateIndex(NStorageFolder.Archive);
        }
示例#3
0
        public int CompareTo(object obj)
        {
            if (obj is NMessage)
            {
                NMessage message = (NMessage)obj;

                return(message.Header.Date.CompareTo(this.Header.Date));
            }
            else
            {
                throw new ArgumentException("Object is not a NMessage.");
            }
        }
示例#4
0
        public void SaveOutbox(NMessage message)
        {
            try
            {
                String fileName = OutboxPath + GenerateId() + ".xml";
                using (StreamWriter sw = new StreamWriter(fileName))
                {
                    XmlSerializer ser = new XmlSerializer(typeof(NMessage));
                    ser.Serialize(sw, message);
                    sw.Close();
                }
                OutboxIndex.LastMessageId = message.Header.MsgID;
            }
            catch (Exception)
            {
#if (DEBUG)
                throw;
#endif
            }
        }
示例#5
0
        public NMessage ReciveMessage(NMessageHeader messageHeader)
        {
            NMessage message = null;

            if (mUserConfiguration == null)
            {
                return(message);
            }
            //Authorize(username, password, lastmsg,type) type=XML
            Hashtable parameters = new Hashtable();

            parameters.Add("username", mUserConfiguration.Username);
            parameters.Add("password", mUserConfiguration.Password);
            parameters.Add("messageid", messageHeader.MsgID);
            parameters.Add("type", "XML");
            parameters.Add("format", "amr");

            try
            {
                List <NMessageBody> messageBodysList = new List <NMessageBody>();

                WebRequest      requestGetMessage  = WebRequest.Create(mUserConfiguration.Url + BulildWebRequestQuery("XMLGetMessage", parameters));
                HttpWebResponse responseGetMessage = (HttpWebResponse)requestGetMessage.GetResponse();
                Stream          dataStream         = responseGetMessage.GetResponseStream();

                //String DATA = new StreamReader(dataStream).ReadToEnd();
                XmlSerializer des         = new XmlSerializer(typeof(NMessageBody));
                NMessageBody  messageBody = (NMessageBody)des.Deserialize(new System.Xml.XmlTextReader(dataStream));
                dataStream.Close();
                message = new NMessage(messageHeader, messageBody);
            }
            catch (Exception ex)
            {
#if (DEBUG)
                throw;
#endif
            }
            return(message);
        }
示例#6
0
 public int Compare(NMessage x, NMessage y)
 {
     return(x.CompareTo(y));
 }
示例#7
0
 public int CompareTo(NMessage message)
 {
     return(message.Header.Date.CompareTo(this.Header.Date));
 }
示例#8
0
        public DeliveryWorker(Configuration configuration, NSettings settings, NStorage storage, BackgroundWorker backgroundWorker)
        {
            try
            {
                this.NewMessages = 0;
                int i = 0;
                mConfiguration = configuration;
                mSettings      = settings;
                mStorage       = storage;
                Boolean    updateIndex = false;
                NTransport mTransport  = new NTransport(this.mConfiguration);

                backgroundWorker.ReportProgress(0);

                #region RECEIVING
                //Receiving Headers
                List <NMessageHeader> messageHeaders = mTransport.ReciveHeaders();

                if (messageHeaders != null && !mCancel)
                {
                    i = 0;
                    foreach (NMessageHeader messageHeader in messageHeaders)
                    {
                        if (!mStorage.InboxIndex.ExistsID(messageHeader.MsgID) && !mStorage.ArchiveIndex.ExistsID(messageHeader.MsgID))
                        {  //skip already downloaded
                            NMessage message = mTransport.ReciveMessage(messageHeader);
                            if (message != null)
                            {
                                mStorage.SaveInbox(message);
                            }
                            updateIndex = true;
                            NewMessages++;
                        }
                        backgroundWorker.ReportProgress(100 * i / (messageHeaders.Count));
                        i++;
                    }
                }

                if (updateIndex)
                {
                    mStorage.UpdateIndex(NStorageFolder.Inbox);
                }

                #endregion

                #region SENDING
                //Sending
                updateIndex = false;
                string[] mFileList = Directory.GetFiles(mStorage.OutboxPath, "*.xml");
                i = 1;
                if (mFileList.Length > 0)
                {
                    backgroundWorker.ReportProgress(100 * i / (mFileList.Length + 1));
                    foreach (String file in mFileList)
                    {
                        if (mCancel)
                        {
                            break;
                        }
                        NMessage message = new NMessage();
                        using (StreamReader sr = new StreamReader(file))
                        {
                            if (File.Exists(file))
                            {
                                XmlSerializer des = new XmlSerializer(typeof(NMessage));
                                message = (NMessage)des.Deserialize(new System.Xml.XmlTextReader(sr));
                                sr.Close();
                            }
                        }

                        if (message != null)
                        {
                            NResultSend resultSend = mTransport.SendMessage(message);
                            if (resultSend.Success)
                            {
                                if (File.Exists(storage.SentPath + file))
                                {
                                    File.Delete(storage.SentPath + file);
                                }
                                File.Move(file, storage.SentPath + Path.GetFileName(file));
                                updateIndex = true;
                            }
                            ;
                        }
                        i++;
                        backgroundWorker.ReportProgress(100 * i / (mFileList.Length + 1));
                    }
                    if (updateIndex)
                    {
                        mStorage.UpdateIndex(NStorageFolder.Outbox);
                        mStorage.UpdateIndex(NStorageFolder.Sent);
                    }
                }
                #endregion
            }
            catch (Exception)
            {
#if (DEBUG)
                throw;
#endif
            }
            backgroundWorker.ReportProgress(100);
        }
示例#9
0
        public NResultSend SendMessage(NMessage message)
        {
            NResultSend resultSend = new NResultSend();

            resultSend.Success = false;
            //$username, $password, $recipients, $compression, $bytes, $type, $format
            try
            {
                String recipients = "";
                foreach (String recipient in message.Header.To)
                {
                    recipients += recipient + ";";
                }
                //XMLSendMessage($username, $password, $recipients, $compression, $bytes, $type, $format)
                Hashtable parameters = new Hashtable();
                parameters.Add("username", mUserConfiguration.Username);
                parameters.Add("password", mUserConfiguration.Password);
                parameters.Add("recipients", recipients);
                parameters.Add("compression", "none");
                parameters.Add("bytes", 0);
                parameters.Add("type", "XML");
                parameters.Add("format", "amr");


                try
                {
                    String     boundary           = Guid.NewGuid().ToString();
                    WebRequest requestSendMessage = WebRequest.Create(mUserConfiguration.Url + BulildWebRequestQuery("XMLSendMessage", parameters));



                    requestSendMessage.ContentType = "multipart/form-data; boundary=" + boundary;
                    requestSendMessage.Method      = "POST";


                    //encode header
                    String postHeader = "";
                    postHeader += "\r\n" + @"--" + boundary + "\r\n";
                    postHeader += "Content-Disposition: form-data; name=\"uploadFile1\"; filename=\"uploadFile1\"\r\n";
                    postHeader += "Content-Type: application/octet-stream\r\n\r\n";

                    byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);
                    byte[] boundaryBytes   = Encoding.UTF8.GetBytes("\r\n" + @"--" + boundary + @"--" + "\r\n");
                    //read in the file as a stream
                    long contentLength = postHeaderBytes.Length + message.Body.AttachementBodys[0].Data.Length + boundaryBytes.Length;

                    requestSendMessage.ContentLength = contentLength;

                    Stream requestStream = requestSendMessage.GetRequestStream();
                    requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
                    requestStream.Write(message.Body.AttachementBodys[0].Data, 0, message.Body.AttachementBodys[0].Data.Length);
                    requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
                    //requestStream.Close();


                    HttpWebResponse responseSendMessage = (HttpWebResponse)requestSendMessage.GetResponse();
                    Stream          dataStream          = responseSendMessage.GetResponseStream();
                    XmlSerializer   des = new XmlSerializer(typeof(NResultSend));
                    resultSend = (NResultSend)des.Deserialize(new System.Xml.XmlTextReader(dataStream));
                    dataStream.Close();
                }
                catch (Exception ex)
                {
#if (DEBUG)
                    throw;
#endif
                }
            }
            catch (Exception)
            {
#if (DEBUG)
                throw;
#endif
            }
            return(resultSend);
        }
示例#10
0
 public void MoveMessage(NStorageFolder destination, NMessage message)
 {
 }
示例#11
0
        public void UpdateIndex(NStorageFolder folder)
        {
            NStorageIndex storageIndex = null;
            String        storagePath  = null;

            switch (folder)
            {
            case NStorageFolder.Inbox:
                storageIndex = InboxIndex;
                break;

            case NStorageFolder.Archive:
                storageIndex = ArchiveIndex;
                break;

            case NStorageFolder.Outbox:
                storageIndex = OutboxIndex;
                break;

            case NStorageFolder.Sent:
                storageIndex = SentIndex;
                break;

            default:
                break;
            }
            if (storageIndex == null || !Directory.Exists(storageIndex.Path))
            {
                return;
            }

            String[] files = Directory.GetFiles(storageIndex.Path, "*.xml");

            //remove deleted

            for (int i = storageIndex.Messages.Count - 1; i >= 0; i--)
            {
                bool exists = false;
                for (int j = 0; j < files.Length; j++)
                {
                    if (files[j] == storageIndex.Messages[i].LocalFileName)
                    {
                        exists = true;
                    }
                }
                if (!exists)
                {
                    storageIndex.Messages.RemoveAt(i);
                }
            }
            //add new
            for (int i = 0; i < files.Length; i++)
            {
                if (!storageIndex.ExistsFile(files[i]))
                {
                    NMessage message = new NMessage();
                    using (StreamReader sr = new StreamReader(files[i]))
                    {
                        if (File.Exists(files[i]))
                        {
                            XmlSerializer des = new XmlSerializer(typeof(NMessage));
                            message = (NMessage)des.Deserialize(new System.Xml.XmlTextReader(sr));
                            sr.Close();
                        }
                    }

                    if (message != null)
                    {
                        message.Body = null;
                        storageIndex.Messages.Add(message);
                        storageIndex.LastMessageId = message.Header.MsgID;
                    }
                }
            }

            //save index
            try
            {
                using (StreamWriter sw = new StreamWriter(storagePath + ".index"))
                {
                    XmlSerializer ser = new XmlSerializer(typeof(NStorageIndex));
                    ser.Serialize(sw, storageIndex);
                    sw.Close();
                }
            }
            catch (Exception ex)
            {
#if (DEBUG)
                throw;
#endif
            }

            storageIndex.SortIndex();
            storageIndex.OnIndexChanged(storageIndex, new EventArgs());
        }