Пример #1
0
        public void Test_packFileInfo()
        {
            byte[]          test  = new byte[] { 0x03, 0x00, 0x00, 0x00 };
            RemoteFile.File file1 = new RemoteFile.File();
            file1.name    = "test.txt";
            file1.length  = 100;
            file1.address = 10000;
            List <byte> data = RemoteFileUtil.packFileInfo(file1, "<");

            Assert.AreEqual(data.Count, RemoteFile.Constants.RMF_FILEINFO_BASE_LEN + file1.name.Length + 1); // +1 null termination
            Assert.IsTrue(data.GetRange(0, 4).SequenceEqual(new List <byte> {
                0x03, 0x00, 0x00, 0x00
            }));
            Assert.IsTrue(data.GetRange(4, 4).SequenceEqual(new List <byte> {
                0x10, 0x27, 0x00, 0x00
            }));
            Assert.IsTrue(data.GetRange(8, 4).SequenceEqual(new List <byte> {
                0x64, 0x00, 0x00, 0x00
            }));
            Assert.IsTrue(data.GetRange(12, 4).SequenceEqual(new List <byte> {
                0x00, 0x00, 0x00, 0x00
            }));
            foreach (byte b in data.GetRange(16, 32))
            {
                Assert.IsTrue(b == 0);
            }
            byte[] bname    = data.GetRange(48, data.Count - 48 - 1).ToArray(); // -1 for null termination
            string fileName = System.Text.Encoding.ASCII.GetString(bname);

            Assert.AreEqual("test.txt", fileName);
            Assert.IsTrue(data[data.Count - 1] == 0);
        }
Пример #2
0
 protected override void _processCmd(List <byte> data)
 {
     if (data.Count >= 4)
     {
         uint cmd = BitConverter.ToUInt32(data.GetRange(0, 4).ToArray(), 0);
         if (cmd == RemoteFile.Constants.RMF_CMD_FILE_INFO)
         {
             RemoteFile.File remoteFile = RemoteFileUtil.unPackFileInfo(data);
             for (int i = 0; i < requestedFiles.Count; i++)
             {
                 if (requestedFiles[i].name == remoteFile.name)
                 {
                     requestedFiles[i].address    = remoteFile.address;
                     requestedFiles[i].fileType   = remoteFile.fileType;
                     requestedFiles[i].digestType = remoteFile.digestType;
                     requestedFiles[i].digestData = remoteFile.digestData;
                     requestedFiles[i].open();
                     remoteFileMap.insert(requestedFiles[i]);
                     Msg msg = new Msg(RemoteFile.Constants.RMF_MSG_FILEOPEN, 0, 0, requestedFiles[i].address);
                     requestedFiles.RemoveAt(i);
                     msgQueue.Add(msg);
                 }
                 else
                 {
                     remoteFileMap.insert(remoteFile);
                 }
             }
         }
         else if (cmd == RemoteFile.Constants.RMF_CMD_FILE_OPEN)
         {
             uint address = RemoteFileUtil.unPackFileOpen(data, "<");
             Console.WriteLine("received open command, address: " + address);
             Apx.File file = localFileMap.findByAddress(address);
             if (file != null)
             {
                 Console.WriteLine("received open command, name: " + file.name);
                 file.open();
                 List <byte> fileContent = file.read(0, (int)file.length);
                 if (fileContent.Count > 0)
                 {
                     Msg msg = new Msg(RemoteFile.Constants.RMF_MSG_WRITE_DATA, file.address, 0, fileContent);
                     msgQueue.Add(msg);
                 }
             }
         }
         else if (cmd == RemoteFile.Constants.RMF_CMD_FILE_CLOSE)
         {
             throw new NotImplementedException();
         }
         else
         {
             throw new ArgumentException("Unknown command, cannot process");
         }
     }
     else
     {
         throw new ArgumentException("too short command to proccess");
     }
 }
Пример #3
0
        public bool assignFileAddressDefault(RemoteFile.File file)
        {
            bool res = false;

            // Typecast RemoteFile.File to Apx.File
            Apx.File apxFile = (Apx.File)file;

            if (apxFile.name.EndsWith(".in") || apxFile.name.EndsWith(".out"))
            {
                res = assignFileAddress(apxFile, Constants.PORT_DATA_START, Constants.DEFINITION_START, Constants.PORT_DATA_BOUNDARY);
            }
            else if (apxFile.name.EndsWith(".apx"))
            {
                res = assignFileAddress(apxFile, Constants.DEFINITION_START, Constants.USER_DATA_START, Constants.DEFINITION_BOUNDARY);
            }
            else
            {
                res = assignFileAddress(apxFile, Constants.USER_DATA_START, Constants.USER_DATA_END, Constants.USER_DATA_BOUNDARY);
            }
            return(res);
        }
Пример #4
0
        // _keys is not used in the c# version of Apx


        public bool insert(RemoteFile.File file)
        {
            bool res = false;

            // Is this even needed anymore?
            if (file.address == uint.MaxValue)
            {
                res = assignFileAddressDefault(file);
                if (res)
                {
                    sortedAddFileToList((Apx.File)file);
                }
                // else return false;
            }
            else
            {
                sortedAddFileToList((Apx.File)file);
                res = true;
            }

            return(res);
        }
Пример #5
0
        public void Test_unPackFileInfo()
        {
            // Header content
            List <byte> data = new List <byte> {
                0x03, 0x00, 0x00, 0x00, 0x10, 0x27, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
            };
            // Digest data
            List <byte> ddata = Enumerable.Repeat((byte)0, 32).ToList();

            data.AddRange(ddata);
            // Name
            List <byte> ndata = ASCIIEncoding.ASCII.GetBytes("test.txt").ToList();

            data.AddRange(ndata);
            // Null termination
            data.Add(0);
            RemoteFile.File file = RemoteFileUtil.unPackFileInfo(data, "<");

            Assert.AreEqual(file.address, (uint)10000);
            Assert.IsTrue(file.digestData.SequenceEqual(ddata));
            Assert.AreEqual(file.digestType, RemoteFile.Constants.RMF_DIGEST_TYPE_NONE);
            Assert.AreEqual(file.name, "test.txt");
        }
Пример #6
0
 public bool remove(RemoteFile.File file)
 {
     Apx.File apxFile = (Apx.File)file;
     throw new System.NotImplementedException("remove not implemented");
 }