Mercurial > projects > ldc
comparison tango/tango/net/cluster/tina/ProtocolReader.d @ 132:1700239cab2e trunk
[svn r136] MAJOR UNSTABLE UPDATE!!!
Initial commit after moving to Tango instead of Phobos.
Lots of bugfixes...
This build is not suitable for most things.
author | lindquist |
---|---|
date | Fri, 11 Jan 2008 17:57:40 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
131:5825d48b27d1 | 132:1700239cab2e |
---|---|
1 /******************************************************************************* | |
2 | |
3 copyright: Copyright (c) 2004 Kris Bell. All rights reserved | |
4 | |
5 license: BSD style: $(LICENSE) | |
6 | |
7 version: July 2004: Initial release | |
8 | |
9 author: Kris | |
10 | |
11 *******************************************************************************/ | |
12 | |
13 module tango.net.cluster.tina.ProtocolReader; | |
14 | |
15 private import tango.io.protocol.Reader, | |
16 tango.io.protocol.Allocator, | |
17 tango.io.protocol.PickleProtocol; | |
18 | |
19 private import tango.net.cluster.model.IMessage; | |
20 | |
21 private import tango.net.cluster.NetworkRegistry; | |
22 | |
23 private import tango.net.cluster.tina.ClusterTypes; | |
24 | |
25 /******************************************************************************* | |
26 | |
27 Objects passed around a cluster are prefixed with a header, so the | |
28 receiver can pick them apart correctly. This header consists of: | |
29 --- | |
30 * the packet size, including the header (16 bits) | |
31 * a command code (8 bits) | |
32 * a version id (8 bits) | |
33 * a timestamp (64 bits) | |
34 * length of the channel name (32 bits) | |
35 * the channel name | |
36 * length of the key (32 bits) | |
37 * the key | |
38 * an optional payload (an IMessage instance) | |
39 --- | |
40 | |
41 Everything is written in Network order (big endian). | |
42 | |
43 *******************************************************************************/ | |
44 | |
45 class ProtocolReader : Reader | |
46 { | |
47 /*********************************************************************** | |
48 | |
49 Construct a ProtocolReader upon the given buffer. As | |
50 Objects are serialized their content is written to this | |
51 buffer. The buffer content is then typically flushed to | |
52 some external conduit, such as a file or socket. | |
53 | |
54 Note that arrays (such as text) are *always* sliced from | |
55 the buffer -- there's no heap activity involved. Thus it | |
56 may be necessary to .dup content where appropriate | |
57 | |
58 ***********************************************************************/ | |
59 | |
60 this (IBuffer buffer) | |
61 { | |
62 super (new BufferSlice (new PickleProtocol (buffer))); | |
63 } | |
64 | |
65 /*********************************************************************** | |
66 | |
67 deserialize a payload into a provided host, or via | |
68 the registered instance of the incoming payload | |
69 | |
70 ***********************************************************************/ | |
71 | |
72 IMessage thaw (IMessage host = null) | |
73 { | |
74 return thaw (NetworkRegistry.shared, host); | |
75 } | |
76 | |
77 /*********************************************************************** | |
78 | |
79 deserialize a payload into a provided host, or via | |
80 the registered instance of the incoming payload | |
81 | |
82 ***********************************************************************/ | |
83 | |
84 IMessage thaw (NetworkRegistry registry, IMessage host = null) | |
85 { | |
86 return registry.thaw (this, host); | |
87 } | |
88 | |
89 /*********************************************************************** | |
90 | |
91 Read the protocol header and return true if there's a | |
92 payload available | |
93 | |
94 ***********************************************************************/ | |
95 | |
96 bool getHeader (inout ubyte cmd, inout char[] channel, inout char[] element) | |
97 { | |
98 auto position = buffer.position; | |
99 | |
100 long time; | |
101 ushort size; | |
102 ubyte versn; | |
103 | |
104 get (size) (cmd) (versn) (time); | |
105 | |
106 // avoid allocation for these two strings | |
107 get (channel) (element); | |
108 | |
109 // is there a payload attached? | |
110 if (size > (buffer.position - position)) | |
111 return true; | |
112 | |
113 return false; | |
114 } | |
115 | |
116 /*********************************************************************** | |
117 | |
118 Return an aliased slice of the buffer representing the | |
119 recieved payload. This is a bit of a hack, but eliminates | |
120 a reasonable amount of overhead. Note that the channel/key | |
121 text is retained right at the start of the returned content, | |
122 enabling the host to toss the whole thing back without any | |
123 further munging. | |
124 | |
125 ***********************************************************************/ | |
126 | |
127 ClusterContent getPacket (inout ubyte cmd, inout char[] channel, inout char[] element, inout long time) | |
128 { | |
129 ushort size; | |
130 ubyte versn; | |
131 | |
132 // load up the header | |
133 get (size) (cmd) (versn) (time); | |
134 | |
135 //printf ("size: %d\n", cast(int) size); | |
136 | |
137 // subtract header size | |
138 size -= buffer.position; | |
139 | |
140 // may throw an exception if the payload is too large to fit | |
141 // completely inside the buffer! | |
142 buffer.slice (size, false); | |
143 | |
144 // slice the remaining packet (with channel/key text) | |
145 auto content = cast(ClusterContent) buffer.slice; | |
146 | |
147 // get a slice upon the channel name | |
148 get (channel); | |
149 | |
150 // get a slice upon the element name | |
151 get (element); | |
152 | |
153 // return the aliased payload (including channel/key text) | |
154 return content; | |
155 } | |
156 } | |
157 |