001    /**
002     * Copyright 2007 Mike Kroutikov.
003     *
004     * This program is free software; you can redistribute it and/or modify
005     *   it under the terms of the Lesser GNU General Public License as 
006     *   published by the Free Software Foundation; either version 3 of
007     *   the License, or (at your option) any later version.
008     *
009     *   This program is distributed in the hope that it will be useful,
010     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
011     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     *   Lesser GNU General Public License for more details.
013     *
014     *   You should have received a copy of the Lesser GNU General Public License
015     *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
016     */
017    
018    package org.otfeed.protocol.request;
019    
020    import org.otfeed.protocol.*;
021    
022    import java.nio.ByteBuffer;
023    
024    
025    /**
026     * OpenTick protocol header. 
027     * This is common part to all OpenTick messages.
028     */
029    public final class Header {
030    
031            private MessageEnum message;
032            private StatusEnum  status;
033            private CommandEnum command;
034            private int         requestId;
035    
036            public Header() { }
037    
038            public Header(MessageEnum m, StatusEnum s, CommandEnum c, int r) {
039                    message = m;
040                    status  = s;
041                    command = c;
042                    requestId = r;
043            }
044    
045            public Header(ByteBuffer in) {
046                    readIn(in);
047            }
048    
049            public MessageEnum getMessage() { return message; }
050            public void setMessage(MessageEnum val) { message = val; }
051    
052            public StatusEnum getStatus() { return status; }
053            public void setStatus(StatusEnum val) { status= val; }
054    
055            public CommandEnum getCommand() { return command; }
056            public void setCommand(CommandEnum val) { command = val; }
057    
058            public int getRequestId() { return requestId; }
059            public void setRequestId(int val) { requestId = val; }
060    
061            public void writeOut(ByteBuffer out) {
062                    out.put((byte) message.code);
063                    out.put((byte) status.code);
064                    out.put((byte) 0);  // reserved
065                    out.put((byte) 0);  // reserved
066                    out.putInt(command.code);
067                    out.putInt(requestId);
068            }
069    
070            public void readIn(ByteBuffer in) {
071                    message = Util.readMessageEnum(in);
072                    status  = Util.readStatusEnum(in);
073                    in.get();  // reserved
074                    in.get();  // reserved
075                    command = Util.readCommandEnum(in);
076                    requestId = in.getInt();
077            }
078    
079            @Override
080            public String toString() { 
081                    return "Header: message=" + message + ", status=" + status
082                            + ", command=" + command + ", requestId=" + requestId;
083            }
084    }