private static void Main(string[] args)
    {
        var factory    = new SubscriberFactory();
        var subscriber = factory.GetSubscriber(1);

        subscriber.Write();
        Console.ReadLine();
    }
        public void CanConstructSubscriberFactory()
        {
            // ARRANGE

            // ACT
            var subjectUnderTest = new SubscriberFactory();

            // ASSERT
            Assert.That(subjectUnderTest, Is.TypeOf(typeof(SubscriberFactory)));
            Assert.That(subjectUnderTest, Is.InstanceOf(typeof(ISubscriberFactory)));
        }
        public void CreateThrowsArgumentExceptionWhenEmailNull()
        {
            // ARRANGE
            var expectedExceptionMessage = "Must have email address to create subscriber. (Parameter 'emailAddress')";
            var subjectUnderTest         = new SubscriberFactory();

            // ACT
            // ASSERT
            var ex = Assert.Throws <ArgumentException>(
                () => subjectUnderTest.Create(null)
                );

            Assert.That(ex.Message, Is.EqualTo(expectedExceptionMessage));
        }
        public void CanCreateSubscriber()
        {
            // ARRANGE
            var expectedEmailAddress = "*****@*****.**";
            var subjectUnderTest     = new SubscriberFactory();

            // ACT
            var result = subjectUnderTest.Create(expectedEmailAddress);

            // ASSERT
            Assert.That(result, Is.Not.Null);
            Assert.That(result, Is.InstanceOf(typeof(ISubscriber)));
            Assert.That(result.EmailAddress, Is.EqualTo(expectedEmailAddress));
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Demo-ObserverPattern - Observers of the Planet Express Newsletter (May or may not be inspired by Futurama).\n");

            // single source for deferring content creation
            var subscribableContentFactory = new SubscribableContentFactory();
            var subscriberFactory          = new SubscriberFactory();
            var personalityFactory         = new PersonalityFactory();

            // create new content for observers to subscribe to
            var content = subscribableContentFactory.CreateContent <PlanetExpressNewsletter>();

            // we all have personalities y'know - the constants were nice at first, but this is gawdy
            var personalityOfFry      = personalityFactory.Create <SubscriberPersonalization>(Constant.Fry.Name, Constant.Fry.OnSubMessage, Constant.Fry.OnUnsubMessage, Constant.Fry.OnContentReceived);
            var personalityOfBender   = personalityFactory.Create <SubscriberPersonalization>(Constant.Bender.Name, Constant.Bender.OnSubMessage, Constant.Bender.OnUnsubMessage, Constant.Bender.OnContentReceived);
            var personalityOfZoidberg = personalityFactory.Create <SubscriberPersonalization>(Constant.Zoidberg.Name, Constant.Zoidberg.OnSubMessage, Constant.Zoidberg.OnUnsubMessage, Constant.Zoidberg.OnContentReceived);

            // subscriber - subscribe to content
            var fry    = subscriberFactory.Create <Person>(personalityOfFry, content);
            var bender = subscriberFactory.Create <Robot>(personalityOfBender, content);

            // subscribable content - emit change
            content.Publish(Constant.Announcements.GoodNewsEveryone);

            // subscriber - subscribe to content explicitly
            var zoidberg = subscriberFactory.Create <Decapodian>(personalityOfZoidberg); // Decapodian: a lobster-esque alien

            content.AddSubscriber(zoidberg);                                             // hey, why not zoidberg?!

            // subscribable content - emit change
            content.Publish(Constant.Announcements.IsThisThingOn);

            // subscriber - unsubscribe from content
            content.RemoveSubscriber(bender);

            // subscribable content - emit change
            content.Publish(Constant.Announcements.GoodNewsButNotOnTv);

            // Pausing the console for inspection
            Console.ReadLine();
        }
Beispiel #6
0
        public override V3Message execute(Request message, RequestContext context)
        {
            object returnValue = null;

            switch (operation)
            {
            case SUBSCRIBE_OPERATION:
            {
                IDestination destObj =
                    ORBConfig.GetInstance().GetDataServices().GetDestinationManager().GetDestination(destination);
                Hashtable headers = new Hashtable();

                RTMPConnection connection = (RTMPConnection)ConnectionHub.getConnectionLocal();

                if (destObj != null)
                {
                    String selectorName = (String)this.headers["DSSelector"];
                    String subtopic     = (String)this.headers["DSSubtopic"];
                    String dsId         = connection == null ? (String)this.headers["DSId"] : connection.GetHashCode().ToString();
                    String channel      = (String)this.headers["DSEndpoint"];

                    Subscriber subscriber = SubscriptionsManager.GetInstance().getSubscriber(
                        Subscriber.buildId(dsId, destObj.GetName(), subtopic, selectorName));

                    if (clientId == null || clientId.Equals(""))
                    {
                        clientId = Guid.NewGuid().ToString().ToUpper();
                    }

                    if (subscriber != null)
                    {
                        if (subscriber.addClient(clientId.ToString()))
                        {
                            destObj.GetServiceHandler().HandleSubscribe(subscriber, clientId.ToString(), this);
                        }

                        return(new AckMessage(messageId, clientId, null, headers));
                    }

                    object wsContext = ThreadContext.getProperties()[ORBConstants.WEB_SOCKET_MODE];

                    if (wsContext != null)
                    {
                        subscriber = new WebSocketSubscriber(selectorName, destObj, (UserContext)wsContext);
                    }
                    else if (connection != null)
                    {
                        subscriber = new DedicatedSubscriber(selectorName, destObj);
                        subscriber.setChannelId(RTMPHandler.getChannelId());
                        subscriber.setConnection(connection);
                    }
                    else
                    {
                        subscriber = SubscriberFactory.CreateSubscriber(channel, selectorName, destObj);
                    }

                    subscriber.setDSId(dsId);
                    subscriber.setSubtopic(subtopic);
                    subscriber.addClient((String)clientId);

                    try
                    {
                        SubscriptionsManager.GetInstance().AddSubscriber(dsId, destObj.GetName(), subscriber);
                    }
                    catch (Exception e)
                    {
                        if (Log.isLogging(LoggingConstants.EXCEPTION))
                        {
                            Log.log(LoggingConstants.EXCEPTION, e);
                        }
                    }

                    destObj.GetServiceHandler().HandleSubscribe(subscriber, clientId.ToString(), this);
                }
                else
                {
                    String error = "Unknown destination " + destination + ". Cannot handle subscription request";

                    if (Log.isLogging(LoggingConstants.ERROR))
                    {
                        Log.log(LoggingConstants.ERROR, error);
                    }

                    return(new ErrMessage(messageId, new Exception(error)));
                }

                return(new AckMessage(messageId, clientId, null, headers));
            }
            break;

            case UNSUBSCRIBE_OPERATION:
            {
                String subtopic     = (String)this.headers["DSSubtopic"];
                String dsId         = (String)this.headers["DSId"];
                String selectorName = (String)this.headers["DSSelector"];

                RTMPConnection connection = (RTMPConnection)ConnectionHub.getConnectionLocal();

                if (connection != null)
                {
                    dsId = connection.GetHashCode().ToString();
                }

                Subscriber subscriber = SubscriptionsManager.GetInstance().getSubscriber(
                    Subscriber.buildId(dsId, destination, subtopic, selectorName));

                if (subscriber != null)
                {
                    SubscriptionsManager.GetInstance().unsubscribe(subscriber, clientId.ToString(), this);
                }
            }
            break;

            case DISCONNECT_OPERATION:
            {
                String         dsId       = (String)this.headers["DSId"];
                RTMPConnection connection = (RTMPConnection)ConnectionHub.getConnectionLocal();

                if (connection != null)
                {
                    dsId = connection.GetHashCode().ToString();
                }

                SubscriptionsManager subscriptionsManager = SubscriptionsManager.GetInstance();
                List <Subscriber>    subscribers          = subscriptionsManager.getSubscribersByDsId(dsId);

                if (subscribers != null)
                {
                    foreach (Subscriber subscriber in subscribers)
                    {
                        if (subscriber != null)
                        {
                            subscriptionsManager.unsubscribe(subscriber, this);
                        }
                    }
                }

                subscriptionsManager.removeSubscriber(dsId);
            }
            break;

            case POLL_OPERATION:
            {
                String dsId = (String)this.headers["DSId"];

                RTMPConnection connection = (RTMPConnection)ConnectionHub.getConnectionLocal();

                if (connection != null)
                {
                    dsId = connection.GetHashCode().ToString() + "";
                }

                try
                {
                    WebORBArray <V3Message> messages =
                        new WebORBArray <V3Message>(SubscriptionsManager.GetInstance().getMessages(dsId));

                    if (messages.Count == 0)
                    {
                        return(new AckMessage(null, null, null, new Hashtable()));
                    }

                    return(new CommandMessage(CLIENT_SYNC_OPERATION, messages));
                }
                catch (Exception e)
                {
                    String error = "Invalid client id " + dsId;

                    if (Log.isLogging(LoggingConstants.ERROR))
                    {
                        Log.log(LoggingConstants.ERROR, error, e);
                    }

                    return(new ErrMessage(messageId, new Exception(error)));
                }
            }
            break;

            case CLIENT_PING_OPERATION:
            {
                Hashtable headers = new Hashtable();

                RTMPConnection connection = (RTMPConnection)ConnectionHub.getConnectionLocal();
                if (connection != null)
                {
                    headers.Add("DSId", connection.GetHashCode().ToString());
                }
                else
                {
                    headers.Add("DSId", Guid.NewGuid().ToString().ToUpper());
                }

                return(new AckMessage(messageId, clientId, null, headers));
            }
            break;

            case LOGOUT_OPERATION:
            {
                ThreadContext.setCallerCredentials(null, null);
                Thread.CurrentPrincipal = null;
            }
            break;

            case LOGIN_OPERATION:
            {
                String credentials = (String)((IAdaptingType)((object[])body.body)[0]).defaultAdapt();
                byte[] bytes       = Convert.FromBase64String(credentials);
                credentials = new String(Encoding.UTF8.GetChars(bytes));
                IAuthenticationHandler authHandler = ORBConfig.GetInstance().getSecurity().GetAuthenticationHandler();

                if (authHandler == null)
                {
                    ErrMessage errorMessage = new ErrMessage(messageId, new ServiceException("Missing authentication handler"));
                    errorMessage.faultCode = "Client.Authentication";
                    return(errorMessage);
                }

                int    index    = credentials.IndexOf(":");
                string userid   = null;
                string password = null;

                if (index != -1 && index != 0 && index != credentials.Length - 1)
                {
                    userid   = credentials.Substring(0, index);
                    password = credentials.Substring(index + 1);

                    try
                    {
                        IPrincipal principal = authHandler.CheckCredentials(userid, password, message);

                        try
                        {
                            Thread.CurrentPrincipal = principal;
                            ThreadContext.currentHttpContext().User = principal;
                        }
                        catch (Exception exception)
                        {
                            if (Log.isLogging(LoggingConstants.ERROR))
                            {
                                Log.log(LoggingConstants.ERROR,
                                        "Unable to set current principal. Make sure your current permission set allows Principal Control",
                                        exception);
                            }

                            throw exception;
                        }

                        Credentials creds = new Credentials();
                        creds.userid   = userid;
                        creds.password = password;
                        ThreadContext.setCallerCredentials(creds, principal);
                    }
                    catch (Exception exception)
                    {
                        ErrMessage errorMessage = new ErrMessage(messageId, exception);
                        errorMessage.faultCode = "Client.Authentication";
                        return(errorMessage);
                    }
                }
                else
                {
                    ErrMessage errorMessage = new ErrMessage(messageId, new ServiceException("Invalid credentials"));
                    errorMessage.faultCode = "Client.Authentication";
                    return(errorMessage);
                }
            }
            break;
            }

            return(new AckMessage(messageId, clientId, returnValue, new Hashtable()));
        }