home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / CPI-C.ZIP / CALLS.C < prev    next >
Text File  |  1992-06-24  |  18KB  |  483 lines

  1. /*
  2.  *  PROGRAM:   JQCPIC -- John Q's Portable CPI-C Abuser
  3.  *
  4.  *  MODULE:    CALLS.C -- issue random CPI-C calls
  5.  *
  6.  *  COPYRIGHTS:
  7.  *             This module contains code made available by IBM
  8.  *             Corporation on an AS IS basis.  Any one receiving the
  9.  *             module is considered to be licensed under IBM copyrights
  10.  *             to use the IBM-provided source code in any way he or she
  11.  *             deems fit, including copying it, compiling it, modifying
  12.  *             it, and redistributing it, with or without
  13.  *             modifications.  No license under any IBM patents or
  14.  *             patent applications is to be implied from this copyright
  15.  *             license.
  16.  *
  17.  *             A user of the module should understand that IBM cannot
  18.  *             provide technical support for the module and will not be
  19.  *             responsible for any consequences of use of the program.
  20.  *
  21.  *             Any notices, including this one, are not to be removed
  22.  *             from the module without the prior written consent of
  23.  *             IBM.
  24.  *
  25.  *  AUTHOR:    Dr. John Q. Walker II
  26.  *             IBM VNET: JOHNQ at RALVM6          IBM tie line: 444-4414
  27.  *             Internet: johnq@vnet.ibm.com        phone: (919) 254-4414
  28.  *
  29.  *  RELATED FILES:
  30.  *             See file JQCPIC.DOC for detailed information.
  31.  *
  32.  *  CHANGE HISTORY:
  33.  *  Date       Description
  34.  *  05/12/92   Added prologue.
  35.  *  06/07/92   Faster get_call_index().
  36.  *  06/20/92   Added check_receive() & check_rts()
  37.  */
  38.  
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42.  
  43. #include "jqcpic.h"
  44. #include "calls.h"
  45. #include "build.h"
  46.  
  47.  
  48. /* local function prototypes */
  49. unsigned get_call_index(const CM_CONVERSATION_STATE);
  50.  
  51.  
  52. #if 0
  53. unsigned get_call_index(const CM_CONVERSATION_STATE conv_state)
  54. {
  55.     /*=========================================================================
  56.      * Determine the index of the next CPI-C call, given the current state.
  57.      *
  58.      * ATTENTION: this procedure is a PIG!  It was written for laziness, not
  59.      * efficiency.  Feel free to re-write it!
  60.      *=======================================================================*/
  61.  
  62.     unsigned target_probability;
  63.     int next_cpic_call;
  64.  
  65.     /* pick a target number between 1 and 100 */
  66.     target_probability = (rand() % (100)) + 1;
  67.  
  68.     /*-------------------------------------------------------------------------
  69.      * Now, find the index of a CPI-C call whose probability in this
  70.      * state is greater than or equal to the target.
  71.      * Note: This loop performs terribly!
  72.      *-----------------------------------------------------------------------*/
  73.     do {
  74.         next_cpic_call = rand() % (NUM_CPIC_CALLS);
  75.     }
  76.     while (cpic_state_table[next_cpic_call][conv_state] <
  77.            target_probability);
  78.  
  79.     return next_cpic_call;
  80. }
  81. #endif
  82.  
  83.  
  84. unsigned get_call_index(const CM_CONVERSATION_STATE conv_state)
  85. {
  86.     /*=========================================================================
  87.      * Get the index of the next CPI-C call.
  88.      *=======================================================================*/
  89.  
  90.     /* pick a target number between 1 and 100 */
  91.     unsigned target_probability = (rand() % (100)) + 1;
  92.     unsigned target_count;
  93.  
  94.     {   /*---------------------------------------------------------------------
  95.          * Find the number of calls that fit this probability.
  96.          *-------------------------------------------------------------------*/
  97.         unsigned ii;
  98.         unsigned count = 0;
  99.         for (ii = 0; ii < NUM_CPIC_CALLS; ii++) {
  100.             if (cpic_state_table[ii][conv_state] >= target_probability) {
  101.                 count++;
  102.             }
  103.         }
  104.         /* pick a target count between 1 and the count */
  105.         target_count = (rand() % count) + 1;
  106.     }
  107.  
  108.     {   /*---------------------------------------------------------------------
  109.          * Find a call that randomly matches this probability.
  110.          *-------------------------------------------------------------------*/
  111.         unsigned ii;
  112.         unsigned count = 0;
  113.         for (ii = 0; ii < NUM_CPIC_CALLS; ii++) {
  114.             if (cpic_state_table[ii][conv_state] >= target_probability) {
  115.                 if (target_count == ++count) {
  116.                     break;
  117.                 }
  118.             }
  119.         }
  120.         return ii;
  121.     }
  122. }
  123.  
  124.  
  125. CM_CONVERSATION_STATE issue_cpic_call(CPIC_CONV_ATTRIB *this)
  126. {
  127.     /*=========================================================================
  128.      * Issue the next CPI-C call, one that's valid for the current state.
  129.      *=======================================================================*/
  130.  
  131.     CM_CONVERSATION_STATE old_conv_state;
  132.  
  133.     unsigned cpic_call_index =
  134.         get_call_index(old_conv_state = this->conversation_state);
  135.  
  136.     show_cpic_call(this, cpic_call_index);
  137.     show_conv_state(old_conv_state);
  138.  
  139.     switch(cpic_call_index) {
  140.         case CMACCP :
  141.             this->conversation_state = build_cmaccp (this);
  142.             break;
  143.         case CMALLC :
  144.             this->conversation_state = build_cmallc (this);
  145.             break;
  146.         case CMCFM  :
  147.             this->conversation_state = build_cmcfm  (this);
  148.             break;
  149.         case CMCFMD :
  150.             this->conversation_state = build_cmcfmd (this);
  151.             break;
  152.         case CMDEALa:
  153.             this->conversation_state = build_cmdeala(this);
  154.             break;
  155.         case CMDEALc:
  156.             this->conversation_state = build_cmdealc(this);
  157.             break;
  158.         case CMDEALf:
  159.             this->conversation_state = build_cmdealf(this);
  160.             break;
  161.         case CMDEALs:
  162.             this->conversation_state = build_cmdeals(this);
  163.             break;
  164.         case CMECS  :
  165.             this->conversation_state = build_cmecs  (this);
  166.             break;
  167.         case CMECT  :
  168.             this->conversation_state = build_cmect  (this);
  169.             break;
  170.         case CMEMN  :
  171.             this->conversation_state = build_cmemn  (this);
  172.             break;
  173.         case CMEPLN :
  174.             this->conversation_state = build_cmepln (this);
  175.             break;
  176.         case CMESL  :
  177.             this->conversation_state = build_cmesl  (this);
  178.             break;
  179.         case CMFLUS :
  180.             this->conversation_state = build_cmflus (this);
  181.             break;
  182.         case CMINIT :
  183.             this->conversation_state = build_cminit (this);
  184.             break;
  185.         case CMPTRc :
  186.             this->conversation_state = build_cmptrc (this);
  187.             break;
  188.         case CMPTRf :
  189.             this->conversation_state = build_cmptrf (this);
  190.             break;
  191.         case CMPTRs :
  192.             this->conversation_state = build_cmptrs (this);
  193.             break;
  194.         case CMRCVi :
  195.             this->conversation_state = build_cmrcvi (this);
  196.             break;
  197.         case CMRCVw :
  198.             this->conversation_state = build_cmrcvw (this);
  199.             break;
  200.         case CMRTS  :
  201.             this->conversation_state = build_cmrts  (this);
  202.             break;
  203.         case CMSCT  :
  204.             this->conversation_state = build_cmsct  (this);
  205.             break;
  206.         case CMSDT  :
  207.             this->conversation_state = build_cmsdt  (this);
  208.             break;
  209.         case CMSED  :
  210.             this->conversation_state = build_cmsed  (this);
  211.             break;
  212.         case CMSEND :
  213.             this->conversation_state = build_cmsend (this);
  214.             break;
  215.         case CMSERR :
  216.             this->conversation_state = build_cmserr (this);
  217.             break;
  218.         case CMSF   :
  219.             this->conversation_state = build_cmsf   (this);
  220.             break;
  221.         case CMSLD  :
  222.             this->conversation_state = build_cmsld  (this);
  223.             break;
  224.         case CMSMN  :
  225.             this->conversation_state = build_cmsmn  (this);
  226.             break;
  227.         case CMSPLN :
  228.             this->conversation_state = build_cmspln (this);
  229.             break;
  230.         case CMSPTR :
  231.             this->conversation_state = build_cmsptr (this);
  232.             break;
  233.         case CMSRC  :
  234.             this->conversation_state = build_cmsrc  (this);
  235.             break;
  236.         case CMSRT  :
  237.             this->conversation_state = build_cmsrt  (this);
  238.             break;
  239.         case CMSSL  :
  240.             this->conversation_state = build_cmssl  (this);
  241.             break;
  242.         case CMSST  :
  243.             this->conversation_state = build_cmsst  (this);
  244.             break;
  245.         case CMSTPN :
  246.             this->conversation_state = build_cmstpn (this);
  247.             break;
  248.         case CMTRTS :
  249.             this->conversation_state = build_cmtrts (this);
  250.             break;
  251.         default     :
  252.             this->conversation_state = (CM_CONVERSATION_STATE)(-1);
  253.             break;
  254.     }
  255.  
  256.     show_end_of_line();
  257.  
  258.     return this->conversation_state;
  259. }
  260.  
  261.  
  262. CM_CONVERSATION_STATE unexpected_rc(CPIC_CONV_ATTRIB *this)
  263. {
  264.     /*=========================================================================
  265.      * Handle an unexpected return code.
  266.      *=======================================================================*/
  267.  
  268.     FILE *log_stream;
  269.  
  270.     char *log_name = (char *)get_memory((size_t)(strlen(program_name) + 1 +
  271.                                                  strlen(LOG_EXTENSION)));
  272.     if ((char *)NULL != log_name) {
  273.         strcpy(log_name, program_name);
  274.         strcat(log_name, LOG_EXTENSION);
  275.         log_stream = fopen(log_name, "w");
  276.         if ((FILE *)NULL != log_stream) {
  277.  
  278.             fprintf(log_stream, "call_index: %ld\n",
  279.                                 this->call_index);
  280.             fprintf(log_stream, "conversation_state: %ld\n",
  281.                                 this->conversation_state);
  282.             fprintf(log_stream, "conversation_type: %ld\n",
  283.                                 this->conversation_type);
  284.             fprintf(log_stream, "data_received: %ld\n",
  285.                                 this->data_received);
  286.             fprintf(log_stream, "deallocate_type: %ld\n",
  287.                                 this->deallocate_type);
  288.             fprintf(log_stream, "error_direction: %ld\n",
  289.                                 this->error_direction);
  290.             fprintf(log_stream, "fill: %ld\n",
  291.                                 this->fill);
  292.             fprintf(log_stream, "log_data_length: %ld\n",
  293.                                 this->log_data_length);
  294.             fprintf(log_stream, "mode_name_length: %ld\n",
  295.                                 this->mode_name_length);
  296.             fprintf(log_stream, "partner_LU_name_length: %ld\n",
  297.                                 this->partner_LU_name_length);
  298.             fprintf(log_stream, "prepare_to_receive_type: %ld\n",
  299.                                 this->prepare_to_receive_type);
  300.             fprintf(log_stream, "receive_type: %ld\n",
  301.                                 this->prepare_to_receive_type);
  302.             fprintf(log_stream, "received_length: %ld\n",
  303.                                 this->received_length);
  304.             fprintf(log_stream, "request_to_send_received: %ld\n",
  305.                                 this->prepare_to_receive_type);
  306.             fprintf(log_stream, "return_code: %ld\n",
  307.                                 this->return_code);
  308.             fprintf(log_stream, "return_control: %ld\n",
  309.                                 this->return_control);
  310.             fprintf(log_stream, "send_length: %ld\n",
  311.                                 this->send_length);
  312.             fprintf(log_stream, "send_type: %ld\n",
  313.                                 this->send_type);
  314.             fprintf(log_stream, "status_received: %ld\n",
  315.                                 this->status_received);
  316.             fprintf(log_stream, "sync_level: %ld\n",
  317.                                 this->sync_level);
  318.             fprintf(log_stream, "TP_name_length: %ld\n",
  319.                                 this->TP_name_length);
  320.  
  321.  
  322.             fclose(log_stream);
  323.         }
  324.     }
  325.  
  326.     return (CM_CONVERSATION_STATE)(-1);
  327. }
  328.  
  329.  
  330. BOOL check_receive(const CPIC_CONV_ATTRIB *this)
  331. {
  332.     /*=========================================================================
  333.      * Validate the values returned on a RECEIVE call.
  334.      *=======================================================================*/
  335.  
  336.     BOOL result = FALSE;
  337.  
  338.     /* Check for valid data_received values */
  339.     if ((CM_OK                 == this->return_code) ||
  340.         (CM_DEALLOCATED_NORMAL == this->return_code)) {
  341.  
  342.         CPIC_CONV_ATTRIB local;             /* one conversation instance     */
  343.  
  344. #ifndef NOCPIC
  345.         cmect(this->conversation_ID,
  346.               &local.conversation_type,
  347.               &local.return_code);
  348. #endif
  349.  
  350.         switch (this->data_received) {
  351.             case CM_NO_DATA_RECEIVED:
  352.                 if ((CM_INT32)0 != this->received_length) {
  353.                     show_error("NO_DATA, but non-zero received length");
  354.                     result = TRUE;
  355.                 }
  356.                 break;
  357.  
  358.             case CM_DATA_RECEIVED:
  359.                 if ((this->received_length > this->requested_length) ||
  360.                     (this->received_length == 0) ||
  361.                     (this->received_length > 32767)) {
  362.                     show_error("Invalid received_length value");
  363.                     result = TRUE;
  364.                 }
  365.                 if (CM_OK == local.return_code) {
  366.                     if (CM_BASIC_CONVERSATION != local.conversation_type) {
  367.                         show_error("DATA_RECEIVED on mapped conversation");
  368.                         result = TRUE;
  369.                     }
  370.                 }
  371.                 break;
  372.  
  373.             case CM_INCOMPLETE_DATA_RECEIVED:
  374.                 if ((CM_RECEIVE_AND_WAIT == this->receive_type) &&
  375.                     (CM_MAPPED_CONVERSATION == local.conversation_type)) {
  376.                     if (this->received_length < this->requested_length) {
  377.                         show_error("INCOMPLETE_DATA_RECEIVED, but "
  378.                                    "received_length < requested_length");
  379.                         result = TRUE;
  380.                     }
  381.                 }
  382.  
  383.                 /* fall through */
  384.  
  385.             case CM_COMPLETE_DATA_RECEIVED:
  386.                 if ((this->received_length > this->requested_length) ||
  387.                     (this->received_length == 0) ||
  388.                     (this->received_length > 32767)) {
  389.                     show_error("Invalid received_length value");
  390.                     result = TRUE;
  391.                 }
  392.                 break;
  393.  
  394.             default:
  395.                 show_error("Invalid data_received value");
  396.                 result = TRUE;
  397.                 break;
  398.         }
  399.     }
  400.  
  401.     /* Check for valid status_received values */
  402.     if (CM_OK == this->return_code) {
  403.  
  404.         CPIC_CONV_ATTRIB local;             /* one conversation instance     */
  405.  
  406. #ifndef NOCPIC
  407.         cmecs(this->conversation_ID,
  408.               &local.conversation_state,
  409.               &local.return_code);
  410. #endif
  411.  
  412.         if (CM_OK == local.return_code) {
  413.             switch (this->status_received) {
  414.                 case CM_NO_STATUS_RECEIVED:
  415.                     if (CM_NO_DATA_RECEIVED == this->data_received) {
  416.                         show_error("No status AND no data received");
  417.                         result = TRUE;
  418.                     }
  419.                     break;
  420.                 case CM_SEND_RECEIVED:
  421.                     if ((CM_SEND_STATE           != local.conversation_state) &&
  422.                         (CM_SEND_PENDING_STATE   != local.conversation_state)) {
  423.                         show_error("SEND status received, but "
  424.                                    "state = %lu",
  425.                                    local.conversation_state);
  426.                         result = TRUE;
  427.                     }
  428.                     break;
  429.                 case CM_CONFIRM_RECEIVED:
  430.                     if (CM_CONFIRM_STATE         != local.conversation_state) {
  431.                         show_error("CONFIRM status received, wrong state");
  432.                         result = TRUE;
  433.                     }
  434.                     break;
  435.                 case CM_CONFIRM_SEND_RECEIVED:
  436.                     if (CM_CONFIRM_SEND_STATE    != local.conversation_state) {
  437.                         show_error("CONFIRM SEND status received, wrong state");
  438.                         result = TRUE;
  439.                     }
  440.                     break;
  441.                 case CM_CONFIRM_DEALLOC_RECEIVED:
  442.                     if (CM_CONFIRM_DEALLOCATE_STATE!=local.conversation_state){
  443.                         show_error("CONFIRM DEAL status received, wrong state");
  444.                         result = TRUE;
  445.                     }
  446.                     break;
  447.                 default:
  448.                     show_error("Invalid status_received value");
  449.                     result = TRUE;
  450.                     break;
  451.             }
  452.         }
  453.     }
  454.  
  455.     if (TRUE == result) protocol_error();
  456.  
  457.     return result;
  458. }
  459.  
  460.  
  461. BOOL check_rts(const CPIC_CONV_ATTRIB *this)
  462. {
  463.     /*=========================================================================
  464.      * Validate the values returned for request_to_send_received.
  465.      *=======================================================================*/
  466.  
  467.     BOOL result = FALSE;
  468.  
  469.     /* Check for valid rts_received values */
  470.     if ((CM_PROGRAM_PARAMETER_CHECK != this->return_code) &&
  471.         (CM_PROGRAM_STATE_CHECK     != this->return_code)) {
  472.         if ((CM_REQ_TO_SEND_RECEIVED     != this->request_to_send_received) &&
  473.             (CM_REQ_TO_SEND_NOT_RECEIVED != this->request_to_send_received)) {
  474.             show_error("Invalid request_to_send_received value");
  475.             result = TRUE;
  476.         }
  477.     }
  478.  
  479.     if (TRUE == result) protocol_error();
  480.  
  481.     return result;
  482. }
  483.