+ if (plug->exit) {
+ plug->exit(plug);
+ } else if (plug->data) {
+ DEBUG1("Plugin %s lacks exit(). Free data anyway.",plug->name);
+ xbt_free(plug->data);
+ }
+
+ xbt_free(plug->name);
+ xbt_free(plug);
+ }
+}
+
+
+/**
+ * gras_trp_socket_new:
+ *
+ * Malloc a new socket, and initialize it with defaults
+ */
+void gras_trp_socket_new(int incoming,
+ gras_socket_t *dst) {
+
+ gras_socket_t sock=xbt_new0(s_gras_socket_t,1);
+
+ DEBUG1("Create a new socket (%p)", (void*)sock);
+
+ sock->plugin = NULL;
+ sock->sd = -1;
+ sock->data = NULL;
+
+ sock->incoming = incoming ? 1:0;
+ sock->outgoing = incoming ? 0:1;
+ sock->accepting = incoming ? 1:0;
+
+ sock->port = -1;
+ sock->peer_port = -1;
+ sock->peer_name = NULL;
+ sock->raw = 0;
+
+ *dst = sock;
+
+ xbt_dynar_push(gras_socketset_get(),dst);
+}
+
+
+/**
+ * gras_socket_server_ext:
+ *
+ * Opens a server socket and make it ready to be listened to.
+ * In real life, you'll get a TCP socket.
+ */
+xbt_error_t
+gras_socket_server_ext(unsigned short port,
+
+ unsigned long int bufSize,
+ int raw,
+
+ /* OUT */ gras_socket_t *dst) {
+
+ xbt_error_t errcode;
+ gras_trp_plugin_t *trp;
+ gras_socket_t sock;
+
+ *dst = NULL;
+
+ DEBUG2("Create a server socket from plugin %s on port %d",
+ gras_if_RL() ? "tcp" : "sg",
+ port);
+ TRY(gras_trp_plugin_get_by_name("buf",&trp));
+
+ /* defaults settings */
+ gras_trp_socket_new(1,&sock);
+ sock->plugin= trp;
+ sock->port=port;
+ sock->bufSize = bufSize;
+ sock->raw = raw;
+
+ /* Call plugin socket creation function */
+ errcode = trp->socket_server(trp, sock);
+ DEBUG3("in=%c out=%c accept=%c",
+ sock->incoming?'y':'n',
+ sock->outgoing?'y':'n',
+ sock->accepting?'y':'n');
+
+ if (errcode != no_error) {
+ xbt_free(sock);
+ return errcode;
+ }
+
+ *dst = sock;
+
+ return no_error;
+}
+
+/**
+ * gras_socket_client_ext:
+ *
+ * Opens a client socket to a remote host.
+ * In real life, you'll get a TCP socket.
+ */
+xbt_error_t
+gras_socket_client_ext(const char *host,
+ unsigned short port,
+
+ unsigned long int bufSize,
+ int raw,
+
+ /* OUT */ gras_socket_t *dst) {
+
+ xbt_error_t errcode;
+ gras_trp_plugin_t *trp;
+ gras_socket_t sock;
+
+ *dst = NULL;
+
+ TRY(gras_trp_plugin_get_by_name("buf",&trp));
+
+ DEBUG1("Create a client socket from plugin %s",gras_if_RL() ? "tcp" : "sg");
+ /* defaults settings */
+ gras_trp_socket_new(0,&sock);
+ sock->plugin= trp;
+ sock->peer_port = port;
+ sock->peer_name = (char*)strdup(host?host:"localhost");
+ sock->bufSize = bufSize;
+ sock->raw = raw;
+
+ /* plugin-specific */
+ errcode= (*trp->socket_client)(trp, sock);
+ DEBUG3("in=%c out=%c accept=%c",
+ sock->incoming?'y':'n',
+ sock->outgoing?'y':'n',
+ sock->accepting?'y':'n');