Back to index

im-sdk  12.3.91
input-method.c
Go to the documentation of this file.
00001 #include <stdlib.h>
00002 
00003 #include <iiimp.h>
00004 
00005 #include "input-method.h"
00006 #include "input-context.h"
00007 #include "handler.h"
00008 #include "stream.h"
00009 #include "role-data-client.h"
00010 #include "role-data-server.h"
00011 
00012 
00013 IIIMF_status
00014 iiimf_im_new(
00015     IIIMF_role              role,
00016     const char *     server_node,
00017     const char *     service,
00018     void *           client_data,
00019     IIIMF_im **             im_ret)
00020 {
00021     IIIMF_im *              im;
00022     IIIMF_status     status;
00023     size_t           size;
00024 
00025     im = (IIIMF_im *)malloc(sizeof (IIIMF_im));
00026     if (NULL == im) return IIIMF_STATUS_MALLOC;
00027 
00028     im->im_id = -1;
00029 
00030     im->data_s = NULL;
00031 
00032     im->role = role;
00033     im->state = 0;
00034 
00035     im->message_pending = NULL;
00036     im->message_depth = 0;
00037     im->message_depth_maximum = 8;
00038 
00039     im->role_data.client = NULL;
00040     im->role_data.server = NULL;
00041 
00042     im->message_handler_call = iiimf_message_handler_call;
00043     im->message_handler = NULL;
00044 
00045     im->ic_list = NULL;
00046 
00047     im->stream = NULL;
00048 
00049     im->client_data = client_data;
00050 
00051     im->data_s = iiimp_data_s_new();
00052     if (NULL == im->data_s) {
00053        iiimf_im_delete(im);
00054        return IIIMF_STATUS_FAIL;
00055     }
00056 
00057     size = ((sizeof (IIIMF_message_pending)) * im->message_depth_maximum);
00058     im->message_pending = (IIIMF_message_pending *)malloc(size);
00059     if (NULL == im->message_pending) {
00060        iiimf_im_delete(im);
00061        return IIIMF_STATUS_MALLOC;
00062     }
00063 
00064     switch (role) {
00065     case IIIMF_ROLE_CLIENT:
00066        status = iiimf_role_data_client_new(server_node, service,
00067                                        &(im->role_data.client));
00068        if (IIIMF_STATUS_SUCCESS != status) {
00069            iiimf_im_delete(im);
00070            return status;
00071        }
00072        break;
00073     case IIIMF_ROLE_SERVER:
00074        status = iiimf_role_data_server_new(service, &(im->role_data.server));
00075        if (IIIMF_STATUS_SUCCESS != status) {
00076            iiimf_im_delete(im);
00077            return status;
00078        }
00079        break;
00080     default:
00081        iiimf_im_delete(im);
00082        return IIIMF_STATUS_ARGUMENT;
00083     }
00084 
00085     status = iiimf_message_handler_entry_new(&(im->message_handler));
00086     if (IIIMF_STATUS_SUCCESS != status) {
00087        iiimf_im_delete(im);
00088        return status;
00089     }
00090 
00091     status = iiimf_stream_new(role, &(im->stream));
00092     if (IIIMF_STATUS_SUCCESS != status) {
00093        iiimf_im_delete(im);
00094        return status;
00095     }
00096 
00097     *im_ret = im;
00098 
00099     return IIIMF_STATUS_SUCCESS;
00100 }
00101 
00102 
00103 void
00104 iiimf_im_delete(IIIMF_im * im)
00105 {
00106     IIIMF_ic *       ic;
00107     IIIMF_ic *       ic_next;
00108 
00109     for (ic = im->ic_list; NULL != ic; ic = ic_next) {
00110        ic_next = ic->next;
00111        iiimf_ic_delete(ic);
00112     }
00113 
00114     switch (im->role) {
00115     case IIIMF_ROLE_CLIENT:
00116        iiimf_role_data_client_delete(im->role_data.client);
00117        break;
00118     case IIIMF_ROLE_SERVER:
00119        iiimf_role_data_server_delete(im->role_data.server);
00120        break;
00121     }
00122 
00123     iiimf_stream_delete(im->stream);
00124     iiimf_message_handler_entry_delete(im->message_handler);
00125     free(im->message_pending);
00126     iiimp_data_s_delete(im->data_s);
00127 
00128     free(im);
00129 }
00130 
00131 
00132 void *
00133 iiimf_im_data_get(IIIMF_im * im)
00134 {
00135     return im->client_data;
00136 }
00137 
00138 
00139 void *
00140 iiimf_im_data_set(IIIMF_im * im, void * client_data)
00141 {
00142     void *    p;
00143     p = im->client_data;
00144     im->client_data = client_data;
00145     return p;
00146 }
00147 
00148 
00149 IIIMP_data_s *
00150 iiimf_im_data_s(IIIMF_im * im)
00151 {
00152     return im->data_s;
00153 }
00154 
00155 
00156 IIIMF_status
00157 iiimf_im_stream_proc_set(
00158     IIIMF_im *                     im,
00159     IIIMF_stream_proc_open  proc_open,
00160     IIIMF_stream_proc_accept       proc_accept,
00161     IIIMF_stream_proc_read  proc_read,
00162     IIIMF_stream_proc_write proc_write,
00163     IIIMF_stream_proc_close proc_close,
00164     IIIMF_stream_proc_data *       proc_data)
00165 {
00166     return iiimf_stream_proc_set(im,
00167                              proc_open, proc_accept, proc_read,
00168                              proc_write, proc_close, proc_data);
00169 }
00170 
00171 
00172 IIIMF_status
00173 iiimf_im_fd_set(IIIMF_im * im, int fd)
00174 {
00175     return iiimf_stream_fd_set(im->stream, fd);
00176 }
00177 
00178 
00179 IIIMF_status
00180 iiimf_im_timeout_set(IIIMF_im * im, int timeout)
00181 {
00182     return iiimf_stream_timeout_set(im->stream, timeout);
00183 }
00184 
00185 
00186 IIIMF_status
00187 iiimf_im_start(IIIMF_im * im)
00188 {
00189     return im->stream->proc_open(im, im->stream->proc_data);
00190 }
00191 
00192 
00193 IIIMF_status
00194 iiimf_im_end(IIIMF_im * im)
00195 {
00196     return im->stream->proc_close(im->stream->proc_data);
00197 }
00198 
00199 
00200 IIIMF_status
00201 iiimf_im_server_accept(IIIMF_im * im, void * client_data, IIIMF_im ** im_ret)
00202 {
00203     return im->stream->proc_accept(im, im->stream->proc_data, client_data,
00204                                im_ret);
00205 }
00206 
00207 
00208 int
00209 iiimf_im_id(IIIMF_im * im)
00210 {
00211     return im->im_id;
00212 }
00213 
00214 
00215 /* Local Variables: */
00216 /* c-file-style: "iiim-project" */
00217 /* End: */