001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.nio.intraband;
016    
017    import com.liferay.portal.kernel.nio.intraband.CompletionHandler.CompletionType;
018    
019    import java.io.IOException;
020    
021    import java.nio.channels.GatheringByteChannel;
022    import java.nio.channels.ScatteringByteChannel;
023    
024    import java.util.EnumSet;
025    
026    /**
027     * @author Shuyang Zhou
028     */
029    public class DatagramHelper {
030    
031            public static Datagram createACKResponseDatagram(long sequenceId) {
032                    return Datagram.createACKResponseDatagram(sequenceId);
033            }
034    
035            public static Datagram createReceiveDatagram() {
036                    return Datagram.createReceiveDatagram();
037            }
038    
039            public static Object getAttachment(Datagram datagram) {
040                    return datagram.attachment;
041            }
042    
043            public static CompletionHandler<Object> getCompletionHandler(
044                    Datagram datagram) {
045    
046                    return datagram.completionHandler;
047            }
048    
049            public static EnumSet<CompletionType> getCompletionTypes(
050                    Datagram datagram) {
051    
052                    return datagram.completionTypes;
053            }
054    
055            public static long getSequenceId(Datagram datagram) {
056                    return datagram.getSequenceId();
057            }
058    
059            public static boolean isAckResponse(Datagram datagram) {
060                    return datagram.isAckResponse();
061            }
062    
063            public static boolean readFrom(
064                            Datagram datagram, ScatteringByteChannel scatteringByteChannel)
065                    throws IOException {
066    
067                    return datagram.readFrom(scatteringByteChannel);
068            }
069    
070            public static void setAckRequest(Datagram datagram) {
071                    datagram.setAckRequest(true);
072            }
073    
074            public static void setAttachment(Datagram datagram, Object attachment) {
075                    datagram.attachment = attachment;
076            }
077    
078            public static void setCompletionHandler(
079                    Datagram datagram, CompletionHandler<Object> completionHandler) {
080    
081                    datagram.completionHandler = completionHandler;
082            }
083    
084            public static void setCompletionTypes(
085                    Datagram datagram, EnumSet<CompletionType> completionTypes) {
086    
087                    datagram.completionTypes = completionTypes;
088            }
089    
090            public static void setSequenceId(Datagram datagram, long sequenceId) {
091                    datagram.setSequenceId(sequenceId);
092            }
093    
094            public static void setTimeout(Datagram datagram, long timeout) {
095                    datagram.timeout = timeout;
096            }
097    
098            public static void writeTo(
099                            Datagram datagram, GatheringByteChannel gatheringByteChannel)
100                    throws IOException {
101    
102                    while (!datagram.writeTo(gatheringByteChannel));
103            }
104    
105    }