/** 
 * 
 * Copyright 2004 Protique Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 * 
 **/
package org.activemq.transport.http;

import EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.activemq.io.TextWireFormat;
import org.activemq.io.WireFormat;
import org.activemq.transport.TransportChannelSupport;

import javax.jms.JMSException;

/**
 * @version $Revision$
 */
public abstract class HttpTransportChannelSupport extends TransportChannelSupport implements Runnable {
    private static final Log log = LogFactory.getLog(HttpTransportChannelSupport.class);

    private TextWireFormat wireFormat;
    private String remoteUrl;
    private Thread thread; // should use pool
    private SynchronizedBoolean closed = new SynchronizedBoolean(false);
    private SynchronizedBoolean started = new SynchronizedBoolean(false);

    public HttpTransportChannelSupport(TextWireFormat wireFormat, String remoteUrl) {
        this.wireFormat = wireFormat;
        this.remoteUrl = remoteUrl;
    }

    public boolean isMulticast() {
        return false;
    }

    public void start() throws JMSException {
        if (started.commit(false, true)) {
            if (getClientID() != null) {
                startThread();
            }
        }
    }

    protected void startThread() {
        thread = new Thread(this, toString());
        thread.start();
    }

    public void stop() {
        if (closed.commit(false, true)) {
            super.stop();
        }
    }

    public synchronized void setClientID(String clientID) {
        super.setClientID(clientID);
        if (clientID != null && thread == null && started.get()) {
            startThread();
        }
    }

    public String toString() {
        return "HTTP Reader " + getRemoteUrl();
    }
    
    /**
     * Can this wireformat process packets of this version
     * @param version the version number to test
     * @return true if can accept the version
     */
    public boolean canProcessWireFormatVersion(int version){
        return wireFormat.canProcessWireFormatVersion(version);
    }
    
    /**
     * @return the current version of this wire format
     */
    public int getCurrentWireFormatVersion(){
        return wireFormat.getCurrentWireFormatVersion();
    }

    // Properties
    //-------------------------------------------------------------------------
    public String getRemoteUrl() {
        return remoteUrl;
    }

    public WireFormat getWireFormat() {
        return wireFormat;
    }
    
    public TextWireFormat getTextWireFormat(){
        return wireFormat;
    }

    public void setWireFormat(TextWireFormat wireFormat) {
        this.wireFormat = wireFormat;
    }

    public SynchronizedBoolean getClosed() {
        return closed;
    }

    public SynchronizedBoolean getStarted() {
        return started;
    }
}
