Exemple #1
0
        protected void ExceptionHandler(RequestState pnRequestState)
        {
            #if (ENABLE_PUBNUB_LOGGING)
            List <ChannelEntity> channelEntities = PubNubInstance.SubscriptionInstance.AllSubscribedChannelsAndChannelGroups;

            this.PubNubInstance.PNLog.WriteToLog(string.Format("InExceptionHandler: responsetype"), PNLoggingMethod.LevelInfo);
            string channelGroups = Helpers.GetNamesFromChannelEntities(channelEntities, true);
            string channels      = Helpers.GetNamesFromChannelEntities(channelEntities, false);
            #endif


            if (!internetStatus)
            {
                #if (ENABLE_PUBNUB_LOGGING)
                this.PubNubInstance.PNLog.WriteToLog(string.Format("ExceptionHandler: Subscribe channels = {0} and channelgroups = {1} - No internet connection. ", channels, channelGroups), PNLoggingMethod.LevelInfo);
                #endif
                if (this.PubNubInstance.PNConfig.ReconnectionPolicy.Equals(PNReconnectionPolicy.NONE))
                {
                    PNStatus pnStatus = Helpers.CreatePNStatus(
                        PNStatusCategory.PNDisconnectedCategory,
                        "",
                        null,
                        true,
                        PNOperationType.PNSubscribeOperation,
                        PubNubInstance.SubscriptionInstance.AllChannels,
                        PubNubInstance.SubscriptionInstance.AllChannelGroups,
                        null,
                        this.PubNubInstance
                        );

                    CreateEventArgsAndRaiseEvent(pnStatus);
                }
                return;
            }

            long tt = lastSubscribeTimetoken;
            if (!enableResumeOnReconnect && reconnect)
            {
                tt = 0; //send 0 time token to enable presence event
                #if (ENABLE_PUBNUB_LOGGING)
                this.PubNubInstance.PNLog.WriteToLog(string.Format("ExceptionHandler: Reconnect true and EnableResumeOnReconnect false sending tt = 0. "), PNLoggingMethod.LevelInfo);
                #endif
            }
            #if (ENABLE_PUBNUB_LOGGING)
            else
            {
                this.PubNubInstance.PNLog.WriteToLog(string.Format("ExceptionHandler: sending tt = {0}. ", tt.ToString()), PNLoggingMethod.LevelInfo);
            }
            #endif


            RunSubscribeRequest(tt, reconnect);
        }
Exemple #2
0
        void RetriesExceededHandler(object sender, EventArgs ea)
        {
            BounceRequest();

            #if (ENABLE_PUBNUB_LOGGING)
            this.PubNubInstance.PNLog.WriteToLog("Retries exceeded", PNLoggingMethod.LevelInfo);
            #endif

            hbWorker.ResetInternetCheckSettings();

            #if (ENABLE_PUBNUB_LOGGING)
            List <ChannelEntity> channelEntities = PubNubInstance.SubscriptionInstance.AllSubscribedChannelsAndChannelGroups;
            string channelGroups = Helpers.GetNamesFromChannelEntities(channelEntities, true);
            string channels      = Helpers.GetNamesFromChannelEntities(channelEntities, false);
            this.PubNubInstance.PNLog.WriteToLog(string.Format("ExceptionHandler: MAX retries reached. Exiting the subscribe for channels = {0} and channelgroups = {1}", channels, channelGroups), PNLoggingMethod.LevelInfo);
            #endif

            UnsubscribeAllBuilder unsubBuilder = new UnsubscribeAllBuilder(this.PubNubInstance);
            unsubBuilder.Async((result, status) => {
                #if (ENABLE_PUBNUB_LOGGING)
                this.PubNubInstance.PNLog.WriteToLog("in UnsubscribeAll", PNLoggingMethod.LevelInfo);
                if (status.Error)
                {
                    this.PubNubInstance.PNLog.WriteToLog(string.Format("In Example, UnsubscribeAll Error: {0} {1} {2}", status.StatusCode, status.ErrorData, status.Category), PNLoggingMethod.LevelInfo);
                }
                else
                {
                    this.PubNubInstance.PNLog.WriteToLog(string.Format("In UnsubscribeAll, result: {0}", result.Message), PNLoggingMethod.LevelInfo);
                }
                #endif
            });

            if (
                PubNubInstance.PNConfig.HeartbeatNotificationOption.Equals(PNHeartbeatNotificationOption.All) ||
                PubNubInstance.PNConfig.HeartbeatNotificationOption.Equals(PNHeartbeatNotificationOption.Failures)
                )
            {
                PNStatus pnStatus = Helpers.CreatePNStatus(
                    PNStatusCategory.PNReconnectionAttemptsExhausted,
                    "",
                    null,
                    true,
                    PNOperationType.PNSubscribeOperation,
                    PubNubInstance.SubscriptionInstance.AllChannels,
                    PubNubInstance.SubscriptionInstance.AllChannelGroups,
                    null,
                    this.PubNubInstance
                    );

                CreateEventArgsAndRaiseEvent(pnStatus);
            }
        }
        void StartPresenceHeartbeat(bool pause, int pauseTime)
        {
            try {
                if (PubNubInstance.SubscriptionInstance.AllNonPresenceChannelsOrChannelGroups.Count > 0)
                {
                    isPresenceHearbeatRunning = true;
                    string channelsJsonState = PubNubInstance.SubscriptionInstance.CompiledUserState;

                    Uri request = BuildRequests.BuildPresenceHeartbeatRequest(
                        Helpers.GetNamesFromChannelEntities(PubNubInstance.SubscriptionInstance.AllNonPresenceChannelsOrChannelGroups, false),
                        Helpers.GetNamesFromChannelEntities(PubNubInstance.SubscriptionInstance.AllNonPresenceChannelsOrChannelGroups, true),
                        channelsJsonState,
                        this.PubNubInstance
                        );

                    RequestState requestState = new RequestState();
                    requestState.OperationType = PNOperationType.PNPresenceHeartbeatOperation;
                    requestState.URL           = request.OriginalString;
                    requestState.Timeout       = PubNubInstance.PNConfig.NonSubscribeTimeout;
                    requestState.Pause         = pauseTime;
                    requestState.Reconnect     = pause;

                    #if (ENABLE_PUBNUB_LOGGING)
                    this.PubNubInstance.PNLog.WriteToLog(string.Format("presenceheartbeat: request.OriginalString {0} ", request.OriginalString), PNLoggingMethod.LevelError);
                    #endif

                    webRequestId = webRequest.Run(requestState);

                    #if (ENABLE_PUBNUB_LOGGING)
                    this.PubNubInstance.PNLog.WriteToLog(string.Format("StartPresenceHeartbeat: PresenceHeartbeat running "), PNLoggingMethod.LevelInfo);
                    #endif
                }
            }
            catch (Exception ex) {
                #if (ENABLE_PUBNUB_LOGGING)
                this.PubNubInstance.PNLog.WriteToLog(string.Format("StartPresenceHeartbeat: PresenceHeartbeat exception {0}", ex.ToString()), PNLoggingMethod.LevelError);
                #endif
            }
        }
Exemple #4
0
        protected override void RunWebRequest(QueueManager qm)
        {
            RequestState requestState = new RequestState();

            requestState.OperationType = OperationType;

            string channels = "";

            if (ChannelsToUse != null)
            {
                channels = string.Join(",", ChannelsToUse.ToArray());
            }
            string channelGroups = "";

            if (ChannelGroupsToUse != null)
            {
                channelGroups = string.Join(",", ChannelGroupsToUse.ToArray());
            }

            if (string.IsNullOrEmpty(channels) && (string.IsNullOrEmpty(channelGroups)))
            {
                #if (ENABLE_PUBNUB_LOGGING)
                this.PubNubInstance.PNLog.WriteToLog(string.Format("Both ChannelGroupsToLeave and ChannelsToLeave are empty, running unsubscribe all"), PNLoggingMethod.LevelInfo);
                #endif

                channelGroups      = Helpers.GetNamesFromChannelEntities(this.PubNubInstance.SubscriptionInstance.AllChannelGroups, true);
                channels           = Helpers.GetNamesFromChannelEntities(this.PubNubInstance.SubscriptionInstance.AllChannels, false);
                ChannelsToUse      = Helpers.CreateListOfStringFromListOfChannelEntity(this.PubNubInstance.SubscriptionInstance.AllChannels);
                ChannelGroupsToUse = Helpers.CreateListOfStringFromListOfChannelEntity(this.PubNubInstance.SubscriptionInstance.AllChannelGroups);
            }

            List <ChannelEntity> subscribedChannels = this.PubNubInstance.SubscriptionInstance.AllSubscribedChannelsAndChannelGroups;

            List <ChannelEntity> newChannelEntities;
            this.PubNubInstance.SubscriptionInstance.TryRemoveDuplicatesCheckAlreadySubscribedAndGetChannels(
                OperationType,
                ChannelsToUse,
                ChannelGroupsToUse,
                true,
                out
                newChannelEntities
                );

            //Retrieve the current channels already subscribed previously and terminate them
            this.PubNubInstance.SubWorker.AbortPreviousRequest(subscribedChannels);

            if (!this.PubNubInstance.PNConfig.SuppressLeaveEvents)
            {
                Uri request = BuildRequests.BuildLeaveRequest(
                    channels,
                    channelGroups,
                    this.PubNubInstance
                    );
                base.RunWebRequest(qm, request, requestState, this.PubNubInstance.PNConfig.NonSubscribeTimeout, 0, this);
            }

            RemoveUnsubscribedChannelsAndDeleteUserState(newChannelEntities);


            //Get all the channels
            this.PubNubInstance.SubWorker.ContinueToSubscribeRestOfChannels();
        }
Exemple #5
0
        void StartPresenceHeartbeat(bool pause, int pauseTime)
        {
            try {
                string channelsJsonState;
                int    allNonPresenceChannelsOrChannelGroupsCount;
                string channels;
                string channelGroups;

                if (RunIndependentOfSubscribe)
                {
                    channelsJsonState = State;
                    allNonPresenceChannelsOrChannelGroupsCount = ((ChannelGroups.Length > 0)?ChannelGroups.Split(',').Count():0) + ((Channels.Length > 0)?Channels.Split(',').Count():0);
                    Debug.Log("allNonPresenceChannelsOrChannelGroupsCount:" + allNonPresenceChannelsOrChannelGroupsCount);
                    channels      = Channels;
                    channelGroups = ChannelGroups;
                }
                else
                {
                    channelsJsonState = PubNubInstance.SubscriptionInstance.CompiledUserState;
                    allNonPresenceChannelsOrChannelGroupsCount = PubNubInstance.SubscriptionInstance.AllNonPresenceChannelsOrChannelGroups.Count;
                    channels      = Helpers.GetNamesFromChannelEntities(PubNubInstance.SubscriptionInstance.AllNonPresenceChannelsOrChannelGroups, false);
                    channelGroups = Helpers.GetNamesFromChannelEntities(PubNubInstance.SubscriptionInstance.AllNonPresenceChannelsOrChannelGroups, true);
                }

                if (allNonPresenceChannelsOrChannelGroupsCount > 0)
                {
                    isPresenceHearbeatRunning = true;
                    Uri request = BuildRequests.BuildPresenceHeartbeatRequest(
                        channels,
                        channelGroups,
                        channelsJsonState,
                        this.PubNubInstance,
                        null
                        );

                    RequestState requestState = new RequestState();
                    requestState.OperationType = PNOperationType.PNPresenceHeartbeatOperation;
                    requestState.URL           = request.OriginalString;
                    requestState.Timeout       = PubNubInstance.PNConfig.NonSubscribeTimeout;
                    requestState.Pause         = pauseTime;
                    requestState.Reconnect     = pause;

                    #if (ENABLE_PUBNUB_LOGGING)
                    this.PubNubInstance.PNLog.WriteToLog(string.Format("presenceheartbeat: /presence/ request.OriginalString {0} ", request.OriginalString), PNLoggingMethod.LevelError);
                    #endif

                    webRequestId = webRequest.Run(requestState);

                    #if (ENABLE_PUBNUB_LOGGING)
                    this.PubNubInstance.PNLog.WriteToLog(string.Format("StartPresenceHeartbeat: PresenceHeartbeat running, {0} ", pauseTime), PNLoggingMethod.LevelInfo);
                    #endif
                }
                #if (ENABLE_PUBNUB_LOGGING)
                else
                {
                    this.PubNubInstance.PNLog.WriteToLog(string.Format("StartPresenceHeartbeat: AllNonPresenceChannelsOrChannelGroups < 0 "), PNLoggingMethod.LevelInfo);
                }
                #endif
            }
            catch (Exception ex) {
                #if (ENABLE_PUBNUB_LOGGING)
                this.PubNubInstance.PNLog.WriteToLog(string.Format("StartPresenceHeartbeat: PresenceHeartbeat exception {0}", ex.ToString()), PNLoggingMethod.LevelError);
                #endif
            }
        }
Exemple #6
0
        private void RunSubscribeRequest(long timetoken, bool reconnect)
        {
            //Exit if the channel is unsubscribed
            #if (ENABLE_PUBNUB_LOGGING)
            this.PubNubInstance.PNLog.WriteToLog("in  RunSubscribeRequest", PNLoggingMethod.LevelInfo);
            #endif

            if (CheckAllChannelsAreUnsubscribed())
            {
                #if (ENABLE_PUBNUB_LOGGING)
                this.PubNubInstance.PNLog.WriteToLog("All channels unsubscribed", PNLoggingMethod.LevelInfo);
                #endif

                return;
            }
            List <ChannelEntity> channelEntities = PubNubInstance.SubscriptionInstance.AllSubscribedChannelsAndChannelGroups;

            // Begin recursive subscribe
            try {
                long lastTimetoken = SaveLastTimetoken(timetoken);

                hbWorker.RunHeartbeat(false, hbWorker.HeartbeatInterval);
                #if (ENABLE_PUBNUB_LOGGING)
                this.PubNubInstance.PNLog.WriteToLog(string.Format("RunRequests: Heartbeat started"), PNLoggingMethod.LevelInfo);
                #endif
                RequestSentAt = DateTime.UtcNow.Second;
                if (PubNubInstance.PNConfig.PresenceInterval > 0)
                {
                    PHBWorker.RunPresenceHeartbeat(false, PubNubInstance.PNConfig.PresenceInterval);
                }

                #if (ENABLE_PUBNUB_LOGGING)
                this.PubNubInstance.PNLog.WriteToLog(string.Format("MultiChannelSubscribeRequest: Building request for {0} with timetoken={1}", Helpers.GetAllNamesFromChannelEntities(channelEntities, true), lastTimetoken), PNLoggingMethod.LevelInfo);
                #endif
                // Build URL
                string channelsJsonState = PubNubInstance.SubscriptionInstance.CompiledUserState;

                string channels      = Helpers.GetNamesFromChannelEntities(channelEntities, false);
                string channelGroups = Helpers.GetNamesFromChannelEntities(channelEntities, true);

                //v2
                string filterExpr = (!string.IsNullOrEmpty(this.PubNubInstance.PNConfig.FilterExpression)) ? this.PubNubInstance.PNConfig.FilterExpression : string.Empty;
                Uri    requestUrl = BuildRequests.BuildSubscribeRequest(
                    channels,
                    channelGroups,
                    lastTimetoken.ToString(),
                    channelsJsonState,
                    region,
                    filterExpr,
                    this.PubNubInstance,
                    this.QueryParams
                    );

                #if (ENABLE_PUBNUB_LOGGING)
                this.PubNubInstance.PNLog.WriteToLog(string.Format("RunSubscribeRequest: {0}", requestUrl.OriginalString), PNLoggingMethod.LevelError);
                #endif

                RequestState requestState = new RequestState();
                requestState.OperationType = PNOperationType.PNSubscribeOperation;
                requestState.URL           = requestUrl.OriginalString;
                requestState.Timeout       = PubNubInstance.PNConfig.SubscribeTimeout;
                requestState.Pause         = 0;
                requestState.Reconnect     = reconnect;
                //http://ps.pndsn.com/v2/presence/sub-key/sub-c-5c4fdcc6-c040-11e5-a316-0619f8945a4f/uuid/UUID_WhereNow?pnsdk=PubNub-Go%2F3.14.0&uuid=UUID_WhereNow
                webRequestId = webRequest.Run(requestState);
            } catch (Exception ex) {
                #if (ENABLE_PUBNUB_LOGGING)
                this.PubNubInstance.PNLog.WriteToLog(string.Format("MultiChannelSubscribeRequest: method:_subscribe \n channel={0} \n timetoken={1} \n Exception Details={2}", Helpers.GetAllNamesFromChannelEntities(channelEntities, true), timetoken.ToString(), ex.ToString()), PNLoggingMethod.LevelError);
                #endif
                this.RunSubscribeRequest(timetoken, false);
            }
        }
Exemple #7
0
        public void AbortPreviousRequest(List <ChannelEntity> existingChannels)
        {
            #if (ENABLE_PUBNUB_LOGGING)
            this.PubNubInstance.PNLog.WriteToLog(string.Format("AbortPreviousRequest: Aborting previous subscribe/presence requests having channel(s)={0} and ChannelGroup(s) = {1}", Helpers.GetNamesFromChannelEntities(existingChannels, false), Helpers.GetNamesFromChannelEntities(existingChannels, true)), PNLoggingMethod.LevelInfo);
            #endif

            webRequest.AbortRequest(webRequestId, false);
        }