summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorfcolin2005-06-02 14:22:48 +0000
committerfcolin2005-06-02 14:22:48 +0000
commitfb9ff051afa822f2d99c167d4ac1a1ca0920b24c (patch)
treeedd2de6423e9110beebefcccc8c8209967441e14 /src
parent0741e17fd6d81351ace0583e30541c9ba0120a5d (diff)
downloadivy-c-fb9ff051afa822f2d99c167d4ac1a1ca0920b24c.zip
ivy-c-fb9ff051afa822f2d99c167d4ac1a1ca0920b24c.tar.gz
ivy-c-fb9ff051afa822f2d99c167d4ac1a1ca0920b24c.tar.bz2
ivy-c-fb9ff051afa822f2d99c167d4ac1a1ca0920b24c.tar.xz
gestion de la priorité entre application
Diffstat (limited to 'src')
-rw-r--r--src/ivy.c88
-rw-r--r--src/ivy.h3
-rw-r--r--src/ivyprobe.c1
3 files changed, 87 insertions, 5 deletions
diff --git a/src/ivy.c b/src/ivy.c
index c06bc2c..ca4d8b3 100644
--- a/src/ivy.c
+++ b/src/ivy.c
@@ -78,6 +78,7 @@ typedef enum {
Pong = 10, /* checks the presence of the other */
AddBinding = 11, /* other methods for binding message based on hash table */
DelBinding = 12, /* other methods for binding message based on hash table */
+ ApplicationId = 13, /* on start send my ID and priority */
} MsgType;
@@ -109,6 +110,7 @@ struct _clnt_lst {
MsgSndPtr msg_send; /* liste des requetes recues */
char *app_name; /* nom de l'application */
char *app_id; /* identificateur unique de l'application (time-ip-port) */
+ int priority; /* client priority */
unsigned short app_port; /* port de l'application */
};
@@ -155,6 +157,8 @@ static IvyClientPtr clients = 0;
static const char *ready_message = 0;
static char *applicationUniqueId = 0;
+static int applicationPriority = DEFAULT_PRIORITY;
+
/* get Current Time in milliseconds */
static long currentTime()
{
@@ -192,6 +196,59 @@ static int SplitArg( char *s, const char separator, char **argv )
}
return argc;
}
+/* returns < 0 if *p sorts lower than *q */
+static int keycmp (IvyClientPtr p, IvyClientPtr q)
+{
+ return p->priority - q->priority;
+}
+
+/* merge 2 lists under dummy head item */
+static IvyClientPtr lmerge (IvyClientPtr p, IvyClientPtr q)
+{
+ IvyClientPtr r;
+ struct _clnt_lst head;
+
+ for ( r = &head; p && q; )
+ {
+ if ( keycmp(p, q) < 0 )
+ {
+ r = r->next = p;
+ p = p->next;
+ }
+ else
+ {
+ r = r->next = q;
+ q = q->next;
+ }
+ }
+ r->next = (p ? p : q);
+ return head.next;
+}
+
+/* split list into 2 parts, sort each recursively, merge */
+static IvyClientPtr lsort (IvyClientPtr p)
+{
+ IvyClientPtr q, r;
+
+ if ( p )
+ {
+ q = p;
+ for ( r = q->next; r && (r = r->next) != NULL; r = r->next )
+ q = q->next;
+ r = q->next;
+ q->next = NULL;
+ if ( r )
+ p = lmerge(lsort(r), lsort(p));
+ }
+ return p;
+}
+
+
+static void SortClients()
+{
+ //TODO sort client list again priority!
+ lsort( clients );
+}
static void MsgSendTo( Client client, MsgType msgtype, int id, const char *message )
{
SocketSend( client, "%d%c%d%c%s%c",
@@ -593,7 +650,17 @@ static void Receive( Client client, void *data, char *line )
IvyCleanup();
exit(0);
break;
-
+ case ApplicationId:
+#ifdef DEBUG
+ printf("ApplicationId priority=%d appid='%s'\n", id, argv[ARG_0]);
+#endif //DEBUG
+ clnt->app_id = strdup( argv[ARG_0] );
+ if ( id != clnt->priority )
+ {
+ clnt->priority = id;
+ SortClients();
+ }
+ break;
default:
printf("Receive unhandled message %s\n", line);
break;
@@ -612,6 +679,8 @@ static IvyClientPtr SendService( Client client )
clnt->client = client;
clnt->app_name = strdup("Unknown");
clnt->app_port = 0;
+ clnt->priority = DEFAULT_PRIORITY;
+ MsgSendTo( client, ApplicationId, applicationPriority, applicationUniqueId );
MsgSendTo( client, StartRegexp, ApplicationPort, ApplicationName);
IVY_LIST_EACH(msg_recv, msg )
{
@@ -678,7 +747,7 @@ static void BroadcastReceive( Client client, void *data, char *line )
char *remotehost = 0;
#endif
- err = sscanf (line,"%d %hu %s %s\n", &version, &serviceport, appname, appid );
+ err = sscanf (line,"%d %hu %s %s\n", &version, &serviceport, appid, appname );
if ( err != 4 ) {
/* ignore the message */
unsigned short remoteport;
@@ -734,6 +803,16 @@ void IvyStop()
IvyChannelStop();
}
+void IvySetApplicationPriority( int priority )
+{
+ IvyClientPtr clnt;
+ applicationPriority = priority;
+ /* Send to already connected clients */
+ IVY_LIST_EACH (clients, clnt ) {
+ MsgSendTo( clnt->client, ApplicationId, applicationPriority, applicationUniqueId);
+ }
+}
+
void IvySetBindCallback(IvyBindCallback bind_callback, void *bind_data
)
{
@@ -839,8 +918,9 @@ void IvyStart (const char* bus)
SocketSendBroadcast (broadcast, mask, SupervisionPort, "%d %hu %s %s\n",
VERSION,
ApplicationPort,
- ApplicationName,
- applicationUniqueId);
+ applicationUniqueId,
+ ApplicationName
+ );
numelem = 0;
mask = 0xffffffff;
}
diff --git a/src/ivy.h b/src/ivy.h
index 7bf2b6e..10c7f86 100644
--- a/src/ivy.h
+++ b/src/ivy.h
@@ -25,6 +25,7 @@ extern "C" {
/* numero par default du bus */
#define DEFAULT_BUS 2010
+#define DEFAULT_PRIORITY 100;
typedef struct _clnt_lst *IvyClientPtr;
@@ -64,7 +65,7 @@ void IvyInit(
IvyDieCallback die_callback, /* last change callback before die */
void *die_data /* user data */
);
-
+void IvySetApplicationPriority( int priority );
void IvySetBindCallback(
IvyBindCallback bind_callback,
void *bind_data );
diff --git a/src/ivyprobe.c b/src/ivyprobe.c
index b643811..fd4e0cb 100644
--- a/src/ivyprobe.c
+++ b/src/ivyprobe.c
@@ -327,6 +327,7 @@ int main(int argc, char *argv[])
glutDisplayFunc(display);
#endif
IvyInit (agentname, agentready, ApplicationCallback,NULL,NULL,NULL);
+ IvySetApplicationPriority( 0 ); /* lower priority */
IvyBindDirectMsg( DirectCallback,NULL);
for (; optind < argc; optind++)
IvyBindMsg (Callback, NULL, argv[optind]);