001/**
002 *
003 * Copyright the original author or authors
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.jingleold;
018
019import java.util.logging.Level;
020import java.util.logging.Logger;
021
022import org.jivesoftware.smack.packet.IQ;
023import org.jivesoftware.smackx.jingleold.packet.Jingle;
024
025/**
026 * Jingle. 
027 *  @author Jeff Williams
028 *  @see JingleSessionState
029 */
030
031public class JingleSessionStatePending extends JingleSessionState {
032    private static final Logger LOGGER = Logger.getLogger(JingleSessionStatePending.class.getName());
033
034    private static JingleSessionStatePending INSTANCE = null;
035
036    protected JingleSessionStatePending() {
037        // Prevent instantiation of the class.
038    }
039
040    /**
041     *  A thread-safe means of getting the one instance of this class.
042     *  @return The singleton instance of this class.
043     */
044    public synchronized static JingleSessionState getInstance() {
045        if (INSTANCE == null) {
046            INSTANCE = new JingleSessionStatePending();
047        }
048        return INSTANCE;
049    }
050
051    @Override
052    public void enter() {
053        // TODO Auto-generated method stub
054
055    }
056
057    @Override
058    public void exit() {
059        // TODO Auto-generated method stub
060
061    }
062
063    @Override
064    public IQ processJingle(JingleSession session, Jingle jingle, JingleActionEnum action) {
065        IQ response = null;
066
067        switch (action) {
068
069            case CONTENT_ACCEPT:
070                response = receiveContentAcceptAction(jingle);
071                break;
072
073            case CONTENT_MODIFY:
074                break;
075
076            case CONTENT_REMOVE:
077                break;
078
079            case SESSION_ACCEPT:
080                response = receiveSessionAcceptAction(session, jingle);
081                break;
082
083            case SESSION_INFO:
084                break;
085
086            case SESSION_TERMINATE:
087                response = receiveSessionTerminateAction(session, jingle);
088                break;
089
090            case TRANSPORT_INFO:
091                break;
092
093            default:
094                // Anything other action is an error.
095                //response = createJingleError(inJingle, JingleError.OUT_OF_ORDER);
096                break;
097        }
098
099        return response;
100    }
101
102    /**
103     * Receive and process the <session-accept> action.
104     */
105    private IQ receiveContentAcceptAction(Jingle inJingle) {
106
107        // According to XEP-167 the only thing we can do is ack.
108        //setSessionState(JingleSessionStateEnum.ACTIVE);
109        //return createAck(inJingle);
110
111        // This is now handled by the media negotiator for the matching <content> segment.
112        return null;
113    }
114
115    /**
116     * Receive and process the <session-accept> action.
117     */
118    private IQ receiveSessionAcceptAction(JingleSession session, Jingle inJingle) {
119
120        // According to XEP-166 the only thing we can do is ack.
121        session.setSessionState(JingleSessionStateActive.getInstance());
122        return session.createAck(inJingle);
123    }
124
125    /**
126     * Receive and process the <session-terminate> action.
127     */
128    private IQ receiveSessionTerminateAction(JingleSession session, Jingle jingle) {
129
130        // According to XEP-166 the only thing we can do is ack.
131        IQ response = session.createAck(jingle);
132
133        try {
134            session.terminate("Closed remotely");
135        } catch (Exception e) {
136            LOGGER.log(Level.WARNING, "exception", e);
137        }
138
139        return response;
140    }
141}