Esempio n. 1
0
        public void Dispose()
        {
            if (!isClosed)
            {
                if (null != mappedBuffer)
                {
                    IoUtil.Unmap(mappedBuffer);
                }

                isClosed = true;
            }
        }
Esempio n. 2
0
        public static MappedByteBuffer MapExistingFile(FileInfo cncFile, Action <string> logger)
        {
            if (cncFile.Exists)
            {
                if (null != logger)
                {
                    logger("INFO: CnC file exists: " + cncFile);
                }

                return(IoUtil.MapExistingFile(cncFile, cncFile.ToString()));
            }

            return(null);
        }
Esempio n. 3
0
        public static MappedByteBuffer MapNewOrExistingCncFile(FileInfo cncFile, bool shouldPreExist,
                                                               int versionFieldOffset, int timestampFieldOffset, long totalFileLength, long timeoutMs,
                                                               IEpochClock epochClock, Action <int> versionCheck, Action <string> logger)
        {
            MappedByteBuffer cncByteBuffer = null;

            try
            {
                cncByteBuffer = IoUtil.MapNewOrExixtingFile(cncFile, totalFileLength);

                UnsafeBuffer cncBuffer = new UnsafeBuffer(cncByteBuffer);

                if (shouldPreExist)
                {
                    int cncVersion = cncBuffer.GetIntVolatile(versionFieldOffset);

                    if (null != logger)
                    {
                        logger("INFO: CnC file exists: " + cncFile);
                    }

                    versionCheck(cncVersion);

                    long timestamp    = cncBuffer.GetLongVolatile(timestampFieldOffset);
                    long now          = epochClock.Time();
                    long timestampAge = now - timestamp;

                    if (null != logger)
                    {
                        logger("INFO: heartbeat is (ms): " + timestampAge);
                    }

                    if (timestampAge < timeoutMs)
                    {
                        throw new System.InvalidOperationException("Active CnC file detected");
                    }
                }
            }
            catch (Exception)
            {
                if (null != cncByteBuffer)
                {
                    IoUtil.Unmap(cncByteBuffer);
                }

                throw;
            }

            return(cncByteBuffer);
        }
Esempio n. 4
0
        public static MappedByteBuffer MapExistingFile(FileInfo cncFile, Action <string> logger, long offset,
                                                       long length)
        {
            if (cncFile.Exists)
            {
                if (null != logger)
                {
                    logger("INFO: CnC file exists: " + cncFile);
                }

                return(IoUtil.MapExistingFile(cncFile, offset, length));
            }

            return(null);
        }
Esempio n. 5
0
        public static void EnsureDirectoryExists(DirectoryInfo directory, string filename, bool warnIfDirectoryExists,
                                                 bool dirDeleteOnStart, int versionFieldOffset, int timestampFieldOffset, long timeoutMs,
                                                 IEpochClock epochClock, Action <int> versionCheck, Action <string> logger)
        {
            FileInfo cncFile = new FileInfo(Path.Combine(directory.FullName, filename));

            if (directory.Exists)
            {
                if (warnIfDirectoryExists && null != logger)
                {
                    logger("WARNING: " + directory + " already exists.");
                }

                if (!dirDeleteOnStart)
                {
                    int offset = Math.Min(versionFieldOffset, timestampFieldOffset);
                    int length = Math.Max(versionFieldOffset, timestampFieldOffset) + BitUtil.SIZE_OF_LONG - offset;
                    MappedByteBuffer cncByteBuffer = MapExistingFile(cncFile, logger, offset, length);

                    try
                    {
                        if (IsActive(cncByteBuffer, epochClock, timeoutMs, versionFieldOffset, timestampFieldOffset,
                                     versionCheck, logger))
                        {
                            throw new System.InvalidOperationException("Active CnC file detected");
                        }
                    }
                    finally
                    {
                        IoUtil.Unmap(cncByteBuffer);
                    }
                }

                IoUtil.Delete(directory, false);
            }

            IoUtil.EnsureDirectoryExists(directory, directory.ToString());
        }
Esempio n. 6
0
 public static MappedByteBuffer MapNewFile(FileInfo cncFile, long length)
 {
     return(IoUtil.MapNewFile(cncFile, length));
 }
Esempio n. 7
0
 public virtual void DeleteDirectory(bool ignoreFailures)
 {
     IoUtil.Delete(parentDir, ignoreFailures);
 }