/interpreter/tags/at2-build270707/src/edu/vub/at/actors/natives/Packet.java

http://ambienttalk.googlecode.com/ · Java · 97 lines · 52 code · 11 blank · 34 comment · 0 complexity · 203be5c1ee6096a163fb9779ba1f6ff1 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * Packet.java created on 29-dec-2006 at 19:15:00
  4. * (c) Programming Technology Lab, 2006 - 2007
  5. * Authors: Tom Van Cutsem & Stijn Mostinckx
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use,
  11. * copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following
  14. * conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. package edu.vub.at.actors.natives;
  29. import edu.vub.at.actors.net.SerializationException;
  30. import edu.vub.at.exceptions.InterpreterException;
  31. import edu.vub.at.exceptions.XClassNotFound;
  32. import edu.vub.at.exceptions.XIOProblem;
  33. import edu.vub.at.objects.ATObject;
  34. import java.io.ByteArrayInputStream;
  35. import java.io.ByteArrayOutputStream;
  36. import java.io.IOException;
  37. import java.io.ObjectInputStream;
  38. import java.io.ObjectOutputStream;
  39. import java.io.Serializable;
  40. /**
  41. * Packet instances are the serialized representation of AmbientTalk messages or other objects.
  42. * They are exchanged directly in between local actors and in a wrapped VMCommand object between
  43. * remote actors.
  44. *
  45. * @author tvcutsem
  46. */
  47. public class Packet implements Serializable {
  48. private final byte[] payload_;
  49. private final String description_;
  50. public Packet(String description, ATObject object) throws XIOProblem {
  51. description_ = description;
  52. try {
  53. payload_ = serialize(object);
  54. } catch (IOException e) {
  55. throw new XIOProblem(e);
  56. }
  57. }
  58. public Packet(ATObject object) throws XIOProblem {
  59. this(object.toString(), object);
  60. }
  61. public ATObject unpack() throws InterpreterException {
  62. try {
  63. return (ATObject) deserialize(payload_);
  64. } catch (SerializationException e) {
  65. throw e.getWrappedException();
  66. } catch (IOException e) {
  67. throw new XIOProblem(e);
  68. } catch (ClassNotFoundException e) {
  69. throw new XClassNotFound(e.getMessage(), e);
  70. }
  71. }
  72. public String toString() { return "packet["+description_+"]"; }
  73. private static byte[] serialize(Object o) throws IOException {
  74. ByteArrayOutputStream out = new ByteArrayOutputStream();
  75. ObjectOutputStream stream = new ObjectOutputStream(out);
  76. stream.writeObject(o);
  77. stream.flush();
  78. stream.close();
  79. return out.toByteArray();
  80. }
  81. private static Object deserialize(byte[] b) throws IOException, ClassNotFoundException {
  82. ByteArrayInputStream in = new ByteArrayInputStream(b);
  83. ObjectInputStream instream = new ObjectInputStream(in);
  84. return instream.readObject();
  85. }
  86. }