Exemple #1
0
        public LensMessage ConvertFile(FileInfo file)
        {
            try
            {
                if (!file.Exists)
                {
                    throw new LensException(ErrorIndex.FILE_NOT_EXISTS);
                }

                String docType = LensMessage.GetDocType(file.Name);

                byte[] data = LensMessage.ReadFile(file.FullName);

                return(ConvertBinaryData(data, docType));
            }
            catch (SocketException sex)
            {
                throw new LensException(ErrorIndex.IO_READ_ERROR, sex);
            }
            catch (LensException lex)
            {
                throw lex;
            }
            catch (IOException iex)
            {
                throw new LensException(ErrorIndex.IO_READ_ERROR, iex);
            }
            catch (Exception ex)
            {
                throw new LensException(ErrorIndex.INTERNAL_ERR, ex);
            }
        }
Exemple #2
0
        /// <summary>
        /// Method to create a LensMessage object.
        /// The flags field contains bit values which indicate things like the
        /// presence of encryption or compression
        /// </summary>
        /// <param name="messageData"></param>
        /// <param name="messageID"></param>
        /// <param name="flags"></param>
        /// <param name="messageType">
        /// <see cref="ERROR_TYPE"/>
        /// <see cref="PING_TYPE"/>
        /// <see cref="TAG_TYPE"/>
        /// <see cref="LOGIN_TYPE"/>
        /// </param>
        /// <returns>LensMessage</returns>
        public static LensMessage Create(String messageData, int messageID, int flags, short messageType)
        {
            Header      header  = Header.Create(ToBytes(messageData).Length + 12, flags, Header.VERSION, messageID, messageType);
            LensMessage message = new LensMessage(header, messageData);

            return(message);
        }
Exemple #3
0
        public LensMessage GetInfo()
        {
            String      message   = "<bgtcmd><info></info></bgtcmd>";
            LensMessage inMessage = LensMessage.Create(message, LensMessage.XML_TYPE);

            return(SendMessage(inMessage));
        }
Exemple #4
0
        /// <summary>
        /// Method to create a LensMessage object with binary message data.
        /// The flags field contains bit values which indicate things like
        /// the presence of encryption or compression
        /// </summary>
        /// <param name="messageData"></param>
        /// <param name="messageID"></param>
        /// <param name="flags"></param>
        /// <param name="version"></param>
        /// <param name="messageType">
        /// <see cref="ERROR_TYPE"/>
        /// <see cref="PING_TYPE"/>
        /// <see cref="TAG_TYPE"/>
        /// <see cref="LOGIN_TYPE"/>
        /// </param>
        /// <returns>LensMessage</returns>
        public static LensMessage Create(byte[] messageData, int messageID, int flags, short version, short messageType)
        {
            Header      header  = Header.Create(messageData.Length + 12, flags, version, messageID, messageType);
            LensMessage message = new LensMessage(header, messageData);

            return(message);
        }
Exemple #5
0
        public LensMessage RegisterFile(FileInfo file, string vendor, string ID, DocKey docKey, char type)
        {
            try
            {
                if (!file.Exists)
                {
                    throw new LensException(ErrorIndex.FILE_NOT_EXISTS);
                }

                byte[] data    = LensMessage.ReadFile(file);
                String docType = LensMessage.GetDocType(file.Name);

                return(RegisterBinaryData(data, docType, vendor, ID, docKey, type));
            }
            catch (LensException lex)
            {
                throw lex;
            }
            catch (SocketException sex)
            {
                throw new LensException(ErrorIndex.IO_READ_ERROR, sex);
            }
            catch (IOException iex)
            {
                throw new LensException(ErrorIndex.IO_READ_ERROR, iex);
            }
            catch (Exception ex)
            {
                throw new LensException(ErrorIndex.INTERNAL_ERR, ex);
            }
        }
Exemple #6
0
 /// <summary>
 /// Method to retrive message body
 /// </summary>
 /// <returns>string</returns>
 public string GetMessageData()
 {
     if (messageData == null)
     {
         return(null);
     }
     return(LensMessage.ToString(messageData));
 }
Exemple #7
0
        public LensMessage TagBinaryDataWithRTF(byte[] data, string docType, char type)
        {
            try
            {
                switch (type)
                {
                case MSLens.RESUME_TYPE:
                case MSLens.POSTING_TYPE:
                    break;

                default:
                    throw new LensException(ErrorIndex.INVALID_DOC_TYPE);
                }

                byte[] body = null;

                int extraLen = 0;
                if (docType != null && docType.Length > 0)
                {
                    extraLen += docType.Length;
                }

                body = new byte[data.Length + 2 + extraLen];

                int index = 0;
                body[index++] = (byte)type;

                if (extraLen > 0)
                {
                    char[] docTypeBytes = docType.ToCharArray();
                    for (int i = 0; i < docTypeBytes.Length; i++)
                    {
                        body[index++] = (byte)docTypeBytes[i];
                    }
                }
                body[index++] = (byte)'\0';

                for (int i = 0; i < data.Length; i++)
                {
                    body[index++] = data[i];
                }

                //String messageData = LensMessage.toString(body);
                LensMessage tagMessage = LensMessage.Create(body, LensMessage.TAG_RTF_TYPE);
                return(SendMessage(tagMessage));
            }
            catch (LensException lex)
            {
                throw lex;
            }
            catch (Exception ex)
            {
                throw new LensException(ErrorIndex.INTERNAL_ERR, ex);
            }
        }
Exemple #8
0
 public LensMessage SendMessage(LensMessage message)
 {
     if (mTimeoutEnabled)
     {
         return(SendMessage(message, mTransactionTimeout));
     }
     else
     {
         return(SendMessage(message, 0));
     }
 }
Exemple #9
0
        public bool Ping()
        {
            LensMessage message = LensMessage.Create("", LensMessage.PING_TYPE);

            if (mTimeoutEnabled)
            {
                SendMessage(message, mTransactionTimeout);
            }
            else
            {
                SendMessage(message, 0);
            }
            return(true);
        }
Exemple #10
0
        public LensMessage ConvertBinaryData(byte[] data, string docType)
        {
            try
            {
                byte[] hint = null;
                byte[] body = null;

                if (docType != null)
                {
                    hint = LensMessage.ToBytes(docType);
                }

                if (hint == null || hint.Length < 1)
                {
                    body = new byte[data.Length + 1];
                }
                else
                {
                    body = new byte[hint.Length + data.Length + 1];
                }

                int index = 0;
                if (hint != null)
                {
                    for (int i = 0; i < hint.Length; i++)
                    {
                        body[index++] = hint[i];
                    }
                }
                body[index++] = Convert.ToByte('\0');

                for (int i = 0; i < data.Length; i++)
                {
                    body[index++] = data[i];
                }

                //String messageData = LensMessage.toString(body);
                LensMessage convertMessage = LensMessage.Create(body, LensMessage.CONVERT_TYPE);
                return(SendMessage(convertMessage));
            }
            catch (LensException lex)
            {
                throw lex;
            }
            catch (Exception ex)
            {
                throw new LensException(ErrorIndex.INTERNAL_ERR, ex);
            }
        }
Exemple #11
0
        public LensMessage Unregister(string vendor, DocKey docKey, char type)
        {
            string message = null;

            switch (type)
            {
            case MSLens.RESUME_TYPE:
                message = "<bgtcmd><unregister type = 'resume' vendor='" + vendor + "' key='" + docKey.GetKey() + "'/></bgtcmd>";
                break;

            case MSLens.POSTING_TYPE:
                message = "<bgtcmd><unregister type = 'posting' vendor='" + vendor + "' key='" + docKey.GetKey() + "'/></bgtcmd>";
                break;

            default:
                throw new LensException(ErrorIndex.INVALID_DOC_TYPE);
            }
            LensMessage unregMessage = LensMessage.Create(message, LensMessage.XML_TYPE);

            return(SendMessage(unregMessage));
        }
Exemple #12
0
        public LensMessage TagFile(FileInfo file, char type)
        {
            try
            {
                if (!file.Exists)
                {
                    throw new LensException(ErrorIndex.FILE_NOT_EXISTS);
                }

                string docType  = "";
                int    extIndex = file.Name.LastIndexOf(".");
                if (extIndex != -1)
                {
                    string fname = file.Name;
                    docType = fname.Substring(extIndex, file.Name.Length - extIndex);
                    docType = file.Name.Substring(extIndex, file.Name.Length - extIndex);
                }

                byte[] data = LensMessage.ReadFile(file);

                return(TagBinaryData(data, docType, type));
            }
            catch (SocketException sex)
            {
                throw new LensException(ErrorIndex.IO_READ_ERROR, sex);
            }
            catch (LensException lex)
            {
                throw lex;
            }
            catch (IOException iex)
            {
                throw new LensException(ErrorIndex.IO_READ_ERROR, iex);
            }
            catch (Exception ex)
            {
                throw new LensException(ErrorIndex.INTERNAL_ERR, ex);
            }
        }
Exemple #13
0
        public LensMessage Unregister(string vendor, string docID, DocKey docKey, char type)
        {
            String message = null;

            switch (type)
            {
            case MSLens.RESUME_TYPE:
                message = "<bgtcmd><unregister type = 'resume' vendor='" + vendor + "' id='" + docID + "'/></bgtcmd>";
                break;

            case MSLens.POSTING_TYPE:
                message = "<bgtcmd><unregister type = 'posting' vendor='" + vendor + "' id='" + docID + "'/></bgtcmd>";
                break;

            default:
                throw new LensException(ErrorIndex.INVALID_DOC_TYPE);
            }
            LensMessage unregMessage = LensMessage.Create(message, LensMessage.XML_TYPE);
            LensMessage outMessage   = SendMessage(unregMessage);
            String      result       = outMessage.GetMessageData();

            // this is cheating, but we just need to extract the key value
            int pos = result.IndexOf("key='");

            if (pos != -1)
            {
                pos += 5;                       // skip key='
                String key = result.Substring(pos, result.IndexOf("'", pos));
                docKey.SetKey(ulong.Parse(key));
            }
            else
            {
                docKey.SetKey(0);
            }

            return(outMessage);
        }
Exemple #14
0
        public LensMessage SendMessage(LensMessage message, ulong timeout)
        {
            mLock.Acquire();

            Timer sendTimer = null;

            mTimedout = false;

            try
            {
                if (!IsOpen())
                {
                    throw new LensException(ErrorIndex.SESSION_NOT_OPEN);
                }

                if (timeout < 0)
                {
                    throw new LensException(ErrorIndex.INVALID_TIMEOUT);
                }
                else
                {
                    sendTimer = new Timer(new TimerCallback(this.TimedOut), null, TimeSpan.FromSeconds(Convert.ToDouble(timeout)), TimeSpan.FromSeconds(Convert.ToDouble(timeout)));
                }

                message.WriteToSocket(tcpClient);

                return(ReadMessage());
            }
            catch (LensException lex)
            {
                throw lex;
            }
            catch (SocketException sex)
            {
                if (mTimedout)
                {
                    throw new LensException(ErrorIndex.UNABLE_TO_CONNECT, sex);
                }
                throw new LensException(ErrorIndex.WRITE_ERROR, sex);
            }
            catch (IOException iex)
            {
                if (mTimedout)
                {
                    throw new LensException(ErrorIndex.TIMEOUT_ERROR);
                }
                throw new LensException(ErrorIndex.WRITE_ERROR, iex);
            }
            catch (Exception ex)
            {
                if (mTimedout)
                {
                    throw new LensException(ErrorIndex.TIMEOUT_ERROR);
                }
                throw new LensException(ErrorIndex.INTERNAL_ERR, ex);
            }
            finally
            {
                try
                {
                    if (sendTimer != null)
                    {
                        sendTimer.Dispose();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.StackTrace + "\n");
                }
                mLock.Release();
            }
        }
Exemple #15
0
        private LensMessage ReadMessage()
        {
            Header header = null;

            byte[] buffer = null;

            if (!IsOpen())
            {
                throw new LensException(ErrorIndex.SESSION_NOT_OPEN);
            }

            // Read the message header first
            try
            {
                header = ReadHeader();
            }
            catch (SocketException sex)
            {
                if (mTimedout)
                {
                    throw new LensException(ErrorIndex.TIMEOUT_ERROR);
                }
                throw new LensException(ErrorIndex.INVALID_HEADER, sex);
            }
            catch (IOException ex)
            {
                if (mTimedout)
                {
                    throw new LensException(ErrorIndex.TIMEOUT_ERROR);
                }
                throw new LensException(ErrorIndex.INVALID_HEADER, ex);
            }

            if (header.GetLength() == 0)
            {
                throw new LensException(ErrorIndex.INVALID_MESSAGE,
                                        new ApplicationException("The message length returned from LENS is 0."));
            }

            //System.Threading.Thread.Sleep(2000);//Delay needed for slower Systems
            //Read the message body
            try
            {
                // Shouldn't we checkig the validity of the message size??
                // EP 15/01/07: Yes, we should! Checked below.

                try
                {
                    buffer = new byte[header.GetDataLength()];
                    int index = 0;

                    while (index < buffer.Length)
                    {
                        int i = tcpClient.GetStream().ReadByte();
                        if (i == -1)
                        {
                            break;
                        }

                        buffer[index++] = (byte)i;
                    }
                }
                catch (SocketException sex)
                {
                    if (mTimedout)
                    {
                        throw new LensException(ErrorIndex.TIMEOUT_ERROR);
                    }
                    throw new LensException(ErrorIndex.READ_ERROR, sex);
                }
                catch (IOException iex)
                {
                    if (mTimedout)
                    {
                        throw new LensException(ErrorIndex.TIMEOUT_ERROR);
                    }
                    throw new LensException(ErrorIndex.READ_ERROR, iex);
                }
                catch (Exception ex)
                {
                    throw new ApplicationException(string.Format("Failed to read message body. Header values:"
                                                                 + " length = {0}, flags = {1}, version = {2}, ID = {3}, type = {4}",
                                                                 header.GetLength(), header.GetFlags(), header.GetVersion(), header.GetMessageID(),
                                                                 header.GetMessageType()), ex);
                }
            }
            catch (Exception ex)
            {
                throw new LensException(ErrorIndex.INTERNAL_ERR, ex);
            }
            //Compose a LensMessage and send back to caller
            return(LensMessage.Create(header, buffer));
        }
Exemple #16
0
        public LensMessage RegisterBinaryData(byte[] data, string docType, string vendor, string ID, DocKey docKey, char type)
        {
            try
            {
                LensMessage tagMessage = TagBinaryData(data, docType, type);
                String      result     = tagMessage.GetMessageData();

                int    spos, epos;
                String cmd = "";

                try
                {
                    switch (type)
                    {
                    case MSLens.RESUME_TYPE:
                        spos = result.IndexOf("<resume");
                        epos = result.LastIndexOf("</resume>") + 9;

                        cmd  = "<bgtcmd><register type='resume' vendor='";
                        cmd += vendor;
                        cmd += "' id='";
                        cmd += ID;
                        cmd += "'>";
                        cmd += result.Substring(spos, epos - spos + 1);
                        cmd += "</register></bgtcmd>";

                        break;

                    case MSLens.POSTING_TYPE:
                        spos = result.IndexOf("<posting");
                        epos = result.LastIndexOf("</posting>") + 10;

                        cmd  = "<bgtcmd><register type='posting' vendor='";
                        cmd += vendor;
                        cmd += "' id='";
                        cmd += ID;
                        cmd += "'>";
                        cmd += result.Substring(spos, epos);
                        cmd += "</register></bgtcmd>";

                        break;

                    default:
                        throw new LensException(ErrorIndex.INVALID_DOC_TYPE);
                    }
                }
                catch (LensException lex)
                {
                    throw lex;
                }
                catch (IndexOutOfRangeException)
                {
                    throw new LensException(ErrorIndex.TAG_TEXT_NOT_GENERATED);
                    //return LensMessage.CreateErrorMessage("Tag text not generated.");
                }

                LensMessage regMessage = SendMessage(LensMessage.Create(cmd, LensMessage.XML_TYPE));
                result = regMessage.GetMessageData();

                // this is cheating, but we just need to extract the key value
                int pos = result.IndexOf("key='");
                if (pos != -1)
                {
                    pos += 5;                     // skip key='
                    string key = result.Substring(pos, result.IndexOf("'", pos));
                    docKey.SetKey(ulong.Parse(key));
                }
                else
                {
                    docKey.SetKey(0);
                }


                return(regMessage);
            }
            catch (LensException lex)
            {
                throw lex;
            }
            catch (Exception ex)
            {
                throw new LensException(ErrorIndex.INTERNAL_ERR, ex);
            }
        }
Exemple #17
0
 public LensMessage RegisterText(string text, string vendor, string ID, DocKey docKey, char type)
 {
     return(RegisterBinaryData(LensMessage.ToBytes(text), "", vendor, ID, docKey, type));
 }
Exemple #18
0
 public LensMessage TagTextWithRTF(string text, string docType, char type)
 {
     return(TagBinaryDataWithRTF(LensMessage.ToBytes(text), docType, type));
 }