package ibis.impl.net.multi;

import ibis.impl.net.NetDriver;
import ibis.impl.net.NetIbis;
import ibis.impl.net.NetInput;
import ibis.impl.net.NetInputUpcall;
import ibis.impl.net.NetOutput;
import ibis.impl.net.NetPortType;
import ibis.ipl.IbisIOException;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.Hashtable;

/**
 * The multieric splitter/poller virtual driver.
 */
public final class Driver extends NetDriver {

	/**
	 * The driver name.
	 */
	private final String name = "multi";

        private Hashtable    pluginTable = new Hashtable();

	/**
	 * Constructor.
	 *
	 * @param ibis the {@link NetIbis} instance.
	 */
	public Driver(NetIbis ibis) {
		super(ibis);
	}

	/**
	 * {@inheritDoc}
	 */
	public String getName() {
		return name;
	}

	/**
	 * {@inheritDoc}
	 */
	public NetInput newInput(NetPortType pt, String context, NetInputUpcall inputUpcall) throws IOException {
		return new MultiPoller(pt, this, context, inputUpcall);
	}

	/**
	 * {@inheritDoc}
	 */
	public NetOutput newOutput(NetPortType pt, String context) throws IOException {
		return new MultiSplitter(pt, this, context);
	}

        protected MultiPlugin loadPlugin(String name) throws IOException {
		MultiPlugin plugin = (MultiPlugin)pluginTable.get(name);

                if (plugin == null) {
                        //System.err.println("Loading multi-protocol plugin ["+name+"]...");

			try {
                                String      clsName  =
                                        getClass().getPackage().getName()
                                        + ".plugins"
                                        + "."
                                        + name;

                                Class       cls      = Class.forName(clsName);
                                Constructor cons     = cls.getConstructor(null);

                                plugin = (MultiPlugin)cons.newInstance(null);
			} catch (Exception e) {
				throw new IbisIOException(e);
			}

			pluginTable.put(name, plugin);

                        //System.err.println("Loading multi-protocol plugin ["+name+"] done");
                }

                return plugin;
        }
}

@@ -706,16 +706,197 @@
 (ibis);%0A
+%09%09System.err.println(%22This has probably been broken by all%22);%0A%09%09System.err.println(%22optimizations, e.g. singleton poller,%22);%0A%09%09System.err.println(%22upcallFunc in constructor etc.%22);%0A
 %09%7D%0A%0A%09/**

Print a warning on usage of multi -- it's probably been broken by all these changes I did