001/* 002 * Trident - A Multithreaded Server Alternative 003 * Copyright 2014 The TridentSDK Team 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package net.tridentsdk.plugin.channel; 019 020import com.google.common.collect.ForwardingCollection; 021import com.google.common.collect.ImmutableList; 022import net.tridentsdk.Trident; 023import net.tridentsdk.registry.Registry; 024import net.tridentsdk.util.TridentLogger; 025 026import javax.annotation.concurrent.ThreadSafe; 027import java.util.Collection; 028import java.util.Map; 029import java.util.concurrent.ConcurrentHashMap; 030 031/** 032 * Manages data channels for sending information over the pipeline 033 * 034 * <p>To access this handler, use this code: 035 * <pre><code> 036 * PluginChannels handler = Registered.channels(); 037 * </code></pre></p> 038 * 039 * @author The TridentSDK Team 040 * @since 0.3-alpha-DP 041 */ 042@ThreadSafe 043public abstract class PluginChannels extends ForwardingCollection<PluginChannel> implements Registry<PluginChannel> { 044 private final Map<String, PluginChannel> channels = new ConcurrentHashMap<>(); 045 046 /** 047 * Do not instantiate 048 * 049 * <p>To access this handler, use this code: 050 * <pre><code> 051 * PluginChannels handler = Registered.channels(); 052 * </code></pre></p> 053 */ 054 public PluginChannels() { 055 if (!Trident.isTrident()) { 056 TridentLogger.get().error(new IllegalAccessException("Only Trident should instantiate this class")); 057 } 058 } 059 060 /** 061 * Send a plugin message 062 * 063 * @param channel name of the channel 064 * @param data the data to send 065 */ 066 protected abstract void sendPluginMessage(String channel, byte... data); 067 068 /** 069 * Registers a channel in the listings 070 * 071 * @param name the name of the channel 072 * @param channel the channel to register 073 */ 074 public void register(String name, PluginChannel channel) { 075 if (this.channels.containsKey(name)) { 076 TridentLogger.get().error(new UnsupportedOperationException("Cannot register 2 channels of the same name")); 077 return; 078 } 079 080 this.channels.put(name, channel); 081 } 082 083 /** 084 * Removes a channel if it exists in the listings 085 * 086 * @param name the name of the channel to remove 087 */ 088 public void unregister(String name) { 089 this.channels.remove(name); 090 } 091 092 /** 093 * Find a channel by its name 094 * 095 * @param name the name to find the channel by 096 * @return the channel having the specified name 097 */ 098 public PluginChannel fromName(String name) { 099 return this.channels.get(name); 100 } 101 102 @Override 103 protected Collection<PluginChannel> delegate() { 104 return ImmutableList.copyOf(channels.values()); 105 } 106}