Free cookie consent management tool by TermsFeed Policy Generator

source: branches/MPI/HeuristicLab.Operators.MPISupport/3.3/BinaryTransport/MPIBinaryTransportWrapper.cs @ 12209

Last change on this file since 12209 was 7544, checked in by svonolfe, 13 years ago

Improved performance, added MPISolutionsCreator (#1542)

File size: 2.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using System.Runtime.Serialization;
27using HeuristicLab.Core;
28using System.IO;
29using HeuristicLab.Persistence.Default.Xml;
30using System.Runtime.Serialization.Formatters.Binary;
31
32namespace HeuristicLab.Operators.MPISupport.BinaryTransport {
33  /// <summary>
34  /// A simple wrapper to transport items over the message passing interface
35  /// </summary>
36  [Serializable]
37  public class MPIBinaryTransportWrapper: ISerializable {
38    [NonSerialized]
39    private object innerItem;
40
41    public IItem GetInnerItem(IExecutionContext globalScope) {
42        return ItemTransfer.Convert(innerItem, globalScope);
43    }
44
45    public MPIBinaryTransportWrapper(object item) {
46      innerItem = item;
47    }
48
49    public MPIBinaryTransportWrapper(IItem item) {
50      innerItem = ItemTransfer.Convert(item);
51    }
52
53    protected MPIBinaryTransportWrapper(SerializationInfo info, StreamingContext context) {
54      byte[] buffer = info.GetValue("Item", typeof(byte[])) as byte[];
55      IFormatter formatter = new BinaryFormatter();
56      if (buffer != null)
57        try {
58        innerItem = formatter.Deserialize(new MemoryStream(buffer));
59        }
60        catch (Exception e) {
61          Console.WriteLine(e);
62          throw e;
63        }
64      else
65        innerItem = null;
66    }
67
68    #region ISerializable Members
69
70    public void GetObjectData(SerializationInfo info, StreamingContext context) {
71      object value = null;
72
73      if (innerItem != null) {
74        MemoryStream stream = new MemoryStream();
75        IFormatter formatter = new BinaryFormatter();
76        try {
77          formatter.Serialize(stream, innerItem);
78        }
79        catch (Exception e) {
80          Console.WriteLine(e);
81          throw e;
82        }
83        value = stream.GetBuffer();
84      }
85       
86      info.AddValue("Item", value);     
87    }
88
89    #endregion
90  }
91}
Note: See TracBrowser for help on using the repository browser.