equilibrium2-mgmt_server.c

Go to the documentation of this file.
00001 /*
00002  * $Id: equilibrium2-mgmt_server.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) 2008, Juniper Networks, Inc.
00011  * All rights reserved.
00012  */
00013 
00022 #include <sync/equilibrium2.h>
00023 #include "equilibrium2-mgmt.h"
00024 
00025 #include <stdlib.h>
00026 #include <string.h>
00027 #include <jnx/pconn.h>
00028 #include <jnx/trace.h>
00029 #include <jnx/junos_trace.h>
00030 
00031 #include EQUILIBRIUM2_OUT_H
00032 
00033 static pconn_server_t *server_hdl;
00034 static client_head_t client_head;
00035 
00046 static client_t *
00047 client_get (pconn_session_t *session)
00048 {
00049     client_t *client;
00050 
00051     LIST_FOREACH(client, &client_head, entry) {
00052         if (client->session == session) {
00053             return client;
00054         }
00055     }
00056     return NULL;
00057 }
00058 
00066 static int
00067 client_add (pconn_session_t *session)
00068 {
00069     client_t *client;
00070 
00071     if (client_get(session)) {
00072         EQ2_LOG(TRACE_LOG_ERR, "%s: Client exists!", __func__);
00073         return -1;
00074     }
00075 
00076     client = calloc(1, sizeof(client_t));
00077     INSIST_ERR(client != NULL);
00078     client->session = session;
00079     pconn_session_get_peer_info(session, &client->info);
00080     LIST_INSERT_HEAD(&client_head, client, entry);
00081     return 0;
00082 }
00083 
00091 static void
00092 client_del (pconn_session_t *session)
00093 {
00094     client_t *client;
00095 
00096     client = client_get(session);
00097     if (client) {
00098         LIST_REMOVE(client, entry);
00099         if (client->msg) {
00100             free(client->msg);
00101         }
00102         free(client);
00103     } else {
00104         EQ2_LOG(TRACE_LOG_ERR, "%s: Client doesn't exist!", __func__);
00105     }
00106 }
00107 
00112 static void
00113 server_event_hdlr (pconn_session_t *session, pconn_event_t event,
00114         void *cookie __unused)
00115 {
00116     switch (event) {
00117     case PCONN_EVENT_ESTABLISHED:
00118         /* A client is connected. */
00119         EQ2_TRACE(EQ2_TRACEFLAG_NORMAL, "%s: Client 0x%x is connected.",
00120                 __func__, session);
00121         client_add(session);
00122         break;
00123     case PCONN_EVENT_SHUTDOWN:
00124         /* A client is dwon. */
00125         EQ2_TRACE(EQ2_TRACEFLAG_NORMAL, "%s: Client 0x%x is gone.",
00126                 __func__, session);
00127         client_del(session);
00128         break;
00129     default:
00130         EQ2_LOG(TRACE_LOG_ERR, "%s: Invalid event %d!", __func__, event);
00131     }
00132 }
00133 
00138 static status_t
00139 server_msg_hdlr (pconn_session_t *session, ipc_msg_t *msg,
00140         void *cookie __unused)
00141 {
00142     client_t *client;
00143 
00144     if (msg->subtype != EQ2_BALANCE_MSG_SVR_GROUP) {
00145         EQ2_LOG(TRACE_LOG_ERR, "%s: Unrecoganized message type!", __func__);
00146         return -1;
00147     }
00148     client = client_get(session);
00149     if (client == NULL) {
00150         EQ2_LOG(TRACE_LOG_ERR, "%s: Client doesn't exist!", __func__);
00151         return -1;
00152     }
00153 
00154     /* Free old message. */
00155     if (client->msg) {
00156         free(client->msg);
00157     }
00158 
00159     EQ2_TRACE(EQ2_TRACEFLAG_NORMAL, "%s: Received message len %d.",
00160             __func__, msg->length);
00161     client->msg = malloc(msg->length);
00162     INSIST_ERR(client->msg != NULL);
00163     bcopy(msg->data, client->msg, msg->length);
00164     return 0;
00165 }
00166 
00177 client_t *
00178 client_get_next (client_t *client)
00179 {
00180     if (client == NULL) {
00181         return LIST_FIRST(&client_head);
00182     } else {
00183         return LIST_NEXT(client, entry);
00184     }
00185 }
00186 
00191 int
00192 server_open (evContext ctx)
00193 {
00194     pconn_server_params_t param;
00195 
00196     LIST_INIT(&client_head);
00197 
00198     bzero(&param, sizeof(param));
00199     param.pconn_port = EQ2_MGMT_SERVER_PORT;
00200     param.pconn_event_handler = server_event_hdlr;
00201 
00202     server_hdl = pconn_server_create(&param, ctx, server_msg_hdlr, NULL);
00203     if (server_hdl == NULL) {
00204         EQ2_LOG(TRACE_LOG_ERR, "%s: Create server ERROR!", __func__);
00205         return -1;
00206     } else {
00207         EQ2_TRACE(EQ2_TRACEFLAG_NORMAL, "%s: Create server OK.", __func__);
00208         return 0;
00209     }
00210 }
00211 
00216 void
00217 server_close (void)
00218 {
00219     pconn_server_shutdown(server_hdl);
00220 }
00221 

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 Equilibrium II: equilibrium2-mgmt 1.0 by Doxygen 1.5.1