DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Classes | Public Member Functions | Protected Member Functions | Properties | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
Deveel.Data.Protocol.NetworkClientConnector Class Referenceabstract
Inheritance diagram for Deveel.Data.Protocol.NetworkClientConnector:
Deveel.Data.Protocol.IClientConnector Deveel.Data.Protocol.IConnector Deveel.Data.Protocol.TcpClientConnector

Classes

class  ClientProcessor
 
class  NetworkTriggerChannel
 

Public Member Functions

void Dispose ()
 
abstract ConnectionEndPoint MakeEndPoint (IDictionary< string, object > properties)
 
virtual IMessageProcessor CreateProcessor ()
 
virtual IMessageEnvelope CreateEnvelope (IDictionary< string, object > metadata, IMessage message)
 
IStreamableObjectChannel CreateObjectChannel (long objectId)
 
ITriggerChannel CreateTriggerChannel (string triggerName, string objectName, TriggerEventType eventType)
 
void SetEncrypton (EncryptionData encryptionData)
 
- Public Member Functions inherited from Deveel.Data.Protocol.IConnector
ITriggerChannel CreateTriggerChannel (string triggerName, string objectName, TriggerEventType eventType)
 

Protected Member Functions

 NetworkClientConnector ()
 
virtual void Dispose (bool disposing)
 
abstract NetworkStream CreateNetworkStream (ConnectionEndPoint remoteEndPoint, FileAccess access)
 
void ChangeState (ConnectorState newState)
 
void OpenConnector (ConnectionEndPoint remoteEndPoint)
 
void Close ()
 
virtual void OnAuthenticated (string username, long timeStamp)
 
virtual void OnConnectorOpen ()
 
virtual IMessage OnProcessServerResponse (IMessageEnvelope envelope)
 
virtual byte[] SerializeEnvelope (IMessageEnvelope envelope)
 
void SendEnvelope (IMessageEnvelope envelope)
 
virtual IMessageEnvelope DeserializeEnvelope (byte[] bytes)
 
virtual bool ShouldReceive (IDictionary< string, object > senderMetadata, IMessageEnvelope envelope)
 
virtual void OnTriggerNotification (IMessageEnvelope envelope)
 

Properties

int Timeout [get, set]
 
Stream InputStream [get, set]
 
Stream OutputStream [get, set]
 
ConnectorState CurrentState [get, private set]
 
abstract ConnectionEndPoint LocalEndPoint [get]
 
ConnectionEndPoint RemoteEndPoint [get, private set]
 
- Properties inherited from Deveel.Data.Protocol.IConnector
ConnectorState CurrentState [get]
 
ConnectionEndPoint LocalEndPoint [get]
 

Private Member Functions

 ~NetworkClientConnector ()
 
void AssertNotDisposed ()
 
void AssertOpen ()
 
ICryptoTransform SelectHashAlgorithm (string name, byte[] key, byte[] iv, FileAccess access)
 
IMessageEnvelope ReceiveEnvelope (int timeout)
 
IMessageEnvelope ReceiveResponse (int timeout, IDictionary< string, object > senderMetadata)
 
void ReceiveEnvelopes ()
 
void DispatchTriggerCallbacks ()
 

Static Private Member Functions

static int ExtractDispatchId (IDictionary< string, object > metadata)
 

Private Attributes

readonly object channelLock = new object()
 
Thread envelopeReceiver
 
List< IMessageEnvelopeenvelopes
 
List< NetworkTriggerChanneltriggerChannels
 

Detailed Description

Definition at line 30 of file NetworkClientConnector.cs.

Constructor & Destructor Documentation

Deveel.Data.Protocol.NetworkClientConnector.NetworkClientConnector ( )
inlineprotected

Definition at line 36 of file NetworkClientConnector.cs.

36  {
37  envelopeReceiver = new Thread(ReceiveEnvelopes) {
38  IsBackground = true,
39  Name = "DeveelDB Network Client Envelope Receiver",
40  Priority = ThreadPriority.AboveNormal
41  };
42 
43  envelopes = new List<IMessageEnvelope>();
44  }
Deveel.Data.Protocol.NetworkClientConnector.~NetworkClientConnector ( )
inlineprivate

Definition at line 46 of file NetworkClientConnector.cs.

Member Function Documentation

void Deveel.Data.Protocol.NetworkClientConnector.AssertNotDisposed ( )
inlineprivate

Definition at line 103 of file NetworkClientConnector.cs.

103  {
104  if (CurrentState == ConnectorState.Disposed)
105  throw new ObjectDisposedException(GetType().AssemblyQualifiedName);
106  }
void Deveel.Data.Protocol.NetworkClientConnector.AssertOpen ( )
inlineprivate

Definition at line 108 of file NetworkClientConnector.cs.

108  {
109  if (CurrentState != ConnectorState.Open)
110  throw new InvalidOperationException();
111  }
void Deveel.Data.Protocol.NetworkClientConnector.ChangeState ( ConnectorState  newState)
inlineprotected
void Deveel.Data.Protocol.NetworkClientConnector.Close ( )
inlineprotected

Definition at line 139 of file NetworkClientConnector.cs.

139  {
140  try {
141  ChangeState(ConnectorState.Closed);
142 
143  if (envelopeReceiver != null &&
144  envelopeReceiver.ThreadState == ThreadState.Running) {
145  envelopeReceiver.Join(1000);
146  envelopeReceiver = null;
147  }
148 
149  if (InputStream != null)
150  InputStream.Close();
151  if (OutputStream != null)
152  OutputStream.Close();
153  } catch (Exception) {
154 
155  throw;
156  }
157  }
virtual IMessageEnvelope Deveel.Data.Protocol.NetworkClientConnector.CreateEnvelope ( IDictionary< string, object >  metadata,
IMessage  message 
)
inlinevirtual

Implements Deveel.Data.Protocol.IConnector.

Definition at line 174 of file NetworkClientConnector.cs.

174  {
175  int dispatchId = ExtractDispatchId(metadata);
176  var envelope = new NetworkEnvelope(dispatchId, message);
177  envelope.IssueDate = DateTime.UtcNow;
178  return envelope;
179  }
static int ExtractDispatchId(IDictionary< string, object > metadata)
abstract NetworkStream Deveel.Data.Protocol.NetworkClientConnector.CreateNetworkStream ( ConnectionEndPoint  remoteEndPoint,
FileAccess  access 
)
protectedpure virtual
IStreamableObjectChannel Deveel.Data.Protocol.NetworkClientConnector.CreateObjectChannel ( long  objectId)
inline

Implements Deveel.Data.Protocol.IConnector.

Definition at line 190 of file NetworkClientConnector.cs.

190  {
191  throw new NotImplementedException();
192  }
virtual IMessageProcessor Deveel.Data.Protocol.NetworkClientConnector.CreateProcessor ( )
inlinevirtual

Implements Deveel.Data.Protocol.IConnector.

Definition at line 170 of file NetworkClientConnector.cs.

170  {
171  return new ClientProcessor(this);
172  }
ITriggerChannel Deveel.Data.Protocol.NetworkClientConnector.CreateTriggerChannel ( string  triggerName,
string  objectName,
TriggerEventType  eventType 
)
inline

Definition at line 194 of file NetworkClientConnector.cs.

194  {
195  throw new NotImplementedException();
196  }
virtual IMessageEnvelope Deveel.Data.Protocol.NetworkClientConnector.DeserializeEnvelope ( byte[]  bytes)
inlineprotectedvirtual

Definition at line 253 of file NetworkClientConnector.cs.

253  {
254  using (var stream = new MemoryStream(bytes, false)) {
255  var formatter = new BinaryFormatter();
256  return (IMessageEnvelope) formatter.Deserialize(stream);
257  }
258  }
void Deveel.Data.Protocol.NetworkClientConnector.DispatchTriggerCallbacks ( )
inlineprivate

Definition at line 396 of file NetworkClientConnector.cs.

396  {
397  try {
398  while (CurrentState != ConnectorState.Closed) {
399  var notifications = new List<IMessageEnvelope>();
400 
401  lock (envelopes) {
402  foreach (var envelope in envelopes) {
403  if (envelope.Message is TriggerEventNotification) {
404  notifications.Add(envelope);
405  }
406  }
407 
408  Monitor.PulseAll(envelopes);
409  }
410 
411  foreach (var envelope in notifications) {
412  OnTriggerNotification(envelope);
413  }
414  }
415  } catch {
416 
417  }
418  }
virtual void OnTriggerNotification(IMessageEnvelope envelope)
void Deveel.Data.Protocol.NetworkClientConnector.Dispose ( )
inline

Definition at line 52 of file NetworkClientConnector.cs.

52  {
53  try {
54  Dispose(true);
55  } catch (Exception) {
56  // we ignore any exception at this point
57  } finally {
58  ChangeState(ConnectorState.Disposed);
59  }
60 
61  GC.SuppressFinalize(this);
62  }
virtual void Deveel.Data.Protocol.NetworkClientConnector.Dispose ( bool  disposing)
inlineprotectedvirtual

Definition at line 64 of file NetworkClientConnector.cs.

64  {
65  if (disposing) {
66  try {
67  Close();
68 
69  if (envelopeReceiver != null) {
70  try {
71  envelopeReceiver.Abort();
72  envelopeReceiver = null;
73  } catch (Exception) {
74 
75  throw;
76  }
77  }
78  } catch (Exception) {
79  }
80 
81  if (InputStream != null)
82  InputStream.Dispose();
83  if (OutputStream != null)
84  OutputStream.Dispose();
85 
86  OutputStream = null;
87  InputStream = null;
88 
89  ChangeState(ConnectorState.Disposed);
90  }
91  }
static int Deveel.Data.Protocol.NetworkClientConnector.ExtractDispatchId ( IDictionary< string, object >  metadata)
inlinestaticprivate

Definition at line 276 of file NetworkClientConnector.cs.

276  {
277  if (metadata == null || metadata.Count == 0)
278  return -1;
279 
280  object id;
281  if (!metadata.TryGetValue(NetworkEnvelopeMetadataKeys.DispatchId, out id))
282  return -1;
283 
284  return (int) id;
285  }
abstract ConnectionEndPoint Deveel.Data.Protocol.NetworkClientConnector.MakeEndPoint ( IDictionary< string, object >  properties)
pure virtual
virtual void Deveel.Data.Protocol.NetworkClientConnector.OnAuthenticated ( string  username,
long  timeStamp 
)
inlineprotectedvirtual

Definition at line 159 of file NetworkClientConnector.cs.

159  {
160  // TODO: make something with username and timeStamp?
161 
162  ChangeState(ConnectorState.Authenticated);
163  }
virtual void Deveel.Data.Protocol.NetworkClientConnector.OnConnectorOpen ( )
inlineprotectedvirtual

Definition at line 165 of file NetworkClientConnector.cs.

165  {
166  }
virtual IMessage Deveel.Data.Protocol.NetworkClientConnector.OnProcessServerResponse ( IMessageEnvelope  envelope)
inlineprotectedvirtual

Definition at line 181 of file NetworkClientConnector.cs.

181  {
182  if (envelope == null)
183  return null;
184  if (envelope.Error != null)
185  throw new DeveelDbException(envelope.Error.ErrorMessage, envelope.Error.ErrorClass, envelope.Error.ErrorCode);
186 
187  return envelope.Message;
188  }
virtual void Deveel.Data.Protocol.NetworkClientConnector.OnTriggerNotification ( IMessageEnvelope  envelope)
inlineprotectedvirtual

Definition at line 386 of file NetworkClientConnector.cs.

386  {
387  if (triggerChannels == null)
388  return;
389 
390  lock (triggerChannels) {
391  foreach (var channel in triggerChannels) {
392  }
393  }
394  }
void Deveel.Data.Protocol.NetworkClientConnector.OpenConnector ( ConnectionEndPoint  remoteEndPoint)
inlineprotected

Definition at line 120 of file NetworkClientConnector.cs.

120  {
121  try {
122  RemoteEndPoint = remoteEndPoint;
123  var readStream = CreateNetworkStream(remoteEndPoint, FileAccess.Read);
124  var writeStream = CreateNetworkStream(remoteEndPoint, FileAccess.Write);
125 
126  InputStream = new BufferedStream(readStream, 1024*3);
127  OutputStream = new BufferedStream(writeStream, 1024*3);
128 
129  OnConnectorOpen();
131 
132  envelopeReceiver.Start();
133  } catch (Exception ex) {
134  //TODO: log somehwere ...
135  throw;
136  }
137  }
abstract NetworkStream CreateNetworkStream(ConnectionEndPoint remoteEndPoint, FileAccess access)
IMessageEnvelope Deveel.Data.Protocol.NetworkClientConnector.ReceiveEnvelope ( int  timeout)
inlineprivate

Definition at line 260 of file NetworkClientConnector.cs.

260  {
261  lock (channelLock) {
262  using (var input = new BinaryReader(InputStream)) {
263  try {
264  int commandLength = input.ReadInt32();
265  var buf = new byte[commandLength];
266  input.Read(buf, 0, commandLength);
267  return DeserializeEnvelope(buf);
268  } catch (Exception) {
269  //TODO: log ...
270  throw;
271  }
272  }
273  }
274  }
virtual IMessageEnvelope DeserializeEnvelope(byte[] bytes)
void Deveel.Data.Protocol.NetworkClientConnector.ReceiveEnvelopes ( )
inlineprivate

Definition at line 328 of file NetworkClientConnector.cs.

328  {
329  try {
330  while (CurrentState != ConnectorState.Closed) {
331  var envelope = ReceiveEnvelope(0);
332  lock (envelopes) {
333  envelopes.Add(envelope);
334 
335  Monitor.PulseAll(envelopes);
336  }
337  }
338  } catch (Exception) {
339  } finally {
340  // Invalidate this object when the thread finishes.
341  object oldEnvelopes = envelopes;
342  lock (oldEnvelopes) {
343  envelopes = null;
344  Monitor.PulseAll(oldEnvelopes);
345  }
346  }
347  }
IMessageEnvelope Deveel.Data.Protocol.NetworkClientConnector.ReceiveResponse ( int  timeout,
IDictionary< string, object >  senderMetadata 
)
inlineprivate

Definition at line 293 of file NetworkClientConnector.cs.

293  {
294  DateTime timeIn = DateTime.Now;
295  DateTime timeOutHigh = timeIn + new TimeSpan(((long) timeout*1000)*TimeSpan.TicksPerMillisecond);
296 
297  lock (envelopes) {
298  if (envelopes == null)
299  throw new DataException("Connection to server closed");
300 
301  while (true) {
302  for (int i = 0; i < envelopes.Count; ++i) {
303  var envelope = envelopes[i];
304  if (ShouldReceive(senderMetadata, envelope)) {
305  envelopes.RemoveAt(i);
306  return envelope;
307  }
308  }
309 
310  // Return null if we haven't received a response input the timeout
311  // period.
312  if (timeout != 0 &&
313  DateTime.Now > timeOutHigh) {
314  return null;
315  }
316 
317  // Wait a second.
318  try {
319  Monitor.Wait(envelopes, 1000);
320  } catch (ThreadInterruptedException) {
321  /* ignore */
322  }
323 
324  } // while (true)
325  }
326  }
virtual bool ShouldReceive(IDictionary< string, object > senderMetadata, IMessageEnvelope envelope)
ICryptoTransform Deveel.Data.Protocol.NetworkClientConnector.SelectHashAlgorithm ( string  name,
byte[]  key,
byte[]  iv,
FileAccess  access 
)
inlineprivate

Definition at line 198 of file NetworkClientConnector.cs.

198  {
199  if (String.Equals(name, EncryptionAlgorithms.HmacMd5, StringComparison.OrdinalIgnoreCase))
200  return new HMACMD5(key);
201  if (String.Equals(name, EncryptionAlgorithms.HmacSha256, StringComparison.OrdinalIgnoreCase))
202  return new HMACSHA256(key);
203  if (String.Equals(name, EncryptionAlgorithms.HmacSha512, StringComparison.OrdinalIgnoreCase))
204  return new HMACSHA512(key);
205 
206  if (String.Equals(name, EncryptionAlgorithms.Des, StringComparison.OrdinalIgnoreCase)) {
207  var des = new DESCryptoServiceProvider();
208  if (access == FileAccess.Read)
209  return des.CreateDecryptor(key, iv);
210  if (access == FileAccess.Write)
211  return des.CreateEncryptor(key, iv);
212  }
213  if (String.Equals(name, EncryptionAlgorithms.TripleDes, StringComparison.OrdinalIgnoreCase)) {
214  var des = new TripleDESCryptoServiceProvider();
215  if (access == FileAccess.Read)
216  return des.CreateDecryptor(key, iv);
217  if (access == FileAccess.Write)
218  return des.CreateEncryptor(key, iv);
219  }
220 
221  throw new NotSupportedException();
222  }
A long string in the system.
void Deveel.Data.Protocol.NetworkClientConnector.SendEnvelope ( IMessageEnvelope  envelope)
inlineprotected

Definition at line 245 of file NetworkClientConnector.cs.

245  {
246  lock (channelLock) {
247  var bytes = SerializeEnvelope(envelope);
248  OutputStream.Write(bytes, 0, bytes.Length);
249  OutputStream.Flush();
250  }
251  }
virtual byte[] SerializeEnvelope(IMessageEnvelope envelope)
virtual byte [] Deveel.Data.Protocol.NetworkClientConnector.SerializeEnvelope ( IMessageEnvelope  envelope)
inlineprotectedvirtual

Definition at line 236 of file NetworkClientConnector.cs.

236  {
237  using (var stream = new MemoryStream()) {
238  var formatter = new BinaryFormatter();
239  formatter.Serialize(stream, envelope);
240  stream.Flush();
241  return stream.ToArray();
242  }
243  }
void Deveel.Data.Protocol.NetworkClientConnector.SetEncrypton ( EncryptionData  encryptionData)
inline

Implements Deveel.Data.Protocol.IClientConnector.

Definition at line 224 of file NetworkClientConnector.cs.

224  {
225  lock (channelLock) {
226  var key = Encoding.Unicode.GetBytes(encryptionData.Key);
227  var iv = Encoding.Unicode.GetBytes(encryptionData.IV);
228  var readHash = SelectHashAlgorithm(encryptionData.HashAlgorithm, key, iv, FileAccess.Read);
229  var writeHash = SelectHashAlgorithm(encryptionData.HashAlgorithm, key, iv, FileAccess.Write);
230 
231  InputStream = new CryptoStream(InputStream, readHash, CryptoStreamMode.Read);
232  OutputStream = new CryptoStream(OutputStream, writeHash, CryptoStreamMode.Write);
233  }
234  }
ICryptoTransform SelectHashAlgorithm(string name, byte[] key, byte[] iv, FileAccess access)
virtual bool Deveel.Data.Protocol.NetworkClientConnector.ShouldReceive ( IDictionary< string, object >  senderMetadata,
IMessageEnvelope  envelope 
)
inlineprotectedvirtual

Definition at line 287 of file NetworkClientConnector.cs.

287  {
288  var senderId = ExtractDispatchId(senderMetadata);
289  var envelopeId = ExtractDispatchId(envelope.Metadata);
290  return senderId == envelopeId;
291  }
static int ExtractDispatchId(IDictionary< string, object > metadata)

Member Data Documentation

readonly object Deveel.Data.Protocol.NetworkClientConnector.channelLock = new object()
private

Definition at line 31 of file NetworkClientConnector.cs.

Thread Deveel.Data.Protocol.NetworkClientConnector.envelopeReceiver
private

Definition at line 33 of file NetworkClientConnector.cs.

List<IMessageEnvelope> Deveel.Data.Protocol.NetworkClientConnector.envelopes
private

Definition at line 34 of file NetworkClientConnector.cs.

List<NetworkTriggerChannel> Deveel.Data.Protocol.NetworkClientConnector.triggerChannels
private

Definition at line 349 of file NetworkClientConnector.cs.

Property Documentation

ConnectorState Deveel.Data.Protocol.NetworkClientConnector.CurrentState
getprivate set

Definition at line 97 of file NetworkClientConnector.cs.

Stream Deveel.Data.Protocol.NetworkClientConnector.InputStream
getsetprivate

Definition at line 93 of file NetworkClientConnector.cs.

abstract ConnectionEndPoint Deveel.Data.Protocol.NetworkClientConnector.LocalEndPoint
get

Definition at line 99 of file NetworkClientConnector.cs.

Stream Deveel.Data.Protocol.NetworkClientConnector.OutputStream
getsetprivate

Definition at line 95 of file NetworkClientConnector.cs.

ConnectionEndPoint Deveel.Data.Protocol.NetworkClientConnector.RemoteEndPoint
getprivate set

Definition at line 101 of file NetworkClientConnector.cs.

int Deveel.Data.Protocol.NetworkClientConnector.Timeout
getsetprotected

Definition at line 50 of file NetworkClientConnector.cs.


The documentation for this class was generated from the following file: