#region License Information
/* HeuristicLab
* Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab 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 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab 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 HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using HeuristicLab.Core;
using System.IO;
using HeuristicLab.Persistence.Default.Xml;
using System.Runtime.Serialization.Formatters.Binary;
namespace HeuristicLab.Operators.MPISupport.BinaryTransport {
///
/// A simple wrapper to transport items over the message passing interface
///
[Serializable]
public class MPIBinaryTransportWrapper: ISerializable {
[NonSerialized]
private object innerItem;
public IItem GetInnerItem(IExecutionContext globalScope) {
return ItemTransfer.Convert(innerItem, globalScope);
}
public MPIBinaryTransportWrapper(object item) {
innerItem = item;
}
public MPIBinaryTransportWrapper(IItem item) {
innerItem = ItemTransfer.Convert(item);
}
protected MPIBinaryTransportWrapper(SerializationInfo info, StreamingContext context) {
byte[] buffer = info.GetValue("Item", typeof(byte[])) as byte[];
IFormatter formatter = new BinaryFormatter();
if (buffer != null)
try {
innerItem = formatter.Deserialize(new MemoryStream(buffer));
}
catch (Exception e) {
Console.WriteLine(e);
throw e;
}
else
innerItem = null;
}
#region ISerializable Members
public void GetObjectData(SerializationInfo info, StreamingContext context) {
object value = null;
if (innerItem != null) {
MemoryStream stream = new MemoryStream();
IFormatter formatter = new BinaryFormatter();
try {
formatter.Serialize(stream, innerItem);
}
catch (Exception e) {
Console.WriteLine(e);
throw e;
}
value = stream.GetBuffer();
}
info.AddValue("Item", value);
}
#endregion
}
}