PageRenderTime 32ms CodeModel.GetById 27ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at_build150307/src/edu/vub/at/actors/natives/Packet.java

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