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