public void TestCloseConnection()
        {
            IChannel cacheServiceChannel = connection.OpenChannel(CacheServiceProtocol.Instance,
                                                                  "CacheServiceProxy", null, null);

            EnsureCacheRequest ensureCacheRequest =
                (EnsureCacheRequest)cacheServiceChannel.MessageFactory.CreateMessage(EnsureCacheRequest.TYPE_ID);

            ensureCacheRequest.CacheName = CacheName;

            string response = (string)cacheServiceChannel.Request(ensureCacheRequest);
            Uri    uri      = new Uri(response);

            Assert.IsNotNull(uri);

            IChannel namedCacheChannel = connection.AcceptChannel(uri, null, null);

            Assert.IsNotNull(namedCacheChannel);

            string channelUri = "channel:" + namedCacheChannel.Id + "#NamedCacheProtocol";

            Assert.AreEqual(channelUri, uri.ToString());

            CloseConnection closeConnection = (CloseConnection)connection.GetChannel(0).MessageFactory.CreateMessage(
                CloseConnection.TYPE_ID);

            connection.GetChannel(0).Send(closeConnection);
        }
        /// <summary>
        /// Create a new <see cref="RemoteNamedCache"/> for the given
        /// <see cref="INamedCache"/> name.
        /// </summary>
        /// <param name="name">
        /// The name of the cache.
        /// </param>
        /// <returns>
        /// A new <b>RemoteNamedCache</b>.
        /// </returns>
        public virtual RemoteNamedCache CreateRemoteNamedCache(string name)
        {
            IChannel           channel    = EnsureChannel();
            IConnection        connection = channel.Connection;
            IMessageFactory    factory    = channel.MessageFactory;
            RemoteNamedCache   cache      = new RemoteNamedCache();
            IPrincipal         principal  = Thread.CurrentPrincipal;
            EnsureCacheRequest request    = (EnsureCacheRequest)factory.CreateMessage(EnsureCacheRequest.TYPE_ID);

            request.CacheName = name;
            Uri uri;

            try
            {
                uri = new Uri((string)channel.Request(request));
            }
            catch (UriFormatException e)
            {
                throw new Exception("error instantiating URI", e);
            }

            cache.CacheName                = name;
            cache.CacheService             = this;
            cache.DeferKeyAssociationCheck = DeferKeyAssociationCheck;
            cache.EventDispatcher          = EnsureEventDispatcher();

            connection.AcceptChannel(uri, cache, principal);

            return(cache);
        }
Beispiel #3
0
        public void TestNamedCacheException()
        {
            TcpInitiator initiator    = GetInitiator();
            IConnection  conn         = initiator.EnsureConnection();
            IChannel     cacheService = conn.OpenChannel(CacheServiceProtocol.Instance,
                                                         "CacheServiceProxy", null, null);

            EnsureCacheRequest ensureCacheRequest =
                (EnsureCacheRequest)cacheService.MessageFactory.CreateMessage(EnsureCacheRequest.TYPE_ID);

            ensureCacheRequest.CacheName = CacheNameTemp;

            string   response   = (string)cacheService.Request(ensureCacheRequest);
            Uri      uri        = new Uri(response);
            IChannel namedCache = conn.AcceptChannel(uri, null, null);

            string[]    keys      = { "Ana Cikic", "Goran Milosavljevic", "Ivan Cikic" };
            string[]    values    = { "10.0.0.120", "10.0.0.180", "10.0.0.125" };
            IDictionary addresses = new Hashtable();

            addresses.Add(convToBinary.Convert(keys[0]), convToBinary.Convert(values[0]));
            addresses.Add(convToBinary.Convert(keys[1]), convToBinary.Convert(values[1]));
            addresses.Add(convToBinary.Convert(keys[2]), convToBinary.Convert(values[2]));

            PutAllRequest putAllRequest =
                (PutAllRequest)namedCache.MessageFactory.CreateMessage(PutAllRequest.TYPE_ID);

            putAllRequest.Map = addresses;
            namedCache.Request(putAllRequest);

            DestroyCacheRequest destroyCacheRequest =
                (DestroyCacheRequest)cacheService.MessageFactory.CreateMessage(DestroyCacheRequest.TYPE_ID);

            destroyCacheRequest.CacheName = CacheNameTemp;
            cacheService.Request(destroyCacheRequest);

            GetAllRequest getAllRequest =
                (GetAllRequest)namedCache.MessageFactory.CreateMessage(GetAllRequest.TYPE_ID);
            ArrayList names = new ArrayList();

            names.Add(convToBinary.Convert(keys[1]));
            names.Add(convToBinary.Convert(keys[2]));
            getAllRequest.Keys = names;

            try
            {
                namedCache.Send(getAllRequest).WaitForResponse(-1);
            }
            catch (PortableException)
            {
            }

            conn.Close();
            initiator.Stop();
        }
        public void PortableExceptionConstructorTest()
        {
            IConnection conn = initiator.EnsureConnection();
            Channel     cacheServiceChannel = (Channel)conn.OpenChannel(CacheServiceProtocol.Instance,
                                                                        "CacheServiceProxy", null, null);

            EnsureCacheRequest ensureCacheRequest =
                (EnsureCacheRequest)cacheServiceChannel.MessageFactory.CreateMessage(EnsureCacheRequest.TYPE_ID);

            ensureCacheRequest.CacheName = "nonexisting";
            Assert.AreEqual(EnsureCacheRequest.TYPE_ID, ensureCacheRequest.TypeId);

            IStatus ensureCacheStatus = cacheServiceChannel.Send(ensureCacheRequest);

            Assert.IsInstanceOf(typeof(EnsureCacheRequest), ensureCacheRequest);
            Assert.AreEqual(EnsureCacheRequest.TYPE_ID, ensureCacheRequest.TypeId);
            try
            {
                ensureCacheStatus.WaitForResponse(-1);
            }
            catch (PortableException pe)
            {
                Assert.IsNotNull(pe);
                Assert.IsTrue(pe.Name.StartsWith("Portable("));
                Assert.IsNull(pe.InnerException);
                Assert.IsTrue(pe.Message.ToLower().IndexOf("no scheme") >= 0);

                string[] fullStackTrace = pe.FullStackTrace;
                string   stackTrace     = pe.StackTrace;
                Assert.IsTrue(fullStackTrace.Length > 0);
                foreach (string s in fullStackTrace)
                {
                    Assert.IsTrue(stackTrace.IndexOf(s) > 0);
                }
                Assert.IsTrue(stackTrace.IndexOf("process boundary") > 0);
                Assert.IsTrue(
                    pe.ToString().Equals(pe.Name + ": " + pe.Message));

                using (Stream stream = new MemoryStream())
                {
                    DataWriter  writer = new DataWriter(stream);
                    IPofContext ctx    = (IPofContext)cacheServiceChannel.Serializer;
                    ctx.Serialize(writer, pe);
                    stream.Position = 0;
                    DataReader        reader = new DataReader(stream);
                    PortableException result = (PortableException)ctx.Deserialize(reader);

                    Assert.AreEqual(pe.Name, result.Name);
                    Assert.AreEqual(pe.Message, result.Message);
                }
            }
        }
        public IChannel GetNamedCacheChannel(IConnection conn)
        {
            IChannel cacheService = conn.OpenChannel(CacheServiceProtocol.Instance,
                                                     "CacheServiceProxy", null, null);
            EnsureCacheRequest ensureCache =
                (EnsureCacheRequest)cacheService.MessageFactory.CreateMessage(EnsureCacheRequest.TYPE_ID);

            ensureCache.CacheName = CacheName;

            string response = (string)cacheService.Request(ensureCache);
            Uri    uri      = new Uri(response);

            return(conn.AcceptChannel(uri, null, null));
        }
        public void TestAcceptChannel()
        {
            IChannel cacheServiceChannel = connection.OpenChannel(CacheServiceProtocol.Instance,
                                                                  "CacheServiceProxy", null, null);
            EnsureCacheRequest ensureCacheRequest =
                (EnsureCacheRequest)cacheServiceChannel.MessageFactory.CreateMessage(
                    EnsureCacheRequest.TYPE_ID);

            ensureCacheRequest.CacheName = CacheName;

            string response = (string)cacheServiceChannel.Request(ensureCacheRequest);
            Uri    uri      = new Uri(response);

            Assert.IsNotNull(uri);

            int id;

            id = Int32.Parse(UriUtils.GetSchemeSpecificPart(uri));
            Assert.IsTrue(id > 0);
            Assert.IsTrue(connection.GetChannel(id) == null);

            AcceptChannel acceptChannel =
                (AcceptChannel)connection.GetChannel(0).MessageFactory.CreateMessage(
                    AcceptChannel.TYPE_ID);

            Assert.IsInstanceOf(typeof(AcceptChannel), acceptChannel);
            Assert.AreEqual(AcceptChannel.TYPE_ID, acceptChannel.TypeId);

            acceptChannel.ChannelUri = uri;
            acceptChannel.Connection = (Connection)connection;

            IResponse acceptChannelResponse = connection.GetChannel(0).Send(acceptChannel).WaitForResponse(-1);

            Assert.IsInstanceOf(typeof(InternalResponse), acceptChannelResponse);
            Request.RequestStatus status = (Request.RequestStatus)acceptChannelResponse.Result;
            Assert.IsNotNull(status);
            Assert.IsNull(status.Exception);
            Assert.IsInstanceOf(typeof(AcceptChannelRequest), status.Request);
        }
        public void PortableExceptionSerializationTest()
        {
            IConnection conn = initiator.EnsureConnection();
            Channel     cacheServiceChannel = (Channel)conn.OpenChannel(CacheServiceProtocol.Instance,
                                                                        "CacheServiceProxy", null, null);

            EnsureCacheRequest ensureCacheRequest =
                (EnsureCacheRequest)cacheServiceChannel.MessageFactory.CreateMessage(EnsureCacheRequest.TYPE_ID);

            ensureCacheRequest.CacheName = "nonexisting";

            IStatus ensureCacheStatus = cacheServiceChannel.Send(ensureCacheRequest);

            try
            {
                ensureCacheStatus.WaitForResponse(-1);
            }
            catch (PortableException pe)
            {
                IFormatter formatter = new BinaryFormatter();
                byte[]     buffer    = new byte[1024 * 16];
                Stream     stream    = new MemoryStream(buffer);
                formatter.Serialize(stream, pe);
                stream.Close();

                stream = new MemoryStream(buffer);
                PortableException desPE = (PortableException)formatter.Deserialize(stream);
                Assert.IsNotNull(desPE);
                stream.Close();

                // these values should be initialized before serialization
                Assert.AreEqual(pe.Name, desPE.Name);
                Assert.AreEqual(pe.Message, desPE.Message);
                Assert.AreEqual(pe.InnerException, desPE.InnerException);
                Assert.IsNull(pe.InnerException);
                Assert.AreEqual(pe.FullStackTrace, desPE.FullStackTrace);
            }
        }