Exemplo n.º 1
0
        private CoreLogic coreLogic;           // the Server logic instance


        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        public Manager()
        {
            log            = LogStub.Instance;
            storageWrapper = null;
            coreLogic      = null;
            AppDirs        = new ServerDirs();
            AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Application start.
        /// </summary>
        protected void Application_Start()
        {
            ConfigurationLoader.AddObjectFactory(new EviStorageFactoryForQueryHandler());

            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            StorageWrapper.AppStart();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes the application storage.
        /// </summary>
        private bool InitStorage()
        {
            storageWrapper = new StorageWrapper(new StorageContext
            {
                App     = ServiceApp.Web,
                AppDirs = AppDirs,
                Log     = Log
            }, InstanceConfig);

            return(storageWrapper.InitStorage());
        }
Exemplo n.º 4
0
        private Stats stats;                       // provides a statistics ID


        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        public WebContext()
        {
            storageWrapper       = null;
            configThread         = null;
            terminated           = false;
            pluginsReady         = false;
            configUpdateRequired = false;
            configUpdateStep     = ConfigUpdateStep.Idle;
            stats = null;

            IsReady                    = false;
            IsReadyToLogin             = false;
            InstanceConfig             = new InstanceConfig();
            AppConfig                  = new WebConfig();
            AppDirs                    = new WebDirs();
            Log                        = LogStub.Instance;
            ConfigDatabase             = new ConfigDatabase();
            ClientPool                 = new ScadaClientPool();
            PluginHolder               = new PluginHolder();
            CacheExpirationTokenSource = new CancellationTokenSource();

            AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
        }
Exemplo n.º 5
0
        public  void SetConverted(object action, string val, IMvcContext context, Action<object,object> directsetter) {
			if(null==val) {
				directsetter(action, null);
				return;
			}
            var sys = System;
            if(ContextSystem) {
                sys = context.Get("system");
            }
            if(sys.noContent()) {
                sys = System;
                if(sys.noContent()) {
                    sys = "Default";
                }
            }
            
            this.storage = this.storage ?? myapp.storage.Get(TargetType, sys, true);
            object key = val;
            if(val.ToStr().like(@"^\d+$")) {
                key = key.toInt();
            }
            var realval = storage.Load(TargetType, key, sys);
            directsetter(action, realval);
        }
 protected StorageWrapper<object> getStorage(string entity)
 {
     if (null == cachedstorage) {
         var map =
             Enumerable.FirstOrDefault(myapp.ioc.get<IConfigurationProvider>().Get(this.System).ClassMappings,
                                       x =>
                                           {
                                               if (entity.Contains(".")) {
                                                   return x.EntityName.ToLower() == entity.ToLower();
                                               }
                                               else {
                                                   return x.EntityName.ToLower().EndsWith("." + entity.ToLower());
                                               }
                                           });
         if (null == map) {
             throw new Exception("Entity " + entity + " not found");
         }
         var type = map.ClassName.toType();
         this.type = type;
         this.storage = myapp.storage.Get(type, System, true);
         cachedstorage =  this.storage;
     }
     return cachedstorage;
 }
Exemplo n.º 7
0
 public DataHelper(){
     this.storage = myapp.storage.GetDefault();
 }
Exemplo n.º 8
0
 /// <summary>
 /// Application end.
 /// </summary>
 protected void Application_End()
 {
     StorageWrapper.AppEnd();
 }
Exemplo n.º 9
0
        /// <summary>
        /// Starts the service.
        /// </summary>
        public bool StartService()
        {
#if DEBUG
            System.Diagnostics.Debugger.Launch();
#endif

            // load instance configuration
            AppDirs.Init(Assembly.GetExecutingAssembly());
            InstanceConfig instanceConfig = new InstanceConfig();
            Locale.SetCultureToEnglish();

            if (instanceConfig.Load(InstanceConfig.GetConfigFileName(AppDirs.InstanceDir), out string errMsg))
            {
                Locale.SetCulture(instanceConfig.Culture);
                AppDirs.UpdateLogDir(instanceConfig.LogDir);
            }
            else
            {
                Console.WriteLine(errMsg);
                Locale.SetCultureToDefault();
            }

            // initialize log
            LogFile logFile = new LogFile(LogFormat.Full, Path.Combine(AppDirs.LogDir, CommUtils.LogFileName))
            {
                Capacity = int.MaxValue
            };

            log = logFile;
            log.WriteBreak();

            // prepare to start service
            log.WriteAction(Locale.IsRussian ?
                            "Коммуникатор {0} запущен" :
                            "Communicator {0} started", EngineUtils.AppVersion);

            storageWrapper = new StorageWrapper(new StorageContext
            {
                App     = ServiceApp.Comm,
                AppDirs = AppDirs,
                Log     = log
            }, instanceConfig);

            CommConfig appConfig = new CommConfig();

            if (AppDirs.CheckExistence(out errMsg) &&
                LocalizeApp() &&
                storageWrapper.InitStorage() &&
                appConfig.Load(storageWrapper.Storage, CommConfig.DefaultFileName, out errMsg))
            {
                // start service
                logFile.CapacityMB = appConfig.GeneralOptions.MaxLogSize;
                coreLogic          = new CoreLogic(appConfig, AppDirs, storageWrapper.Storage, log);

                if (coreLogic.StartProcessing())
                {
                    return(true);
                }
            }
            else if (!string.IsNullOrEmpty(errMsg))
            {
                log.WriteError(errMsg);
            }

            log.WriteError(CommonPhrases.ExecutionImpossible);
            return(false);
        }
 public PersistentRoleProvider()
 {
     storage = myapp.storage.Get<IRoleResolverRecord>(false);
 }
Exemplo n.º 11
0
        public static void StartDownload(Parameters parameters, Flag doneFlag, StatusDelegate statusFunction, ErrorDelegate errorFunction, FinishedDelegate finishedFunction)
        {
            if (parameters.ResponseFile.Length == 0 && parameters.Url.Length == 0)
            {
                throw new BitTorrentException("需要Response file 或者 Url");
            }

            Parameters = parameters;
            Stream stream = null;

            byte[] response;
            long   length = 0;

            try
            {
                if (parameters.ResponseFile.Length != 0)
                {
                    stream = File.OpenRead(parameters.ResponseFile);
                    length = stream.Length;
                }

                else
                {
                    WebRequest  webRequest  = WebRequest.Create(parameters.Url);
                    WebResponse webResponse = webRequest.GetResponse();
                    stream = webResponse.GetResponseStream();
                    length = webResponse.ContentLength;
                }

                response = new byte[length];
                stream.Read(response, 0, (int)length);
            }

            catch
            {
                throw new BitTorrentException("Problem getting response info");
            }

            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }

            DictNode rootNode;

            try
            {
                rootNode = BEncodingFactory.Decode(response) as DictNode;
                //BTFormat.CheckMessage(rootNode);
            }
            catch
            {
                throw new BitTorrentException("got bad file");
            }

            DictNode       infoNode = rootNode["info"] as DictNode;
            List <BitFile> files    = new List <BitFile>();
            string         file;
            long           fileLength;

            try
            {
                if (infoNode.ContainsKey("length"))
                {
                    fileLength = (infoNode["length"] as IntNode).Value;
                    BytesNode nameNode = (infoNode["name"] as BytesNode);
                    if (nameNode == null)
                    {
                        return;
                    }
                    file = @"k:\torrent\" + nameNode.StringText;
                    Make(file, false);
                    files.Add(new BitFile(file, fileLength));
                }

                else
                {
                    fileLength = 0L;
                    ListNode filesNode = infoNode["files"] as ListNode;
                    foreach (BEncodedNode handler in filesNode)
                    {
                        DictNode fileNode = infoNode["files"] as DictNode;
                        fileLength += (fileNode["length"] as IntNode).Value;
                    }
                    //访问文件夹
                    BytesNode nameNode = infoNode["name"] as BytesNode;
                    if (nameNode == null)
                    {
                        return;
                    }
                    file = @"C:\torrent\" + nameNode.StringText;
                    // if this path exists, and no files from the info dict exist, we assume it's a new download and
                    // the user wants to create a new directory with the default name
                    bool existed = false;
                    if (Directory.Exists(file))
                    {
                        foreach (BEncodedNode handler in filesNode)
                        {
                            DictNode fileNode = handler as DictNode;
                            ListNode pathNode = fileNode["path"] as ListNode;
                            if (File.Exists(Path.Combine(file, (pathNode[0] as BytesNode).StringText)))
                            {
                                existed = true;
                                break;
                            }
                        }

                        if (!existed)
                        {
                            file = Path.Combine(file, (infoNode["name"] as BytesNode).StringText);
                        }
                    }
                    Make(file, true);

                    // alert the UI to any possible change in path
                    //TODO: if (pathFunc != null)
                    // pathFunc(file)

                    foreach (BEncodedNode handler in filesNode)
                    {
                        DictNode fileNode = handler as DictNode;
                        ListNode pathNode = fileNode["path"] as ListNode;
                        string   n        = file;
                        foreach (BEncodedNode stringHandler in pathNode)
                        {
                            n = Path.Combine(n, (stringHandler as BytesNode).StringText);
                        }
                        files.Add(new BitFile(n, (fileNode["length"] as IntNode).Value));
                        Make(n, false);
                    }
                }
            }
            catch
            {
                throw new BitTorrentException("Couldn't allocate directory...");
            }

            Flag           finishFlag     = new Flag();
            FinishedHelper finishedHelper = new FinishedHelper();

            finishedHelper.ErrorFunction    = errorFunction;
            finishedHelper.FinishedFunction = finishedFunction;
            finishedHelper.DoneFlag         = finishFlag;

            string sID = DateTime.Now.ToLongDateString() + "www.wallywood.co.uk";

            byte[] myID = Globals.GetSha1Hash(Encoding.ASCII.GetBytes(sID));//Globals.Sha1.ComputeHash(Encoding.Default.GetBytes(sID));

            byte[]        piece  = (infoNode["pieces"] as BytesNode).ByteArray;
            List <byte[]> pieces = new List <byte[]>();

            for (int i = 0; i < piece.Length; i += 20)
            {
                byte[] temp = new byte[20];
                Buffer.BlockCopy(piece, i, temp, 0, 20);
                pieces.Add(temp);
            }

            Storage _storage = null;

            try
            {
                try
                {
                    //_storage = new Storage(files, parameters.AllocatePause, statusFunction);
                    finishedHelper.Storage = _storage;
                }
                catch (Exception ex)
                {
                    errorFunction("trouble accessing files - " + ex.Message);
                }
                IntNode pieceLengthNode = infoNode["piece length"] as IntNode;
                StorageWrapper = new StorageWrapper(_storage, parameters.DownloadSliceSize, pieces, (int)pieceLengthNode.Value,
                                                    finishedHelper.Finished, finishedHelper.Failed, statusFunction, finishFlag, parameters.CheckHashes,
                                                    finishedHelper.DataFlunked);
            }
            // Catch ValueError
            // failed("bad data")
            // catch IO Error
            catch (Exception ex)
            {
                finishedHelper.Failed("Problem - " + ex.Message);
            }

            if (finishFlag.IsSet)
            {
                return;
            }

            RawServer rawServer = new RawServer(finishFlag, parameters.TimeoutCheckInterval, parameters.Timeout, false);

            if (parameters.MaxPort < parameters.MinPort)
            {
                int temp = parameters.MinPort;
                parameters.MinPort = parameters.MaxPort;
                parameters.MaxPort = parameters.MinPort;
            }

            ushort listenPort;

            for (listenPort = parameters.MinPort; listenPort <= parameters.MaxPort; listenPort++)
            {
                try
                {
                    rawServer.Bind(listenPort, parameters.Bind, false);
                    break;
                }
                catch (SocketException)
                {
                    //TODO: Error Code
                }
            }

            //TODO: Check whether nothing bound
            Choker = new Choker(parameters.MaxUploads, rawServer.AddTask, finishFlag);
            Measure     uploadMeasure   = new Measure(parameters.MaxRatePeriod, parameters.UploadRateFudge);
            Measure     downloadMeasure = new Measure(parameters.MaxRatePeriod);
            RateMeasure rateMeasure     = new RateMeasure(StorageWrapper.LeftLength);
            Downloader  downloader      =
                new NormalDownloader(StorageWrapper, new PiecePicker(pieces.Count), parameters.RequestBackLog,
                                     parameters.MaxRatePeriod, pieces.Count, downloadMeasure, parameters.SnubTime,
                                     rateMeasure.DataCameIn);

            Connecter connecter =
                new Connecter(downloader, Choker, pieces.Count, StorageWrapper.IsEverythingPending, uploadMeasure,
                              parameters.MaxUploadRate << 10, rawServer.AddTask);

            byte[] infoHash = Globals.GetSha1Hash(BEncodingFactory.ByteArrayEncode(infoNode));//Globals.Sha1.ComputeHash(BEncodingFactory.ByteArrayEncode(infoNode));

            Encrypter encrypter = new Encrypter(connecter, rawServer, myID, parameters.MaxMessageLength, rawServer.AddTask,
                                                parameters.KeepAliveInterval, infoHash, parameters.MaxInitiate);
            //ReRequester reRequester =
            //    new ReRequester((rootNode["announce"] as BytesNode).StringText, parameters.RerequestInterval,
            //                    rawServer.AddTask, connecter.GetConnectionsCount, parameters.MinPeers,
            //                    encrypter.StartConnect, rawServer.AddExternalTask,
            //                    StorageWrapper.GetLeftLength, uploadMeasure.GetTotalLength, downloadMeasure.GetTotalLength,
            //                    listenPort, parameters.IP,
            //                    myID, infoHash, parameters.HttpTimeout, null, parameters.MaxInitiate, finishFlag);

            DownloaderFeedback downloaderFeedback =
                new DownloaderFeedback(Choker, rawServer.AddTask, statusFunction, uploadMeasure.GetUpdatedRate,
                                       downloadMeasure.GetUpdatedRate, rateMeasure.GetTimeLeft, rateMeasure.GetLeftTime,
                                       fileLength, finishFlag, parameters.DisplayInterval,
                                       parameters.Spew);

            statusFunction("connection to peers", -1, -1, -1, -1);
            //TODO: finishedHelper.errorfunc

            finishedHelper.FinishFlag = finishFlag;
            //finishedHelper.ReRequester = reRequester;
            finishedHelper.RateMeasure = rateMeasure;

            //reRequester.d(0);
            rawServer.ListenForever(encrypter);
            //reRequester.Announce(2, null);
        }