Advertisement
LexManos

Forge Message Manager Draft

Feb 7th, 2012
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.57 KB | None | 0 0
  1. package net.minecraft.src.forge;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Hashtable;
  5. import java.util.Map.Entry;
  6.  
  7. import net.minecraft.src.NetworkManager;
  8.  
  9. public class MessageManager
  10. {
  11.     private Hashtable<NetworkManager, ConnectionInstance> connections = new Hashtable<NetworkManager, ConnectionInstance>();
  12.    
  13.     public class ConnectionInstance
  14.     {
  15.         private NetworkManager network;
  16.         private Hashtable<String, ArrayList<IPacketHandler>> channelToHandlers = new Hashtable<String, ArrayList<IPacketHandler>>();
  17.         private Hashtable<IPacketHandler, ArrayList<String>> handlerToChannels = new Hashtable<IPacketHandler, ArrayList<String>>();
  18.        
  19.         public ConnectionInstance(NetworkManager mgr)
  20.         {
  21.             network = mgr;
  22.         }
  23.        
  24.         /**
  25.          * Retreives the associated NetworkManager
  26.          * @return The associated NetworkManager;
  27.          */
  28.         public NetworkManager getNetwork()
  29.         {
  30.             return network;
  31.         }
  32.        
  33.         /**
  34.          * Removes all channels and handlers from the registration.
  35.          *
  36.          * @return An array of channels that were in the registration
  37.          *   If the connection is still active, you should send UNREGISTER messages for these.
  38.          */
  39.         public String[] unregisterAll()
  40.         {
  41.             String[] ret = getRegisteredChannels();
  42.             channelToHandlers.clear();
  43.             handlerToChannels.clear();
  44.             return ret;
  45.         }
  46.  
  47.         /**
  48.          * Registers a channel to a specific handler.
  49.          *
  50.          * @param handler The handler to register
  51.          * @param channel The channel to register on
  52.          * @return True if the channel was not previously registered to any handlers.
  53.          *      If True, the connection is still active, and this is not the OnLogin event,
  54.          *      you should send a REGISTER command for this channel.
  55.          */
  56.         public boolean registerChannel(IPacketHandler handler, String channel)
  57.         {
  58.             ArrayList<IPacketHandler> handlers = channelToHandlers.get(channel);
  59.             ArrayList<String> channels = handlerToChannels.get(handler);
  60.            
  61.             if (handlers == null)
  62.             {
  63.                 handlers = new ArrayList<IPacketHandler>();
  64.                 channelToHandlers.put(channel, handlers);
  65.             }
  66.            
  67.             if (channels == null)
  68.             {
  69.                 channels = new ArrayList<String>();
  70.                 handlerToChannels.put(handler, channels);
  71.             }
  72.            
  73.             boolean ret = false;
  74.             if (!channels.contains(channel))
  75.             {
  76.                 channels.add(channel);
  77.                 ret = true;
  78.             }
  79.             if (!handlers.contains(handler))
  80.             {
  81.                 handlers.add(handler);
  82.                 ret = true;
  83.             }
  84.             return ret;
  85.         }
  86.        
  87.         /**
  88.          * Unregisters a channel from the specified handler.
  89.          *
  90.          * @param handler The handler to remove the channel from.
  91.          * @param channel The channel to remove from the handler registration.
  92.          * @return True if this was the last handler registered with the specified channel.
  93.          *      If this is the case, and the network connection is still alive, you should send
  94.          *      a UNREGISTER message for this channel.
  95.          */
  96.         public boolean unregisterChannel(IPacketHandler handler, String channel)
  97.         {
  98.             boolean ret = false;
  99.             ArrayList<IPacketHandler> handlers = channelToHandlers.get(channel);
  100.             ArrayList<String> channels = handlerToChannels.get(handler);
  101.  
  102.             if (handlers != null && handlers.contains(handler))
  103.             {
  104.                 ret = true;
  105.                 handlers.remove(handler);
  106.                 if (handlers.size() == 0)
  107.                 {
  108.                     channelToHandlers.remove(channel);
  109.                 }
  110.             }
  111.            
  112.             if (channels != null && channels.contains(channel))
  113.             {
  114.                 ret = true;
  115.                 channels.remove(channel);
  116.                 if (handlers.size() == 0)
  117.                 {
  118.                     handlerToChannels.remove(handler);
  119.                 }
  120.             }
  121.            
  122.             return ret;
  123.         }
  124.  
  125.         /**
  126.          * Unregisters a handler from all of it's associated channels.
  127.          *
  128.          * @param handler The handler to unregister
  129.          * @return A list of channels that now have no handlers.
  130.          *      If the connection is still active, you should send a UNREGISTER
  131.          *      message for each channel in this list.
  132.          */
  133.         public String[] unregisterHandler(IPacketHandler handler)
  134.         {
  135.             ArrayList<String> tmp = handlerToChannels.get(handler);
  136.             if (tmp != null)
  137.             {
  138.                 String[] channels = tmp.toArray(new String[0]);
  139.                 tmp = new ArrayList<String>();
  140.                
  141.                 for (String channel : channels)
  142.                 {
  143.                     if (unregisterChannel(handler, channel))
  144.                     {
  145.                         tmp.add(channel);
  146.                     }
  147.                 }
  148.                 return tmp.toArray(new String[0]);
  149.             }
  150.             return new String[0];
  151.         }
  152.  
  153.         /**
  154.          * Retrieves a list of all unique channels that currently have valid handlers.
  155.          *
  156.          * @return The channel list
  157.          */
  158.         public String[] getRegisteredChannels()
  159.         {
  160.             int x = 0;
  161.             String[] ret = new String[channelToHandlers.size()];
  162.            
  163.             for (String value : channelToHandlers.keySet())
  164.             {
  165.                 ret[x++] = value;
  166.             }
  167.             return ret;
  168.         }
  169.     }
  170.    
  171.     /**
  172.      * Retrieves, or creates a ConnectionInstance associated with the specific NetworkManager.
  173.      *
  174.      * @param manager The NetworkManager to look for.
  175.      * @return A ConnectionInstance channel manager for this NetworkManager
  176.      */
  177.     public ConnectionInstance getConnection(NetworkManager manager)
  178.     {
  179.         ConnectionInstance ret = connections.get(manager);
  180.         if (ret == null)
  181.         {
  182.             ret = new ConnectionInstance(manager);
  183.             connections.put(manager, ret);
  184.         }
  185.         return ret;
  186.     }
  187.    
  188.     /**
  189.      * Removes the associated channel manager, and unregisters all channels/handlers from it.
  190.      *
  191.      * @param manager The NetworkManager to look for.
  192.      * @return An array of all channels that were still registered to this NetowrkManager.
  193.      *      If the connection is still active, you should send a UNREGISTER request for
  194.      *      all of these channels.
  195.      */
  196.     public String[] removeConnection(NetworkManager manager)
  197.     {
  198.         if (connections.containsKey(manager))
  199.         {
  200.             ConnectionInstance con = getConnection(manager);
  201.             String[] ret = con.unregisterAll();
  202.             connections.remove(manager);
  203.             return ret;
  204.         }
  205.         return new String[0];
  206.     }
  207.    
  208.  
  209.     /**
  210.      * Registers a channel to a specific handler.
  211.      *
  212.      * @param manager The manager to register to
  213.      * @param handler The handler to register
  214.      * @param channel The channel to register on
  215.      * @return True if the channel was not previously registered to any handlers.
  216.      *      If True, the connection is still active, and this is not the OnLogin event,
  217.      *      you should send a REGISTER command for this channel.
  218.      */
  219.     public boolean registerChannel(NetworkManager manager, IPacketHandler handler, String channel)
  220.     {
  221.         ConnectionInstance con = getConnection(manager);
  222.         return con.registerChannel(handler, channel);
  223.     }
  224.  
  225.     /**
  226.      * Unregisters a channel from the specified handler.
  227.      *
  228.      * @param manager The manager to register to
  229.      * @param handler The handler to remove the channel from.
  230.      * @param channel The channel to remove from the handler registration.
  231.      * @return True if this was the last handler registered with the specified channel.
  232.      *      If this is the case, and the network connection is still alive, you should send
  233.      *      a UNREGISTER message for this channel.
  234.      */
  235.     public boolean unregisterChannel(NetworkManager manager, IPacketHandler handler, String channel)
  236.     {
  237.         if (connections.containsKey(manager))
  238.         {
  239.             ConnectionInstance con = getConnection(manager);
  240.             return con.unregisterChannel(handler, channel);
  241.         }
  242.         return false;
  243.     }
  244.  
  245.     /**
  246.      * Unregisters a handler from all of it's associated channels.
  247.      *
  248.      * @param manager The manager to register to
  249.      * @param handler The handler to unregister
  250.      * @return A list of channels that now have no handlers.
  251.      *      If the connection is still active, you should send a UNREGISTER
  252.      *      message for each channel in this list.
  253.      */
  254.     public String[] unregisterHandler(NetworkManager manager, IPacketHandler handler)
  255.     {
  256.         if (connections.containsKey(manager))
  257.         {
  258.             ConnectionInstance con = getConnection(manager);
  259.             return con.unregisterHandler(handler);
  260.         }
  261.         return new String[0];
  262.     }
  263.  
  264.     /**
  265.      * Retrieves a list of all unique channels that currently have valid handlers.
  266.      *
  267.      * @param manager The NetworkManager to look for.
  268.      * @return The channel list
  269.      */
  270.     public String[] getRegisteredChannels(NetworkManager manager)
  271.     {
  272.         if (connections.containsKey(manager))
  273.         {
  274.             ConnectionInstance con = getConnection(manager);
  275.             return con.getRegisteredChannels();
  276.         }
  277.         return new String[0];
  278.     }
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement