PageRenderTime 21ms CodeModel.GetById 12ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/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 */
28package edu.vub.at.actors.natives;
29
30import edu.vub.at.actors.net.SerializationException;
31import edu.vub.at.exceptions.InterpreterException;
32import edu.vub.at.exceptions.XClassNotFound;
33import edu.vub.at.exceptions.XIOProblem;
34import edu.vub.at.objects.ATObject;
35
36import java.io.ByteArrayInputStream;
37import java.io.ByteArrayOutputStream;
38import java.io.IOException;
39import java.io.ObjectInputStream;
40import java.io.ObjectOutputStream;
41import java.io.Serializable;
42
43/**
44 * Packet instances are the serialized representation of AmbientTalk messages or other objects.
45 * They are exchanged directly in between local actors and in a wrapped VMCommand object between
46 * remote actors.
47 *
48 * @author tvcutsem
49 */
50public class Packet implements Serializable {
51	
52	private final byte[]	payload_;
53	private final String	description_;
54
55	public Packet(String description, ATObject object) throws XIOProblem {
56		description_ = description;
57		try {
58			payload_ = serialize(object);
59		} catch (IOException e) {
60			throw new XIOProblem(e);
61		}
62	}
63	
64	public Packet(ATObject object) throws XIOProblem {
65		this(object.toString(), object);
66	}
67	
68	public ATObject unpack() throws InterpreterException {
69		try {
70			return (ATObject) deserialize(payload_);
71        } catch (SerializationException e) {
72          throw e.getWrappedException();
73		} catch (IOException e) {
74			throw new XIOProblem(e);
75		} catch (ClassNotFoundException e) {
76			throw new XClassNotFound(e.getMessage(), e);
77		} 
78	}
79	
80	public String toString() { return "packet["+description_+"]"; }
81	
82	private static byte[] serialize(Object o) throws IOException {
83		ByteArrayOutputStream out = new ByteArrayOutputStream();
84		ObjectOutputStream stream = new ObjectOutputStream(out);
85		stream.writeObject(o);
86		stream.flush();
87		stream.close();
88		return out.toByteArray();
89	}
90	
91	private static Object deserialize(byte[] b) throws IOException, ClassNotFoundException {
92		ByteArrayInputStream in = new ByteArrayInputStream(b);
93		ObjectInputStream instream = new ObjectInputStream(in);
94		return instream.readObject();
95	}
96	
97}