1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2010 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file contains implementation of the "change app state" command
9 **************************************************************************
11 * LADI Session Handler is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * LADI Session Handler is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
23 * or write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include "../dbus/error.h"
31 #include "../proxies/notify_proxy.h"
32 #include "virtualizer.h"
34 struct ladish_command_change_app_state
36 struct ladish_command command
; /* must be the first member */
39 unsigned int target_state
;
40 const char * target_state_description
;
41 bool (* run_target
)(struct ladish_command_change_app_state
*, ladish_app_supervisor_handle
, ladish_app_handle
);
42 void (* initiate_stop
)(ladish_app_handle app
);
45 static bool run_target_start(struct ladish_command_change_app_state
* cmd_ptr
, ladish_app_supervisor_handle supervisor
, ladish_app_handle app
)
47 ASSERT(cmd_ptr
->command
.state
== LADISH_COMMAND_STATE_PENDING
);
48 ASSERT(cmd_ptr
->initiate_stop
== NULL
);
50 if (!ladish_studio_is_started())
52 log_error("cannot start app because studio is not started", cmd_ptr
->opath
);
53 ladish_notify_simple(LADISH_NOTIFY_URGENCY_HIGH
, "Cannot start app because studio is not started", NULL
);
57 if (ladish_app_is_running(app
))
59 log_error("App %s is already running", ladish_app_get_name(app
));
60 ladish_notify_simple(LADISH_NOTIFY_URGENCY_HIGH
, "Cannot start app because it is already running", NULL
);
64 if (!ladish_app_supervisor_start_app(supervisor
, app
))
66 ladish_notify_simple(LADISH_NOTIFY_URGENCY_HIGH
, "Cannot start app (execution failed)", NULL
);
70 cmd_ptr
->command
.state
= LADISH_COMMAND_STATE_DONE
;
74 static bool run_target_stop(struct ladish_command_change_app_state
* cmd_ptr
, ladish_app_supervisor_handle supervisor
, ladish_app_handle app
)
76 const char * app_name
;
79 ASSERT(cmd_ptr
->initiate_stop
!= NULL
);
81 app_name
= ladish_app_get_name(app
);
82 ladish_app_get_uuid(app
, app_uuid
);
84 if (ladish_app_is_running(app
))
86 if (cmd_ptr
->command
.state
== LADISH_COMMAND_STATE_PENDING
)
88 cmd_ptr
->initiate_stop(app
);
89 cmd_ptr
->command
.state
= LADISH_COMMAND_STATE_WAITING
;
93 ASSERT(cmd_ptr
->command
.state
== LADISH_COMMAND_STATE_WAITING
);
94 log_info("Waiting '%s' process termination (%s)...", app_name
, cmd_ptr
->target_state_description
);
98 if (!ladish_virtualizer_is_hidden_app(ladish_studio_get_jack_graph(), app_uuid
, app_name
))
100 log_info("Waiting '%s' client disappear (%s)...", app_name
, cmd_ptr
->target_state_description
);
104 if (cmd_ptr
->command
.state
== LADISH_COMMAND_STATE_PENDING
)
106 log_info("App %s is already stopped (%s)", app_name
, cmd_ptr
->target_state_description
);
109 cmd_ptr
->command
.state
= LADISH_COMMAND_STATE_DONE
;
113 #define cmd_ptr ((struct ladish_command_change_app_state *)context)
115 static bool run(void * context
)
117 ladish_app_supervisor_handle supervisor
;
118 ladish_app_handle app
;
120 if (cmd_ptr
->command
.state
== LADISH_COMMAND_STATE_PENDING
)
122 log_info("%s app command. opath='%s'", cmd_ptr
->target_state_description
, cmd_ptr
->opath
);
125 supervisor
= ladish_studio_find_app_supervisor(cmd_ptr
->opath
);
126 if (supervisor
== NULL
)
128 log_error("cannot find supervisor '%s' to %s app", cmd_ptr
->opath
, cmd_ptr
->target_state_description
);
129 ladish_notify_simple(LADISH_NOTIFY_URGENCY_HIGH
, "Cannot change app state because of internal error (unknown supervisor)", NULL
);
133 app
= ladish_app_supervisor_find_app_by_id(supervisor
, cmd_ptr
->id
);
136 log_error("App with ID %"PRIu64
" not found (%s)", cmd_ptr
->id
, cmd_ptr
->target_state_description
);
137 ladish_notify_simple(LADISH_NOTIFY_URGENCY_HIGH
, "Cannot change app state because it is not found", NULL
);
141 return cmd_ptr
->run_target(cmd_ptr
, supervisor
, app
);
144 static void destructor(void * context
)
146 log_info("change_app_state command destructor");
147 free(cmd_ptr
->opath
);
152 bool ladish_command_change_app_state(void * call_ptr
, struct ladish_cqueue
* queue_ptr
, const char * opath
, uint64_t id
, unsigned int target_state
)
154 struct ladish_command_change_app_state
* cmd_ptr
;
157 opath_dup
= strdup(opath
);
158 if (opath_dup
== NULL
)
160 lash_dbus_error(call_ptr
, LASH_DBUS_ERROR_GENERIC
, "strdup('%s') failed.", opath
);
164 cmd_ptr
= ladish_command_new(sizeof(struct ladish_command_change_app_state
));
167 lash_dbus_error(call_ptr
, LASH_DBUS_ERROR_GENERIC
, "ladish_command_new() failed.");
168 goto fail_free_opath
;
171 cmd_ptr
->command
.run
= run
;
172 cmd_ptr
->command
.destructor
= destructor
;
173 cmd_ptr
->opath
= opath_dup
;
175 cmd_ptr
->target_state
= target_state
;
177 switch (target_state
)
179 case LADISH_APP_STATE_STARTED
:
180 cmd_ptr
->target_state_description
= "start";
181 cmd_ptr
->run_target
= run_target_start
;
182 cmd_ptr
->initiate_stop
= NULL
;
184 case LADISH_APP_STATE_STOPPED
:
185 cmd_ptr
->target_state_description
= "stop";
186 cmd_ptr
->run_target
= run_target_stop
;
187 cmd_ptr
->initiate_stop
= ladish_app_stop
;
189 case LADISH_APP_STATE_KILL
:
190 cmd_ptr
->target_state_description
= "kill";
191 cmd_ptr
->run_target
= run_target_stop
;
192 cmd_ptr
->initiate_stop
= ladish_app_kill
;
196 lash_dbus_error(call_ptr
, LASH_DBUS_ERROR_GENERIC
, "Invalid target state (internal error).", opath
);
197 goto fail_destroy_command
;
200 if (!ladish_cqueue_add_command(queue_ptr
, &cmd_ptr
->command
))
202 lash_dbus_error(call_ptr
, LASH_DBUS_ERROR_GENERIC
, "ladish_cqueue_add_command() failed.");
203 goto fail_destroy_command
;
208 fail_destroy_command
: