aboutsummaryrefslogtreecommitdiff
path: root/ext/ivy/ivy.c
blob: aea51c74248d80e6a9b080c9a54f8d000380f152 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
 * Copyright (C) 2007 Gregoire Lejeune <gregoire.lejeune@free.fr>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 */

#include "rb_ivy.h"

void ruby_ivy_free( RbTIvy *pRbTIvy ) {
  if (pRbTIvy != NULL)
    free(pRbTIvy);
}

void ruby_ivy_mark( RbTIvy *pRbTIvy ) {
  if( pRbTIvy == NULL ) return;
  if( !NIL_P(pRbTIvy->strAgentName) ) rb_gc_mark( pRbTIvy->strAgentName );
  if( !NIL_P(pRbTIvy->strReadyMsg) ) rb_gc_mark( pRbTIvy->strReadyMsg );
}

VALUE ruby_ivy_new( VALUE class ) {
	
  RbTIvy *pRbTIvy;

  pRbTIvy = (RbTIvy *)malloc(sizeof(RbTIvy));
  if( pRbTIvy == NULL )
    rb_raise(rb_eNoMemError, "No memory left for IVY struct");

  return( Data_Wrap_Struct( class, ruby_ivy_mark, ruby_ivy_free, pRbTIvy ) );
}

/** o = IVY.new( strAgentName, [strReadyMessage] ) */
static VALUE ruby_ivy_initialize( int iNbArgs, VALUE *vArgs, VALUE class ) {
	
  RbTIvy *pRbTIvy;
  Data_Get_Struct( class, RbTIvy, pRbTIvy );

  if( iNbArgs < 1 || iNbArgs > 2 ) {
	rb_raise( rb_eArgError, "wrong number of arguments (%d for 1)", iNbArgs );
  }

  pRbTIvy->strAgentName = vArgs[0];
  if( iNbArgs == 2 ) {
	pRbTIvy->strReadyMsg = vArgs[1];
  } else {
	pRbTIvy->strReadyMsg = Qnil;
  }

  IvyInit( STR2CSTR( pRbTIvy->strAgentName ), NULL, NULL, NULL, NULL, NULL );

  return( class );
}

/** ------------------------------------------------------------------------ */

void ruby_ivy_message_free( RbTIvyMsg *pRbTIvyMsg ) {
  if (pRbTIvyMsg != NULL)
    free(pRbTIvyMsg);
}

void ruby_ivy_message_mark( RbTIvyMsg *pRbTIvyMsg ) {
  if( pRbTIvyMsg == NULL ) return;
  if( !NIL_P(pRbTIvyMsg->cb) ) rb_gc_mark( pRbTIvyMsg->cb );
  if( !NIL_P(pRbTIvyMsg->data) ) rb_gc_mark( pRbTIvyMsg->data );
  if( !NIL_P(pRbTIvyMsg->regexp) ) rb_gc_mark( pRbTIvyMsg->regexp );
}

/** o = IVY::Message.new() */
// VALUE ruby_ivy_message_new( VALUE class, VALUE cb, VALUE data, VALUE regexp ) {
VALUE ruby_ivy_message_new( VALUE class, VALUE data, VALUE regexp ) {
  RbTIvyMsg *pRbTIvyMsg;
  
  pRbTIvyMsg = (RbTIvyMsg *)malloc(sizeof(RbTIvyMsg));
  if( pRbTIvyMsg == NULL )
    rb_raise(rb_eNoMemError, "No memory left for IVY::MESSAGE struct");

  pRbTIvyMsg->cb     = rb_block_proc( );
  pRbTIvyMsg->data   = data;
  pRbTIvyMsg->regexp = regexp;
  pRbTIvyMsg->id = IvyBindMsg( callback, (void*)pRbTIvyMsg, STR2CSTR( pRbTIvyMsg->regexp ) );
  
  return( Data_Wrap_Struct( class, ruby_ivy_message_mark, ruby_ivy_message_free, pRbTIvyMsg ) );
}

/** ------------------------------------------------------------------------ */

void ruby_ivy_client_free( RbTIvyClient *pRbTIvyClient ) {
  if (pRbTIvyClient != NULL)
    free(pRbTIvyClient);
}

void ruby_ivy_client_mark( RbTIvyClient *pRbTIvyClient ) {
  if( pRbTIvyClient == NULL ) return;
}

VALUE ruby_ivy_client_new_internal( VALUE class, IvyClientPtr client ) {
  RbTIvyClient *pRbTIvyClient;
  
  pRbTIvyClient = (RbTIvyClient *)malloc(sizeof(RbTIvyClient));
  if( pRbTIvyClient == NULL )
    rb_raise(rb_eNoMemError, "No memory left for IVY::Client struct");

  pRbTIvyClient->client = client;
  
  return( Data_Wrap_Struct( class, ruby_ivy_client_mark, ruby_ivy_client_free, pRbTIvyClient ) );
}

/** o = IVY::Client.new( xClientName ) */
VALUE ruby_ivy_client_new( VALUE class, VALUE clientName ) {
  RbTIvyClient *pRbTIvyClient;
  
  pRbTIvyClient = (RbTIvyClient *)malloc(sizeof(RbTIvyClient));
  if( pRbTIvyClient == NULL )
    rb_raise(rb_eNoMemError, "No memory left for IVY::Client struct");

  pRbTIvyClient->client = IvyGetApplication( STR2CSTR( clientName ) );
  
  return( Data_Wrap_Struct( class, ruby_ivy_client_mark, ruby_ivy_client_free, pRbTIvyClient ) );
}

/** ------------------------------------------------------------------------ */

VALUE ruby_ivy_appname( VALUE class ) {
	RbTIvyClient *pRbTIvyClient;
	char *xAppName = NULL;
	
	Data_Get_Struct( class, RbTIvyClient, pRbTIvyClient );
	
	if( pRbTIvyClient->client != NULL )
		xAppName = IvyGetApplicationName( pRbTIvyClient->client );
	
	return( rb_str_new2( xAppName ) );
}

VALUE ruby_ivy_apphost( VALUE class ) {
	RbTIvyClient *pRbTIvyClient;
	char *xAppHost = NULL;
	
	Data_Get_Struct( class, RbTIvyClient, pRbTIvyClient );
	
	if( pRbTIvyClient->client != NULL )
		xAppHost = IvyGetApplicationHost( pRbTIvyClient->client );
	
	return( rb_str_new2( xAppHost ) );
}

/** ------------------------------------------------------------------------ */

VALUE ruby_ivy_start( int iNbArgs, VALUE *vArgs, VALUE class ) {
	RbTIvy *pRbTIvy;
  	const char *bus = NULL;

	Data_Get_Struct( class, RbTIvy, pRbTIvy );
	
	if( iNbArgs == 0 ) {
		bus = getenv( "IVYBUS" );
	} else if( iNbArgs == 1 ) {
		bus = STR2CSTR( vArgs[0] );
	} else {
		rb_raise( rb_eArgError, "wrong number of arguments (%d for 1)", iNbArgs );
	}
	
	IvyStart( bus );
	
	return( Qnil );
}

VALUE ruby_ivy_mainloop( VALUE class ) {
	RbTIvy *pRbTIvy;
  	Data_Get_Struct( class, RbTIvy, pRbTIvy );
	
	IvyMainLoop( );
	
	return( Qnil );
}

VALUE ruby_ivy_stop( VALUE class ) {
	RbTIvy *pRbTIvy;
  	Data_Get_Struct( class, RbTIvy, pRbTIvy );
	
	IvyStop( );
	
	return( Qnil );
}

VALUE ruby_ivy_sendmsg( VALUE class, VALUE message ) {
	RbTIvy *pRbTIvy;
  	Data_Get_Struct( class, RbTIvy, pRbTIvy );
	
	IvySendMsg( STR2CSTR( message ) );
	
	return( Qnil );
}

VALUE ruby_ivy_applist( VALUE class ) {
	RbTIvy *pRbTIvy;
	char *xAppList = NULL;
	VALUE aryResult = rb_ary_new();
	
  	Data_Get_Struct( class, RbTIvy, pRbTIvy );
	
	xAppList = IvyGetApplicationList( "|" );
	xAppList = strtok( xAppList, "|" );
	while( xAppList ) {
		rb_ary_push( aryResult, rb_str_new2( xAppList ) );
		xAppList = strtok (NULL, " ");
	}
	return( aryResult );
}

/** ------------------------------------------------------------------------ */

// VALUE ruby_ivy_bindmsg( VALUE class, VALUE data, VALUE regexp ) {
VALUE ruby_ivy_bindmsg( VALUE class, VALUE data, VALUE regexp ) {
	VALUE method;
	RbTIvy *pRbTIvy;
  	
	Data_Get_Struct( class, RbTIvy, pRbTIvy );
	
	return( ruby_ivy_message_new( cIvyMsg, data, regexp ) );
}

VALUE ruby_ivy_unbindmsg( VALUE class ) {
	RbTIvyMsg *pRbTIvyMsg;
	Data_Get_Struct( class, RbTIvyMsg, pRbTIvyMsg );
	
	if( pRbTIvyMsg->id != NULL ) { 
		IvyUnbindMsg( pRbTIvyMsg->id );
		pRbTIvyMsg->id = NULL;
	} else {
		rb_warn( "IVY::Message not binded!" );
	}
	
	return( Qnil );
}

/** ------------------------------------------------------------------------ */

static void callback( IvyClientPtr app, void *data, int argc, char **argv ) {
	RbTIvyMsg *pRbTIvyMsg = (RbTIvyMsg *)data;
	
	int callID = rb_intern ("call");
	VALUE res;
	
	int i;
	VALUE *aryArgs;
	
	aryArgs = (VALUE *)malloc( sizeof( VALUE ) );
	
	aryArgs[0] = ruby_ivy_client_new_internal( cIvyClient, app ); // IvyClientPtr app
	aryArgs[1] = pRbTIvyMsg->data; // void *data
	aryArgs[2] = rb_ary_new();
	for( i = 0; i < argc; i++ ) {
		rb_ary_push( aryArgs[2], rb_str_new2( argv[i] ) );
	}
	
	res = rb_funcall2( pRbTIvyMsg->cb, callID, 3, aryArgs );
}

/** ------------------------------------------------------------------------ */

void Init_ivy( void ) {
  cIvy = rb_define_class( "IVY", rb_cObject );
  cIvyMsg = rb_define_class_under( cIvy, "Message", rb_cObject );
  cIvyClient = rb_define_class_under( cIvy, "Client", rb_cObject );

  rb_define_const( cIvy, "RB_IVY_VERSION", rb_str_new2(RUBY_RBIVY_VERSION) );
  rb_define_const( cIvy, "IVY_VERSION", rb_str_new2(RUBY_IVY_VERSION) );
  
  rb_define_alloc_func( cIvy, ruby_ivy_new );
  rb_define_private_method( cIvy, "initialize", ruby_ivy_initialize, -1);

	// IVY
  rb_define_method( cIvy, "start",   			ruby_ivy_start,		-1 );
  rb_define_method( cIvy, "mainloop",			ruby_ivy_mainloop,	0 );
  rb_define_method( cIvy, "stop",       		ruby_ivy_stop,		0 );
  rb_define_method( cIvy, "sendmsg",			ruby_ivy_sendmsg,	1 );
  rb_define_method( cIvy, "applicationList",	ruby_ivy_applist,	0 );
  rb_define_method( cIvy, "bindmsg",			ruby_ivy_bindmsg,	2 );  // renvoi un message de type IVY::MESSAGE
  
	// IVY::Message
  rb_define_method( cIvyMsg,	"unbindmsg",	ruby_ivy_unbindmsg,	0 );

	// IVY::Client
  rb_define_singleton_method( cIvyClient, "new", ruby_ivy_client_new, 1 );
  rb_define_method( cIvyClient,	"applicationName", ruby_ivy_appname, 0);
  rb_define_method( cIvyClient,	"applicationHost", ruby_ivy_apphost, 0);
}