TOC PREV NEXT INDEX


Procedure for Using the Stream-Oriented C++ Control Class Decode Method



Normally the receiving message can be one of several different message types. It is therefore necessary to determine the type of message that was received so that the appropriate decode function can be called to decode it. The ASN1BERInputStream class has standard methods for parsing the initial tag/length from a message to determine the type of message received. These calls are used in conjunction with a switch statement on generated tag constants for the known message set. Each switch case statement contains logic to create an object instance of a specific ASN1C generated control class and to invoke and then to invoke that object's decode method.

A program fragment that could be used to decode an employee record is as follows:

   #include employee.h         // include file generated by ASN1C
 

 
   main ()
 
   {
 
      ASN1TAG tag;
 
      int i, len;
 
      const char*  filename = "message.dat";
 
      ASN1BOOL     trace = TRUE;
 

 
      try {
 
         // Decode 
 

 
         ASN1BERFileInputStream in (filename);
 

 
         try {
 
            in.mark (32);
 
            in.decodeTagAndLen (tag, len);
 
            in.reset ();
 

// Now switch on initial tag value to determine what
            // type of message was received..
 

 
            switch (msgtag)
 
            {
 
               case TV_PersonnelRecord:  // compiler generated
 
                                         // constant
 
               {
 
                  ASN1T_PersonnelRecord msgData;
 
                  ASN1C_PersonnelRecord employee (msgData);
 

 
                  in >> employee;
 
        
 
                  // or employee.DecodeFrom (in);
 

 
                   }
 

 
                  case TV_ ...		// handle other known messages
 
                  ...
 
            }
 
          }
 
         catch (ASN1RTLException& ex) {
 
            printf ("decode of PersonnelRecord failed\n");
 
            printf ("Exception thrown: Status = %i\n",
 
                        ex.getStatus());
 
            in.printErrorInfo ();
 
            return -1;
 
             
 
      }
 
      catch (ASN1RTLException& ex) {
 
         printf ("Exception thrown: Status = %i\n", ex.getStatus());
 
         return -1;
 
      } 
 

 
      return 0;
 
   }
 

Note that the call to free memory and the stream's close method are not required to release dynamic memory when using the C++ interface. This is because the control class hides all of the details of managing the context and releasing dynamic memory. The memory is automatically released when both the input stream object (ASN1BERInputStream and derived classes) and the control class object (ASN1C_<ProdName>) are deleted or go out of scope. Reference counting of a context variable shared by both interfaces is used to accomplish this.



Objective Systems, Inc.

102 Pickering Way, Suite #506
Exton, Pennsylvania 19341
http://www.obj-sys.com
Phone: (484) 875-9841
Toll-free: (877) 307-6855 (US only)
Fax: (484) 875-9830
info@obj-sys.com
TOC PREV NEXT INDEX