Exemplo n.º 1
0
            async Task ProcessAsync(AttachContext attachContext)
            {
                if (attachContext.Attach.LinkName == "")
                {
                    // how to fail the attach request
                    attachContext.Complete(new Error(ErrorCode.InvalidField)
                    {
                        Description = "Empty link name not allowed."
                    });
                }
                else if (attachContext.Link.Role)
                {
                    var target = attachContext.Attach.Target as Target;
                    if (target != null)
                    {
                        if (string.Compare(target.Address, TargetAddress, true) == 0)
                        {
                            // how to do manual link flow control
                            attachContext.Complete(new InternalIncomingLinkEndpoint(parentProcessor, TargetAddress), 300);
                        }
                        else
                        {
                            attachContext.Complete(new Error(ErrorCode.InvalidField)
                            {
                                Description = "Target address not found."
                            });
                        }
                    }
                }

                await Task.Yield();
            }
Exemplo n.º 2
0
            public void Process(AttachContext attachContext)
            {
                if (!attachContext.Attach.Role)
                {
                    attachContext.Complete(new Error()
                    {
                        Condition = ErrorCode.NotAllowed, Description = "Only receiver link is allowed."
                    });
                    return;
                }

                string address = ((Source)attachContext.Attach.Source).Address;

                if (!string.Equals("$monitoring", address, StringComparison.OrdinalIgnoreCase))
                {
                    attachContext.Complete(new Error()
                    {
                        Condition = ErrorCode.NotFound, Description = "Cannot find address " + address
                    });
                    return;
                }

                Console.WriteLine("A client has connected. LinkName " + attachContext.Attach.LinkName);
                attachContext.Complete(new MonitoringLinkEndpoint(attachContext.Link), 0);
            }
Exemplo n.º 3
0
        private void AttachOutgoingLink(AttachContext attachContext, Source source)
        {
            IEntity entity = _entityLookup.Find(source.Address);

            if (entity == null)
            {
                attachContext.Complete(new Error(ErrorCode.NotFound)
                {
                    Description = "Entity not found."
                });
                _logger.LogError($"Could not attach outgoing link to non-existing entity '{source.Address}'.");
                return;
            }

            DeliveryQueue queue = entity.DeliveryQueue;

            if (queue == null)
            {
                attachContext.Complete(new Error(ErrorCode.NotFound)
                {
                    Description = "Queue not found."
                });
                _logger.LogError($"Could not attach outgoing link to non-existing queue '{source.Address}'.");
                return;
            }

            var outgoingLinkEndpoint = new OutgoingLinkEndpoint(queue);

            attachContext.Complete(outgoingLinkEndpoint, 0);
            _logger.LogDebug($"Attached outgoing link to queue '{source.Address}'.");
        }
Exemplo n.º 4
0
        public void Process(AttachContext attachContext)
        {
            if (string.IsNullOrEmpty(attachContext.Attach.LinkName))
            {
                attachContext.Complete(new Error(ErrorCode.InvalidField)
                {
                    Description = "Empty link name not allowed."
                });
                _logger.LogError($"Could not attach empty link to {GetType().Name}.");
                return;
            }

            if (!_securityContext.IsAuthorized(attachContext.Link.Session.Connection))
            {
                attachContext.Complete(new Error(ErrorCode.UnauthorizedAccess)
                {
                    Description = "Not authorized."
                });
                _logger.LogError($"Could not attach unathorized link to {GetType().Name}.");
                return;
            }

            if (attachContext.Link.Role)
            {
                AttachIncomingLink(attachContext, (Target)attachContext.Attach.Target);
            }
            else
            {
                AttachOutgoingLink(attachContext, (Source)attachContext.Attach.Source);
            }
        }
Exemplo n.º 5
0
 public SlowLinkEndpoint(AttachContext attachContext)
 {
     this.link    = attachContext.Link;
     this.cts     = new CancellationTokenSource();
     link.Closed += (o, e) => this.cts.Cancel();
     attachContext.Complete(this, 0);
     this.link.SetCredit(1, false, false);
 }
Exemplo n.º 6
0
        public void Process(AttachContext attachContext)
        {
            if (this.OnLinkAttached != null)
            {
                this.OnLinkAttached(attachContext.Link);
            }

            attachContext.Complete(new TestLinkEndpoint(), attachContext.Attach.Role ? 0 : 30);
        }
        public void Process(AttachContext attachContext)
        {
            if (attachHandler != null && attachHandler(attachContext))
            {
                return;
            }

            attachContext.Complete(
                factory != null ? factory(attachContext.Link) : new TestLinkEndpoint(),
                attachContext.Attach.Role ? 0 : 30);
        }
Exemplo n.º 8
0
        public void Process(AttachContext attachContext)
        {
            if (_attachHandler != null)
            {
                if (_attachHandler(attachContext))
                {
                    return;
                }
            }

            attachContext.Complete(new TestLinkEndpoint(), attachContext.Attach.Role ? 0 : 30);
        }
Exemplo n.º 9
0
 public void Process(AttachContext attachContext)
 {
     if (attachContext.Link.Role &&
         ((Target)attachContext.Attach.Target).Address == Name)
     {
         attachContext.Complete(new IncomingLinkEndpoint(), 30);
     }
     else
     {
         attachContext.Complete(new Error()
         {
             Condition = ErrorCode.NotImplemented
         });
     }
 }
Exemplo n.º 10
0
 public void AttachLink(AttachContext attachContext)
 {
     this.semaphore.WaitAsync(30000).ContinueWith(
         t =>
     {
         if (!t.Result)
         {
             attachContext.Complete(new Error(ErrorCode.ResourceLimitExceeded));
         }
         else
         {
             this.semaphore.Release();
             attachContext.Complete(this, 1);
         }
     });
 }
        public void Process(AttachContext attachContext)
        {
            if (!attachContext.Link.Role)
            {
                Consumer = attachContext;
                attachContext.Link.AddClosedCallback((sender, error) => { Consumer = null; });
            }
            else
            {
                Producer = attachContext;
                attachContext.Link.AddClosedCallback((sender, error) =>
                {
                    Producer = null;
                });
            }

            attachContext.Complete(new TestLinkEndpoint(messages), attachContext.Attach.Role ? 0 : 30);
        }
Exemplo n.º 12
0
        private void AttachIncomingLink(AttachContext attachContext, Target target)
        {
            IEntity entity = _entityLookup.Find(target.Address);

            if (entity == null)
            {
                attachContext.Complete(new Error(ErrorCode.NotFound)
                {
                    Description = "Entity not found."
                });
                _logger.LogError($"Could not attach incoming link to non-existing entity '{target.Address}'.");
                return;
            }

            var incomingLinkEndpoint = new IncomingLinkEndpoint(entity);

            attachContext.Complete(incomingLinkEndpoint, 300);
            _logger.LogDebug($"Attached incoming link to entity '{target.Address}'.");
        }
Exemplo n.º 13
0
 public void AttachLink(AttachContext attachContext)
 {
     this.semaphore.WaitAsync(30000).ContinueWith(
         t =>
     {
         if (t.IsCanceled || t.IsFaulted)
         {
             attachContext.Complete(new Error()
             {
                 Condition = ErrorCode.ResourceLimitExceeded
             });
         }
         else
         {
             this.semaphore.Release();
             attachContext.Complete(this, 1);
         }
     });
 }
Exemplo n.º 14
0
        public async Task IsAuthorized_ReturnsFalse_WhenSessionConnectionClosedBeforeAuthorized()
        {
            ListenerLink   link              = null;
            var            authorized        = false;
            ILinkProcessor fakeLinkProcessor = Substitute.For <ILinkProcessor>();

            fakeLinkProcessor
            .When(instance => instance.Process(Arg.Any <AttachContext>()))
            .Do(c =>
            {
                AttachContext attachContext = c.ArgAt <AttachContext>(0);
                link = attachContext.Link;
                attachContext.Complete(new Error(ErrorCode.IllegalState)
                {
                    Description = "Test"
                });
            });

            ContainerHost host = TestAmqpHost.Open();

            try
            {
                host.RegisterLinkProcessor(fakeLinkProcessor);
                Connection connection = await host.ConnectAndAttachAsync();

                await connection.CloseAsync();

                await Task.Delay(500);

                var securityContext = new SecurityContext();
                securityContext.Authorize(link.Session.Connection);

                authorized = securityContext.IsAuthorized(link.Session.Connection);
            }
            finally
            {
                host.Close();
            }

            authorized.ShouldBeFalse();
        }
Exemplo n.º 15
0
        async Task ProcessAsync(AttachContext attachContext)
        {
            // simulating an async operation required to complete the task
            await Task.Delay(100);

            if (attachContext.Attach.LinkName == "")
            {
                // how to fail the attach request
                attachContext.Complete(
                    new Error(ErrorCode.InvalidField)
                {
                    Description = "Empty link name not allowed."
                });
            }
            else if (attachContext.Link.Role)
            {
                var target = attachContext.Attach.Target as Target;
                if (target != null)
                {
                    if (target.Address == "slow-queue")
                    {
                        // how to do manual link flow control
                        new SlowLinkEndpoint(attachContext);
                    }
                    else if (target.Address == "shared-queue")
                    {
                        // how to do flow control across links
                        this.sharedLinkEndpoint.AttachLink(attachContext);
                    }
                    else
                    {
                        // default link flow control
                        attachContext.Complete(new IncomingLinkEndpoint(), 300);
                    }
                }
            }
            else
            {
                attachContext.Complete(new OutgoingLinkEndpoint(), 0);
            }
        }
Exemplo n.º 16
0
        void ILinkProcessor.Process(AttachContext attachContext)
        {
            var client = AttachedClients.GetClientFromConnection(attachContext.Link.Session.Connection);

            // If a Receiver link is attaching
            if (attachContext.Attach.Role)
            {
                var endpoint = new ServerToClientLinkEndpoint(attachContext.Link);
                client.SetServerToClientLinkEndpoint(endpoint);

                // Completes the attach operation with success
                attachContext.Complete(endpoint, 0);
            }
            else
            {
                var endpoint = new ClientToServerLinkEndpoint(attachContext.Link);
                client.SetClientToServerLinkEndpoint(endpoint);

                // Completes the attach operation with success
                attachContext.Complete(endpoint, 1);
            }
        }
Exemplo n.º 17
0
            async Task ProcessAsync(AttachContext attachContext)
            {
                // simulating an async operation required to complete the task
                await Task.Delay(100);

                if (attachContext.Attach.LinkName == "")
                {
                    // how to fail the attach request
                    attachContext.Complete(new Error()
                    {
                        Condition = ErrorCode.InvalidField, Description = "Empty link name not allowed."
                    });
                }
                else if (attachContext.Link.Role)
                {
                    attachContext.Complete(new IncomingLinkEndpoint(), 300);
                }
                else
                {
                    attachContext.Complete(new OutgoingLinkEndpoint(), 0);
                }
            }
Exemplo n.º 18
0
        public async Task IsAuthorized_ReturnsTrue_WhenSameConnectionAuthorizedTwice()
        {
            var            authorized        = false;
            var            links             = new List <ListenerLink>();
            ILinkProcessor fakeLinkProcessor = Substitute.For <ILinkProcessor>();

            fakeLinkProcessor
            .When(instance => instance.Process(Arg.Any <AttachContext>()))
            .Do(c =>
            {
                AttachContext attachContext = c.ArgAt <AttachContext>(0);
                links.Add(attachContext.Link);
                attachContext.Complete(new Error(ErrorCode.IllegalState)
                {
                    Description = "Test"
                });
            });

            ContainerHost host = TestAmqpHost.Open();

            try
            {
                host.RegisterLinkProcessor(fakeLinkProcessor);
                Connection connection = await host.ConnectAndAttachAsync(2);

                var securityContext = new SecurityContext();
                securityContext.Authorize(links[0].Session.Connection);
                securityContext.Authorize(links[1].Session.Connection);
                authorized = securityContext.IsAuthorized(links[1].Session.Connection);

                await connection.CloseAsync();
            }
            finally
            {
                host.Close();
            }

            authorized.ShouldBeTrue();
        }
Exemplo n.º 19
0
        public override object VisitAttach([NotNull] AttachContext context)
        {
            //      Console.WriteLine("VisitAttach: " + context.process()[0].ID() + "   " + context.process()[1].ID());

            SysProcess procObj = (SysProcess)VisitProcess(context.process()[1]);
            SysProcess first   = procObj;

            foreach (var expr in context.processexpr())
            {
                //          Console.WriteLine(expr.process().ID());

                SysProcess next = (SysProcess)VisitProcess(expr.process());
                next         = setOperator(next, expr);
                procObj.next = next;

                procObj = next;
            }
            Attachment attach = new Attachment(
                context.process()[0].ID()[0].GetText()
                , (SysProcess)VisitProcess(context.process()[0])
                , first);

            return(attach);
        }
Exemplo n.º 20
0
 public void Process(AttachContext attachContext)
 {
     handler(attachContext);
 }
Exemplo n.º 21
0
 public void Process(AttachContext attachContext)
 {
     var task = this.ProcessAsync(attachContext);
 }
Exemplo n.º 22
0
 public void Process(AttachContext attachContext)
 {
     // start a task to process this request
     var task = this.ProcessAsync(attachContext);
 }
Exemplo n.º 23
0
	public AttachContext attach() {
		AttachContext _localctx = new AttachContext(Context, State);
		EnterRule(_localctx, 60, RULE_attach);
		try {
			int _alt;
			EnterOuterAlt(_localctx, 1);
			{
			State = 1192; k_attach();
			State = 1196;
			ErrorHandler.Sync(this);
			_alt = Interpreter.AdaptivePredict(TokenStream,35,Context);
			while ( _alt!=2 && _alt!=global::Antlr4.Runtime.Atn.ATN.InvalidAltNumber ) {
				if ( _alt==1 ) {
					{
					{
					State = 1193; attachparam();
					}
					} 
				}
				State = 1198;
				ErrorHandler.Sync(this);
				_alt = Interpreter.AdaptivePredict(TokenStream,35,Context);
			}
			State = 1214;
			switch (TokenStream.La(1)) {
			case COL:
				{
				State = 1199; Match(COL);
				State = 1200; uri();
				}
				break;
			case SCOL:
				{
				State = 1201; Match(SCOL);
				State = 1202; k_encoding();
				State = 1203; Match(ASSIGN);
				State = 1204; k_base();
				State = 1205; Match(D6);
				State = 1206; Match(D4);
				State = 1207; Match(SCOL);
				State = 1208; k_value();
				State = 1209; Match(ASSIGN);
				State = 1210; k_binary();
				State = 1211; Match(COL);
				State = 1212; binary();
				}
				break;
			default:
				throw new NoViableAltException(this);
			}
			State = 1216; Match(CRLF);
			}
		}
		catch (RecognitionException re) {
			_localctx.exception = re;
			ErrorHandler.ReportError(this, re);
			ErrorHandler.Recover(this, re);
		}
		finally {
			ExitRule();
		}
		return _localctx;
	}
Exemplo n.º 24
0
 public void Process(AttachContext attachContext)
 {
     attachContext.Complete(new TestLinkEndpoint(), attachContext.Attach.Role ? 0 : 30);
 }
Exemplo n.º 25
0
 public void Process(AttachContext attachContext)
 {
     attachContext.Complete(_incommingLink, 300);
 }