// ========================================================================
// Copyright 2007 Mort Bay Consulting Pty. 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 dojox.cometd.demo;


import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;

import dojox.cometd.Bayeux;
import dojox.cometd.Client;
import dojox.cometd.Listener;

public class BayeuxStartupListener implements ServletContextAttributeListener
{
    public void initialize(Bayeux bayeux)
    {
        synchronized(bayeux)
        {
            
            if(!bayeux.hasChannel("/service/echo"))
            {
                bayeux.subscribe("/service/echo",bayeux.newClient("echo",new EchoRPC(bayeux)));

                Client monitor_client=bayeux.newClient("monitor", new dojox.cometd.Listener()
                {
                    public void removed(String id,boolean timeout){}
                    public void deliver(Client fromClient, Client toClient, Map<String, Object> msg)
                    {
                        System.out.println("BayeuxStartupListener: "+fromClient+"->"+toClient+": "+msg);
                    }
                });

                bayeux.subscribe("/meta/handshake",monitor_client);
                bayeux.subscribe("/meta/connect",monitor_client);
                bayeux.subscribe("/meta/subscribe",monitor_client);
                bayeux.subscribe("/meta/unsubscribe",monitor_client);
            }
        }
    }
    
    public void attributeAdded(ServletContextAttributeEvent scab)
    {
        if (scab.getName().equals(Bayeux.DOJOX_COMETD_BAYEUX))
        {
            Bayeux bayeux=(Bayeux)scab.getValue();
            initialize(bayeux);
        }
    }

    public void attributeRemoved(ServletContextAttributeEvent scab)
    {

    }

    public void attributeReplaced(ServletContextAttributeEvent scab)
    {

    }

    
    private static class EchoRPC implements Listener
    {
        Bayeux _bayeux;
        
        public EchoRPC(Bayeux bayeux)
        {
            _bayeux=bayeux;
        }

        public void removed(String id,boolean timeout){}
        
        public void deliver(Client fromClient, Client toClient, Map<String, Object> msg)
        {
	    System.err.println("ECHO from "+fromClient+" "+msg);
            Map<String,Object> reply = new HashMap<String,Object>();
            reply.put(Bayeux.DATA_FIELD,msg.get(Bayeux.DATA_FIELD));
            reply.put(Bayeux.ID_FIELD,"R"+msg.get(Bayeux.ID_FIELD));
            
            _bayeux.deliver(toClient,fromClient,(String)msg.get(Bayeux.CHANNEL_FIELD),reply);
        }
        
    }
    
}
