001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 *   This file is part of the Granite Data Services Platform.
006 *
007 *   Granite Data Services is free software; you can redistribute it and/or
008 *   modify it under the terms of the GNU Lesser General Public
009 *   License as published by the Free Software Foundation; either
010 *   version 2.1 of the License, or (at your option) any later version.
011 *
012 *   Granite Data Services is distributed in the hope that it will be useful,
013 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015 *   General Public License for more details.
016 *
017 *   You should have received a copy of the GNU Lesser General Public
018 *   License along with this library; if not, write to the Free Software
019 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020 *   USA, or see <http://www.gnu.org/licenses/>.
021 */
022package flex.messaging.messages;
023
024/**
025 * @author Franck WOLFF
026 */
027public class CommandMessage extends AsyncMessage {
028
029    private static final long serialVersionUID = 1L;
030
031    public static final String SELECTOR_HEADER = "DSSelector";
032
033    public static final int SUBSCRIBE_OPERATION = 0;
034    public static final int UNSUBSCRIBE_OPERATION = 1;
035    public static final int POLL_OPERATION = 2;
036    public static final int CLIENT_SYNC_OPERATION = 4;
037    public static final int CLIENT_PING_OPERATION = 5;
038    public static final int CLUSTER_REQUEST_OPERATION = 7;
039    public static final int LOGIN_OPERATION = 8;
040    public static final int LOGOUT_OPERATION = 9;
041    public static final int SESSION_INVALIDATE_OPERATION = 10;
042    public static final int UNKNOWN_OPERATION = 10000;
043
044    // Gravity extension (tunnel connection).
045    public static final int CONNECT_OPERATION = 20;
046    public static final int DISCONNECT_OPERATION = 21;
047
048    private String messageRefType;
049    private int operation;
050
051    public CommandMessage() {
052        super();
053    }
054
055    public String getMessageRefType() {
056        return messageRefType;
057    }
058    public void setMessageRefType(String messageRefType) {
059        this.messageRefType = messageRefType;
060    }
061
062    public int getOperation() {
063        return operation;
064    }
065    public void setOperation(int operation) {
066        this.operation = operation;
067    }
068
069    public boolean isSecurityOperation() {
070        return isLoginOperation() || isLogoutOperation();
071    }
072    public boolean isLoginOperation() {
073        return operation == LOGIN_OPERATION;
074    }
075    public boolean isLogoutOperation() {
076        return operation == LOGOUT_OPERATION;
077    }
078
079    public boolean isClientPingOperation() {
080        return operation == CLIENT_PING_OPERATION;
081    }
082
083    @Override
084    public String toString() {
085        return toString("");
086    }
087
088    @Override
089    public String toString(String indent) {
090        StringBuilder sb = new StringBuilder(512);
091        sb.append(getClass().getName()).append(" {");
092        sb.append('\n').append(indent).append("  messageRefType: ").append(messageRefType);
093        sb.append('\n').append(indent).append("  operation: ").append(getReadableOperation(operation));
094        super.toString(sb, indent, (isLoginOperation() ? HIDDEN_CREDENTIALS : null));
095        sb.append('\n').append(indent).append('}');
096        return sb.toString();
097    }
098
099    private static String getReadableOperation(int operation) {
100        switch (operation) {
101        case SUBSCRIBE_OPERATION:
102            return "SUBSCRIBE";
103        case UNSUBSCRIBE_OPERATION:
104            return "UNSUBSCRIBE";
105        case POLL_OPERATION:
106            return "POLL";
107        case CLIENT_SYNC_OPERATION:
108            return "CLIENT_SYNC";
109        case CLIENT_PING_OPERATION:
110            return "CLIENT_PING";
111        case CLUSTER_REQUEST_OPERATION:
112            return "CLUSTER_REQUEST";
113        case LOGIN_OPERATION:
114            return "LOGIN";
115        case LOGOUT_OPERATION:
116            return "LOGOUT";
117        case SESSION_INVALIDATE_OPERATION:
118            return "SESSION_INVALIDATE";
119        case UNKNOWN_OPERATION:
120            return "UNKNOWN";
121
122        // Gravity extension (tunnel [dis]connection).
123        case CONNECT_OPERATION:
124            return "CONNECT";
125        case DISCONNECT_OPERATION:
126            return "DISCONNECT";
127
128        default:
129            return "REALLY UNKNOWN: 0x" + Integer.toBinaryString(operation);
130        }
131    }
132}