source: BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/BMX41000/IHS BMX Framework/IndianHealthService.BMXNet/Sevices/BMXNetRemoteSession.cs@ 1722

Last change on this file since 1722 was 1180, checked in by Sam Habiel, 14 years ago

RemoteSession.cs Interface: Added:

  • Lock method to lock glvns on M db.
  • Encoding ConnectionEncoding to set the connection encoding on the DB

BMXNetSessionConnection: Added:

  • Abstract ConnectionEncoding property
  • Clarified error message that gets called. It now says that you don't have TransmitRPC writer lock??? When the error could be any BMXNetException.

BMXNetSessionSocketConnection:

  • Added ConnectionEncoding property
  • ReceiveString completely refactored: now we get much better performance
  • Timers and Debug Writes are all over the place now.

BMXNetRemoteSession:

  • Implemented the 2 new 'stuff' in Interface RemoteSesssion: -- Lock glvn -- Encoding Property
  • TableFromSQL with Dataset has an honest to god bug in it: The passed dataset and table name are not used even though they are passed.

BMXNetSessionConnectionOverAnotherSessionConnection:

  • Implemented the Encoding Property in Interface RemoteSession to have the project compile.

Updated dlls. Have fun.

File size: 36.7 KB
Line 
1using System;
2using System.Data;
3using System.Text;
4using System.Windows.Forms;
5using IndianHealthService.BMXNet.Model;
6using IndianHealthService.BMXNet.Ado;
7using System.Collections.Generic;
8using System.Threading;
9
10namespace IndianHealthService.BMXNet.Services
11{
12 /// <summary>
13 /// Extends BMX functionality for easier development and debugging.
14 /// Copied from Component Framework project IndianHealthService.Xo.Framework.Rpms
15 /// </summary>
16 internal class BMXNetRemoteSession : RemoteSession, RemoteEventService
17 {
18 public int ReceiveTimeout
19 {
20 get { return this.SessionConnection.ReceiveTimeout; }
21 set { this.SessionConnection.ReceiveTimeout = value; }
22 }
23
24 public int SendTimeout
25 {
26 get { return this.SessionConnection.SendTimeout; }
27 set { this.SessionConnection.SendTimeout = value; }
28 }
29
30 private Log _log = new NullLog();
31
32 public Log Log
33 {
34 get { return _log; }
35 set { _log = value; }
36 }
37
38 private object _rpcLock = new object();
39
40 public object RpcLock
41 {
42 get { return _rpcLock; }
43 set { _rpcLock = value; }
44 }
45
46 private Guid _id = Guid.NewGuid();
47
48 public Guid Id
49 {
50 get { return _id; }
51 set { _id = value; }
52 }
53
54 private System.Timers.Timer _pollingTimer = null;
55
56 protected System.Timers.Timer PollingTimer
57 {
58 get { return _pollingTimer; }
59 set { _pollingTimer = value; }
60 }
61
62 private int _eventPollingInterval = 5000;
63
64 public int EventPollingInterval
65 {
66 get {
67 return _eventPollingInterval;
68 }
69 set
70 {
71 _eventPollingInterval = value;
72 if (this.IsEventPollingEnabled)
73 {
74 this.PollingTimer.Interval = value;
75 }
76 }
77 }
78
79 public Encoding ConnectionEncoding
80 {
81 get
82 {
83 return SessionConnection.ConnectionEncoding;
84 }
85 set
86 {
87 SessionConnection.ConnectionEncoding = value;
88 }
89 }
90
91 public bool IsEventPollingEnabled
92 {
93 get
94 {
95 return this.PollingTimer != null;
96 }
97 set
98 {
99 if (value)
100 {
101 this.StartPollingTimer();
102 }
103 else
104 {
105 this.StopPollingTimer();
106 }
107 }
108 }
109
110 private void AssertPrimarySession()
111 {
112 if (!this.IsPrimary)
113 {
114 throw new NotSupportedException("This behavior is not supported on non-primary sessions. See documentation.");
115 }
116 }
117
118
119 private void StartPollingTimer()
120 {
121 if (this.PollingTimer == null)
122 {
123 this.PollingTimer = new System.Timers.Timer(this.EventPollingInterval);
124 this.PollingTimer.Enabled = true;
125 this.PollingTimer.Elapsed += new System.Timers.ElapsedEventHandler(PollingTimer_Elapsed);
126 this.PollingTimer.Start();
127 }
128 }
129
130 private bool _inPollForEvent = false;
131
132 protected bool InPollForEvent
133 {
134 get { return _inPollForEvent; }
135 set { _inPollForEvent = value; }
136 }
137
138
139 void PollingTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
140 {
141 if (!this.InPollForEvent)
142 {
143 try
144 {
145 this.InPollForEvent = true;
146 this.PollForEvent();
147 }
148 finally
149 {
150 this.InPollForEvent = false;
151 }
152 }
153 }
154
155 private void StopPollingTimer()
156 {
157 if (this.PollingTimer != null)
158 {
159 this.PollingTimer.Enabled = false;
160 this.PollingTimer.Stop();
161 this.PollingTimer.Elapsed -= new System.Timers.ElapsedEventHandler(PollingTimer_Elapsed);
162 this.PollingTimer = null;
163 }
164 }
165
166 public String Job
167 {
168 get { return this.SessionConnection == null ? "" : this.SessionConnection.Job; }
169 }
170
171 public override string ToString()
172 {
173 return "RemoteSession [Process " + this.Job + (this.IsPrimary ? ", Primary Session]" : "]");
174 }
175 private bool _isPrimary = false;
176
177 public bool IsPrimary
178 {
179 get { return _isPrimary; }
180 set { _isPrimary = value; }
181 }
182
183 private bool _isConnected = false;
184
185 public bool IsConnected
186 {
187 get
188 {
189 return (_isConnected || this.IsPrimary) && this.SessionConnection.IsConnected;
190 }
191 set { _isConnected = value; }
192 }
193
194 public User User
195 {
196 get { return this.Broker.User; }
197 }
198
199 public BMXNetBroker Broker
200 {
201 get { return this.SessionConnection.Broker; }
202
203 }
204 private BMXNetSessionConnection _sessionConnection;
205
206 internal BMXNetSessionConnection SessionConnection
207 {
208 get { return _sessionConnection; }
209 set { _sessionConnection = value; }
210 }
211
212 private BMXNetDataAdapter m_cachedDataAdapter;
213
214 private String _debugLastRpcSignature;
215
216 public String DebugLastRpcSignature
217 {
218 get { return _debugLastRpcSignature; }
219 set { _debugLastRpcSignature = value; }
220 }
221
222 private String _debugLastRpcResult;
223
224 public String DebugLastRpcResult
225 {
226 get { return _debugLastRpcResult; }
227 set { _debugLastRpcResult = value; }
228 }
229
230 private DataTable _debugLastTableResult;
231
232 public DataTable DebugLastTableResult
233 {
234 get { return _debugLastTableResult; }
235 set { _debugLastTableResult = value; }
236 }
237
238 private String _rpcResult;
239
240 public BMXNetRemoteSession()
241 {
242 this.m_cachedDataAdapter = new BMXNetDataAdapter();
243 }
244
245 internal void Begin(BMXNetSessionConnection aConnection, BMXRemoteSessionPool aPool)
246 {
247 this.SessionConnection = aConnection;
248 this.SessionPool = aPool;
249 this.IsConnected = true;
250 }
251
252 private void PollForTimerEvent()
253 {
254 if (this.TimerEvent != null)
255 {
256 try
257 {
258 this.TimerEvent(this, new EventArgs());
259 }
260 catch (Exception problemToIgnore)
261 {
262 this.Log.Log("BMXNetRemoteSession", "Exception", problemToIgnore.Message);
263 }
264 }
265 }
266
267 public RemoteEventService EventServices
268 {
269 get { return this; }
270 }
271
272 private Control _invokedControl = null;
273
274 public Control InvokedControl
275 {
276 get { return _invokedControl; }
277 set { _invokedControl = value; }
278 }
279
280
281 private DataTable _debugLastTableChanges = null;
282
283 public DataTable DebugLastTableChanges
284 {
285 get { return _debugLastTableChanges; }
286 set { _debugLastTableChanges = value; }
287 }
288
289 public event EventHandler<RemoteEventArgs> RpmsEvent;
290 public event EventHandler TimerEvent;
291
292 internal void AuthenticatedConnectionInfo(BMXNetSessionConnection aSessionConnection)
293 {
294 this.SessionConnection = aSessionConnection;
295 }
296
297 public virtual String TransmitRPC(String rpcCommand, String rpcParameter, String aContext)
298 {
299 this.DebugLastRpcSignature = rpcCommand + "^" + rpcParameter + "^" + aContext;
300 this.DebugLastRpcResult = null;
301
302 lock (this.RpcLock)
303 {
304 this.AppContext = aContext;
305 this.RpcResult = this.SessionConnection.TransmitRPC(rpcCommand, rpcParameter);
306 this.DebugLastRpcResult = this.RpcResult;
307 }
308
309 return this.RpcResult;
310 }
311
312 private void AssertConnected()
313 {
314 if (!this.IsConnected)
315 {
316 throw new BMXNetException("Session is not connected or has been closed.");
317 }
318 }
319
320 public String SafelyTransmitRPC(String rpcCommand, String rpcParameter, String context)
321 {
322 this.AssertConnected();
323
324 try
325 {
326 return this.TransmitRPC(rpcCommand, rpcParameter, context);
327 }
328 catch (Exception exception)
329 {
330 return "Exception: " + exception.Message;
331 }
332 }
333
334
335
336 public bool SaveChanges(DataTable aDataTable)
337 {
338 this.AssertConnected();
339
340 DataTable changes = aDataTable.GetChanges();
341
342 this.DebugLastTableChanges = changes;
343
344 if (changes == null)
345 return false;
346
347 BMXNetConnection bmxConnection = null;
348
349 lock (this.RpcLock)
350 {
351 try
352 {
353 bmxConnection = new BMXNetConnection(this.SessionConnection);
354 bmxConnection.Open();
355
356 if (bmxConnection.State == ConnectionState.Open)
357 {
358 BMXNetDataAdapter bmxDataAdaptor = this.m_cachedDataAdapter;
359
360 BMXNetCommand bmxSelectCommand = bmxConnection.CreateCommand() as BMXNetCommand;
361 bmxSelectCommand.CommandText = aDataTable.ExtendedProperties["BMXNetSelectStatementForUpdate"] as String;
362 bmxDataAdaptor.SelectCommand = bmxSelectCommand;
363
364 DataTable schema = bmxDataAdaptor.FillSchema(aDataTable, SchemaType.Source);
365 BMXNetCommand bmxUpdateCommand = bmxConnection.CreateCommand() as BMXNetCommand;
366 bmxUpdateCommand.BMXBuildUpdateCommand(schema);
367 bmxDataAdaptor.UpdateCommand = bmxUpdateCommand;
368
369 bmxDataAdaptor.Update(changes);
370 aDataTable.AcceptChanges();
371
372 return true;
373 }
374 else
375 {
376 throw new BMXNetException("Unable to connect to RPMS.");
377 }
378 }
379
380 catch (Exception exception)
381 {
382 throw new BMXNetException("Unable to save data to RPMS.", exception);
383 }
384 finally
385 {
386 if (bmxConnection != null)
387 {
388 bmxConnection.Close();
389 }
390 }
391 }
392 }
393
394
395 public virtual DataTable TableFromCommand(String aCommand)
396 {
397 return this.TableFromCommand(aCommand, this.AppContext);
398 }
399
400 public DataTable TableFromCommand(string aCommand, string aContext)
401 {
402 return this.TableFromCommand(aCommand, null, null,aContext);
403 }
404
405
406 public DataTable TableFromCommand(String aCommand, DataSet aDataSet, string aTableName)
407 {
408 return this.TableFromCommand(aCommand, aDataSet, aTableName, this.AppContext);
409 }
410
411 public virtual DataTable TableFromCommand(String aCommand, DataSet aDataSet, string aTableName, string aContext)
412 {
413 this.Log.Log("BMX NET", "Debug", "TableFromCommand >> " + aCommand, aTableName == null ? "Null Table" : aTableName, aContext == null ? "Null Context" : aContext);
414 this.DebugLastTableResult = null;
415
416 this.AssertConnected();
417
418 DataSet dataSet = aDataSet == null ? new DataSet() : aDataSet;
419
420 BMXNetConnection bmxConnection = new BMXNetConnection(this.SessionConnection);
421 bmxConnection.Open();
422
423 if (bmxConnection.State != ConnectionState.Open)
424 throw new BMXNetException("Unable to connect to RPMS.");
425
426 lock (this.RpcLock)
427 {
428 try
429 {
430 this.AppContext = aContext;
431 BMXNetCommand bmxCommand = bmxConnection.CreateCommand() as BMXNetCommand;
432 bmxCommand.CommandText = aCommand;
433 BMXNetDataAdapter bmxDataAdaptor = new BMXNetDataAdapter();
434 bmxDataAdaptor.SelectCommand = bmxCommand;
435
436
437 DataTable answer = null;
438 String validTableName = aTableName == null ? this.DefaultTableName : aTableName;
439 DateTime stopWatch = DateTime.Now;
440
441 if (aDataSet == null)
442 {
443 answer = new DataTable(validTableName);
444 bmxDataAdaptor.Fill(answer);
445 }
446 else
447 {
448 DataTable dataTable = aDataSet.Tables.Contains(validTableName) ? aDataSet.Tables[validTableName] : aDataSet.Tables.Add(validTableName);
449 bmxDataAdaptor.Fill(aDataSet, validTableName);
450 answer = aDataSet.Tables[validTableName];
451 }
452
453 answer.ExtendedProperties["BMXNetSelectStatementForUpdate"] = bmxCommand.CommandText;
454 answer.ExtendedProperties["BMXQueryStart"] = stopWatch;
455 answer.ExtendedProperties["BMXQueryTime"] = (DateTime.Now - stopWatch).TotalMilliseconds;
456 answer.ExtendedProperties["RpmsQuery"] = aCommand;
457 answer.ExtendedProperties["RpmsSchema"] = this.RpmsSchema(aCommand);
458
459 this.DebugLastTableResult = answer;
460
461 return answer;
462 }
463
464 catch (Exception exception)
465 {
466 throw new BMXNetException("Unable to retrive data from RPMS.", exception);
467 }
468 finally
469 {
470 if (bmxConnection != null)
471 {
472 bmxConnection.Close();
473 }
474 }
475 }
476 }
477
478 private String RpmsSchema(String aString)
479 {
480 char[] carrotDelimiter = "^".ToCharArray();
481 String[] parts = aString.Split(carrotDelimiter);
482 return (parts.Length > 2) ? parts[1] : "";
483 }
484
485 public DataTable TableFromRPC(String rpcCommand, String rpcParameter)
486 {
487 return this.TableFromRPC(rpcCommand, rpcParameter, this.AppContext);
488 }
489
490 public DataTable TableFromRPC(string rpcCommand, string rpcParameter, string aContext)
491 {
492 return this.TableFromRPC(rpcCommand, rpcParameter, new DataSet(), this.DefaultTableName, aContext);
493
494 }
495
496 public DataTable TableFromRPC(string rpcCommand, string rpcParameter, DataSet aDataSet, string aTableName)
497 {
498 return this.TableFromRPC(rpcCommand, rpcParameter, aDataSet, aTableName, this.AppContext);
499 }
500
501 public DataTable TableFromRPC(String rpcCommand, String rpcParameter, DataSet aDataSet, String aTableName, String aContext)
502 {
503 String generatedCommand = this.TransmitRPC(rpcCommand, rpcParameter, aContext);
504
505 return this.IsBmxAdoCommand(generatedCommand) ? this.TableFromCommand(generatedCommand, aDataSet, aTableName, aContext) : null;
506 }
507
508 public String RpcResult
509 {
510 get { return this._rpcResult; }
511 set { this._rpcResult = value; }
512 }
513
514 public virtual DataTable TableFromSQL(String sql)
515 {
516 return this.TableFromSQL(sql, this.AppContext);
517 }
518
519 public DataTable TableFromSQL(string sql, string aContext)
520 {
521 return this.TableFromSQL(sql, new DataSet(), this.DefaultTableName,aContext);
522 }
523
524 //smh: THIS IS WRONG. If you pass the data set, why make a new dataset...
525 public DataTable TableFromSQL(string sql, DataSet aDataSet, string aTableName)
526 {
527 return this.TableFromSQL(sql, aDataSet, aTableName, this.AppContext);
528 }
529
530 public DataTable TableFromSQL(string sql, DataSet aDataSet, string aTableName, String aContext)
531 {
532 this.Log.Log("BMX NET", "Debug", "TableFromSQL>> " + sql, aTableName == null ? "Null Table" : aTableName, aContext == null ? "Null Context" : aContext);
533 this.DebugLastTableResult = null;
534
535 this.AssertConnected();
536
537 BMXNetConnection bmxConnection;
538 BMXNetCommand bmxCommand;
539 BMXNetDataAdapter bmxDataAdaptor;
540
541 bmxConnection = new BMXNetConnection(this.SessionConnection);
542 bmxConnection.Open();
543 if (bmxConnection.State != ConnectionState.Open)
544 {
545 throw new BMXNetException("Unable to connect to RPMS.");
546 }
547
548 lock (this.RpcLock)
549 {
550 try
551 {
552 this.AppContext = aContext;
553
554 bmxCommand = bmxConnection.CreateCommand() as BMXNetCommand;
555 bmxCommand.CommandText = sql;
556 bmxDataAdaptor = new BMXNetDataAdapter();
557 bmxDataAdaptor = this.m_cachedDataAdapter;
558 bmxDataAdaptor.SelectCommand = bmxCommand;
559
560 DataTable answer = null;
561 String validTableName = aTableName == null ? this.DefaultTableName : aTableName;
562 DateTime stopWatch = DateTime.Now;
563
564 if (aDataSet == null)
565 {
566 answer = new DataTable(validTableName);
567 bmxDataAdaptor.Fill(answer);
568 }
569 else
570 {
571 DataTable dataTable = aDataSet.Tables.Contains(validTableName) ? aDataSet.Tables[validTableName] : aDataSet.Tables.Add(validTableName);
572 bmxDataAdaptor.Fill(aDataSet, validTableName);
573 answer = aDataSet.Tables[validTableName];
574 }
575
576
577 answer.ExtendedProperties["BMXNetSelectStatementForUpdate"] = bmxCommand.CommandText;
578 answer.ExtendedProperties["SqlQuery"] = sql;
579 answer.ExtendedProperties["BMXQueryStart"] = stopWatch;
580 answer.ExtendedProperties["BMXQueryTime"] = (DateTime.Now - stopWatch).TotalMilliseconds;
581
582 this.DebugLastTableResult = answer;
583
584 return answer;
585 }
586
587 catch (Exception exception)
588 {
589 throw new Exception("Unable to retrive sql data from RPMS.", exception);
590 }
591 finally
592 {
593 if (bmxConnection != null)
594 bmxConnection.ToString();
595 }
596 }
597 }
598
599 public bool IsBmxAdoCommand(String aString)
600 {
601 String validFetchTransactionPrefix = "BMX ADO SS";
602
603 return aString.StartsWith(validFetchTransactionPrefix);
604 }
605
606 private BMXRemoteSessionPool _sessionPool = null;
607
608 internal BMXRemoteSessionPool SessionPool
609 {
610 get { return _sessionPool; }
611 set { _sessionPool = value; }
612 }
613
614 private bool _hasSubscribedForAsyncEvents = false;
615
616 internal bool HasSubscribedForAsyncEvents
617 {
618 get { return _hasSubscribedForAsyncEvents; }
619 set { _hasSubscribedForAsyncEvents = value; }
620 }
621
622 public virtual void Close()
623 {
624 if (this.HasSubscribedForAsyncEvents)
625 {
626 this.EventServices.Unsubscribe(this.AsyncFinishedEventName);
627 }
628 this.StopPollingTimer();
629 this.SessionPool.CloseSession(this);
630 }
631
632 public string HostAddress
633 {
634 get { throw new NotImplementedException(); }
635 }
636
637 public string AuthenicatedDuz
638 {
639 get { return this.SessionConnection.DUZ; }
640 }
641
642
643 public string AuthenicatedUserName
644 {
645 get { return this.SessionConnection.UserName; }
646 }
647
648 public string AppContext
649 {
650 get
651 {
652 return this.SessionConnection.AppContext;
653 }
654 set
655 {
656 this.SessionConnection.AppContext = value;
657 }
658 }
659
660 public string TransmitRPC(string rpcCommand, string rpcParameter)
661 {
662 return this.TransmitRPC(rpcCommand, rpcParameter, this.AppContext);
663 }
664
665 public string SafelyTransmitRPC(string rpcCommand, string rpcParameter)
666 {
667 return this.SafelyTransmitRPC(rpcCommand, rpcParameter, this.AppContext);
668 }
669
670 public System.Data.DataTable TableFromRPC(string rpcCommand, string rpcParameter, System.Data.DataSet aDataSet)
671 {
672 return this.TableFromRPC(rpcCommand, rpcParameter, aDataSet);
673 }
674
675 public int Subscribe(string anEventName)
676 {
677 int result=this.EventManagmentCall("BMX EVENT REGISTER^" + anEventName);
678 if (result == 0)
679 {
680 this.Log.Log("BMX NET", "Events", "Subscribed to " + anEventName);
681 }
682 else
683 {
684 this.Log.Log("BMX NET", "Events", "Unable to subscribe to " + anEventName);
685 }
686 return result;
687 }
688
689 public int Unsubscribe(string anEventName)
690 {
691 int result=this.EventManagmentCall("BMX EVENT UNREGISTER^" + anEventName);
692 if (result == 0)
693 {
694 this.Log.Log("BMX NET", "Events", "Unsubscribed from " + anEventName);
695 }
696 else
697 {
698 this.Log.Log("BMX NET", "Events", "Unable to unsubscribe from " + anEventName);
699 }
700 return result;
701 }
702
703 public int TriggerEvent(string anEventName, string aParameter, bool doRaiseBack)
704 {
705 this.Log.Log("BMX NET", "Remote Events", "Triggered " + anEventName);
706 return this.EventManagmentCall("BMX EVENT RAISE^" + anEventName + "^" + aParameter + "^" + doRaiseBack.ToString().ToUpper() + "^");
707 }
708
709 protected int EventManagmentCall(String aCommand)
710 {
711 try
712 {
713 DataTable table = this.TableFromCommand(aCommand);
714 return (int)table.Rows[0]["ERRORID"];
715 }
716 catch
717 {
718 return 99;
719 }
720 }
721
722 delegate void UiCallPollForEvent();
723
724 protected void PollForEvent()
725 {
726 if (this.InvokedControl != null && this.InvokedControl.InvokeRequired)
727 {
728 this.InvokedControl.Invoke(new UiCallPollForEvent(SafePollForEvent));
729 }
730 else
731 {
732 this.SafePollForEvent();
733 }
734 }
735
736 protected void SafePollForEvent()
737 {
738 this.Log.Log("BMX NET", "Events", "Polling for event (" + this.PollingTimer.Interval.ToString() + ")");
739
740 this.PollForTimerEvent();
741
742 if (this.RpmsEvent != null || this.AsyncFutureLookup.Count > 0)
743 {
744 if (Monitor.TryEnter(this.RpcLock))
745 {
746 Monitor.Exit(this.RpcLock);
747 }
748 else
749 {
750 return;
751 }
752 DataTable events = this.TableFromCommand("BMX EVENT POLL");
753
754 foreach (DataRow row in events.Rows)
755 {
756 try
757 {
758 RemoteEventArgs args = new RemoteEventArgs();
759 args.EventType = row["EVENT"].ToString();
760 args.Details = row["PARAM"].ToString();
761 this.Log.Log("BMX NET", "Remote Events", "Raised: " + args.EventType + "(" + args.Details + ")");
762 if (args.EventType.Equals(this.AsyncFinishedEventName))
763 {
764 this.AsyncTableRequestReturned(args);
765 }
766 else
767 {
768 this.TriggerRpmsEvent(args);
769 }
770 }
771 catch
772 {
773 }
774 }
775 }
776 }
777
778
779 delegate void UiCallTriggerRpmsEvent(RemoteEventArgs args);
780
781 protected void TriggerRpmsEvent(RemoteEventArgs args)
782 {
783 if (this.RpmsEvent != null)
784 {
785 if (this.InvokedControl != null && this.InvokedControl.InvokeRequired)
786 {
787 this.InvokedControl.Invoke(new UiCallTriggerRpmsEvent(SafeTriggerRpmsEvent), args);
788 }
789 else
790 {
791 this.SafeTriggerRpmsEvent(args);
792 }
793 }
794 }
795
796 private void SafeTriggerRpmsEvent(RemoteEventArgs args)
797 {
798 this.RpmsEvent.Invoke(this, args);
799 }
800
801 private void AsyncTableRequestReturned(RemoteEventArgs args)
802 {
803 int key = 0;
804 if (int.TryParse(M.Piece(args.Details,"~",1), out key) && this.AsyncFutureLookup.ContainsKey(key))
805 {
806 DataTableFuture future = this.AsyncFutureLookup[key];
807 this.RemoveFromFutures(future);
808 future.FutureHasReturned(this, M.Piece(args.Details, "~", 2));
809 }
810 }
811
812 private double _timerEventPollingMultiple = 1;
813
814 public double TimerEventPollingMultiple
815 {
816 get { return _timerEventPollingMultiple; }
817 set { _timerEventPollingMultiple = value; }
818 }
819
820 public DataTable VersionInfo
821 {
822 get { return this.TableFromCommand("BMX VERSION INFO^EBCI"); }//"+this.Broker.NameSpace); }
823 }
824
825 internal void Resume(BMXRemoteSessionPool bMXRemoteSessionPool)
826 {
827 this._isConnected = true;
828 }
829
830 internal void Suspend(BMXRemoteSessionPool bMXRemoteSessionPool)
831 {
832 this._isConnected = false;
833 if (this.EventSourcePrimarySession != null)
834 {
835 this.EventSourcePrimarySession.Unsubscribe(this.AsyncFinishedEventName);
836 this.EventSourcePrimarySession.RpmsEvent -= new EventHandler<RemoteEventArgs>(aSession_RpmsEvent);
837 }
838 this.IsEventPollingEnabled = false;
839 }
840
841
842
843 #region RemoteSession Members
844
845 private String _defaultTableName = "BmxResultTable";
846
847 public String DefaultTableName
848 {
849 get { return _defaultTableName; }
850 set { _defaultTableName = value; }
851 }
852
853
854 public DataTableFuture AsyncTableFromCommand(string aCommand)
855 {
856 return this.AsyncTableFromCommand(aCommand, this.AppContext);
857 }
858
859 public DataTableFuture AsyncTableFromCommand(string aCommand, string aContext)
860 {
861 return this.AsyncTableFromCommand(aCommand, new DataSet(), this.DefaultTableName, aContext);
862 }
863
864 public DataTableFuture AsyncTableFromCommand(string aCommand, DataSet aDataSet, string aTableName)
865 {
866 return this.AsyncTableFromCommand(aCommand, aDataSet, aTableName, this.AppContext);
867 }
868
869 private String _asyncFinishedEventName=null;
870
871 public String AsyncFinishedEventName
872 {
873 get
874 {
875 if (_asyncFinishedEventName == null)
876 {
877 _asyncFinishedEventName = "BMX ASYNC FINISHED " + this.Id.ToString();
878 }
879 return _asyncFinishedEventName;
880 }
881
882 set { _asyncFinishedEventName = value; }
883 }
884
885 private Dictionary<int, DataTableFuture> _asyncFutureLookup = new Dictionary<int, DataTableFuture>();
886
887 protected Dictionary<int, DataTableFuture> AsyncFutureLookup
888 {
889 get { return _asyncFutureLookup; }
890 set { _asyncFutureLookup = value; }
891 }
892
893 public DataTableFuture AsyncTableFromCommand(string aCommand, DataSet aDataSet, string aTableName, string aContext)
894 {
895 int errorCode = -1;
896
897 try
898 {
899 if (!this.HasSubscribedForAsyncEvents)
900 {
901 this.HasSubscribedForAsyncEvents = 0 == this.EventServices.Subscribe(this.AsyncFinishedEventName);
902 }
903
904 //replace ^'s in CommandString with $c(30)'s
905 DataTable infoTable = this.TableFromCommand("BMX ASYNC QUEUE^"+aCommand.Replace('^', (char)30) + "^" + this.AsyncFinishedEventName,aContext);
906
907 if (infoTable.Rows.Count == 1)
908 {
909 DataRow queueingInfo = infoTable.Rows[0];
910 errorCode = int.Parse(queueingInfo["ERRORID"].ToString());
911 if (errorCode == 1)
912 {
913 int resultId = int.Parse(queueingInfo["PARAM"].ToString());
914
915 DataTableFuture answer = new DataTableFuture();
916 answer.InvokedControl = this.InvokedControl;
917 answer.ResultDataSet = aDataSet;
918 answer.ResultTableName = aTableName;
919 answer.ResultAppContext = aContext;
920 answer.ResultId = resultId;
921 this.AddToFutures(answer);
922 return answer;
923 }
924 else
925 {
926 throw new BMXNetException("Async call failed: " + queueingInfo["PARAM"].ToString());
927 }
928
929 }
930 else
931 {
932 throw new BMXNetException("Error code not available.");
933 }
934
935 }
936 catch (Exception problem)
937 {
938 String codeString = (errorCode == -1) ? "" : "Error code " + errorCode.ToString();
939 throw new BMXNetException("BMX Unable to places async request on queue. " + codeString,problem);
940 }
941 }
942
943 private void AddToFutures(DataTableFuture aFuture)
944 {
945 if (this.AsyncFutureLookup.ContainsKey(aFuture.ResultId))
946 {
947 DataTableFuture toCancel = this.AsyncFutureLookup[aFuture.ResultId];
948 toCancel.Cancel();
949 }
950
951 lock (this.AsyncFutureLookup)
952 {
953 aFuture.Session = this;
954 this.AsyncFutureLookup[aFuture.ResultId] = aFuture;
955 }
956
957 }
958
959 internal void RemoveFromFutures(DataTableFuture aFuture)
960 {
961 if (this.AsyncFutureLookup.ContainsKey(aFuture.ResultId))
962 {
963 lock (this.AsyncFutureLookup)
964 {
965 this.AsyncFutureLookup.Remove(aFuture.ResultId);
966 }
967 }
968 }
969
970
971 public DataTableFuture AsyncTableFromSQL(string sql)
972 {
973 return this.AsyncTableFromSQL(sql, this.AppContext);
974 }
975
976 public DataTableFuture AsyncTableFromSQL(string sql, string aContext)
977 {
978 return this.AsyncTableFromSQL(sql, new DataSet(),this.DefaultTableName, this.AppContext);
979 }
980
981 public DataTableFuture AsyncTableFromSQL(string sql, DataSet aDataSet, string aTableName)
982 {
983 return this.AsyncTableFromSQL(sql, aDataSet, aTableName, this.AppContext);
984 }
985
986
987 public DataTableFuture AsyncTableFromSQL(string sql, DataSet aDataSet, string aTableName, string aContext)
988 {
989 int errorCode = -1;
990
991 try
992 {
993 if (!this.HasSubscribedForAsyncEvents)
994 {
995 this.HasSubscribedForAsyncEvents = 0== this.EventServices.Subscribe(this.AsyncFinishedEventName);
996 }
997
998 //replace ^'s in CommandString with $c(30)'s
999 DataTable infoTable = this.TableFromCommand("BMX ASYNC QUEUE^" + sql.Replace('^', (char)30) + "^" + this.AsyncFinishedEventName, aContext);
1000
1001 DataRow queueingInfo = infoTable.Rows[0];
1002 errorCode = int.Parse(queueingInfo["ERRORID"].ToString());
1003 if (errorCode == 1)
1004 {
1005 int resultId = int.Parse(queueingInfo["PARAM"].ToString());
1006
1007 DataTableFuture answer = new DataTableFuture();
1008 answer.ResultDataSet = aDataSet;
1009 answer.ResultTableName = aTableName;
1010 answer.ResultAppContext = aContext;
1011 answer.ResultId = resultId;
1012 this.AddToFutures(answer);
1013 return answer;
1014 }
1015 else
1016 {
1017 throw new BMXNetException("Unexcepted error code");
1018 }
1019
1020
1021 }
1022 catch (Exception problem)
1023 {
1024 String codeString = (errorCode == -1) ? "" : "Error code " + errorCode.ToString();
1025 throw new BMXNetException("BMX Unable to places async request on queue. " + codeString, problem);
1026 }
1027
1028 }
1029
1030
1031 public DataTableFuture AsyncTableFromRPC(string rpcCommand, string rpcParameter)
1032 {
1033 return this.AsyncTableFromRPC(rpcCommand, rpcParameter, this.AppContext);
1034 }
1035
1036
1037 public DataTableFuture AsyncTableFromRPC(string rpcCommand, string rpcParameter, string aContext)
1038 {
1039 return this.AsyncTableFromRPC(rpcParameter, rpcParameter, new DataSet(), this.DefaultTableName, aContext);
1040 }
1041
1042
1043 public DataTableFuture AsyncTableFromRPC(string rpcCommand, string rpcParameter, DataSet aDataSet, string aTableName)
1044 {
1045 return this.AsyncTableFromRPC(rpcCommand, rpcParameter, aDataSet, aTableName, this.AppContext);
1046 }
1047
1048 public DataTableFuture AsyncTableFromRPC(string rpcCommand, string rpcParameter, DataSet aDataSet, string aTableName, string aContext)
1049 {
1050 String generatedCommand = this.TransmitRPC(rpcCommand, rpcParameter, aContext);
1051
1052 return this.IsBmxAdoCommand(generatedCommand) ? this.AsyncTableFromCommand(generatedCommand, aDataSet, aTableName, aContext) : null;
1053
1054 }
1055
1056 #endregion
1057
1058 private BMXNetRemoteSession _eventSourcePrimarySession = null;
1059
1060 public BMXNetRemoteSession EventSourcePrimarySession
1061 {
1062 get { return _eventSourcePrimarySession; }
1063 set { _eventSourcePrimarySession = value; }
1064 }
1065
1066
1067 internal void PrimarySession(BMXNetRemoteSession aSession)
1068 {
1069 this.EventSourcePrimarySession = aSession;
1070 // this.EventSourcePrimarySession.RpmsEvent += new EventHandler<RemoteEventArgs>(aSession_RpmsEvent);
1071 // this.EventSourcePrimarySession.Subscribe(this.AsyncFinishedEventName);
1072 }
1073
1074 void aSession_RpmsEvent(object sender, RemoteEventArgs e)
1075 {
1076 this.Log.Log("BMX NET", "Remote Events", "Raised: " + e.EventType + "(" + e.Details + ")");
1077 if (e.EventType.Equals(this.AsyncFinishedEventName))
1078 {
1079 this.AsyncTableRequestReturned(e);
1080 }
1081 else if (this.RpmsEvent != null)
1082 {
1083 this.RpmsEvent(this, e);
1084 }
1085 }
1086
1087 public bool Lock(string lvnToLock, string addOrSubstract)
1088 {
1089 return SessionConnection.Lock(lvnToLock, addOrSubstract);
1090 }
1091 }
1092}
Note: See TracBrowser for help on using the repository browser.