dispatcher.java

来自「java语言开发的P2P流媒体系统」· Java 代码 · 共 133 行

JAVA
133
字号
/* 
 * P2P-Radio - Peer to peer streaming system
 * Project homepage: http://p2p-radio.sourceforge.net/
 * Copyright (C) 2003-2004 Michael Kaufmann <hallo@michael-kaufmann.ch>
 * 
 * ---------------------------------------------------------------------------
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * ---------------------------------------------------------------------------
 */

package p2pradio.monitor;
import stream2stream.network.*;
import p2pradio.Messages;
import p2pradio.packets.*;
import java.io.*;
import java.net.*;
import java.util.logging.*;


public class Dispatcher extends Thread
{
	private DatagramSocket udpSocket;
	private MonitorApp monitorApp;
	private boolean enabled = true;
	
	private static Logger monitorLogger = Logger.getLogger("p2pradio.MonitorLogger"); //$NON-NLS-1$
	
	static
	{
		monitorLogger.setUseParentHandlers(false);
		monitorLogger.setLevel(Level.FINE);
	}
	
	public Dispatcher(MonitorApp monitorApp)
	{
		super(Messages.getString("MonitorDispatcher.THREAD_NAME")); //$NON-NLS-1$
		setDaemon(true);
		
		this.monitorApp = monitorApp;
		
		try
		{
			udpSocket = new DatagramSocket(Peer.MONITOR_PORT);
		}
		catch (Exception e)
		{
			System.err.println(Messages.getString("COULD_NOT_CREATE_UDP_SOCKET")); //$NON-NLS-1$
			System.exit(1);
		}

	}

	public void run()
	{
		try
		{
			byte[] buffer = new byte[p2pradio.packets.MonitorNotificationPacket.MAX_MESSAGE_SIZE];
			DatagramPacket inPacket = new DatagramPacket(buffer, buffer.length);
					
			// Endlosschleife
			while (true)
			{
				// Auf ein Paket warten
				try
				{
					udpSocket.receive(inPacket);
				}
				catch (IOException e)
				{
					System.err.println(Messages.getString("IO_ERROR")); //$NON-NLS-1$
					e.printStackTrace();
					continue;
				}
				
				if (!enabled)
				{
					return;
				}
				
				Packet actionPacket = PacketFactory.createPacket(inPacket);
				
				if (!(actionPacket instanceof MonitorNotificationPacket))
				{
					System.err.println(Messages.getString("MonitorDispatcher.RECEIVED_A_NON_MONITOR_PACKET")); //$NON-NLS-1$
					continue;
				}
				
				MonitorNotificationPacket monitorPacket = (MonitorNotificationPacket)actionPacket;
				
				// Informationen in ein neues MonitorLogRecord-Objekt eintragen
				Level level = Level.parse(monitorPacket.getMessageType());
				MonitorLogRecord monitorLogRecord = new MonitorLogRecord(level, monitorPacket.getMessage(), monitorPacket.getSender(), monitorPacket.getSupplier(), monitorPacket.getServer());
				monitorLogRecord.setSourceClassName(monitorPacket.getSourceClassName());
				monitorLogRecord.setParameters(monitorPacket.getParameters());
				
				if (monitorPacket.getThrowableString() != null)
				{
					monitorLogRecord.setThrown(new Exception(monitorPacket.getThrowableString()));
				}
				
				// An Logger weiterleiten
				monitorApp.getLogger().log(monitorLogRecord);
			}
		}
		catch (Exception e)
		{
			System.err.println(Messages.getString("MonitorDispatcher.INTERNAL_ERROR")); //$NON-NLS-1$
			e.printStackTrace();
		}
	}
	
	public boolean isEnabled()
	{
		return enabled;
	}
	
	public void setEnabled(boolean enabled)
	{
		this.enabled = enabled;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?