public void open() { in.addMessageListener(this); System.out.println( "Set up a pipe from=" + in.getChannelString() + " to=" + out.getChannelString()); out.send("Set up a pipe from=" + in.getChannelString() + " to=" + out.getChannelString()); in.send("Set up a pipe from this channel to=" + out.getChannelString()); }
@Test public void testChannelGroupReceive() throws Exception { final Channel<String> channel1 = newChannel(); final Channel<String> channel2 = newChannel(); final Channel<String> channel3 = newChannel(); final ReceivePortGroup<String> group = new ReceivePortGroup<String>(channel1, channel2, channel3); Fiber fib = new Fiber( "fiber", fjPool, new SuspendableRunnable() { @Override public void run() throws SuspendExecution, InterruptedException { String m1 = group.receive(); String m2 = channel2.receive(); assertThat(m1, equalTo("hello")); assertThat(m2, equalTo("world!")); } }) .start(); Thread.sleep(100); channel3.send("hello"); Thread.sleep(100); if (policy != OverflowPolicy.BLOCK) { channel1.send("goodbye"); // TransferChannel will block here Thread.sleep(100); } channel2.send("world!"); fib.join(); }
@Test public void testBlockingChannelSendingThread() throws Exception { assumeThat(policy, is(OverflowPolicy.BLOCK)); final Channel<Integer> ch = newChannel(); Fiber<Integer> fib = new Fiber<Integer>( fjPool, new SuspendableCallable<Integer>() { @Override public Integer run() throws SuspendExecution, InterruptedException { int i = 0; while (ch.receive() != null) { i++; Fiber.sleep(50); } return i; } }) .start(); for (int i = 0; i < 10; i++) ch.send(i); ch.close(); assertThat(fib.get(), is(10)); }
@Ignore @Test public void whenReceiveNotCalledFromOwnerThenThrowException4() throws Exception { assumeTrue(Debug.isAssertionsEnabled()); final Channel<String> ch = newChannel(); Thread thread = new Thread( new Runnable() { @Override public void run() { try { ch.receive(); } catch (InterruptedException ex) { throw new AssertionError(ex); } catch (SuspendExecution e) { throw new AssertionError(e); } } }); thread.start(); Thread.sleep(100); ch.send("a message"); boolean thrown = false; try { ch.receive(); } catch (Throwable e) { thrown = true; } assertTrue(thrown); thread.join(); }
@Ignore @Test public void whenReceiveNotCalledFromOwnerThenThrowException2() throws Exception { assumeTrue(Debug.isAssertionsEnabled()); final Channel<String> ch = newChannel(); Fiber fib = new Fiber( "fiber", fjPool, new SuspendableRunnable() { @Override public void run() throws SuspendExecution, InterruptedException { String m = ch.receive(); assertThat(m, equalTo("a message")); } }) .start(); Thread.sleep(50); ch.send("a message"); boolean thrown = false; try { ch.receive(); } catch (Throwable e) { thrown = true; } assertTrue(thrown); fib.join(); }
/** Remove a channel. */ public void removeChannel(String name) { // get the channel Channel channel = getChannel(name); removeChannel(channel); // close it as soon as the last client leaves channel.getConfig().setPersistent(false); channel.send(new ShutdownMessage()); }
@Test public void testChannelCloseWithSleep() throws Exception { final Channel<Integer> ch = newChannel(); Fiber fib = new Fiber( "fiber", fjPool, new SuspendableRunnable() { @Override public void run() throws SuspendExecution, InterruptedException { for (int i = 1; i <= 5; i++) { Integer m = ch.receive(); assertThat(m, equalTo(i)); } Integer m = ch.receive(); assertThat(m, nullValue()); assertTrue(ch.isClosed()); } }) .start(); Thread.sleep(50); ch.send(1); ch.send(2); ch.send(3); ch.send(4); ch.send(5); Thread.sleep(50); ch.close(); ch.send(6); ch.send(7); fib.join(); }
private void send( final Address dest, final int num_msgs, final int num_threads, final double oob_prob) throws Exception { if (num_threads <= 0) throw new IllegalArgumentException("number of threads <= 0"); if (num_msgs % num_threads != 0) throw new IllegalArgumentException( "number of messages ( " + num_msgs + ") needs to be divisible by " + "the number o threads (" + num_threads + ")"); if (num_threads > 1) { final int msgs_per_thread = num_msgs / num_threads; Thread[] threads = new Thread[num_threads]; final AtomicInteger counter = new AtomicInteger(0); for (int i = 0; i < threads.length; i++) { threads[i] = new Thread() { public void run() { for (int j = 0; j < msgs_per_thread; j++) { Channel sender = Util.tossWeightedCoin(0.5) ? a : b; boolean oob = Util.tossWeightedCoin(oob_prob); int num = counter.incrementAndGet(); Message msg = new Message(dest, null, num); if (oob) msg.setFlag(Message.OOB); try { sender.send(msg); } catch (Exception e) { e.printStackTrace(); } } } }; threads[i].start(); } for (int i = 0; i < threads.length; i++) { threads[i].join(20000); } return; } for (int i = 0; i < num_msgs; i++) { Channel sender = Util.tossWeightedCoin(0.5) ? a : b; boolean oob = Util.tossWeightedCoin(oob_prob); Message msg = new Message(dest, null, i); if (oob) msg.setFlag(Message.OOB); sender.send(msg); } }
@Test public void testChannelGroupReceiveWithTimeout() throws Exception { final Channel<String> channel1 = newChannel(); final Channel<String> channel2 = newChannel(); final Channel<String> channel3 = newChannel(); final ReceivePortGroup<String> group = new ReceivePortGroup<String>(channel1, channel2, channel3); Fiber fib = new Fiber( "fiber", fjPool, new SuspendableRunnable() { @Override public void run() throws SuspendExecution, InterruptedException { String m1 = group.receive(); String m2 = channel2.receive(); String m3 = group.receive(10, TimeUnit.MILLISECONDS); String m4 = group.receive(200, TimeUnit.MILLISECONDS); assertThat(m1, equalTo("hello")); assertThat(m2, equalTo("world!")); assertThat(m3, nullValue()); assertThat(m4, equalTo("foo")); } }) .start(); Thread.sleep(100); channel3.send("hello"); Thread.sleep(100); channel2.send("world!"); Thread.sleep(100); channel1.send("foo"); fib.join(); }
@Test public void whenChannelOverflowsThrowException() throws Exception { assumeThat(policy, is(OverflowPolicy.THROW)); assumeThat(mailboxSize, greaterThan(0)); final Channel<Integer> ch = newChannel(); int i = 0; try { for (i = 0; i < 10; i++) ch.send(i); fail(); } catch (QueueCapacityExceededException e) { System.out.println("i = " + i); } }
private static void send( Channel sender_channel, Address dest, boolean oob, boolean mixed, int num_msgs) throws Exception { long seqno = 1; for (int i = 0; i < num_msgs; i++) { Message msg = new Message(dest, null, seqno++); if (mixed) { if (i % 2 == 0) msg.setFlag(Message.OOB); } else if (oob) { msg.setFlag(Message.OOB); } sender_channel.send(msg); } }
public void receive(Message msg) { Message reply = new Message(msg.getSrc()); try { System.out.println( "-- MySimpleReplier[" + channel.getAddress() + "]: received message from " + msg.getSrc()); if (handle_requests) { System.out.println(", sending reply"); channel.send(reply); } else System.out.println("\n"); } catch (Exception e) { e.printStackTrace(); } }
@Override public void deliver(Channel channel, byte[] bytes) { ByteBuffer buffer = ByteBuffer.allocate(bytes.length); buffer.put(bytes); buffer.flip(); int length = buffer.getInt(); byte messageID = buffer.get(); switch (messageID) { case 0: byte[] deliver_array = new byte[length - 9]; int sender_id = buffer.getInt(); int lamport_timestamp = buffer.getInt(); buffer.get(deliver_array, 0, deliver_array.length); /*for (int i = 0; i < deliver_array.length; i++) { System.out.print(deliver_array[i] + " "); }*/ System.out.println("\n" + "---------------------------------------------------"); break; case 3: sender_id = buffer.getInt(); lamport_timestamp = buffer.getInt(); /*Si on est à l'origine du BroadcastJoin, on envoie d'abord la liste des autres peers avant de nous même se bloquer*/ if (engine.getId() == sender_id) { engine.setTimestamp(engine.getTimestamp() + 1); Message m = new MemberListMessage(engine.getTimestamp(), engine.getId(), engine.getPeersList()); byte[] message_array = m.sendMessage(); for (Channel other_channel : engine.getChannelList()) { if (((NioChannel) other_channel).isNouveauvenu()) { other_channel.send(message_array, 0, message_array.length); ((NioChannel) other_channel).setNouveauvenu(false); } } } ((NioChannel) channel).setBlocked(true); break; } }
@Test public void sendMessageFromThreadToFiber() throws Exception { final Channel<String> ch = newChannel(); Fiber fib = new Fiber( "fiber", fjPool, new SuspendableRunnable() { @Override public void run() throws SuspendExecution, InterruptedException { String m = ch.receive(); assertThat(m, equalTo("a message")); } }) .start(); Thread.sleep(50); ch.send("a message"); fib.join(); }
public void revieved(Channel c, String message) { // out.send("<"+c+"> "+message); out.send(message); }