summaryrefslogtreecommitdiff
path: root/IvyPPC/IvyTCPStreamV4.cs
blob: 9a96d62d258ecfe46815727bb92f7eb987b68b65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
using System.IO;

namespace IvyBus
{
	/// <summary>
	/// Description résumée de IvyStream.
	/// </summary>
    internal class IvyTCPStreamV4 : NetworkStream , IvyProtocol
	{
		BinaryReader input;
		BinaryWriter output;
        IvyProtocol receiver;

        /* the protocol magic numbers */
        internal enum MessageType : ushort
        {
            Bye = 0,			/* end of the peer */
            AddRegexp = 1,		/* the peer adds a regexp */
            Msg = 2,			/* the peer sends a message */
            Error = 3,			/* error message */
            DelBinding = 4,		/* the peer removes one of his regex */ // OLD DelRegexp rename to DelBinding
            EndRegexp = 5,		/* no more regexp in the handshake */
            StartRegexp = 6,	/* avoid race condition in concurrent connexions */
            DirectMsg = 7,		/* the peer sends a direct message */
            Die = 8,			/* the peer wants us to quit */
            Ping = 9,			/* checks the presence of the other */
            Pong = 10,			/* checks the presence of the other */
            ApplicationId = 11,	/* on start send my ID and priority */
            AddBinding = 12,	/* other methods for binding message based on hash table */

        };

		internal IvyTCPStreamV4(Socket socket, IvyProtocol _receiver) : base( socket ) 
		{

            input = new BinaryReader(this, Encoding.ASCII);
            output = new BinaryWriter(this, Encoding.ASCII);
            receiver = _receiver;
		}
		
   
		/*
		 * message Syntax:
		 * this is a binary formated message use of network representation
		 * 
		 * message Format:
				MessageType, id , length, string 
		 */
        private void Serialize(short arg)
        {
            output.Write((ushort)IPAddress.HostToNetworkOrder(arg));
        }
        private void Serialize(string arg)
        {
            short length = arg != null ? (short)arg.Length : (short)0;
            Serialize(length);
            if (length != 0)
                output.Write(arg.ToCharArray());
        }
        private void Serialize(string[] arg)
        {

            /* serialize count */
            Serialize((short)arg.Length);

            for (int i = 0; i < arg.Length; i++)
            {
                Serialize(arg[i]);
            }
        }
        private void sendMsg(MessageType type, int id, params string[] arg)
		{
		
			Serialize( (short)type  );
			Serialize( (short)id  );
            Serialize(arg);
			output.Flush();
		
		}
        void IvyProtocol.TokenStartRegexp(ushort port, string appName)
        {
            sendMsg(MessageType.StartRegexp, port, appName);
        }
        void IvyProtocol.TokenEndRegexp()
        {
            sendMsg(MessageType.EndRegexp, 0, "");
        }
        void IvyProtocol.TokenApplicationId(ushort priority, string appId)
        {
            sendMsg(MessageType.ApplicationId, priority, appId);
        }

        void IvyProtocol.TokenAddBinding(BindingType type, ushort id, string expression)
        {
            switch (type)
            {
                case BindingType.Regexp:
                    sendMsg(MessageType.AddRegexp, id, expression); /* perhaps we should perform some checking here */
                    break;
                case BindingType.Simple:
                    sendMsg(MessageType.AddBinding, id, expression); /* perhaps we should perform some checking here */
                    break;
            }
        }

        void IvyProtocol.TokenDelBinding(ushort id)
        {
            sendMsg(MessageType.DelBinding, id, null );
        }

        void IvyProtocol.TokenDirectMsg(ushort id, string message)
        {
            sendMsg(MessageType.DirectMsg, id, message);
        }
        void IvyProtocol.TokenPong(string s)
        {
            sendMsg(MessageType.Pong, 0, s);
        }
        void IvyProtocol.TokenPing(string s)
        {
            sendMsg(MessageType.Ping, 0, s);
        }

        void IvyProtocol.TokenBye(ushort id, string message)
        {
            sendMsg(MessageType.Bye, id, message);
        }

        void IvyProtocol.TokenDie(ushort id, string message)
        {
            sendMsg(MessageType.Die, id, message);
        }

        void IvyProtocol.TokenMsg(ushort key, string[] args)
		{
            sendMsg(MessageType.Msg, key, args );
        }

        void IvyProtocol.TokenError(ushort key, string arg)
        {
            sendMsg(MessageType.Msg, key, arg);
        }

        private short DeserializeShort()
        {
            return IPAddress.NetworkToHostOrder((short)input.ReadUInt16());
        }
        private string DeserializeString()
        {
            string arg;
            int val_len;
            char[] data;
            val_len = (ushort)DeserializeShort();
            if (val_len != 0)
            {
                data = input.ReadChars(val_len);
                arg = new String(data);
            }
            else
                arg = "";
            return arg;
        }
        
        
        private string[] DeserializeArgument()
        {
            int nb_children;
            string[] arg;

            /* Deserialize childrens */
            nb_children = (ushort)DeserializeShort();
            /* deserialize Value */
            arg = new string[nb_children];

            for (int i = 0; i < nb_children; i++)
            {
                arg[i] = DeserializeString();
            }
            return arg;
        }
        bool IvyProtocol.receiveMsg()
		{
            MessageType msgType = MessageType.Die;
            ushort msgId = 0;
            string[] msgData = null;

			try
			{
                msgType = (MessageType)(ushort)DeserializeShort();
                msgId = (ushort)DeserializeShort();
				msgData = DeserializeArgument();
        
			switch (msgType)
			{
				case MessageType.Die:
                    receiver.TokenDie(msgId, msgData[0]);
					break;
				
				case MessageType.Bye:
                    receiver.TokenBye(msgId, msgData[0]);
					break;
				
				case MessageType.AddRegexp:
                    receiver.TokenAddBinding(BindingType.Regexp, msgId, msgData[0]);
					break;
				
				case MessageType.AddBinding:
                    receiver.TokenAddBinding(BindingType.Simple, msgId, msgData[0]);
					break;
				
				case MessageType.DelBinding:
                    receiver.TokenDelBinding(msgId);
					break;
				
				case MessageType.EndRegexp:
                    receiver.TokenEndRegexp();
					break;
				
				case MessageType.Msg: 
					receiver.TokenMsg( msgId, msgData );
					break;
				
				case MessageType.Pong:
                    receiver.TokenPong(msgData[0]);
					break;
				
				case MessageType.Ping:
                    receiver.TokenPing(msgData[0]);
					break;
				
				case MessageType.Error:
                    receiver.TokenError(msgId, msgData[0]);
					break;
				
				case MessageType.StartRegexp:
                    receiver.TokenStartRegexp(msgId, msgData[0]);
					break;
				
				case MessageType.DirectMsg:
                    receiver.TokenDirectMsg(msgId, msgData[0]);
					break;
				case MessageType.ApplicationId:
                    receiver.TokenApplicationId(msgId, msgData[0]);
					break;
				default: 
					throw new IvyException("protocol error, unknown message type " + msgType);
				
			}
		
		
	
			}
			catch (EndOfStreamException)
			{
				return false;
			}
			catch (FormatException)
			{
				throw new IvyException("protocol error on msgType");
			}
			
			
			return true;
		}
		
	}
}