2 Copyright (C) 2016-2018 Ben Kibbey <bjk@luxsci.net>
4 This file is part of libpwmd.
6 Libpwmd is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
11 Libpwmd is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Libpwmd. If not, see <http://www.gnu.org/licenses/>.
29 static char *socket
, *file
, *cmd
;
31 static pthread_mutex_t count_mutex
= PTHREAD_MUTEX_INITIALIZER
;
34 static int lock_timeout
;
38 status_cb (void *data
, const char *line
)
40 pthread_t tid
= pthread_self ();
43 fprintf(stdout
, "STATUS: %p: %s\n", (pthread_t
*)tid
, line
);
52 pthread_t tid
= pthread_self ();
56 fprintf(stdout
, "START: %p\n", (pthread_t
*)tid
);
57 rc
= pwmd_new ("test-client", &pwm
);
61 pwmd_setopt (pwm
, PWMD_OPTION_LOCK_ON_OPEN
, lock
);
62 pwmd_setopt (pwm
, PWMD_OPTION_STATUS_CB
, status_cb
);
64 rc
= pwmd_connect (pwm
, socket
, NULL
);
70 rc
= pwmd_command (pwm
, NULL
, NULL
, NULL
, NULL
, "OPTION lock-timeout=%i",
78 rc
= pwmd_command (pwm
, NULL
, NULL
, NULL
, NULL
, "OPTION client-state=%i",
84 rc
= pwmd_process (pwm
);
90 rc
= pwmd_open (pwm
, file
, NULL
, NULL
);
95 rc
= pwmd_process (pwm
);
99 rc
= pwmd_command (pwm
, &result
, NULL
, NULL
, NULL
, cmd
);
104 fprintf(stdout
, "RESULT: %p: '%s'\n", (pthread_t
*) tid
, result
);
106 rc
= pwmd_process (pwm
);
112 fprintf(stdout
, "ERROR: %p: %u: %s\n", (pthread_t
*)tid
, rc
,
115 pthread_mutex_lock (&count_mutex
);
117 pthread_mutex_unlock (&count_mutex
);
119 fprintf(stdout
, "END: %p\n", (pthread_t
*)tid
);
125 usage (const char *pn
, int status
)
127 fprintf(status
== EXIT_SUCCESS
? stdout
: stderr
,
128 "Usage: %s [-hsnq] [-l N] <clients> <socket> [<datafile>] '<command_with_args>'\n", pn
);
129 fprintf(status
== EXIT_SUCCESS
? stdout
: stderr
,
130 " -n don't lock data file mutex\n"
131 " -l N lock timeout in tenths of a second\n"
132 " -q quiet (can be used more than once)\n"
133 " -s receive client state status messages\n"
134 " -h this help text\n");
138 int main(int argc
, char **argv
)
147 while ((opt
= getopt (argc
, argv
, "hnsl:q")) != -1)
155 lock_timeout
= atoi (optarg
);
164 usage (argv
[0], EXIT_SUCCESS
);
169 if (argc
- optind
< 3)
170 usage (argv
[0], EXIT_FAILURE
);
172 nclients
= atoi (argv
[optind
++]);
173 socket
= strdup (argv
[optind
++]);
175 if ((argc
-optind
) > 1)
176 file
= strdup (argv
[optind
++]);
178 cmd
= strdup (argv
[optind
++]);
180 fprintf(stdout
, "Doing: '%s'\n", cmd
);
182 pthread_attr_init (&attr
);
183 pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
);
185 for (i
= 0; i
< nclients
; i
++)
188 int e
= pthread_create (&tid
, NULL
, start
, NULL
);
193 fprintf(stdout
, "%s\n", strerror (e
));
197 pthread_mutex_lock (&count_mutex
);
199 pthread_mutex_unlock (&count_mutex
);
202 pthread_attr_destroy (&attr
);
208 pthread_mutex_lock (&count_mutex
);
210 pthread_mutex_unlock (&count_mutex
);
213 fprintf(stdout
, "%i clients remain ...\n", n
);