1 /* 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package jdk.incubator.http.internal.websocket; 25 26 import java.util.Queue; 27 import java.util.concurrent.CompletionStage; 28 import java.util.concurrent.ConcurrentLinkedQueue; 29 import java.util.function.Consumer; 30 31 public abstract class MockTransmitter extends Transmitter { 32 33 private final long startTime = System.currentTimeMillis(); 34 35 private final Queue<OutgoingMessage> messages = new ConcurrentLinkedQueue<>(); 36 37 public MockTransmitter() { 38 super(null); 39 } 40 41 @Override 42 public void send(OutgoingMessage message, 43 Consumer<Exception> completionHandler) { 44 System.out.printf("[%6s ms.] begin send(%s)%n", 45 System.currentTimeMillis() - startTime, 46 message); 47 messages.add(message); 48 whenSent().whenComplete((r, e) -> { 49 System.out.printf("[%6s ms.] complete send(%s)%n", 50 System.currentTimeMillis() - startTime, 51 message); 52 if (e != null) { 53 completionHandler.accept((Exception) e); 54 } else { 55 completionHandler.accept(null); 56 } 57 }); 58 System.out.printf("[%6s ms.] end send(%s)%n", 59 System.currentTimeMillis() - startTime, 60 message); 61 } 62 63 @Override 64 public void close() { } 65 66 protected abstract CompletionStage<?> whenSent(); 67 68 public Queue<OutgoingMessage> queue() { 69 return messages; 70 } 71 }