hellopics-data_conn.c

Go to the documentation of this file.
00001 /*
00002  * $Id: hellopics-data_conn.c 346460 2009-11-14 05:06:47Z ssiano $
00003  *
00004  * This code is provided as is by Juniper Networks SDK Developer Support.
00005  * It is provided with no warranties or guarantees, and Juniper Networks
00006  * will not provide support or maintenance of this code in any fashion.
00007  * The code is provided only to help a developer better understand how
00008  * the SDK can be used.
00009  *
00010  * Copyright (c) 2007-2008, Juniper Networks, Inc.
00011  * All rights reserved.
00012  */
00013 
00022 #include <string.h>
00023 #include <jnx/aux_types.h>
00024 #include <jnx/trace.h>
00025 #include <netinet/in.h>
00026 #include <arpa/inet.h>
00027 #include <jnx/pconn.h>
00028 #include <sync/hellopics_ipc.h>
00029 #include "hellopics-data_conn.h"
00030 #include "hellopics-data_logging.h"
00031 
00032 /*** Constants ***/
00033 #define RETRY_CONNECT_INTERVAL 200
00034 #define MGMT_CLIENT_CONNECT_RETRIES    3  
00035 #define CTRL_CLIENT_CONNECT_RETRIES    3  
00036 
00037 /*** Data Structures ***/
00038 
00039 static pconn_client_t     * ctrl_client;     
00040 static pconn_client_t     * mgmt_client;     
00041 static evContext ev_ctx; 
00042 static evTimerID  mgmt_timer_id;     
00043 
00044 
00045 /*** STATIC/INTERNAL Functions ***/
00046 
00047 static void
00048 connect_mgmt(evContext ctx,
00049             void * uap  __unused,
00050             struct timespec due __unused,
00051             struct timespec inter __unused);
00052 
00065 static void
00066 ctrl_client_connection(pconn_client_t * session,
00067                        pconn_event_t event,
00068                        void * cookie __unused)
00069 {
00070     int rc;
00071     uint32_t id;
00072 
00073     INSIST_ERR(session == ctrl_client);
00074 
00075     switch (event) {
00076 
00077         case PCONN_EVENT_ESTABLISHED:
00078 
00079             LOG(LOG_INFO, "%s: Connected to the control component",
00080                 __func__);
00081             
00082             id = htonl(HELLOPICS_ID_DATA);
00083             
00084             rc = pconn_client_send(ctrl_client, MSG_ID, &id, sizeof(id));
00085             if(rc != PCONN_OK) {
00086                 LOG(LOG_ERR, "%s: Failed to send MSG_ID to the control "
00087                     "component. Error: %d", __func__, rc);
00088                 break;
00089             }
00090 
00091             rc = pconn_client_send(mgmt_client, MSG_READY, NULL, 0);
00092             if(rc != PCONN_OK) {
00093                 LOG(LOG_ERR, "%s: Failed to send MSG_READY to the management "
00094                     "component. Error: %d", __func__, rc);
00095                 break;
00096             }
00097 
00098             rc = pconn_client_send(ctrl_client, MSG_READY, NULL, 0);
00099             if(rc != PCONN_OK) {
00100                 LOG(LOG_ERR, "%s: Failed to send MSG_READY to the control "
00101                     "component. Error: %d", __func__, rc);
00102                 break;
00103             }
00104 
00105             break;
00106 
00107         case PCONN_EVENT_SHUTDOWN:
00108             ctrl_client = NULL;
00109             break;
00110 
00111         default:
00112             LOG(LOG_ERR, "%s: Received an unknown or PCONN_EVENT_FAILED event",
00113                  __func__);
00114             break;
00115     }
00116 }
00117 
00118 
00134 static status_t
00135 ctrl_client_message(pconn_client_t * session,
00136                     ipc_msg_t * msg,
00137                     void * cookie __unused)
00138 {
00139     int rc;
00140     
00141     INSIST_ERR(session == ctrl_client);
00142     INSIST_ERR(msg != NULL);
00143 
00144     switch(msg->subtype) {
00145         
00146         case MSG_HELLO:
00147             // Received a HELLO from the ctrl component, so
00148             // pass it along to the mgmt component
00149             
00150             INSIST_ERR(msg->length == sizeof(uint32_t));
00151             LOG(LOG_INFO, "%s: Forwarding HELLO packet from ctrl to mgmt",
00152                 __func__);
00153             rc = pconn_client_send(mgmt_client, MSG_HELLO,
00154                                    msg->data, sizeof(uint32_t));
00155             if(rc != PCONN_OK) {
00156                 LOG(LOG_ERR, "%s: Failed to send HELLO to mgmt component. "
00157                     "Error: %d", __func__, rc);
00158                 return EFAIL;
00159             }
00160 
00161             break;
00162         
00163         default:
00164         
00165             LOG(LOG_ERR, "%s: Received an unknown message type (%d) from the "
00166                 "control component", __func__, msg->subtype);
00167             return EFAIL;
00168     }
00169 
00170     
00171     return SUCCESS;
00172 }
00173 
00174 
00187 static void
00188 mgmt_client_connection(pconn_client_t * session,
00189                        pconn_event_t event,
00190                        void * cookie __unused)
00191 {
00192     int rc;
00193     uint32_t id;
00194 
00195     INSIST_ERR(session == mgmt_client);
00196 
00197     switch (event) {
00198 
00199         case PCONN_EVENT_ESTABLISHED:
00200         // clear the retry timer
00201         if(evTestID(mgmt_timer_id)) {
00202             evClearTimer(ev_ctx, mgmt_timer_id);
00203             evInitID(&mgmt_timer_id);
00204         }
00205             LOG(LOG_INFO, "%s: Connected to the management component",
00206                 __func__);
00207             
00208             id = htonl(HELLOPICS_ID_DATA);
00209             
00210             rc = pconn_client_send(mgmt_client, MSG_ID, &id, sizeof(id));
00211             if(rc != PCONN_OK) {
00212                 LOG(LOG_ERR, "%s: Failed to send MSG_ID to the mgmt component. "
00213                     "Error: %d", __func__, rc);
00214                 break;
00215             }
00216             
00217             rc = pconn_client_send(mgmt_client, MSG_GET_PEER, NULL, 0);
00218             
00219             if(rc != PCONN_OK) {
00220                 LOG(LOG_ERR, "%s: Failed to send MSG_GET_PEER to the mgmt "
00221                     "component. Error: %d", __func__, rc);
00222                 break;
00223             }
00224             
00225             break;
00226 
00227         case PCONN_EVENT_SHUTDOWN:
00228         if(mgmt_client != NULL) {
00229             LOG(LOG_INFO, "%s: Disconnected from the hellopics-mgmt component"
00230                     " on the RE", __func__);
00231         }
00232             mgmt_client = NULL;
00233         // Reconnect to it if timer not going
00234         if(!evTestID(mgmt_timer_id) &&
00235            evSetTimer(ev_ctx, connect_mgmt, NULL, evConsTime(0,0),
00236                evConsTime(RETRY_CONNECT_INTERVAL, 0), &mgmt_timer_id)) {
00237 
00238             LOG(LOG_EMERG, "%s: Failed to initialize a re-connect timer to "
00239                 "reconnect to the mgmt component", __func__);
00240         }
00241             break;
00242     case PCONN_EVENT_FAILED:
00243 
00244         LOG(LOG_ERR, "%s: Received a PCONN_EVENT_FAILED event", __func__);
00245         
00246         if(mgmt_client != NULL) {
00247             LOG(LOG_INFO, "%s: Disconnected from the hellopics-mgmt component"
00248                     " on the RE", __func__);
00249         }
00250         
00251         mgmt_client = NULL; // connection will be closed
00252         
00253         // Reconnect to it if timer not going
00254         if(!evTestID(mgmt_timer_id) &&
00255            evSetTimer(ev_ctx, connect_mgmt, NULL, evConsTime(0,0),
00256                evConsTime(RETRY_CONNECT_INTERVAL, 0), &mgmt_timer_id)) {
00257 
00258             LOG(LOG_EMERG, "%s: Failed to initialize a re-connect timer to "
00259                 "reconnect to the mgmt component", __func__);
00260         }
00261         
00262         break;
00263         default:
00264             LOG(LOG_ERR, "%s: Received an unknown or PCONN_EVENT_FAILED event",
00265                  __func__);
00266             break;
00267     }
00268 }
00269 
00270 
00287 static status_t
00288 mgmt_client_message(pconn_client_t * session,
00289                     ipc_msg_t * msg,
00290                     void * cookie __unused)
00291 {
00292     int rc;
00293     pconn_client_params_t c_params;
00294     pconn_peer_info_t * info;
00295     
00296     INSIST_ERR(session == mgmt_client);
00297     INSIST_ERR(msg != NULL);
00298 
00299     switch(msg->subtype) {
00300         
00301         case MSG_HELLO:
00302             // Received a HELLO from the mgmt component, so
00303             // pass it along to the ctrl component
00304             
00305             INSIST_ERR(msg->length == sizeof(uint32_t));
00306             LOG(LOG_INFO, "%s: Forwarding HELLO packet from mgmt to ctrl",
00307                 __func__);
00308             rc = pconn_client_send(ctrl_client, MSG_HELLO,
00309                                    msg->data, sizeof(uint32_t));
00310             if(rc != PCONN_OK) {
00311                 LOG(LOG_ERR, "%s: Failed to send HELLO to ctrl component. "
00312                     "Error: %d", __func__, rc);
00313                 return EFAIL;
00314             }
00315 
00316             break;
00317         
00318         case MSG_PEER:
00319             
00320             LOG(LOG_INFO, "%s: Received peer info. Connecting to control "
00321                 "component", __func__);
00322             
00323             INSIST_ERR(msg->length == sizeof(pconn_peer_info_t));
00324             info = (pconn_peer_info_t *)msg->data; // the ctrl component's info
00325             
00326             bzero(&c_params, sizeof(pconn_client_params_t));
00327             
00328             // setup the client args
00329             c_params.pconn_peer_info.ppi_peer_type = PCONN_PEER_TYPE_PIC;
00330             c_params.pconn_peer_info.ppi_fpc_slot = ntohl(info->ppi_fpc_slot);
00331             c_params.pconn_peer_info.ppi_pic_slot = ntohl(info->ppi_pic_slot);
00332             
00333             c_params.pconn_port          = HELLOPICS_PORT_NUM;
00334             c_params.pconn_num_retries   = CTRL_CLIENT_CONNECT_RETRIES;
00335             c_params.pconn_event_handler = ctrl_client_connection;
00336             
00337             // connect
00338 
00339             ctrl_client = pconn_client_connect_async(&c_params, ev_ctx,
00340                 ctrl_client_message, NULL);
00341             
00342             if(ctrl_client == NULL) {
00343                 LOG(LOG_ERR, "%s: Failed to initialize the pconn client "
00344                     "connection to the control component", __func__);
00345                 return EFAIL;
00346             }
00347 
00348             break;
00349         
00350         default:
00351             LOG(LOG_ERR, "%s: Received an unknown message type (%d) from the "
00352                 "management component", __func__, msg->subtype);
00353             return EFAIL;
00354     }
00355 
00356     
00357     return SUCCESS;
00358 }
00359 
00360 
00361 /*** GLOBAL/EXTERNAL Functions ***/
00362 
00363 
00370 static void
00371 connect_mgmt(evContext ctx,
00372             void * uap  __unused,
00373             struct timespec due __unused,
00374             struct timespec inter __unused)
00375 {
00376     pconn_client_params_t m_params;
00377     
00378     // init this client we connect later
00379     ctrl_client = NULL;
00380     
00381     ev_ctx = ctx;
00382     
00383     bzero(&m_params, sizeof(pconn_client_params_t));
00384     
00385     // setup the client args
00386     m_params.pconn_peer_info.ppi_peer_type = PCONN_PEER_TYPE_RE;
00387     m_params.pconn_port                    = HELLOPICS_PORT_NUM;
00388     m_params.pconn_num_retries             = MGMT_CLIENT_CONNECT_RETRIES;
00389     m_params.pconn_event_handler           = mgmt_client_connection;
00390     
00391     // connect
00392     mgmt_client = pconn_client_connect_async(
00393                     &m_params, ctx, mgmt_client_message, NULL);
00394     
00395     if(mgmt_client == NULL) {
00396         LOG(LOG_ERR, "%s: Failed to initialize the pconn client connection "
00397             "to the management component", __func__);
00398     }
00399 }
00409 status_t
00410 init_connections(evContext ctx)
00411 {
00412     mgmt_client = NULL;
00413     
00414     ev_ctx = ctx;
00415     
00416     evInitID(&mgmt_timer_id);
00417     
00418     // Connect to the MGMT component
00419     if(evSetTimer(ctx, connect_mgmt, NULL, evNowTime(),
00420         evConsTime(RETRY_CONNECT_INTERVAL, 0), &mgmt_timer_id)) {
00421 
00422         LOG(LOG_EMERG, "%s: Failed to initialize a connect timer to connect "
00423             "to the MGMT component", __func__);
00424         return EFAIL;
00425     }
00426 
00427     return SUCCESS;
00428 }
00429 
00433 void
00434 close_connections(void)
00435 {
00436     if(ctrl_client) {
00437         pconn_client_close(ctrl_client);
00438         ctrl_client = NULL;
00439     }
00440 
00441     if(mgmt_client) {
00442         pconn_client_close(mgmt_client);
00443         mgmt_client = NULL;
00444     }
00445 }

2007-2009 Juniper Networks, Inc. All rights reserved. The information contained herein is confidential information of Juniper Networks, Inc., and may not be used, disclosed, distributed, modified, or copied without the prior written consent of Juniper Networks, Inc. in an express license. This information is subject to change by Juniper Networks, Inc. Juniper Networks, the Juniper Networks logo, and JUNOS are registered trademarks of Juniper Networks, Inc. in the United States and other countries. All other trademarks, service marks, registered trademarks, or registered service marks are the property of their respective owners.
Generated on Sun May 30 20:26:58 2010 for SDK Your Net Corporation Hellopics Example: hellopics-data 1.0 by Doxygen 1.5.1