Changeset 18

Show
Ignore:
Timestamp:
09/06/07 13:04:36 (1 year ago)
Author:
tmiller
Message:

Adapt tests and examples to the new API

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/binary-mq/libsipc/examples/Makefile

    r9 r18  
    2020OBJS=mq_creator mq_destroyer mq_sender mq_reader mq_bin_sender mq_bin_reader shm_creator shm_destroyer shm_sender shm_reader  
    2121LDFLAGS=-L../src -lsipc -Wl,-rpath,$(SHLIBDIR) 
     22CFLAGS+=-g3 -I../include 
    2223SUBDIRS=policy 
    2324 
  • branches/binary-mq/libsipc/examples/README

    r4 r18  
    33Creating an IPC Resource 
    44======================== 
    5 Libsipc was designed with the intention of minimizing backchannels between reader and  
    6 writer processes. Therefore, creation and destruction of IPC resources must be controlled  
    7 by applications outside of the communicating processes.  
    8 The shm_creator and mq_creator targets in this directory are examples of such helper applications; they simply call sipc_create(3).  
    9 Likewise, the shm_destroyer and mq_destroyer targets in this directory are also helper applications; they call sipc_destroy_resource(3). 
     5Libsipc was designed with the intention of minimizing backchannels 
     6between reader and writer processes. Therefore, creation and destruction 
     7of IPC resources must be controlled by applications outside of the 
     8communicating processes.  The shm_creator and mq_creator targets in this 
     9directory are examples of such helper applications; they simply call 
     10sipc_open(3) with the SIPC_CREATOR flag.  Likewise, the shm_destroyer 
     11and mq_destroyer targets in this directory are also helper applications; 
     12they call sipc_unlink(3). 
    1013 
    1114Connecting 
    1215========== 
    13 Connecting a process to an IPC resource created through libsipc involves calling sipc_init(3). 
    14 This function will return an IPC handle, which contains an internal buffer and state information relevant 
    15 to the communications process. A call to sipc_init(3) will allocate the IPC handle's data member.  
    16 After the handle has been initialized, a call to calling sipc_connect(3) will actually connect the handle to the IPC resource: 
     16Connecting a process to an IPC resource created through libsipc involves 
     17calling sipc_open(3) with the SIPC_SENDER or SIPC_RECEIVER flag.  This 
     18function will return an IPC handle, which contains an internal buffer 
     19and state information relevant to the communications process. When the 
     20SIPC_SENDER or SIPC_RECEIVER flag is specified, a call to sipc_open(3) 
     21will allocate the IPC handle's data member and connect the handle to 
     22the IPC resource: 
    1723 
    18         ipc = sipc_init("ipc_key", size, ipc_type);      
    19         sipc_connect(ipc); 
     24        ipc = sipc_open("ipc_key", role, ipc_type, size);        
    2025 
    21 Note the distinction between an IPC handle and an IPC resource: an IPC handle has an underlying IPC mechanism,  
    22 such as message queues or shared memory, as specified by the ipc_type parameter to sipc_init(3).  
    23 This underlying mechanism and its associated system resources shall be known as an "IPC resource." Processes communicating 
    24 using libsipc do not directly access IPC resources, rather they operate entirely on IPC handles. 
     26where "role" is either SIPC_SENDER or SIPC_RECEIVER.  Note the distinction 
     27between an IPC handle and an IPC resource: an IPC handle has an underlying 
     28IPC mechanism, such as message queues or shared memory, as specified by 
     29the ipc_type parameter to sipc_open(3).  This underlying mechanism and its 
     30associated system resources shall be known as an "IPC resource." Processes 
     31communicating using libsipc do not directly access IPC resources, rather 
     32they operate entirely on IPC handles. 
    2533 
    2634Sending Data 
    2735============ 
    28 To send data to a connected IPC handle, the data must first be copied into the handle's internal data member.   
    29 To do this, a pointer to the handle's internal data member can be obtained via sipc_get_data_ptr(3): 
     36To send data to a connected IPC handle, the data must first be copied 
     37into the handle's internal data member.  To do this, a pointer to the 
     38handle's internal data member can be obtained via sipc_get_data_ptr(3): 
    3039 
    3140        char my_data[] = "Hello, world."; 
     
    3645Receiving Data 
    3746============== 
    38 To receive data from a connected IPC handle, you must call sipc_recv_data(3), passing it a buffer  
    39 for data storage: 
     47To receive data from a connected IPC handle, you must call 
     48sipc_recv_data(3), passing it a buffer for data storage: 
    4049 
    4150        char *data = sipc_get_data_ptr(ipc); 
     
    4453        sipc_shm_recv_done(ipc);  /* Only needed when using shm */ 
    4554 
    46 When using shared memory, a call to sipc_shm_recv_done(3) is needed to unblock the sending process,  
    47 allowing it to send more data.  
     55When using shared memory, a call to sipc_shm_recv_done(3) is needed to 
     56unblock the sending process, allowing it to send more data. 
    4857 
    4958Cleaning Up 
    5059=========== 
    51 Disconnecting an IPC handle from an IPC resource can be done with sipc_disconnect(3).  
    52 To eliminate backchannels between sending and receiving processes, freeing IPC resources acquired  
    53 during the communications process should be done in a separate process (see "Creating an IPC Resource").  
    54 This destroyer process should call sipc_destroy_resource(3) to free IPC resources.  Note that  
    55 the destroyer process should be run only when all communicating processes have disconnected from 
    56 their respective IPC resource. 
     60Closing an IPC handle from an IPC resource can be done with sipc_close(3). 
     61To eliminate backchannels between sending and receiving processes, 
     62freeing IPC resources acquired during the communications process 
     63should be done in a separate process (see "Creating an IPC Resource"). 
     64This destroyer process should call sipc_unlink(3) to free IPC resources. 
     65Note that the destroyer process should be run only when all communicating 
     66processes have closed their respective IPC handles. 
    5767 
    5868Handling Errors 
    5969=============== 
    60 When using libsipc, various errors may occur in the communication process.  
    61 The sipc_error(3) function has been provided to handle the displaying of error messages  
    62 when such conditions arise: 
     70When using libsipc, various errors may occur in the communication process. 
     71The sipc_error(3) function has been provided to handle the displaying 
     72of error messages when such conditions arise: 
    6373 
    6474        if (sipc_send_data(ipc) < 0)  
    6575                sipc_error(ipc, "Error sending data\n");         
    66  
    67  
  • branches/binary-mq/libsipc/examples/mq_bin_reader.c

    r9 r18  
    5656          } 
    5757 
    58         /* Initialize the IPC handle */ 
    59         sipc_t *ipc = sipc_init(SIPC_KEY, SIPC_SYSV_MQUEUES, DATA_LEN, 0); 
     58        /* Create a new IPC handle for receiving */ 
     59        sipc_t *ipc = sipc_open(SIPC_KEY, SIPC_RECEIVER, SIPC_SYSV_MQUEUES, DATA_LEN); 
    6060        if (!ipc) { 
    6161                fprintf(stderr, "Error: unable to create IPC resource\n"); 
    6262                return 1; 
    6363        } 
    64  
    65         /* Connect to the IPC resource */ 
    66         if (sipc_connect(ipc)) { 
    67                 fprintf(stderr, "Error: unable to connect IPC resource\n"); 
    68                 return 1; 
    69         } 
    70          
    71  
    7264 
    7365        /* First receive the file */ 
     
    8476         
    8577        /* Cleanup */ 
    86         sipc_disconnect(ipc); 
    87         sipc_destroy_handle(ipc); 
     78        sipc_close(ipc); 
    8879        return 0; 
    8980} 
  • branches/binary-mq/libsipc/examples/mq_bin_sender.c

    r9 r18  
    6464                          
    6565        /* Initialize and connect IPC handle */ 
    66         ipc = sipc_init(SIPC_KEY, SIPC_SYSV_MQUEUES, IPC_LEN, 1); 
     66        ipc = sipc_open(SIPC_KEY, SIPC_SENDER, SIPC_SYSV_MQUEUES, IPC_LEN); 
    6767        if (!ipc) { 
    6868                fprintf(stderr, "Unable to create IPC resource\n"); 
    6969                goto out; 
    7070        } 
    71         if (sipc_connect(ipc)) { 
    72                 fprintf(stderr, "Unable to connect IPC resource\n"); 
    73                 goto out; 
    74         }        
    7571 
    7672        /* Get pointer to the handle's internal buffer */ 
     
    120116out: 
    121117        /* Cleanup */ 
    122         sipc_destroy_handle(ipc); 
     118        sipc_close(ipc); 
    123119        return retv; 
    124120} 
  • branches/binary-mq/libsipc/examples/mq_creator.c

    r4 r18  
    3333int main() 
    3434{ 
    35         if (sipc_create(SIPC_KEY, SIPC_SYSV_MQUEUES) < 0) { 
     35        sipc_t *sipc; 
     36 
     37        /* Do not need to specify a max size when creating an mqueue. */ 
     38        sipc = sipc_open(SIPC_KEY, SIPC_CREATOR, SIPC_SYSV_MQUEUES, 0); 
     39        if (sipc == NULL) { 
    3640                fprintf(stderr, "Unable to create message queue.\n"); 
    3741                return -1; 
    3842        } 
     43        sipc_close(sipc); 
    3944 
    4045        return 0; 
  • branches/binary-mq/libsipc/examples/mq_destroyer.c

    r4 r18  
    3636int main() 
    3737{ 
    38         sipc_destroy_resource(SIPC_KEY, SIPC_SYSV_MQUEUES); 
     38        sipc_unlink(SIPC_KEY, SIPC_SYSV_MQUEUES); 
    3939        return 0; 
    4040} 
  • branches/binary-mq/libsipc/examples/mq_reader.c

    r4 r18  
    4747 
    4848        /* Initialize the IPC handle */ 
    49         sipc_t *ipc = sipc_init(SIPC_KEY, SIPC_SYSV_MQUEUES, DATA_LEN, 0); 
     49        sipc_t *ipc = sipc_open(SIPC_KEY, SIPC_RECEIVER, SIPC_SYSV_MQUEUES, DATA_LEN); 
    5050        if (!ipc) { 
    5151                fprintf(stderr, "Error: unable to create IPC resource\n"); 
     
    5353        } 
    5454 
    55         /* Connect to the IPC resource */ 
    56         if (sipc_connect(ipc)) { 
    57                 fprintf(stderr, "Error: unable to connect IPC resource\n"); 
    58                 return 1; 
    59         } 
    60          
    6155        /* Receive data from shared memory until the end of transmission 
    6256         * marker has been received. */ 
     
    7165 
    7266        /* Cleanup */ 
    73         sipc_disconnect(ipc); 
    74         sipc_destroy_handle(ipc); 
     67        sipc_close(ipc); 
    7568        return 0; 
    7669} 
  • branches/binary-mq/libsipc/examples/mq_sender.c

    r4 r18  
    5858 
    5959        /* Initialize and connect IPC handle */ 
    60         ipc = sipc_init(SIPC_KEY, SIPC_SYSV_MQUEUES, IPC_LEN, 1); 
     60        ipc = sipc_open(SIPC_KEY, SIPC_SENDER, SIPC_SYSV_MQUEUES, IPC_LEN); 
    6161        if (!ipc) { 
    6262                fprintf(stderr, "Unable to create IPC resource\n"); 
    6363                goto out; 
    6464        } 
    65         if (sipc_connect(ipc)) { 
    66                 fprintf(stderr, "Unable to connect IPC resource\n"); 
    67                 goto out; 
    68         }        
    6965 
    7066        /* Get pointer to the handle's internal buffer */ 
     
    10096out: 
    10197        /* Cleanup */ 
    102         sipc_destroy_handle(ipc); 
     98        sipc_close(ipc); 
    10399        return retv; 
    104100} 
  • branches/binary-mq/libsipc/examples/shm_creator.c

    r4 r18  
    3333 * by both the sender and receiver. The file must already exist */ 
    3434#define SIPC_KEY "sipc_shm_test"   
     35 
     36/* Amount of data to allocate inside the IPC handle */ 
     37#define READ_LEN 4096         
    3538                                            
    3639int main() 
    3740{ 
    38         if (sipc_create(SIPC_KEY, SIPC_SYSV_SHM) < 0) { 
     41        sipc_t *sipc; 
     42 
     43        sipc = sipc_open(SIPC_KEY, SIPC_CREATOR, SIPC_SYSV_SHM, READ_LEN); 
     44        if (sipc == NULL) { 
    3945                fprintf(stderr, "Unable to create message queue.\n"); 
    4046                return -1; 
    4147        } 
     48        sipc_close(sipc); 
    4249 
    4350        return 0; 
  • branches/binary-mq/libsipc/examples/shm_destroyer.c

    r4 r18  
    3636int main() 
    3737{ 
    38         sipc_destroy_resource(SIPC_KEY, SIPC_SYSV_SHM); 
     38        sipc_unlink(SIPC_KEY, SIPC_SYSV_SHM); 
    3939        return 0; 
    4040} 
  • branches/binary-mq/libsipc/examples/shm_reader.c

    r4 r18  
    4747 
    4848        /* Initialize the IPC handle */ 
    49         sipc_t *ipc = sipc_init(SIPC_KEY, SIPC_SYSV_SHM, READ_LEN, 0); 
     49        sipc_t *ipc = sipc_open(SIPC_KEY, SIPC_RECEIVER, SIPC_SYSV_SHM, READ_LEN); 
    5050        if (!ipc) { 
    5151                fprintf(stderr, "Could not create IPC resource\n"); 
     
    5353        } 
    5454 
    55         /* Connect shm */ 
    56         if (sipc_connect(ipc) < 0) { 
    57                 sipc_error(ipc, "Unable to connect to IPC resource\n"); 
    58                 return -1; 
    59         } 
    60          
    6155        /* Receive data from shared memory until an end of transmission 
    6256         * marker has been received */ 
     
    7064        } 
    7165         
    72         sipc_disconnect(ipc); 
    73         sipc_destroy_handle(ipc); 
     66        sipc_close(ipc); 
    7467        return 0; 
    7568} 
  • branches/binary-mq/libsipc/examples/shm_sender.c

    r4 r18  
    5252 
    5353        /* Initialize the IPC handle */ 
    54         sipc_t *ipc = sipc_init(SIPC_KEY, SIPC_SYSV_SHM, READ_LEN, 1); 
     54        sipc_t *ipc = sipc_open(SIPC_KEY, SIPC_SENDER, SIPC_SYSV_SHM, READ_LEN); 
    5555        if (!ipc) 
    5656                goto err;        
    57          
    58         /* Connect shm */ 
    59         if (sipc_connect(ipc) < 0)  
    60                 goto err; 
    6157         
    6258        /* Get a pointer to data */ 
     
    8884        if (ifile) 
    8985                fclose(ifile); 
    90         sipc_disconnect(ipc); 
    91         sipc_destroy_handle(ipc); 
     86        sipc_close(ipc); 
    9287        return 0; 
    9388} 
  • branches/binary-mq/libsipc/tests/ipc_creator.c

    r4 r18  
    2323#include <sipc/sipc.h> 
    2424 
     25#define DATA_LEN 8192 
     26 
    2527int main(int argc, char *argv[]) 
    2628{ 
     29        sipc_t *sipc; 
     30 
    2731        if (argc != 3) { 
    28                 fprintf(stderr, "Usage: ipc_creator <ipc type> <key>\n"); 
     32                fprintf(stderr, "Usage: ipc_creator <key> <ipc type>\n"); 
    2933                return -1; 
    3034        } 
    3135                         
    32         if (sipc_create(argv[1], atoi(argv[2])) < 0) { 
    33                 fprintf(stderr, "Unable to create message queue.\n"); 
     36        sipc = sipc_open(argv[1], SIPC_CREATOR, atoi(argv[2]), DATA_LEN); 
     37        if (sipc == NULL) { 
     38                fprintf(stderr, "Unable to create ipc.\n"); 
    3439                return -1; 
    3540        } 
  • branches/binary-mq/libsipc/tests/ipc_destroyer.c

    r4 r18  
    3030        } 
    3131 
    32         sipc_destroy_resource(argv[1], atoi(argv[2])); 
     32        sipc_unlink(argv[1], atoi(argv[2])); 
    3333        return 0; 
    3434} 
  • branches/binary-mq/libsipc/tests/mqueue.c

    r4 r18  
    7272        wait(&status); 
    7373 
    74         /* Initialize IPCs */    
    75         if ((writer_ipc = sipc_init(SIPC_MQ_KEY, SIPC_SYSV_MQUEUES, DATA_LEN, 1)) == NULL) { 
     74        /* Open IPCs */  
     75        writer_ipc = 
     76            sipc_open(SIPC_MQ_KEY, SIPC_SENDER, SIPC_SYSV_MQUEUES, DATA_LEN); 
     77        if (writer_ipc == NULL) { 
    7678                fprintf(stderr, "Unable to initialize IPC resource\n"); 
    7779                return -1; 
    7880        } 
    79         if ((reader_ipc = sipc_init(SIPC_MQ_KEY, SIPC_SYSV_MQUEUES, DATA_LEN, 0)) == NULL) { 
     81        reader_ipc = 
     82            sipc_open(SIPC_MQ_KEY, SIPC_RECEIVER, SIPC_SYSV_MQUEUES, DATA_LEN); 
     83        if (reader_ipc == NULL) { 
    8084                fprintf(stderr, "Error initializing IPC resource\n"); 
    81                 return -1; 
    82         } 
    83  
    84         /* Connect IPCs */ 
    85         if (sipc_connect(writer_ipc) < 0) { 
    86                 sipc_error(writer_ipc, "Unable to connect IPC resource\n"); 
    87                 return -1; 
    88         }        
    89         if (sipc_connect(reader_ipc) < 0) { 
    90                 sipc_error(reader_ipc, "Unable to connect IPC resource\n"); 
    9185                return -1; 
    9286        } 
     
    127121 
    128122        while (!sipc_recv_data(reader_ipc, &data, &len)) { 
    129                 if (is_end_xmit(data)) 
     123                if (data != NULL && is_end_xmit(data)) 
    130124                        break; 
    131125        } 
    132126 
    133127        /* Cleanup handle here */ 
    134         sipc_destroy_handle(reader_ipc); 
     128        sipc_close(reader_ipc); 
    135129} 
    136130 
     
    158152         
    159153        /* Send data, then end of xmit */ 
    160         CU_ASSERT(sipc_send_data(writer_ipc) == 0); 
     154        CU_ASSERT(sipc_send_data(writer_ipc, DATA_LEN) == 0); 
    161155        CU_ASSERT(send_end_xmit(writer_ipc) == 0); 
    162156        fclose(ifile);   
    163157 
    164158        /* Cleanup handle here */ 
    165         sipc_destroy_handle(writer_ipc); 
     159        sipc_close(writer_ipc); 
    166160} 
    167161 
     
    208202        bzero(data, DATA_LEN); 
    209203        strncpy(data, XMIT_END, DATA_LEN-1); 
    210         if (sipc_send_data(ipc) < 0) { 
     204        if (sipc_send_data(ipc, DATA_LEN) < 0) { 
    211205                sipc_error(ipc, "Unable to send end of transmission marker\n"); 
    212206                return -1; 
  • branches/binary-mq/libsipc/tests/shm.c

    r4 r18  
    7575        wait(&status); 
    7676         
    77         /* Initialize IPCs */ 
    78         writer_ipc = sipc_init(SIPC_SHM_KEY, SIPC_SYSV_SHM, DATA_LEN, 1); 
    79         if (!writer_ipc) { 
     77        /* Open IPCs */ 
     78        writer_ipc = 
     79            sipc_open(SIPC_SHM_KEY, SIPC_SENDER, SIPC_SYSV_SHM, DATA_LEN); 
     80        if (writer_ipc == NULL) { 
    8081                fprintf(stderr, "Could not initialize IPC resource\n"); 
    8182                return -1; 
    8283        } 
    83         reader_ipc = sipc_init(SIPC_SHM_KEY, SIPC_SYSV_SHM, DATA_LEN, 0); 
    84         if (!reader_ipc) { 
     84        reader_ipc = 
     85            sipc_open(SIPC_SHM_KEY, SIPC_RECEIVER, SIPC_SYSV_SHM, DATA_LEN); 
     86        if (reader_ipc == NULL) { 
    8587                fprintf(stderr, "Could not initialize IPC resource\n"); 
    8688                return -1; 
    8789        }                     
    88  
    89         /* Connect IPCs */ 
    90         if (sipc_connect(writer_ipc) < 0) { 
    91                 sipc_error(writer_ipc, "Unable to connect writer IPC\n"); 
    92                 return -1; 
    93         } 
    94         if (sipc_connect(reader_ipc) < 0) { 
    95                 sipc_error(writer_ipc, "Unable to connect reader IPC\n"); 
    96                 return -1; 
    97         }        
    9890 
    9991        return 0; 
     
    175167        if ((rbytes = fread(shm_data, sizeof(char), DATA_LEN-1, ifile)) < 0) { 
    176168                sipc_error(writer_ipc, "fread: %s\n", strerror(errno)); 
     169                fclose(ifile); 
    177170                return; 
    178171        }  
     172        fclose(ifile); 
    179173 
    180174        /* Send the data */ 
    181         if (sipc_send_data(writer_ipc) < 0) { 
     175        if (sipc_send_data(writer_ipc, DATA_LEN) < 0) { 
    182176                sipc_error(writer_ipc, "Unable to send data\n"); 
    183177                return;  
     
    186180        CU_PASS("Successfully sent data via SYSV shared memory");               
    187181 
    188         sipc_disconnect(writer_ipc); 
    189         sipc_destroy_handle(reader_ipc); 
     182        sipc_close(writer_ipc); 
    190183} 
    191184 
     
    214207        } 
    215208 
    216         sipc_disconnect(reader_ipc); 
    217         sipc_destroy_handle(reader_ipc);         
     209        sipc_close(reader_ipc);  
    218210} 
    219211 
     
    228220        bzero(data, DATA_LEN); 
    229221        strncpy(data, DATA_END, DATA_LEN); 
    230         if (sipc_send_data(sipc) < 0) { 
     222        if (sipc_send_data(sipc, DATA_LEN) < 0) { 
    231223                sipc_error(sipc, "Unable to send end of transmission message.\n"); 
    232224                return; 
     
    249241        
    250242        rbytes = fread(data, sizeof(char), DATA_LEN-1, ifile); 
    251         if (rbytes < 0)  
     243        if (rbytes < 0) { 
     244                fclose(ifile); 
    252245                return -1;       
     246        } 
     247        fclose(ifile); 
    253248 
    254249        if (strcmp(data, recv_data))