⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 monitorloghandler.java

📁 java语言开发的P2P流媒体系统
💻 JAVA
字号:
/* 
 * 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.logging;

import p2pradio.*;
import p2pradio.packets.*;
import stream2stream.network.*;

import java.util.logging.*;
import java.io.*;
import java.net.*;


/**
 * This log {@link java.util.logging.Handler} sends all log messages to
 * a monitor. It also sends the addresses of the server, the current
 * supplier and the peer itself.
 * <P>
 * The log messages are sent untranslated, because the monitor will translate them.
 * The following parameters of a {@link java.util.logging.LogRecord} will be converted
 * to a byte array representation: {@link p2pradio.RemotePeer} and {@link java.lang.Integer}.
 * Parameters of other types will be converted to a String.
 * 
 * @author Michael Kaufmann
 */
public class MonitorLogHandler extends Handler
{
	private RemotePeer monitor;
	private Peer peer;
	private DatagramSocket udpSocket;
	
	public MonitorLogHandler(Peer peer)
	{
		if (Radio.enableMonitor)
		{
			this.peer = peer;
		
			try
			{
				udpSocket = new DatagramSocket();
			}
			catch (Exception e)
			{
				Logger.warning("MonitorLogHandler", "COULD_NOT_CREATE_UDP_SOCKET"); //$NON-NLS-1$ //$NON-NLS-2$
			}
		}
	}
	
	public void setMonitorAddress(InetSocketAddress monitorAddress)
	{
		if ((monitor == null) || (!monitor.getSocketAddress().equals(monitorAddress)))
		{
			monitor = new RemotePeer(monitorAddress);
		}
	}
		
	public InetSocketAddress getMonitorAddress()
	{
		if (monitor != null)
		{
			return monitor.getSocketAddress();
		}
		else
		{
			return null;
		}
	}
	
	public void publish(LogRecord record)
	{
		if (!Radio.enableMonitor || (monitor == null) || (udpSocket == null) || (peer == null))
		{
			return;
		}
		
		if (getFilter() != null)
		{
			if (!getFilter().isLoggable(record))
			{
				return;
			}
		}
		
		DatagramPacket outPacket = null; 
		try
		{
			if (peer.isServer())
			{
				outPacket = PacketFactory.createUDPPacket(new MonitorNotificationPacket(peer, null, peer, record.getSourceClassName(), record.getLevel().getName(), record.getMessage(), record.getParameters(), record.getThrown()), monitor);
			}
			else
			{
				outPacket = PacketFactory.createUDPPacket(new MonitorNotificationPacket(peer, peer.getSupplier(), peer.getServer(), record.getSourceClassName(), record.getLevel().getName(), record.getMessage(), record.getParameters(), record.getThrown()), monitor);				
			}
		}
		catch (SocketException e)
		{
			Logger.warning("MonitorLogHandler", "COULD_NOT_CREATE_UDP_PACKET"); //$NON-NLS-1$ //$NON-NLS-2$
			return;
		}
		
		try
		{
			udpSocket.send(outPacket);
		}
		catch(IOException e)
		{
			Logger.warning("MonitorLogHandler", "IO_ERROR", e); //$NON-NLS-1$ //$NON-NLS-2$
		}
	}

	public void flush()
	{
	}

	public void close() throws SecurityException
	{
		udpSocket.close();
		
		monitor = null;
		peer = null;
		udpSocket = null;
	}
}

⌨️ 快捷键说明

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