1: using Dart.PowerTCP.Telnet;
2: using System;
3: using System.Collections.Generic;
4: using System.Diagnostics;
5:
6: namespace Ia.Ngn.Cl.Model.Client.Ericsson
7: {
8: ////////////////////////////////////////////////////////////////////////////
9:
10: /// <summary publish="true">
11: /// Ericsson's AXE support class for Ericsson's PSTN Exchange Migration to Optical Fiber Network (OFN) client model.
12: /// </summary>
13: ///
14: /// <remarks>
15: /// Copyright © 2018-2020 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
16: ///
17: /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
18: /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
19: ///
20: /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21: /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
22: ///
23: /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
24: ///
25: /// Copyright notice: This notice may not be removed or altered from any source distribution.
26: /// </remarks>
27: public class Axe
28: {
29: private byte[] buffer = new byte[1024];
30: internal Telnet telnet;
31:
32: /// <summary/>
33: public Ia.Ngn.Cl.Model.Business.NetworkDesignDocument.Pstn Pstn { get; private set; }
34:
35: /// <summary/>
36: public string LastSentCommand { get; private set; }
37:
38: /// <summary/>
39: public Queue<string> ReceiveQueue { get; set; }
40:
41: /// <summary/>
42: public Queue<string> SendQueue { get; set; }
43:
44: ////////////////////////////////////////////////////////////////////////////
45:
46: /// <summary>
47: ///
48: /// </summary>
49: public enum ResultCode
50: {
51: Successful = 0,
52: NotAcceptedUnreasonableValue = 1,
53: NotAcceptedCommandUnknown = 2,
54: SubscriberNotConnected = 41,
55: UnknownError = 100,
56: Failed = 101,
57: TimeOut = 102,
58: }
59:
60: ////////////////////////////////////////////////////////////////////////////
61:
62: /// <summary>
63: ///
64: /// </summary>
65: public Axe(Ia.Ngn.Cl.Model.Business.NetworkDesignDocument.Pstn pstn)
66: {
67: Pstn = pstn;
68:
69: telnet = new Dart.PowerTCP.Telnet.Telnet();
70:
71: telnet.ClientOptions.Add(new Dart.PowerTCP.Telnet.Option(Dart.PowerTCP.Telnet.OptionCode.SuppressGoAheads, null, Dart.PowerTCP.Telnet.OptionState.RequestOn));
72: telnet.ClientOptions.Add(new Dart.PowerTCP.Telnet.Option(Dart.PowerTCP.Telnet.OptionCode.SuppressLocalEcho, null, Dart.PowerTCP.Telnet.OptionState.RequestOn));
73:
74: telnet.ServerOptions.Add(new Dart.PowerTCP.Telnet.Option(Dart.PowerTCP.Telnet.OptionCode.SuppressGoAheads, null, Dart.PowerTCP.Telnet.OptionState.RequestOn));
75: telnet.ServerOptions.Add(new Dart.PowerTCP.Telnet.Option(Dart.PowerTCP.Telnet.OptionCode.SuppressLocalEcho, null, Dart.PowerTCP.Telnet.OptionState.RequestOn));
76: telnet.ServerOptions.Add(new Dart.PowerTCP.Telnet.Option(Dart.PowerTCP.Telnet.OptionCode.OutputPageSize, null, Dart.PowerTCP.Telnet.OptionState.RequestOn));
77:
78: telnet.TerminalType = "tty";
79:
80: telnet.EndReceive += new Dart.PowerTCP.Telnet.SegmentEventHandler(this.Telnet_EndReceive);
81: telnet.ConnectedChangedEx += new Dart.PowerTCP.Telnet.EventHandlerEx(this.Telnet_ConnectedChangedEx);
82:
83: ReceiveQueue = new Queue<string>(100);
84: SendQueue = new Queue<string>(100);
85:
86: IsLoggedIn = false;
87: }
88:
89: ////////////////////////////////////////////////////////////////////////////
90:
91: /// <summary>
92: ///
93: /// </summary>
94: ~Axe()
95: {
96: Dispose();
97: }
98:
99: ////////////////////////////////////////////////////////////////////////////
100:
101: /// <summary>
102: ///
103: /// </summary>
104: public Ia.Cl.Model.Result Connect()
105: {
106: Ia.Cl.Model.Result result;
107:
108: result = new Ia.Cl.Model.Result();
109:
110: try
111: {
112: telnet.Connect(Ia.Ngn.Cl.Model.Business.Ericsson.Axe.Host(Pstn), Ia.Ngn.Cl.Model.Business.Ericsson.Axe.Port.ToString());
113:
114: SendQueue.Clear();
115:
116: // receive using asynchronous technique
117: telnet.ReceiveTimeout = 0;
118: telnet.BeginReceive(buffer);
119:
120: result.AddSuccess(Pstn.Name + ": Connected. ");
121: }
122: catch (Exception ex)
123: {
124: result.AddError(Pstn.Name + ": Exception: " + ex.Message);
125:
126: telnet.Close();
127: }
128:
129: return result;
130: }
131:
132: ////////////////////////////////////////////////////////////////////////////
133:
134: /// <summary>
135: ///
136: /// </summary>
137: public Ia.Cl.Model.Result Disconnect()
138: {
139: Ia.Cl.Model.Result result;
140:
141: result = new Ia.Cl.Model.Result();
142:
143: try
144: {
145: telnet.Close();
146:
147: SendQueue.Clear();
148:
149: result.AddSuccess(Pstn.Name + ": Disconnected. ");
150: }
151: catch (Exception ex)
152: {
153: result.AddError(Pstn.Name + ": Exception: " + ex.Message);
154: }
155:
156: return result;
157: }
158:
159: ////////////////////////////////////////////////////////////////////////////
160:
161: /// <summary>
162: ///
163: /// </summary>
164: public bool IsConnected
165: {
166: get
167: {
168: return telnet != null && telnet.Connected;
169: }
170: }
171:
172: ////////////////////////////////////////////////////////////////////////////
173:
174: /// <summary>
175: ///
176: /// </summary>
177: public bool IsLoggedIn { get; set; }
178:
179: ////////////////////////////////////////////////////////////////////////////
180:
181: /// <summary>
182: ///
183: /// </summary>
184: public void Dispose()
185: {
186: telnet.Dispose();
187: }
188:
189: ////////////////////////////////////////////////////////////////////////////
190:
191: /// <summary>
192: ///
193: /// </summary>
194: public void SendQueueUserName()
195: {
196: SendQueue.Enqueue(Ia.Ngn.Cl.Model.Business.Ericsson.Axe.UserName(Pstn));
197: }
198:
199: ////////////////////////////////////////////////////////////////////////////
200:
201: /// <summary>
202: ///
203: /// </summary>
204: public void SendQueuePassword()
205: {
206: SendQueue.Enqueue(Ia.Ngn.Cl.Model.Business.Ericsson.Axe.Password(Pstn));
207: }
208:
209: ////////////////////////////////////////////////////////////////////////////
210:
211: /// <summary>
212: ///
213: /// </summary>
214: public void SendQueueLogoutCommand()
215: {
216: SendQueue.Clear();
217:
218: SendQueue.Enqueue(Ia.Ngn.Cl.Model.Business.Ericsson.Axe.LogoutCommand);
219: }
220:
221: ////////////////////////////////////////////////////////////////////////////
222:
223: /// <summary>
224: ///
225: /// </summary>
226: public Ia.Cl.Model.Result Send(string text)
227: {
228: Ia.Cl.Model.Result result;
229:
230: result = new Ia.Cl.Model.Result();
231:
232: try
233: {
234: if (telnet.Connected)
235: {
236: if (text != null)
237: {
238: LastSentCommand = text;
239:
240: telnet.Send(text + "\r\n");
241:
242: result.AddSuccess(Pstn.Name + ": Sent: " + text);
243: }
244: else
245: {
246: result.AddError(Pstn.Name + ": No text to send. ");
247: }
248: }
249: else
250: {
251: result.AddError(Pstn.Name + ": No established telnet connection. ");
252: //if(processRunning) waitToConnectionCounter = waitToConnectionCounterSeconds;
253: }
254: }
255: catch (System.Reflection.TargetInvocationException ex)
256: {
257: result.AddError(ex.Message);
258: }
259:
260: return result;
261: }
262:
263: ////////////////////////////////////////////////////////////////////////////
264:
265: /// <summary>
266: ///
267: /// </summary>
268: private void Telnet_EndReceive(object sender, Dart.PowerTCP.Telnet.SegmentEventArgs e)
269: {
270: string rowString, filteredString;
271:
272: if (e.Exception == null)
273: {
274: rowString = e.Segment.ToString();
275:
276: filteredString = rowString.Replace("\u0003", "").Replace("<", "").Trim();
277:
278: if (!string.IsNullOrEmpty(filteredString) && !string.IsNullOrWhiteSpace(filteredString))
279: {
280: if (filteredString != LastSentCommand) // to ignore echoed command
281: {
282: ReceiveQueue.Enqueue(filteredString);
283: }
284: }
285: else
286: {
287:
288: }
289:
290: if (telnet.Connected) telnet.BeginReceive(buffer);
291: }
292: else
293: {
294: Debug.WriteLine("Telnet_EndReceive(): Exception: " + e.Exception.Message);
295:
296: // I will not throw or handle the exception from the event handler
297: // https://stackoverflow.com/questions/3114543/should-event-handlers-in-c-sharp-ever-raise-exceptions
298: //throw new Exception("Telnet_EndReceive(): " + e.Exception.Message);
299: }
300: }
301:
302: ////////////////////////////////////////////////////////////////////////////
303:
304: /// <summary>
305: ///
306: /// </summary>
307: private void Telnet_ConnectedChangedEx(object sender, System.EventArgs e)
308: {
309:
310: }
311:
312: ////////////////////////////////////////////////////////////////////////////
313:
314: /// <summary>
315: ///
316: /// </summary>
317: public Ia.Cl.Model.Result Update(string formattedString, ref Ia.Ngn.Cl.Model.Client.Ericsson.Axe axe)
318: {
319: Ia.Cl.Model.Result result;
320:
321: Ia.Ngn.Cl.Model.Data.Ericsson.Axe.UpdateDatabaseWithAxeCommandOutput(formattedString, ref axe, out result);
322:
323: return result;
324: }
325:
326: ////////////////////////////////////////////////////////////////////////////
327: ////////////////////////////////////////////////////////////////////////////
328: }
329:
330: ////////////////////////////////////////////////////////////////////////////
331: ////////////////////////////////////////////////////////////////////////////
332: }