شركة التطبيقات المتكاملة لتصميم النظم البرمجية الخاصة

Integrated Applications Programming Company

Skip Navigation LinksHome » Code Library » Ems

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

Optical Fiber Network Management Intranet Portal (OFN) client support class for Huawei's Next Generation Network (NGN) EMS client model.

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Diagnostics;
   4:  using Dart.PowerTCP.Telnet;
   5:   
   6:  namespace Ia.Ngn.Cl.Model.Client.Huawei
   7:  {
   8:      ////////////////////////////////////////////////////////////////////////////
   9:   
  10:      /// <summary publish="true">
  11:      /// Optical Fiber Network Management Intranet Portal (OFN) client support class for Huawei's Next Generation Network (NGN) EMS client model.
  12:      /// </summary>
  13:      /// 
  14:      /// <remarks> 
  15:      /// Copyright © 2017-2018 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet 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 Ems
  28:      {
  29:          private bool isLoggedIn;
  30:   
  31:          // interactive Telnet does not require a very large buffer, so 1024 is fine
  32:          private byte[] buffer = new byte[1024];
  33:          private string receiveString;
  34:          internal Telnet telnet;
  35:   
  36:          /// <summary/>
  37:          public static int WaitAfterSendInMillisecond { get { return 4000; } }
  38:          public static int WaitAfterSendForCfgCommandInMillisecond { get { return 10000; } }
  39:   
  40:          public string LastSentCommand { get; private set; }
  41:   
  42:          public Queue<string> ReceiveQueue { get; set; }
  43:   
  44:          public Queue<string> SendQueue { get; set; }
  45:   
  46:          ////////////////////////////////////////////////////////////////////////////
  47:   
  48:          /// <summary>
  49:          ///
  50:          /// </summary>
  51:          public enum ResultCode
  52:          {
  53:          }
  54:   
  55:          ////////////////////////////////////////////////////////////////////////////
  56:   
  57:          /// <summary>
  58:          ///
  59:          /// </summary>
  60:          public Ems()
  61:          {
  62:              this.telnet = new Dart.PowerTCP.Telnet.Telnet(); // this.components);
  63:   
  64:              this.telnet.ClientOptions.AddRange(new Dart.PowerTCP.Telnet.Option[] {
  65:              new Dart.PowerTCP.Telnet.Option(Dart.PowerTCP.Telnet.OptionCode.SuppressGoAheads, null, Dart.PowerTCP.Telnet.OptionState.RequestOn),
  66:              new Dart.PowerTCP.Telnet.Option(Dart.PowerTCP.Telnet.OptionCode.WindowSize, new byte[] {
  67:                          ((byte)(0)),
  68:                          ((byte)(80)),
  69:                          ((byte)(0)),
  70:                          ((byte)(24))}, Dart.PowerTCP.Telnet.OptionState.RequestOn),
  71:              new Dart.PowerTCP.Telnet.Option(Dart.PowerTCP.Telnet.OptionCode.TerminalType, new byte[] {
  72:                          ((byte)(0)),
  73:                          ((byte)(120)),
  74:                          ((byte)(116)),
  75:                          ((byte)(101)),
  76:                          ((byte)(114)),
  77:                          ((byte)(109))}, Dart.PowerTCP.Telnet.OptionState.RequestOn)});
  78:   
  79:              this.telnet.ServerOptions.AddRange(new Dart.PowerTCP.Telnet.Option[] {
  80:              new Dart.PowerTCP.Telnet.Option(Dart.PowerTCP.Telnet.OptionCode.SuppressGoAheads, null, Dart.PowerTCP.Telnet.OptionState.RequestOn),
  81:              new Dart.PowerTCP.Telnet.Option(Dart.PowerTCP.Telnet.OptionCode.Echo, null, Dart.PowerTCP.Telnet.OptionState.RequestOn)});
  82:   
  83:              //this.telnet.SynchronizingObject = this;
  84:              this.telnet.TerminalType = "xterm";
  85:              //this.telnet.WindowSize = new System.Drawing.Size(80, 24);
  86:              this.telnet.EndReceive += new Dart.PowerTCP.Telnet.SegmentEventHandler(this.Telnet_EndReceive);
  87:              this.telnet.ConnectedChangedEx += new Dart.PowerTCP.Telnet.EventHandlerEx(this.Telnet_ConnectedChangedEx);
  88:   
  89:              receiveString = string.Empty;
  90:              ReceiveQueue = new Queue<string>(100);
  91:              SendQueue = new Queue<string>(100);
  92:   
  93:              isLoggedIn = false;
  94:          }
  95:   
  96:          ////////////////////////////////////////////////////////////////////////////
  97:   
  98:          /// <summary>
  99:          ///
 100:          /// </summary>
 101:          ~Ems()
 102:          {
 103:              Dispose();
 104:          }
 105:   
 106:          ////////////////////////////////////////////////////////////////////////////
 107:   
 108:          /// <summary>
 109:          ///
 110:          /// </summary>
 111:          public void Connect(out Ia.Cl.Model.Result result)
 112:          {
 113:              result = new Ia.Cl.Model.Result();
 114:   
 115:              try
 116:              {
 117:                  telnet.ClientOptions.Add(new Dart.PowerTCP.Telnet.Option(Dart.PowerTCP.Telnet.OptionCode.SuppressGoAheads, null, Dart.PowerTCP.Telnet.OptionState.RequestOn));
 118:                  telnet.ClientOptions.Add(new Dart.PowerTCP.Telnet.Option(Dart.PowerTCP.Telnet.OptionCode.TerminalType, new System.Byte[] { ((System.Byte)(0)), ((System.Byte)(116)), ((System.Byte)(116)), ((System.Byte)(121)) }, Dart.PowerTCP.Telnet.OptionState.RequestOn));
 119:                  telnet.ServerOptions.Add(new Dart.PowerTCP.Telnet.Option(Dart.PowerTCP.Telnet.OptionCode.SuppressGoAheads, null, Dart.PowerTCP.Telnet.OptionState.RequestOn));
 120:                  telnet.ServerOptions.Add(new Dart.PowerTCP.Telnet.Option(Dart.PowerTCP.Telnet.OptionCode.Echo, null, Dart.PowerTCP.Telnet.OptionState.RequestOn));
 121:                  telnet.ServerOptions.Add(new Dart.PowerTCP.Telnet.Option(Dart.PowerTCP.Telnet.OptionCode.OutputPageSize, null, Dart.PowerTCP.Telnet.OptionState.RequestOn));
 122:   
 123:                  telnet.Connect(Ia.Ngn.Cl.Model.Business.Huawei.Ems.Host, Ia.Ngn.Cl.Model.Business.Huawei.Ems.Port.ToString());
 124:   
 125:                  // receive using asynchronous technique
 126:                  telnet.ReceiveTimeout = 0;
 127:                  telnet.BeginReceive(buffer);
 128:   
 129:                  result.AddSuccess("Connected. ");
 130:              }
 131:              catch (Exception ex)
 132:              {
 133:                  result.AddError("Exception: " + ex.Message);
 134:   
 135:                  telnet.Close();
 136:              }
 137:          }
 138:   
 139:          ////////////////////////////////////////////////////////////////////////////
 140:   
 141:          /// <summary>
 142:          ///
 143:          /// </summary>
 144:          public void Disconnect(out Ia.Cl.Model.Result result)
 145:          {
 146:              result = new Ia.Cl.Model.Result();
 147:   
 148:              try
 149:              {
 150:                  telnet.Close();
 151:   
 152:                  result.AddSuccess("Disconnected. ");
 153:              }
 154:              catch (Exception ex)
 155:              {
 156:                  result.AddError(ex.Message);
 157:              }
 158:          }
 159:   
 160:          ////////////////////////////////////////////////////////////////////////////
 161:   
 162:          /// <summary>
 163:          ///
 164:          /// </summary>
 165:          public bool IsConnected
 166:          {
 167:              get
 168:              {
 169:                  return telnet.Connected;
 170:              }
 171:          }
 172:   
 173:          ////////////////////////////////////////////////////////////////////////////
 174:   
 175:          /// <summary>
 176:          ///
 177:          /// </summary>
 178:          public bool IsLoggedIn { get { return isLoggedIn; } set { isLoggedIn = value; } }
 179:   
 180:          ////////////////////////////////////////////////////////////////////////////
 181:   
 182:          /// <summary>
 183:          ///
 184:          /// </summary>
 185:          public void Dispose()
 186:          {
 187:              telnet.Dispose();
 188:          }
 189:   
 190:          ////////////////////////////////////////////////////////////////////////////
 191:   
 192:          /// <summary>
 193:          ///
 194:          /// </summary>
 195:          public void Login()
 196:          {
 197:              // sendQueue.Enqueue(Ia.Ngn.Cl.Model.Business.Huawei.Ems.Semicolon); did not make any different
 198:   
 199:              // below: I need this because U2000 somehow needs to be triggered before I send login information
 200:              SendQueue.Enqueue(Ia.Ngn.Cl.Model.Business.Huawei.Ems.EmsKeepAliveCommand);
 201:              SendQueue.Enqueue(Ia.Ngn.Cl.Model.Business.Huawei.Ems.EmsKeepAliveCommand);
 202:   
 203:              SendQueue.Enqueue(Ia.Ngn.Cl.Model.Business.Huawei.Ems.LoginUser);
 204:          }
 205:   
 206:          ////////////////////////////////////////////////////////////////////////////
 207:   
 208:          /// <summary>
 209:          ///
 210:          /// </summary>
 211:          public void Logout()
 212:          {
 213:              SendQueue.Enqueue(Ia.Ngn.Cl.Model.Business.Huawei.Ems.LogoutUser);
 214:          }
 215:   
 216:          ////////////////////////////////////////////////////////////////////////////
 217:   
 218:          /// <summary>
 219:          ///
 220:          /// </summary>
 221:          public void Send(string text, bool skipSleep, out Ia.Cl.Model.Result result)
 222:          {
 223:              result = new Ia.Cl.Model.Result();
 224:   
 225:              try
 226:              {
 227:                  if (telnet.Connected)
 228:                  {
 229:                      if (text != null)
 230:                      {
 231:                          LastSentCommand = text;
 232:   
 233:                          //this.textBox.AppendText("\r\n/* Sending: " + text + " */\r\n");
 234:   
 235:                          telnet.Send(text);
 236:   
 237:                          result.AddSuccess("Sent: " + text);
 238:                      }
 239:                      else
 240:                      {
 241:                          result.AddError("No text to send. ");
 242:                      }
 243:                  }
 244:                  else
 245:                  {
 246:                      result.AddError("No established telnet connection. ");
 247:                      //if(processRunning) waitToConnectionCounter = waitToConnectionCounterSeconds;
 248:                  }
 249:              }
 250:              catch (Exception ex)
 251:              {
 252:                  //if(processRunning) waitToConnectionCounter = waitToConnectionCounterSeconds;
 253:   
 254:                  result.AddError(ex.Message);
 255:              }
 256:   
 257:              if (!skipSleep)
 258:              {
 259:                  // this is meant to give the Telnet_EndReceive() time to receive and process data from session
 260:                  // we will wait according to the type of commands send
 261:   
 262:                  if (Ia.Ngn.Cl.Model.Business.Huawei.Ems.IsACfgCommand(LastSentCommand))
 263:                  {
 264:                      System.Threading.Thread.Sleep(Ia.Ngn.Cl.Model.Client.Huawei.Ems.WaitAfterSendForCfgCommandInMillisecond);
 265:                  }
 266:                  else
 267:                  {
 268:                      System.Threading.Thread.Sleep(Ia.Ngn.Cl.Model.Client.Huawei.Ems.WaitAfterSendInMillisecond);
 269:                  }
 270:              }
 271:          }
 272:   
 273:          ////////////////////////////////////////////////////////////////////////////
 274:   
 275:          /// <summary>
 276:          ///
 277:          /// </summary>
 278:          private void Telnet_EndReceive(object sender, Dart.PowerTCP.Telnet.SegmentEventArgs e)
 279:          {
 280:              string message, entry;
 281:              string[] receiveStringSplit;
 282:   
 283:              Debug.WriteLine(" ");
 284:              Debug.WriteLine("============================================");
 285:   
 286:              try
 287:              {
 288:                  if (e.Exception == null)
 289:                  {
 290:                      receiveString += e.Segment.ToString();
 291:   
 292:                      Debug.WriteLine("Telnet_EndReceive(): e.Segment: [" + e.Segment.ToString() + "], receiveString: [" + receiveString + "]");
 293:   
 294:                      if (!string.IsNullOrEmpty(receiveString))
 295:                      {
 296:                          receiveStringSplit = receiveString.Split(new string[] { "\r\n;" }, StringSplitOptions.None);
 297:   
 298:                          if (receiveStringSplit.Length > 0)
 299:                          {
 300:                              for (int i = 0; i < receiveStringSplit.Length; i++)
 301:                              {
 302:                                  entry = receiveStringSplit[i];
 303:   
 304:                                  if (!string.IsNullOrEmpty(entry) && !string.IsNullOrWhiteSpace(entry))
 305:                                  {
 306:                                      // will not enqueue an empty entry
 307:   
 308:                                      if (i == receiveStringSplit.Length - 1)
 309:                                      {
 310:                                          // if there is a non empty last entry that means its incomplete and we will assign it to receiveString
 311:   
 312:                                          receiveString = entry;
 313:                                      }
 314:                                      else
 315:                                      {
 316:                                          message = entry + "\r\n;"; // important
 317:   
 318:                                          if (!ReceiveQueue.Contains(message))
 319:                                          {
 320:                                              // will not enqueue duplicate
 321:   
 322:                                              ReceiveQueue.Enqueue(message);
 323:   
 324:                                              Debug.WriteLine("Telnet_EndReceive(): ReceiveQueue.Enqueue(message): [" + message + "]");
 325:                                          }
 326:   
 327:                                          receiveString = string.Empty;
 328:                                      }
 329:                                  }
 330:                              }
 331:                          }
 332:   
 333:                          Debug.WriteLine("Telnet_EndReceive(): receiveString: [" + receiveString + "]");
 334:                      }
 335:                      else
 336:                      {
 337:   
 338:                      }
 339:   
 340:                      telnet.BeginReceive(buffer);
 341:                  }
 342:                  else
 343:                  {
 344:                      throw new Exception("Telnet_EndReceive(): " + e.Exception.Message);
 345:                  }
 346:              }
 347:              catch (System.Net.Sockets.SocketException se)
 348:              {
 349:                  Debug.WriteLine("Telnet_EndReceive(): System.Net.Sockets.SocketException: " + se.ToString());
 350:              }
 351:   
 352:              Debug.WriteLine("============================================");
 353:              Debug.WriteLine(" ");
 354:          }
 355:   
 356:          ////////////////////////////////////////////////////////////////////////////
 357:   
 358:          /// <summary>
 359:          ///
 360:          /// </summary>
 361:          private void Telnet_ConnectedChangedEx(object sender, System.EventArgs e)
 362:          {
 363:              // always raised when Connection is established or closed (Connected property changes)
 364:   
 365:              //UpdateToolStripStatusLabel();
 366:          }
 367:   
 368:          ////////////////////////////////////////////////////////////////////////////
 369:   
 370:          /// <summary>
 371:          ///
 372:          /// </summary>
 373:          public void Update(string rowData, ref Ia.Ngn.Cl.Model.Client.Huawei.Ems ems, out Ia.Cl.Model.Result result)
 374:          {
 375:              Ia.Ngn.Cl.Model.Business.Huawei.Ems.UpdateDatabaseWithEmsCommandOutput(rowData, ref ems, out result);
 376:          }
 377:   
 378:          ////////////////////////////////////////////////////////////////////////////
 379:          ////////////////////////////////////////////////////////////////////////////
 380:      }
 381:   
 382:      ////////////////////////////////////////////////////////////////////////////
 383:      ////////////////////////////////////////////////////////////////////////////
 384:  }