Ejemplo n.º 1
0
        // subscriptions
        private ISubscription SubscribePrivate(
            Type dataType, IExpression filter,
            bool excludeExisting, bool waitForExisting, bool excludeDataBody,
            SubscriptionCallback userCallback, object userContext)
        {
            if (dataType == null)
            {
                throw new ArgumentNullException(nameof(dataType));
            }
            if (filter == null)
            {
                throw new ArgumentNullException(nameof(filter));
            }

            // create subscription
            ISubscription subscription = Client.CreateTypedSubscription(dataType, filter);

            subscription.UserCallback    = SubscriptionCallback;
            subscription.UserContext     = null;
            subscription.ItemKind        = ItemKind.Object;
            subscription.ExcludeExisting = excludeExisting;
            subscription.WaitForExisting = waitForExisting;
            subscription.ExcludeDataBody = excludeDataBody;
            var userParams = new CoreItemCacheParams(dataType, subscription.Id);

            _Subscriptions.Set(subscription.Id, new CacheSubscription(subscription, userParams, userCallback, userContext));
            // hack - todo - do initial load if required (remove when server supports waitForExisting)
            const long minimumUSN = 0;

            if (!excludeExisting)
            {
                List <ICoreItem> items = Client.LoadItems(dataType, ItemKind.Object, filter, 0, true);
                foreach (var newItem in items)
                {
                    Cache.Put(newItem.Name, newItem, LoadSaveType.Avoid, userParams, TimeSpan.MaxValue);
                }
                // notify user
                NotifyUserDataChange(Cache.GetUpdates());
            }
            // start subscription
            subscription.MinimumUSN = minimumUSN;
            //_Logger.LogDebug("Cache: Creating subscription[{0}]: <{1}> {2} ({3})",
            //    filter.GetHashCode(), dataType.FullName,
            //    filter.DisplayString(), subscription.Id);
            subscription.Start();
            return(subscription);
        }
Ejemplo n.º 2
0
        private void ProgressCallback(ISubscription subscription, ICoreItem item)
        {
            try
            {
                ProgressObj oldProgress = null;
                ProgressObj newProgress = null;
                if (item != null)
                {
                    // get old request view object
                    Guid requestId = item.AppProps.GetValue <Guid>(RequestBase.Prop.RequestId, true);
                    oldProgress = _ProgressCache.Remove(requestId);
                    // build the new request view object
                    if (item.IsCurrent())
                    {
                        if (item.DataType.IsSubclassOf(typeof(RequestBase)))
                        {
                            RequestBase request = item.Data as RequestBase;
                            if (request != null)
                            {
                                newProgress = new ProgressObj(requestId, item.Created, oldProgress, request);
                            }
                        }
                        else if (item.DataType.IsSubclassOf(typeof(ResponseBase)))
                        {
                            ResponseBase response = item.Data as ResponseBase;
                            if (response != null)
                            {
                                newProgress = new ProgressObj(requestId, item.Created, oldProgress, response);
                            }
                        }
                        else
                        {
                            throw new NotSupportedException(String.Format("Type: '{0}'", item.DataType.Name));
                        }
                    }
                    if (newProgress != null)
                    {
                        _ProgressCache.Set(requestId, newProgress);
                    }
                }
                else
                {
                    throw new ArgumentNullException("item");
                }

                // determine the change type
                CacheChange change = CacheChange.Undefined;
                if (oldProgress != null)
                {
                    // updated or deleted
                    if (newProgress != null)
                    {
                        change = CacheChange.ItemUpdated;
                    }
                    else
                    {
                        change = CacheChange.ItemRemoved;
                    }
                }
                else
                {
                    // created or ???
                    if (newProgress != null)
                    {
                        change = CacheChange.ItemCreated;
                    }
                }
                if (change != CacheChange.Undefined)
                {
                    _ProgressView.UpdateData(new ViewChangeNotification <ProgressObj>()
                    {
                        Change = change, OldData = oldProgress, NewData = newProgress
                    });
                }
            }
            catch (Exception excp)
            {
                _MainLog.Log(excp);
            }
        }
Ejemplo n.º 3
0
        protected override void OnStart()
        {
            // restore un-expired connections
            Logger.LogDebug("Restoring connections...");
            DateTimeOffset    dtNow     = DateTimeOffset.Now;
            List <CommonItem> connItems = _cacheEngine.GetCacheItems(
                null, ItemKind.Local, typeof(ClientConnectionState).FullName, null, 0, dtNow, true, false);

            foreach (CommonItem item in connItems)
            {
                try
                {
                    var oldConn = XmlSerializerHelper.DeserializeFromString <ClientConnectionState>(
                        CompressionHelper.DecompressToString(item.YData));
                    IConnection connection = null;
                    if (oldConn.Contract == typeof(ITransferV341).FullName)
                    {
                        var clientId = new Guid(oldConn.SourceId);
                        connection = new ConnectionV34(
                            Logger, _cacheEngine, _serverCfg,
                            clientId, oldConn.ReplyAddress, NodeType.Client);
                        connection.ExtendExpiry();
                        _connectionIndex.Set(clientId, connection);
                    }
                    if (connection != null)
                    {
                        Logger.LogDebug("Restored connection:");
                        Logger.LogDebug("  Client Id. : {0}", connection.ClientId);
                        Logger.LogDebug("  Client Addr: {0}", connection.ReplyAddress);
                    }
                    else
                    {
                        Logger.LogDebug("Ignoring unsupported connection: '{0}'", oldConn.ReplyAddress);
                    }
                }
                catch (Exception e)
                {
                    // failed, however the show must go on
                    Logger.Log(e);
                }
            }
            // restore subscriptions
            Logger.LogDebug("Restoring subscriptions...");
            List <CommonItem> subsItems = _cacheEngine.GetCacheItems(
                null, ItemKind.Local, typeof(ClientSubscriptionState).FullName, null, 0, dtNow, true, false);

            foreach (CommonItem item in subsItems)
            {
                try
                {
                    var oldSubscription = XmlSerializerHelper.DeserializeFromString <ClientSubscriptionState>(
                        CompressionHelper.DecompressToString(item.YData));
                    var         subscription = new ClientSubscription(oldSubscription);
                    var         clientId     = new Guid(oldSubscription.ConnectionId);
                    IConnection connection   = GetValidConnection(clientId);
                    if (connection != null)
                    {
                        _cacheEngine.RestoreSubscription(subscription);
                        Logger.LogDebug("Restored subscription:");
                        Logger.LogDebug("  Client Id: {0}", connection.ClientId);
                        Logger.LogDebug("  Address  : {0}", connection.ReplyAddress);
                        Logger.LogDebug("  Subs. Id : {0}", subscription.SubscriptionId);
                        Logger.LogDebug("  AppScopes: {0}", (subscription.AppScopes == null) ? "*" : String.Join(",", subscription.AppScopes));
                        Logger.LogDebug("  ItemKind : {0}", (subscription.ItemKind == ItemKind.Undefined) ? "(any)" : subscription.ItemKind.ToString());
                        Logger.LogDebug("  DataType : {0}", subscription.DataTypeName ?? "(any)");
                        Logger.LogDebug("  Query    : {0}", subscription.Expression.DisplayString());
                        Logger.LogDebug("  MinimumUSN > : {0}", subscription.MinimumUSN);
                        Logger.LogDebug("  Excl.Deleted?: {0}", (subscription.ExcludeDeleted));
                        Logger.LogDebug("  Excl.DataBody: {0}", subscription.ExcludeDataBody);
                    }
                    else
                    {
                        _cacheEngine.DeleteSubscriptionState(subscription.SubscriptionId);
                        Logger.LogDebug("Ignoring expired subscription id: {0}", oldSubscription.SubscriptionId);
                    }
                }
                catch (Exception e)
                {
                    // failed, however the show must go on
                    Logger.Log(e);
                }
            }
            string svcName = EnvHelper.SvcPrefix(SvcId.CoreServer);

            // discovery service
            _discoverV111ServerHost = new CustomServiceHost <IDiscoverV111, DiscoverRecverV111>(
                Logger, new DiscoverRecverV111(this), _serverCfg.V31DiscoEndpoints,
                svcName, typeof(IDiscoverV111).Name, true);
            // V3.4 services
            _sessCtrlV131ServerHost = new CustomServiceHost <ISessCtrlV131, SessCtrlRecverV131>(
                Logger, new SessCtrlRecverV131(this), _serverCfg.V31DiscoEndpoints,
                svcName, typeof(ISessCtrlV131).Name, true);
            _transferV341ServerHost = new CustomServiceHost <ITransferV341, TransferRecverV341>(
                Logger, new TransferRecverV341(this), _serverCfg.V31AsyncEndpoints,
                svcName, typeof(ITransferV341).Name, true);
            // start housekeeping timer
            _housekeepTimer = new Timer(DispatchHousekeepTimeout, null, ServerCfg.CommsHousekeepInterval, ServerCfg.CommsHousekeepInterval);
        }