summaryrefslogtreecommitdiff
path: root/Ivy/IvyCbindings.cxx
blob: abe94cffa0dac473bcc926a9a9e3130a43d8b3a0 (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

#include "IvyStdAfx.h"
#include <stdarg.h>
#include "Ivy.h"
#include "IvyApplication.h"

#include "IvyCbindings.h"

static Ivy *bus = NULL;
static bool synchronous = false;

// application callback wrappers
static IvyCApplicationCallback app_cb = NULL;
static void * app_user_data = NULL;
static void app_conn( IvyApplication *app )
{
	if ( app_cb )
		(*app_cb)(app, app_user_data, IvyApplicationConnected );
}
static void app_discon( IvyApplication *app )
{
	if ( app_cb )
		(*app_cb)(app, app_user_data, IvyApplicationDisconnected );
}
#ifdef USE_MESSAGE_MAINLOOP
static void MyMainLoop()
{
	MSG msg;
    BOOL bRet;
	while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
    { 
        if (bRet == -1)
        {
            // handle the error and possibly exit
			break;
        }
        else
        {
            TranslateMessage(&msg); 
            DispatchMessage(&msg); 
        }
    } 
}
#endif
void IvyInit(
	 const char *AppName,		/* nom de l'application */
	 const char *ready,		/* ready Message peut etre NULL */
	 IvyCApplicationCallback callback, /* callback appele sur connection deconnection d'une appli */
	 void *data,			/* user data passe au callback */
	 IvyCDieCallback die_callback,	/* last change callback before die */
	 void *die_data	)		/* user data */
{
	bus = new Ivy(AppName, ready, BUS_APPLICATION_CALLBACK(app_conn,app_discon) , synchronous );
}
/* filtrage des regexps */
void IvySetFilter( int argc, const char **argv)
{
	bus->SetFilter( argc, argv );
}

void IvyStart (const char* domain)
{
	bus->start(domain);
}
void IvyStop ()
{
	bus->stop();
}

/* query sur les applications connectees */
const char *IvyGetApplicationName( IvyClientPtr app )
{
	return ((IvyApplication*)app)->GetName();
}
const char *IvyGetApplicationHost( IvyClientPtr app )
{
	ivy::string host;
	UINT port;
	((IvyApplication*)app)->GetPeerName(host,port);
	return host.c_str();
}
IvyClientPtr IvyGetApplication( char *name )
{
	return NULL;
}
const char *IvyGetApplicationList()
{
	return "Not yiet implemented";
}
const char **IvyGetApplicationMessages( IvyClientPtr app)
{
	return NULL;
}

MsgRcvPtr IvyBindMsg( IvyCMsgCallback callback, void *user_data, const char *fmt_regexp, ... )
{
	int count;
	char buf_regexp[2048];
	va_list  args;
	va_start( args, fmt_regexp );
	_vsnprintf_s( buf_regexp, sizeof(buf_regexp), sizeof(buf_regexp)-1, fmt_regexp, args );
	count = bus->BindMsg(buf_regexp, BUS_CALLBACK( ((IvyMessageCallbackFunction::IvyMessageCallback_fun)callback), user_data ) );
	va_end( args );
	return count;
}
void IvyUnbindMsg( MsgRcvPtr id )
{
	bus->UnbindMsg( id );
}

/* emission d'un message d'erreur */
void IvySendError( IvyClientPtr app, int id, const char *fmt, ... )
{
	char buf[2048];
	va_list  args;
	va_start( args, fmt );
	_vsnprintf_s( buf, sizeof(buf), sizeof(buf)-1, fmt, args );
	((IvyApplication*)app)->SendMsg( IvyApplication::Error, id, buf );
	va_end( args );
}

/* emmission d'un message die pour terminer l'application */
void IvySendDieMsg( IvyClientPtr app )
{
	((IvyApplication*)app)->SendMsg( IvyApplication::Die, 0 );
}

/* emission d'un message retourne le nb effectivement emis */

int IvySendMsg( const char *fmt_message, ... )
{
	int count;
	char buf[2048];
	va_list  args;
	va_start( args, fmt_message );
	_vsnprintf_s( buf, sizeof(buf), sizeof(buf)-1, fmt_message, args );
	count = bus->SendMsg(buf);
	va_end( args );
	return count;
}

void IvyBindDirectMsg( IvyCMsgDirectCallback callback, void *user_data)
{
}
void IvySendDirectMsg( IvyClientPtr app, int id, char *msg )
{
}

void IvyMainLoop()
{
#ifdef USE_MESSAGE_MAINLOOP
	if ( synchronous )
		MyMainLoop();
	else
#endif
		Sleep( INFINITE );
}