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