/*
* 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.packets;
import p2pradio.Messages;
import p2pradio.RemotePeer;
import java.io.*;
public class MonitorNotificationPacket extends Packet
{
private RemotePeer sender;
private RemotePeer supplier;
private RemotePeer server;
private String sourceClassName;
private String messageType;
private String message;
private Object[] parameters;
private String throwableString;
private static final byte TYPE_STRING = 1;
private static final byte TYPE_REMOTEPEER = 2;
private static final byte TYPE_INTEGER = 3;
public static final int MAX_MESSAGE_SIZE = 1000;
public MonitorNotificationPacket(RemotePeer sender, RemotePeer supplier, RemotePeer server, String sourceClassName, String messageType, String message, Object[] parameters, Throwable throwable)
{
// L�ngenberechnung w�re viel zu aufwendig
this(new byte[MAX_MESSAGE_SIZE], sender, supplier, server, sourceClassName, messageType, message, parameters, throwable);
}
public MonitorNotificationPacket(byte content[], RemotePeer sender, RemotePeer supplier, RemotePeer server, String sourceClassName, String messageType, String message, Object[] parameters, Throwable throwable)
{
super(Packet.MONITOR_NOTIFICATION, content);
if ((messageType == null) || (message == null))
{
throw new IllegalArgumentException();
}
// Sender
checkBytesLeft(1);
if (sender == null)
{
content[offset++] = 0;
}
else
{
content[offset++] = 1;
AddressContainingPacket.addressToBytes(this, sender.getSocketAddress());
this.sender = sender;
}
// Zulieferer
checkBytesLeft(1);
if (supplier == null)
{
content[offset++] = 0;
}
else
{
content[offset++] = 1;
AddressContainingPacket.addressToBytes(this, supplier.getSocketAddress());
this.supplier = supplier;
}
// Server
checkBytesLeft(1);
if (server == null)
{
content[offset++] = 0;
}
else
{
content[offset++] = 1;
AddressContainingPacket.addressToBytes(this, server.getSocketAddress());
this.server = server;
}
// Klassenname, wo das Ereignis auftrat
checkBytesLeft(1);
if (sourceClassName == null)
{
content[offset++] = 0;
}
else
{
content[offset++] = 1;
writeByteArray(sourceClassName.getBytes());
}
this.sourceClassName = sourceClassName;
// Nachrichten-Typ
writeByteArray(messageType.getBytes());
this.messageType = messageType;
// Nachricht
writeByteArray(message.getBytes());
this.message = message;
// Parameter schreiben
// Anzahl Parameter
int numberOfParameters;
if (parameters != null)
{
numberOfParameters = parameters.length;
}
else
{
numberOfParameters = 0;
}
writeInt(numberOfParameters);
for (int i=0; i < numberOfParameters; i++)
{
checkBytesLeft(1);
if (parameters[i] instanceof RemotePeer)
{
content[offset++] = TYPE_REMOTEPEER;
AddressContainingPacket.addressToBytes(this, ((RemotePeer)parameters[i]).getSocketAddress());
}
else if (parameters[i] instanceof Integer)
{
content[offset++] = TYPE_INTEGER;
writeInt(((Integer)parameters[i]).intValue());
}
else
{
// Als String �bermitteln
content[offset++] = TYPE_STRING;
writeByteArray(parameters[i].toString().getBytes());
}
}
this.parameters = parameters;
// Exception schreiben
checkBytesLeft(1);
if (throwable == null)
{
content[offset++] = 0;
}
else
{
content[offset++] = 1;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(outputStream);
PrintWriter printWriter = new PrintWriter(writer);
throwable.printStackTrace(printWriter);
printWriter.flush();
byte[] throwableBytes = outputStream.toByteArray();
writeByteArray(throwableBytes);
throwableString = outputStream.toString();
}
}
protected MonitorNotificationPacket(byte content[], int contentLength)
{
super(content, contentLength);
// Sender
checkBytesLeft(1);
if (content[offset++] != 0)
{
sender = new RemotePeer(AddressContainingPacket.bytesToAddress(this));
}
// Zulieferer
checkBytesLeft(1);
if (content[offset++] != 0)
{
supplier = new RemotePeer(AddressContainingPacket.bytesToAddress(this));
}
// Server
checkBytesLeft(1);
if (content[offset++] != 0)
{
server = new RemotePeer(AddressContainingPacket.bytesToAddress(this));
}
// Klassenname, wo das Ereignis auftrat
checkBytesLeft(1);
if (content[offset++] != 0)
{
byte[] sourceClassNameBytes = readByteArray();
sourceClassName = new String(sourceClassNameBytes);
}
// Nachrichten-Typ
byte[] messageTypeBytes = readByteArray();
messageType = new String(messageTypeBytes);
// Nachricht
byte[] messageBytes = readByteArray();
message = new String(messageBytes);
// Parameter lesen
// Anzahl Parameter
int numberOfParameters = readInt();
if (numberOfParameters != 0)
{
parameters = new Object[numberOfParameters];
}
else
{
parameters = null;
}
for (int i=0; i < numberOfParameters; i++)
{
checkBytesLeft(1);
byte type = content[offset++];
if (type == TYPE_REMOTEPEER)
{
parameters[i] = new RemotePeer(AddressContainingPacket.bytesToAddress(this));
}
else if (type == TYPE_INTEGER)
{
parameters[i] = new Integer(readInt());
}
else if (type == TYPE_STRING)
{
parameters[i] = new String(readByteArray());
}
else
{
System.err.println(Messages.getString("MonitorNotificationPacket.UNKNOWN_FORMAT_TYPE", new Integer(type))); //$NON-NLS-1$
}
}
// Exception lesen
checkBytesLeft(1);
if (content[offset++] != 0)
{
throwableString = new String(readByteArray());
}
}
// Die Methode getMaxLength() gibt hier nur eine
// Mindestl�nge zur�ck
protected static int getMaxLength()
{
return SimplePacket.getMaxLength();
}
protected String getTypeString()
{
return "MonitorNotification"; //$NON-NLS-1$
}
public RemotePeer getSender()
{
return sender;
}
public RemotePeer getSupplier()
{
return supplier;
}
public RemotePeer getServer()
{
return server;
}
public String getSourceClassName()
{
return sourceClassName;
}
public String getMessageType()
{
return messageType;
}
public String getMessage()
{
return message;
}
public Object[] getParameters()
{
return parameters;
}
public String getThrowableString()
{
return throwableString;
}
}