org.apache.qpid.proton.engine.Link Java Examples

The following examples show how to use org.apache.qpid.proton.engine.Link. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Server.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
@Override
public void onDelivery(Event evt) {
    Delivery dlv = evt.getDelivery();
    Link link = dlv.getLink();
    if (link instanceof Sender) {
        dlv.settle();
    } else {
        Receiver rcv = (Receiver) link;
        if (!dlv.isPartial()) {
            byte[] bytes = new byte[dlv.pending()];
            rcv.recv(bytes, 0, bytes.length);
            String address = router.getAddress(rcv);
            Message message = new Message(bytes);
            messages.put(address, message);
            dlv.disposition(Accepted.getInstance());
            dlv.settle();
            if (!quiet) {
                System.out.println(String.format("Got message(%s): %s", address, message));
            }
            send(address);
        }
    }
}
 
Example #2
Source File: EventImpl.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
@Override
public Reactor getReactor() {
    if (context instanceof Reactor) {
        return (Reactor) context;
    } else if (context instanceof Task) {
        return ((Task)context).getReactor();
    } else if (context instanceof Transport) {
        return ((TransportImpl)context).getReactor();
    } else if (context instanceof Delivery) {
        return ((Delivery)context).getLink().getSession().getConnection().getReactor();
    } else if (context instanceof Link) {
        return ((Link)context).getSession().getConnection().getReactor();
    } else if (context instanceof Session) {
        return ((Session)context).getConnection().getReactor();
    } else if (context instanceof Connection) {
        return ((Connection)context).getReactor();
    } else if (context instanceof Selectable) {
        return ((Selectable)context).getReactor();
    }
    return null;
}
 
Example #3
Source File: Spout.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
@Override
public void onLinkFlow(Event evt) {
    Link link = evt.getLink();
    if (link instanceof Sender) {
        Sender sender = (Sender) link;
        while ((sent < count) && sender.getCredit() > 0) {
            Delivery dlv = sender.delivery(String.format("spout-%s", sent).getBytes());

            Message msg = new Message(String.format("Hello World! [%s]", sent));
            byte[] bytes = msg.getBytes();
            sender.send(bytes, 0, bytes.length);
            sender.advance();

            if (!quiet) {
                System.out.println(String.format("Sent %s to %s: %s", new String(dlv.getTag()),
                                                 sender.getTarget().getAddress(), msg));
            }
            sent++;
        }
    }
}
 
Example #4
Source File: AMQPConnectionContext.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
protected void remoteLinkOpened(Link link) throws Exception {

      AMQPSessionContext protonSession = getSessionExtension(link.getSession());

      link.setSource(link.getRemoteSource());
      link.setTarget(link.getRemoteTarget());
      if (link instanceof Receiver) {
         Receiver receiver = (Receiver) link;
         if (link.getRemoteTarget() instanceof Coordinator) {
            Coordinator coordinator = (Coordinator) link.getRemoteTarget();
            protonSession.addTransactionHandler(coordinator, receiver);
         } else {
            protonSession.addReceiver(receiver);
         }
      } else {
         Sender sender = (Sender) link;
         protonSession.addSender(sender);
      }
   }
 
Example #5
Source File: Pool.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
private <T extends Link> T resolve(String remote, String local,
                                   LinkResolver<T> resolver,
                                   LinkConstructor<T> constructor) {
    String host = remote.substring(2).split("/", 2)[0];
    T link = resolver.resolve(remote);
    if (link == null) {
        Connection conn = connections.get(host);
        if (conn == null) {
            conn = Connection.Factory.create();
            conn.collect(collector);
            conn.setHostname(host);
            conn.open();
            connections.put(host, conn);
        }

        Session ssn = conn.session();
        ssn.open();

        link = constructor.create(ssn, remote, local);
        link.open();
    }
    return link;
}
 
Example #6
Source File: FlowController.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
@Override
public void onUnhandled(Event event) {
    int window = this.window;
    Link link = event.getLink();

    switch(event.getType()) {
    case LINK_LOCAL_OPEN:
    case LINK_REMOTE_OPEN:
    case LINK_FLOW:
    case DELIVERY:
        if (link instanceof Receiver) {
            this.drained += link.drained();
            if (this.drained == 0) {
                topup((Receiver)link, window);
            }
        }
        break;
    default:
        break;
    }
}
 
Example #7
Source File: EventImpl.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public Sender getSender()
{
    if (context instanceof Sender) {
        return (Sender) context;
    } else {
        Link link = getLink();
        if (link instanceof Sender) {
            return (Sender) link;
        }
        return null;
    }
}
 
Example #8
Source File: ProtonConnectionImpl.java    From vertx-proton with Apache License 2.0 5 votes vote down vote up
void fireRemoteLinkOpen(Link link) {
  if (link instanceof Sender) {
    if (senderOpenHandler != null) {
      senderOpenHandler.handle(new ProtonSenderImpl((Sender) link));
    }
  } else {
    if (receiverOpenHandler != null) {
      receiverOpenHandler.handle(new ProtonReceiverImpl((Receiver) link));
    }
  }
}
 
Example #9
Source File: EventImpl.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public Receiver getReceiver()
{
    if (context instanceof Receiver) {
        return (Receiver) context;
    } else {
        Link link = getLink();
        if (link instanceof Receiver) {
            return (Receiver) link;
        }
        return null;
    }
}
 
Example #10
Source File: Handshaker.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public void onLinkRemoteOpen(Event event) {
    Link link = event.getLink();
    if (link.getLocalState() == EndpointState.UNINITIALIZED) {
        if (link.getRemoteSource() != null) {
            link.setSource(link.getRemoteSource().copy());
        }
        if (link.getRemoteTarget() != null) {
            link.setTarget(link.getRemoteTarget().copy());
        }
    }
    open(link);
}
 
Example #11
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
void doOpenLinkBeforeOpenConnectionTestImpl(boolean receiverLink)
{
    MockTransportImpl transport = new MockTransportImpl();
    Connection connection = Proton.connection();
    transport.bind(connection);

    Session session = connection.session();
    session.open();

    Link link = null;
    if(receiverLink)
    {
        link = session.receiver("myReceiver");
    }
    else
    {
        link = session.sender("mySender");
    }
    link.open();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 0, transport.writes.size());

    // Now open the connection, expect the Open, Begin, and Attach frames
    connection.open();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 3, transport.writes.size());

    assertTrue("Unexpected frame type", transport.writes.get(0) instanceof Open);
    assertTrue("Unexpected frame type", transport.writes.get(1) instanceof Begin);
    assertTrue("Unexpected frame type", transport.writes.get(2) instanceof Attach);
}
 
Example #12
Source File: EventImplTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetTransportWithLinkContext()
{
    Transport transport = Transport.Factory.create();
    Connection connection = Connection.Factory.create();
    transport.bind(connection);

    Session session = connection.session();
    Link link = session.receiver("myReceiver");

    EventImpl event = createEvent(link, Event.Type.LINK_INIT);

    assertNotNull("No transport returned", event.getTransport());
    assertSame("Incorrect transport returned", transport, event.getTransport());
}
 
Example #13
Source File: ProtonServerSenderContext.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private static boolean hasRemoteDesiredCapability(Link link, Symbol capability) {
   Symbol[] remoteDesiredCapabilities = link.getRemoteDesiredCapabilities();
   if (remoteDesiredCapabilities != null) {
      for (Symbol cap : remoteDesiredCapabilities) {
         if (capability.equals(cap)) {
            return true;
         }
      }
   }
   return false;
}
 
Example #14
Source File: AMQPConnectionContext.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
public void onRemoteClose(Link link) throws Exception {
   handler.requireHandler();

   // We scheduled it for later, as that will work through anything that's pending on the current deliveries.
   runNow(() -> {

      ProtonDeliveryHandler linkContext = (ProtonDeliveryHandler) link.getContext();
      if (linkContext != null) {
         try {
            linkContext.close(true);
         } catch (Exception e) {
            log.error(e.getMessage(), e);
         }
      }

      /// we have to perform the link.close after the linkContext.close is finished.
      // linkeContext.close will perform a few executions on the netty loop,
      // this has to come next
      runLater(() -> {
         link.close();
         link.free();
         flush();
      });

   });
}
 
Example #15
Source File: AMQPConnectionContext.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
public void onRemoteDetach(Link link) throws Exception {
   handler.requireHandler();
   boolean handleAsClose = link.getSource() != null && ((Source) link.getSource()).getExpiryPolicy() == TerminusExpiryPolicy.LINK_DETACH;

   if (handleAsClose) {
      onRemoteClose(link);
   } else {
      link.detach();
      link.free();
   }
}
 
Example #16
Source File: AMQPConnectionContext.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
public void onLocalDetach(Link link) throws Exception {
   handler.requireHandler();
   Object context = link.getContext();
   if (context instanceof ProtonServerSenderContext) {
      ProtonServerSenderContext senderContext = (ProtonServerSenderContext) context;
      senderContext.close(false);
   }
}
 
Example #17
Source File: Server.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public void onLinkFlow(Event evt) {
    Link link = evt.getLink();
    if (link instanceof Sender) {
        Sender snd = (Sender) link;
        send(router.getAddress(snd), snd);
    }
}
 
Example #18
Source File: EventImpl.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public Session getSession()
{
    if (context instanceof Session) {
        return (Session) context;
    } else {
        Link link = getLink();
        if (link == null) {
            return null;
        }
        return link.getSession();
    }
}
 
Example #19
Source File: Router.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
private void add(Link link) {
    if (link instanceof Sender) {
        add((Sender) link);
    } else {
        add((Receiver) link);
    }
}
 
Example #20
Source File: Router.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
private void remove(Link link) {
    if (link instanceof Sender) {
        remove((Sender) link);
    } else {
        remove((Receiver) link);
    }
}
 
Example #21
Source File: Handshaker.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public void onLinkRemoteOpen(Event evt) {
    Link link = evt.getLink();
    if (link.getLocalState() == EndpointState.UNINITIALIZED) {
        link.setSource(link.getRemoteSource());
        link.setTarget(link.getRemoteTarget());
        link.open();
    }
}
 
Example #22
Source File: Handshaker.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public void onLinkRemoteClose(Event evt) {
    Link link = evt.getLink();
    if (link.getLocalState() != EndpointState.CLOSED) {
        link.close();
    }
}
 
Example #23
Source File: Drain.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public void onLinkLocalOpen(Event evt) {
    Link link = evt.getLink();
    if (link instanceof Receiver) {
        Receiver receiver = (Receiver) link;

        if (block) {
            receiver.flow(count);
        } else {
            receiver.drain(count);
        }
    }
}
 
Example #24
Source File: Drain.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public void onLinkFlow(Event evt) {
    Link link = evt.getLink();
    if (link instanceof Receiver) {
        Receiver receiver = (Receiver) link;

        if (!receiver.draining()) {
            receiver.getSession().getConnection().close();
        }
    }
}
 
Example #25
Source File: FlowController.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public void onLinkLocalOpen(Event evt) {
    Link link = evt.getLink();
    if (link instanceof Receiver) {
        topUp((Receiver) link);
    }
}
 
Example #26
Source File: ConnectionImpl.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public Link linkHead(EnumSet<EndpointState> local, EnumSet<EndpointState> remote)
{
    if(_linkHead == null)
    {
        return null;
    }
    else
    {
        LinkNode.Query<LinkImpl> query = new EndpointImplQuery<LinkImpl>(local, remote);
        LinkNode<LinkImpl> node = query.matches(_linkHead) ? _linkHead : _linkHead.next(query);
        return node == null ? null : node.getValue();
    }
}
 
Example #27
Source File: EventImpl.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public Link getLink()
{
    if (context instanceof Link) {
        return (Link) context;
    } else {
        Delivery dlv = getDelivery();
        if (dlv == null) {
            return null;
        }
        return dlv.getLink();
    }
}
 
Example #28
Source File: FlowController.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public void onDelivery(Event evt) {
    Link link = evt.getLink();
    if (link instanceof Receiver) {
        topUp((Receiver) link);
    }
}
 
Example #29
Source File: FlowController.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public void onLinkFlow(Event evt) {
    Link link = evt.getLink();
    if (link instanceof Receiver) {
        topUp((Receiver) link);
    }
}
 
Example #30
Source File: FlowController.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public void onLinkRemoteOpen(Event evt) {
    Link link = evt.getLink();
    if (link instanceof Receiver) {
        topUp((Receiver) link);
    }
}