package org.omo.cash;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.omo.core.MapModel;
import org.omo.core.Model;
import org.omo.core.MapModel.Adaptor;
import org.omo.jms.RemotingFactory;

public class Server {

	private final static Log LOG = LogFactory.getLog(Server.class);

	private static final String META_MODEL = "MetaModel";
	private final int timeout=60000;
	private final Connection connection;
	private final Session session;
	private final RemotingFactory<Model<String, String>> factory;
	private final MapModel<String, String> metaModel;
	private final Adaptor<String, String> adaptor = new  Adaptor<String, String>() {@Override public String getKey(String value) {return value;}};

	public Server(String name, ConnectionFactory connectionFactory) throws JMSException {
		connection = connectionFactory.createConnection();
		connection.start();
		session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		factory = new RemotingFactory<Model<String, String>>(session, Model.class, (Destination)null, timeout);
		metaModel = new MapModel<String, String>(META_MODEL, adaptor);
		Queue queue = session.createQueue(name + "." + META_MODEL);
		factory.createServer(metaModel, queue);
		metaModel.upsert(metaModel.getName()); // The metaModel is a Model !
		LOG.info("Listening on: " + queue);
		
		// adding TradeFeed
		String tradeFeedName = "TradeFeed";
		RemotingFactory<Model<Integer, Trade>> serverFactory = new RemotingFactory<Model<Integer, Trade>>(session, Model.class, (Destination)null, timeout);
		Model<Integer, Trade> tradeFeed= new TradeFeed(tradeFeedName, 10,100L);
		serverFactory.createServer(tradeFeed, session.createQueue("Server."+tradeFeedName));
		tradeFeed.start();
		metaModel.upsert(tradeFeedName);
	}
	
	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		String name = (args.length == 0 ? "Server" : args[0]);
		String url = "peer://" + name + "/broker0?broker.persistent=false&useJmx=false";
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
		LOG.info("Broker URL: " +url);
		new Server(name, connectionFactory);
		
		// keep going...
		while (true)
			try {
				Thread.sleep(60*1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

}

@@ -1613,16 +1613,21 @@
 adeFeed%0A
+%09%09%7B%0A%09
 %09%09String
@@ -1656,16 +1656,17 @@
 eFeed%22;%0A
+%09
 %09%09Remoti
@@ -1810,16 +1810,17 @@
 out);%0A%09%09
+%09
 Model%3CIn
@@ -1883,16 +1883,17 @@
 ,100L);%0A
+%09
 %09%09server
@@ -1973,16 +1973,17 @@
 me));%0A%09%09
+%09
 tradeFee
@@ -1993,16 +1993,17 @@
 tart();%0A
+%09
 %09%09metaMo
@@ -2029,16 +2029,933 @@
 dName);%0A
+%09%09%7D%0A%09%09// adding AccountFeed%0A%09%09%7B%0A%09%09%09String accountFeedName = %22AccountFeed%22;%0A%09%09%09RemotingFactory%3CModel%3CInteger, Trade%3E%3E serverFactory = new RemotingFactory%3CModel%3CInteger, Trade%3E%3E(session, Model.class, (Destination)null, timeout);%0A%09%09%09Model%3CInteger, Trade%3E tradeFeed= new TradeFeed(accountFeedName, 10,100L);%0A%09%09%09serverFactory.createServer(tradeFeed, session.createQueue(%22Server.%22+accountFeedName));%0A%09%09%09tradeFeed.start();%0A%09%09%09metaModel.upsert(accountFeedName);%0A%09%09%7D%0A%09%09// adding AccountFeed%0A%09%09%7B%0A%09%09%09String currencyFeedName = %22CurrencyFeed%22;%0A%09%09%09RemotingFactory%3CModel%3CInteger, Trade%3E%3E serverFactory = new RemotingFactory%3CModel%3CInteger, Trade%3E%3E(session, Model.class, (Destination)null, timeout);%0A%09%09%09Model%3CInteger, Trade%3E tradeFeed= new TradeFeed(currencyFeedName, 10,100L);%0A%09%09%09serverFactory.createServer(tradeFeed, session.createQueue(%22Server.%22+currencyFeedName));%0A%09%09%09tradeFeed.start();%0A%09%09%09metaModel.upsert(currencyFeedName);%0A%09%09%7D%0A
 %09%7D%0A%09%0A%09/*

add new feeds - need extending and refactoring