Mercurial > hg > ucis.core
comparison Pml/PmlCommunicator2.cs @ 103:8fe322656807
Cleanup some old PML code
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Sat, 11 Oct 2014 14:03:31 +0200 |
parents | 3ab940a0c7a0 |
children | 327be9216006 |
comparison
equal
deleted
inserted
replaced
102:1474f92cf7e7 | 103:8fe322656807 |
---|---|
12 private interface ISession { | 12 private interface ISession { |
13 void MessageIn(PmlElement message); | 13 void MessageIn(PmlElement message); |
14 void CloseIn(); | 14 void CloseIn(); |
15 UInt32 ID { get; } | 15 UInt32 ID { get; } |
16 } | 16 } |
17 /*public abstract class SessionBase : ISession { | |
18 private bool pActive; | |
19 private PmlCommunicator _communicator; | |
20 private UInt32 _id; | |
21 | |
22 public uint SID { get { return _id; } } | |
23 public bool Active { get { return pActive; } } | |
24 public PmlCommunicator Communicator { get { return _communicator; } } | |
25 | |
26 protected SessionBase(PmlCommunicator Connection) { | |
27 _communicator = Connection; | |
28 } | |
29 | |
30 protected void Accept(UInt32 sid) { | |
31 if (pActive) throw new InvalidOperationException("Session is active"); | |
32 _id = sid; | |
33 lock (_communicator._sessions) _communicator._sessions.Add(_id, this); | |
34 pActive = true; | |
35 } | |
36 protected void Request() { | |
37 Request(null); | |
38 } | |
39 protected void Request(PmlElement Message) { | |
40 if (pActive) throw new InvalidOperationException("Session is active"); | |
41 _id = _communicator.GetNextSessionId(true); | |
42 lock (_communicator._sessions) _communicator._sessions.Add(_id, this); | |
43 _communicator.WriteSessionMessage(_id, 0, Message); | |
44 pActive = true; | |
45 } | |
46 | |
47 uint ISession.ID { get { return _id; } } | |
48 void ISession.MessageIn(PmlElement message) { this.MessageIn(message); } | |
49 void ISession.CloseIn() { | |
50 pActive = false; | |
51 _communicator.RemoveSession(this); | |
52 Closed(null); | |
53 } | |
54 | |
55 protected internal abstract void MessageIn(PmlElement Message); | |
56 | |
57 protected void SendMessage(PmlElement Message) { | |
58 if (!pActive) throw new InvalidOperationException("Session is not active"); | |
59 _communicator.WriteSessionMessage(_id, 1, Message); | |
60 } | |
61 | |
62 public void Close() { | |
63 if (!pActive) return; | |
64 pActive = false; | |
65 _communicator.WriteSessionMessage(_id, 2, null); | |
66 _communicator.RemoveSession(this); | |
67 } | |
68 | |
69 protected virtual void Closed(PmlElement Message) { } | |
70 } | |
71 public class Session : SessionBase { | |
72 public event MessageReceivedEventHandler MessageReceived; | |
73 public delegate void MessageReceivedEventHandler(PmlElement Message); | |
74 public event SessionClosedEventHandler SessionClosed; | |
75 public delegate void SessionClosedEventHandler(PmlElement Message); | |
76 | |
77 public Session(PmlCommunicator Connection) : base(Connection) { } | |
78 | |
79 public new void Accept(UInt32 SID) { | |
80 base.Accept(SID); | |
81 } | |
82 public new void Request() { | |
83 Request(null); | |
84 } | |
85 public new void Request(PmlElement Message) { | |
86 base.Request(Message); | |
87 } | |
88 | |
89 protected internal override void MessageIn(PmlElement Message) { | |
90 if (MessageReceived != null) MessageReceived(Message); | |
91 } | |
92 | |
93 public new void SendMessage(PmlElement Message) { | |
94 base.SendMessage(Message); | |
95 } | |
96 | |
97 protected override void Closed(PmlElement Message) { | |
98 if (SessionClosed != null) SessionClosed(Message); | |
99 } | |
100 }*/ | |
101 | |
102 private class PmlSubChannel : ActivePmlChannel, ISession { | 17 private class PmlSubChannel : ActivePmlChannel, ISession { |
103 private enum ChannelState { Requesting, Acknowledged, Closed } | 18 private enum ChannelState { Requesting, Acknowledged, Closed } |
104 | 19 |
105 private PmlCommunicator _communicator; | 20 private PmlCommunicator _communicator; |
106 private UInt32 _id; | 21 private UInt32 _id; |
146 _communicator = communicator; | 61 _communicator = communicator; |
147 _data = message; | 62 _data = message; |
148 _sid = sid; | 63 _sid = sid; |
149 _accepted = _rejected = false; | 64 _accepted = _rejected = false; |
150 } | 65 } |
151 public UInt32 AcceptSession() { | |
152 if (_accepted || _rejected) throw new InvalidOperationException("The channel has already been accepted or rejected"); | |
153 _accepted = true; | |
154 return _sid; | |
155 } | |
156 public override IPmlChannel Accept() { | 66 public override IPmlChannel Accept() { |
157 if (_accepted || _rejected) throw new InvalidOperationException("The channel has already been accepted or rejected"); | 67 if (_accepted || _rejected) throw new InvalidOperationException("The channel has already been accepted or rejected"); |
158 _accepted = true; | 68 _accepted = true; |
159 return new PmlSubChannel(_communicator, _sid, true); | 69 return new PmlSubChannel(_communicator, _sid, true); |
160 } | 70 } |
161 public override void Reject() { | 71 public override void Reject() { |
162 if (_accepted) throw new InvalidOperationException("The channel has already been accepted"); | 72 if (_accepted) throw new InvalidOperationException("The channel has already been accepted"); |
163 if (_rejected) return; | 73 if (_rejected) return; |
164 _rejected = true; | 74 _rejected = true; |
165 _communicator.WriteSessionMessage(_sid, 2, null); | 75 _communicator.WriteSessionMessage(_sid, 2, null); |
166 //_channel.RejectOut(); | |
167 } | 76 } |
168 internal void RejectIfNotAccepted() { | 77 internal void RejectIfNotAccepted() { |
169 if (!_accepted) Reject(); | 78 if (!_accepted) Reject(); |
170 } | 79 } |
171 public override PmlElement Data { | 80 public override PmlElement Data { get { return _data; } } |
172 get { | |
173 return _data; | |
174 } | |
175 } | |
176 } | 81 } |
177 | 82 |
178 public event EventHandler<PmlCallReceivedEventArgs> CallReceived; | 83 public event EventHandler<PmlCallReceivedEventArgs> CallReceived; |
179 public event EventHandler<PmlChannelRequestReceivedEventArgs> ChannelRequestReceived; | 84 public event EventHandler<PmlChannelRequestReceivedEventArgs> ChannelRequestReceived; |
180 public event EventHandler Closed; | 85 public event EventHandler Closed; |
266 } | 171 } |
267 private void processMessage(PmlElement Message) { | 172 private void processMessage(PmlElement Message) { |
268 if (Message is PmlString) { | 173 if (Message is PmlString) { |
269 string Cmd = Message.ToString(); | 174 string Cmd = Message.ToString(); |
270 if (Cmd.Equals("PING")) { | 175 if (Cmd.Equals("PING")) { |
271 _WriteMessage(new PmlString("PONG")); | 176 _WriteMessage("PONG"); |
272 /*} else if (Cmd.Equals("PONG")) { | 177 } else if (Cmd.Equals("PONG")) { |
273 Ping = 0;*/ | |
274 } | 178 } |
275 } else if (Message is PmlDictionary) { | 179 } else if (Message is PmlDictionary) { |
276 string Cmd = Message.GetChild("CMD").ToString(); | 180 string Cmd = Message.GetChild("CMD").ToString(); |
277 if (Cmd.Equals("SES")) { | 181 if (Cmd.Equals("SES")) { |
278 processSessionMessage(Message); | 182 processSessionMessage(Message); |