)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » SoftX (Ia.Ftn.Cl.Models.Client.Huawei)

Public general use code classes and xml files that we've compiled and used over the years:

U2020 Northbound Interface IP (SoftX) support class for Huawei's Fixed Telecommunications Network (FTN) client model.

    1: using Dart.Telnet;
    2: using System;
    3: using System.Collections.Generic;
    4: using System.Diagnostics;
    5: using System.Security.Cryptography.X509Certificates;
    6: using System.Text.RegularExpressions;
    7:  
    8: namespace Ia.Ftn.Cl.Models.Client.Huawei
    9: {
   10:     ////////////////////////////////////////////////////////////////////////////
   11:  
   12:     /// <summary publish="true">
   13:     /// U2020 Northbound Interface IP (SoftX) support class for Huawei's Fixed Telecommunications Network (FTN) client model.
   14:     /// </summary>
   15:     /// 
   16:     /// <remarks> 
   17:     /// Copyright © 2018-2025 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   18:     ///
   19:     /// 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
   20:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   21:     ///
   22:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   23:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   24:     /// 
   25:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   26:     /// 
   27:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   28:     /// </remarks> 
   29:     public class SoftX
   30:     {
   31:         private string receiveString;
   32:  
   33:         private byte[] buffer = new byte[1024];
   34:         private TelnetModel telnetModel;
   35:         private Dart.Telnet.Telnet telnet;
   36:         private X509CertificateCollection clientCertificates = new X509CertificateCollection();
   37:         private System.ComponentModel.IContainer components;
   38:  
   39:         /// <summary/>
   40:         public static int WaitAfterSendInMillisecond { get { return 4000; } }
   41:  
   42:         /// <summary/>
   43:         public static int WaitAfterSendForCfgCommandInMillisecond { get { return 10000; } }
   44:  
   45:         /// <summary/>
   46:         public string LastSentCommand { get; private set; }
   47:  
   48:         /// <summary/>
   49:         public Queue<string> ReceiveQueue { get; set; }
   50:  
   51:         /// <summary/>
   52:         public Queue<string> SendQueue { get; set; }
   53:  
   54:         ////////////////////////////////////////////////////////////////////////////
   55:  
   56:         /// <summary>
   57:         ///
   58:         /// </summary>
   59:         public enum ResultCode
   60:         {
   61:             /// <summary/>
   62:             OperationSucceeded = 0,
   63:  
   64:             /// <summary/>
   65:             InvalidCommand = 999,
   66:  
   67:             /// <summary/>
   68:             InsufficientAuthority = 1515,
   69:  
   70:             /// <summary/>
   71:             InformationForThisMgwAlreadyExists = 63029,
   72:  
   73:             /// <summary/>
   74:             TheUserDoesNotExist = 96013
   75:         }
   76:  
   77:         ////////////////////////////////////////////////////////////////////////////
   78:  
   79:         /// <summary>
   80:         ///
   81:         /// </summary>
   82:         public SoftX()
   83:         {
   84:             components = new System.ComponentModel.Container();
   85:  
   86:             Dart.Telnet.Option option1 = new Dart.Telnet.Option();
   87:             Dart.Telnet.Option option2 = new Dart.Telnet.Option();
   88:             Dart.Telnet.Option option3 = new Dart.Telnet.Option();
   89:             Dart.Telnet.Option option4 = new Dart.Telnet.Option();
   90:             Dart.Telnet.Option option5 = new Dart.Telnet.Option();
   91:  
   92:             telnet = new Dart.Telnet.Telnet(components);
   93:  
   94:             option1.Code = Dart.Telnet.OptionCode.SuppressGoAheads;
   95:             option2.Code = Dart.Telnet.OptionCode.WindowSize;
   96:             option2.SubOption = new byte[] { 0, 80, 0, 24 };
   97:             option3.Code = Dart.Telnet.OptionCode.TerminalType;
   98:             option3.SubOption = new byte[] { 0, 116, 116, 121 };
   99:             telnet.ClientOptions.AddRange(new Dart.Telnet.Option[] { option1, option2, option3 });
  100:  
  101:             option4.Code = Dart.Telnet.OptionCode.SuppressGoAheads;
  102:             option5.Code = Dart.Telnet.OptionCode.Echo;
  103:             telnet.ServerOptions.AddRange(new Dart.Telnet.Option[] { option4, option5 });
  104:  
  105:             telnet.SocketOption.ReceiveTimeout = 1000;
  106:             telnet.SynchronizingObject = null;
  107:             telnet.ClientOptionChanged += Telnet_ClientOptionChanged;
  108:             telnet.ServerOptionChanged += Telnet_ServerOptionChanged;
  109:             telnet.Data += Telnet_Data;
  110:             telnet.StateChanged += Telnet_StateChanged;
  111:             telnet.Log += Telnet_Log;
  112:             telnet.Error += Telnet_Error;
  113:  
  114:             telnetModel = new TelnetModel();
  115:  
  116:             // set non-serializable Telnet component
  117:             telnetModel.Telnet = telnet;
  118:             telnetModel.ReceiveLoopRequired = true;
  119:  
  120:             // subscribe to certificate events
  121:             telnetModel.CertificateRequested += new EventHandler<LocalCertificateEventArgs>(TelnetModel_CertificateRequested);
  122:             telnetModel.CertificatePresented += new EventHandler<RemoteCertificateEventArgs>(TelnetModel_CertificatePresented);
  123:             /*
  124:             telnet = new Dart.Telnet.Telnet();
  125: 
  126:             telnet.ClientOptions.Add(new Dart.Telnet.Option(Dart.Telnet.OptionCode.SuppressGoAheads, null, Dart.Telnet.OptionState.RequestOn));
  127:             //telnet.ClientOptions.Add(new Dart.Telnet.Option(Dart.Telnet.OptionCode.TerminalType, new System.Byte[] { ((System.Byte)(0)), ((System.Byte)(116)), ((System.Byte)(116)), ((System.Byte)(121)) }, Dart.Telnet.OptionState.RequestOn)); // tty
  128:             telnet.ClientOptions.Add(new Dart.Telnet.Option(Dart.Telnet.OptionCode.TerminalType, new System.Byte[] { ((System.Byte)(0)), ((System.Byte)(120)), ((System.Byte)(116)), ((System.Byte)(101)), ((System.Byte)(114)), ((System.Byte)(109)) }, Dart.Telnet.OptionState.RequestOn)); // xterm
  129: 
  130:             telnet.ServerOptions.Add(new Dart.Telnet.Option(Dart.Telnet.OptionCode.SuppressGoAheads, null, Dart.Telnet.OptionState.RequestOn));
  131:             telnet.ServerOptions.Add(new Dart.Telnet.Option(Dart.Telnet.OptionCode.Echo, null, Dart.Telnet.OptionState.RequestOn));
  132:             telnet.ServerOptions.Add(new Dart.Telnet.Option(Dart.Telnet.OptionCode.OutputPageSize, null, Dart.Telnet.OptionState.RequestOn));
  133:             */
  134:  
  135:             //telnet.TerminalType = "tty";
  136:             //telnet.TerminalType = "xterm";
  137:  
  138:             ////telnet.EndReceive += new Dart.Telnet.SegmentEventHandler(this.Telnet_EndReceive);
  139:             ////telnet.ConnectedChangedEx += new Dart.Telnet.EventHandlerEx(this.Telnet_ConnectedChangedEx);
  140:  
  141:             receiveString = string.Empty;
  142:             ReceiveQueue = new Queue<string>(100);
  143:             SendQueue = new Queue<string>(100);
  144:  
  145:             IsLoggedIn = false;
  146:         }
  147:  
  148:         ////////////////////////////////////////////////////////////////////////////
  149:  
  150:         /// <summary>
  151:         ///
  152:         /// </summary>
  153:         ~SoftX()
  154:         {
  155:             Dispose(true);
  156:         }
  157:  
  158:         ////////////////////////////////////////////////////////////////////////////
  159:  
  160:         /// <summary>
  161:         ///
  162:         /// </summary>
  163:         public Ia.Cl.Models.Result Connect()
  164:         {
  165:             var result = new Ia.Cl.Models.Result();
  166:  
  167:             // if a model with a matching server exists, remove it, then add it so that it is first in the list
  168:             telnetModel.Session.RemoteEndPoint = new IPEndPoint(Ia.Ftn.Cl.Models.Business.Huawei.SoftX.Host, Ia.Ftn.Cl.Models.Business.Huawei.SoftX.Port);
  169:             telnetModel.Credentials.Username = string.Empty; // txtUsername.Text;
  170:             telnetModel.Credentials.Password = string.Empty; // txtPassword.Text;
  171:             telnetModel.Credentials.CommandPrompt = string.Empty; // txtCommandPrompt.Text;
  172:             telnetModel.Credentials.UsernamePrompt = string.Empty; // txtLoginPrompt.Text;
  173:             telnetModel.Credentials.PasswordPrompt = string.Empty; // txtPasswordPrompt.Text;
  174:             telnetModel.SecurityType = SecurityType.None; // (SecurityType)cboSecurity.SelectedIndex;
  175:             telnetModel.SaveSession();
  176:  
  177:             if (telnetModel.Session.RemoteEndPoint.Port == 23)
  178:             {
  179:                 telnetModel.Telnet.ClientOptions.Add(new Option(OptionCode.SuppressGoAheads, null, OptionState.RequestOn));
  180:                 telnetModel.Telnet.ClientOptions.Add(new Option(OptionCode.WindowSize, new System.Byte[] { ((System.Byte)(0)), ((System.Byte)(80)), ((System.Byte)(0)), ((System.Byte)(24)) }, OptionState.RequestOn));
  181:                 telnetModel.Telnet.ClientOptions.Add(new Option(OptionCode.TerminalType, new System.Byte[] { ((System.Byte)(0)), ((System.Byte)(116)), ((System.Byte)(116)), ((System.Byte)(121)) }, OptionState.RequestOn));
  182:  
  183:                 telnetModel.Telnet.ServerOptions.Add(new Option(OptionCode.SuppressGoAheads, null, OptionState.RequestOn));
  184:                 telnetModel.Telnet.ServerOptions.Add(new Option(OptionCode.Echo, null, OptionState.RequestOn));
  185:                 telnetModel.Telnet.ServerOptions.Add(new Option(OptionCode.OutputPageSize, null, OptionState.RequestOn));
  186:             }
  187:             else
  188:             {
  189:                 telnetModel.Telnet.ClientOptions.Clear();
  190:                 telnetModel.Telnet.ServerOptions.Clear();
  191:             }
  192:  
  193:             // connect and receive data on a separate thread
  194:             telnet.Start(telnetModel.Connect, null);
  195:  
  196:             result.AddSuccess("Connected");
  197:  
  198:             return result;
  199:         }
  200:  
  201:         ////////////////////////////////////////////////////////////////////////////
  202:  
  203:         /// <summary>
  204:         ///
  205:         /// </summary>
  206:         public Ia.Cl.Models.Result Disconnect()
  207:         {
  208:             var result = new Ia.Cl.Models.Result();
  209:  
  210:             try
  211:             {
  212:                 telnetModel.Telnet.Close();
  213:  
  214:                 result.AddSuccess("Disconnected");
  215:             }
  216:             catch (Exception ex)
  217:             {
  218:                 result.AddError(ex.Message);
  219:             }
  220:  
  221:             return result;
  222:         }
  223:  
  224:         ////////////////////////////////////////////////////////////////////////////
  225:  
  226:         /// <summary>
  227:         ///
  228:         /// </summary>
  229:         public bool IsConnected
  230:         {
  231:             get
  232:             {
  233:                 return telnetModel.IsConnected;
  234:             }
  235:         }
  236:  
  237:         ////////////////////////////////////////////////////////////////////////////
  238:  
  239:         /// <summary>
  240:         ///
  241:         /// </summary>
  242:         public bool IsLoggedIn { get; set; }
  243:  
  244:         ////////////////////////////////////////////////////////////////////////////
  245:  
  246:         /// <summary>
  247:         ///
  248:         /// </summary>
  249:         protected /*override*/ void Dispose(bool disposing)
  250:         {
  251:             if (disposing)
  252:             {
  253:                 if (components != null)
  254:                 {
  255:                     components.Dispose();
  256:                 }
  257:             }
  258:  
  259:             //base.Dispose(disposing);
  260:         }
  261:  
  262:         ////////////////////////////////////////////////////////////////////////////
  263:  
  264:         /// <summary>
  265:         ///
  266:         /// </summary>
  267:         public void Login()
  268:         {
  269:             SendQueue.Enqueue("\r\n");
  270:             SendQueue.Enqueue(Ia.Ftn.Cl.Models.Business.Huawei.SoftX.LoginUser);
  271:         }
  272:  
  273:         ////////////////////////////////////////////////////////////////////////////
  274:  
  275:         /// <summary>
  276:         ///
  277:         /// </summary>
  278:         public void Logout()
  279:         {
  280:             SendQueue.Enqueue(Ia.Ftn.Cl.Models.Business.Huawei.SoftX.LogoutUser);
  281:         }
  282:  
  283:         ////////////////////////////////////////////////////////////////////////////
  284:  
  285:         /// <summary>
  286:         ///
  287:         /// </summary>
  288:         public Ia.Cl.Models.Result Send(string text)
  289:         {
  290:             var result = new Ia.Cl.Models.Result();
  291:  
  292:             try
  293:             {
  294:                 if (telnetModel.IsConnected)
  295:                 {
  296:                     if (!string.IsNullOrEmpty(text))
  297:                     {
  298:                         LastSentCommand = text;
  299:  
  300:                         // below: I have to put ';' before send or it will not run. I do not know why I have to do this
  301:                         //text = Ia.Ftn.Cl.Model.Business.Huawei.SoftX.SemiColon + text;
  302:  
  303:                         ////telnet.Send(text);
  304:                         telnetModel.WriteData(text + "\r\n"); // I added "\r\n", not sure if it will work with read server
  305:  
  306:                         ////telnet.Send("\r\n"); // important
  307:                         //telnetModel.WriteData("\r\n");
  308:  
  309:                         Debug.WriteLine("Send() sent text: [" + text + "]");
  310:  
  311:                         //result.AddSuccess("Sent: [" + text + "]");
  312:                     }
  313:                     else result.AddError("No text to send");
  314:                 }
  315:                 else
  316:                 {
  317:                     result.AddError("No established telnet connection.");
  318:                     //if(processRunning) waitToConnectionCounter = waitToConnectionCounterSeconds;
  319:                 }
  320:             }
  321:             catch (Exception ex)
  322:             {
  323:                 result.AddError(ex.Message);
  324:             }
  325:  
  326:             return result;
  327:         }
  328:  
  329:         ////////////////////////////////////////////////////////////////////////////
  330:  
  331:         /// <summary>
  332:         ///
  333:         /// </summary>
  334:         private void Telnet_Data(object sender, DataEventArgs e)
  335:         {
  336:             string message;//, entry;
  337:  
  338:             Debug.WriteLine(" ");
  339:             Debug.WriteLine("============================================");
  340:  
  341:             /*
  342:             Debug.WriteLine("Telnet_EndReceive(): e.Segment: [" + e.Segment.ToString() + "]");
  343: 
  344:             receiveString = e.Segment.ToString();
  345: 
  346:             if (!string.IsNullOrEmpty(receiveString) && !string.IsNullOrWhiteSpace(receiveString))
  347:             {
  348:                 ReceiveQueue.Enqueue(receiveString);
  349:             }
  350:             else
  351:             {
  352: 
  353:             }
  354:             */
  355:  
  356:             receiveString += e.Data.ToString();
  357:  
  358:             Debug.WriteLine("Telnet_Data(): e.Segment: [" + e.Data.ToString() + "], receiveString: [" + receiveString + "]");
  359:  
  360:             if (!string.IsNullOrEmpty(receiveString) && !string.IsNullOrWhiteSpace(receiveString))
  361:             {
  362:                 var match = Regex.Match(receiveString, @"\+\+\+(.+?)\-\-\-\s+END", RegexOptions.Singleline);
  363:  
  364:                 if (match.Success)
  365:                 {
  366:                     message = match.Value;
  367:  
  368:                     ReceiveQueue.Enqueue(message);
  369:  
  370:                     receiveString = string.Empty;
  371:  
  372:                     Debug.WriteLine("Telnet_Data(): ReceiveQueue.Enqueue(message): [" + message + "]");
  373:                 }
  374:  
  375:                 Debug.WriteLine("Telnet_Data(): receiveString: [" + receiveString + "]");
  376:             }
  377:             else
  378:             {
  379:  
  380:             }
  381:  
  382:             ////if (telnet.Connected) telnet.BeginReceive(buffer);
  383:  
  384:             Debug.WriteLine("============================================");
  385:             Debug.WriteLine(" ");
  386:         }
  387:  
  388:         ////////////////////////////////////////////////////////////////////////////
  389:  
  390:         /// <summary>
  391:         ///
  392:         /// </summary>
  393:         private void Telnet_StateChanged(object sender, EventArgs e)
  394:         {
  395:             // always raised when connection is established or closed (state property changes)
  396:             switch (telnetModel.Telnet.State)
  397:             {
  398:                 case ConnectionState.Connected:
  399:                 case ConnectionState.ConnectedAndSecure: break;
  400:                 case ConnectionState.Closed: break;
  401:             }
  402:         }
  403:  
  404:         ////////////////////////////////////////////////////////////////////////////
  405:  
  406:         /// <summary>
  407:         ///
  408:         /// </summary>
  409:         private void Telnet_ClientOptionChanged(object sender, OptionEventArgs e)
  410:         {
  411:         }
  412:  
  413:         ////////////////////////////////////////////////////////////////////////////
  414:  
  415:         /// <summary>
  416:         ///
  417:         /// </summary>
  418:         private void Telnet_ServerOptionChanged(object sender, OptionEventArgs e)
  419:         {
  420:         }
  421:  
  422:         ////////////////////////////////////////////////////////////////////////////
  423:  
  424:         /// <summary>
  425:         ///
  426:         /// </summary>
  427:         private void Telnet_Error(object sender, Dart.Telnet.ErrorEventArgs e)
  428:         {
  429:         }
  430:  
  431:         ////////////////////////////////////////////////////////////////////////////
  432:  
  433:         /// <summary>
  434:         ///
  435:         /// </summary>
  436:         private void Telnet_Log(object sender, DataEventArgs e)
  437:         {
  438:             // e.Data;
  439:         }
  440:  
  441:         ////////////////////////////////////////////////////////////////////////////
  442:  
  443:         /// <summary>
  444:         ///
  445:         /// </summary>
  446:         void TelnetModel_CertificateRequested(object sender, LocalCertificateEventArgs e)
  447:         {
  448:         }
  449:  
  450:         ////////////////////////////////////////////////////////////////////////////
  451:  
  452:         /// <summary>
  453:         ///
  454:         /// </summary>
  455:         void TelnetModel_CertificatePresented(object sender, RemoteCertificateEventArgs e)
  456:         {
  457:         }
  458:  
  459:         ////////////////////////////////////////////////////////////////////////////
  460:  
  461:         /// <summary>
  462:         ///
  463:         /// </summary>
  464:         public void Update(string rowData, ref Ia.Ftn.Cl.Models.Client.Huawei.SoftX softX, out Ia.Cl.Models.Result result)
  465:         {
  466:             Ia.Ftn.Cl.Models.Data.Huawei.SoftX.UpdateDatabaseWithSoftXCommandOutput(rowData, ref softX, out result);
  467:         }
  468:  
  469:         ////////////////////////////////////////////////////////////////////////////
  470:         ////////////////////////////////////////////////////////////////////////////
  471:     }
  472:  
  473:     ////////////////////////////////////////////////////////////////////////////
  474:     ////////////////////////////////////////////////////////////////////////////
  475: }