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.jingle;
018
019import org.jivesoftware.smack.packet.IQ;
020import org.jivesoftware.smackx.jingle.packet.Jingle;
021
022/**
023 *  @author Jeff Williams
024 *  @see JingleSessionState
025 */
026
027public class JingleSessionStatePending extends JingleSessionState {
028    private static JingleSessionStatePending INSTANCE = null;
029
030    protected JingleSessionStatePending() {
031        // Prevent instantiation of the class.
032    }
033
034    /**
035     *  A thread-safe means of getting the one instance of this class.
036     *  @return The singleton instance of this class.
037     */
038    public synchronized static JingleSessionState getInstance() {
039        if (INSTANCE == null) {
040            INSTANCE = new JingleSessionStatePending();
041        }
042        return INSTANCE;
043    }
044
045    public void enter() {
046        // TODO Auto-generated method stub
047
048    }
049
050    public void exit() {
051        // TODO Auto-generated method stub
052
053    }
054
055    public IQ processJingle(JingleSession session, Jingle jingle, JingleActionEnum action) {
056        IQ response = null;
057
058        switch (action) {
059
060            case CONTENT_ACCEPT:
061                response = receiveContentAcceptAction(jingle);
062                break;
063
064            case CONTENT_MODIFY:
065                break;
066
067            case CONTENT_REMOVE:
068                break;
069
070            case SESSION_ACCEPT:
071                response = receiveSessionAcceptAction(session, jingle);
072                break;
073
074            case SESSION_INFO:
075                break;
076
077            case SESSION_TERMINATE:
078                response = receiveSessionTerminateAction(session, jingle);
079                break;
080
081            case TRANSPORT_INFO:
082                break;
083
084            default:
085                // Anything other action is an error.
086                //response = createJingleError(inJingle, JingleError.OUT_OF_ORDER);
087                break;
088        }
089
090        return response;
091    }
092
093    /**
094     * Receive and process the <session-accept> action.
095     */
096    private IQ receiveContentAcceptAction(Jingle inJingle) {
097
098        // According to XEP-167 the only thing we can do is ack.
099        //setSessionState(JingleSessionStateEnum.ACTIVE);
100        //return createAck(inJingle);
101
102        // This is now handled by the media negotiator for the matching <content> segment.
103        return null;
104    }
105
106    /**
107     * Receive and process the <session-accept> action.
108     */
109    private IQ receiveSessionAcceptAction(JingleSession session, Jingle inJingle) {
110
111        // According to XEP-166 the only thing we can do is ack.
112        session.setSessionState(JingleSessionStateActive.getInstance());
113        return session.createAck(inJingle);
114    }
115
116    /**
117     * Receive and process the <session-terminate> action.
118     */
119    private IQ receiveSessionTerminateAction(JingleSession session, Jingle jingle) {
120
121        // According to XEP-166 the only thing we can do is ack.
122        IQ response = session.createAck(jingle);
123
124        try {
125            session.terminate("Closed remotely");
126        } catch (Exception e) {
127            e.printStackTrace();
128        }
129
130        return response;
131    }
132}