use of com.yahoo.fs4.mplex.FS4Channel in project vespa by vespa-engine.
the class FS4Channel method receivePackets.
/**
* Receives the given number of packets and returns them, OR
* <ul>
* <li>Returns a smaller number of packets if an error or eol packet is received
* <li>Throws a ChannelTimeoutException if timeout occurs before all packets
* are received. Packets received with the wrong channel id are ignored.
* </ul>
*
* @param timeout the number of ms to attempt to get packets before throwing an exception
* @param packetCount the number of packets to receive, or -1 to receive any number up to eol/error
*/
public BasicPacket[] receivePackets(long timeout, int packetCount) throws InvalidChannelException, ChannelTimeoutException {
ensureValid();
List<BasicPacket> packets = new ArrayList<>(12);
long startTime = SystemTimer.INSTANCE.milliTime();
long timeLeft = timeout;
try {
while (timeLeft >= 0) {
BasicPacket p = nextPacket(timeLeft);
if (p == null)
throw new ChannelTimeoutException("Timed out");
if (!isPingChannel && ((Packet) p).getChannel() != getChannelId().intValue()) {
log.warning("Ignoring received " + p + ", when excepting channel " + getChannelId());
continue;
}
packets.add(p);
if (isLastPacket(p) || hasEnoughPackets(packetCount, packets)) {
BasicPacket[] packetArray = new BasicPacket[packets.size()];
packets.toArray(packetArray);
return packetArray;
}
// doing this last might save us one system call for the last
// packet.
timeLeft = timeout - (SystemTimer.INSTANCE.milliTime() - startTime);
}
} catch (InvalidChannelException e) {
// nop. if we get this we want to return the default
// zero length packet array indicating that we have no
// valid response
log.info("FS4Channel was invalid. timeLeft=" + timeLeft + ", timeout=" + timeout);
} catch (InterruptedException e) {
log.info("FS4Channel was interrupted. timeLeft=" + timeLeft + ", timeout=" + timeout);
Thread.currentThread().interrupt();
}
// did not get the end of the packet stream
throw new ChannelTimeoutException();
}
use of com.yahoo.fs4.mplex.FS4Channel in project vespa by vespa-engine.
the class BackendTestCase method testBackend.
@Test
public void testBackend() throws IOException, InvalidChannelException {
FS4Channel channel = backend.openChannel();
Query q = new Query("/?query=a");
BasicPacket[] b = null;
int channelId = channel.getChannelId();
server.dispatch.channelId = channelId;
assertTrue(backend.sendPacket(QueryPacket.create(q), channelId));
try {
b = channel.receivePackets(1000, 1);
} catch (ChannelTimeoutException e) {
fail("Could not get packets from simulated backend.");
}
assertEquals(1, b.length);
assertEquals(217, b[0].getCode());
channel.close();
}
use of com.yahoo.fs4.mplex.FS4Channel in project vespa by vespa-engine.
the class BackendTestCase method requireStatistics.
@Test
public void requireStatistics() throws IOException, InvalidChannelException {
FS4Channel channel = backend.openPingChannel();
server.dispatch.channelId = -1;
server.dispatch.packetData = PONG;
assertTrue(channel.sendPacket(new PingPacket()));
try {
channel.receivePackets(1000, 1);
} catch (ChannelTimeoutException e) {
fail("Could not get packets from simulated backend.");
}
BackendStatistics stats = backend.getStatistics();
assertEquals(1, stats.totalConnections());
}
Aggregations