1%%%
2%%%  Copyright 2011 © INRIA
3%%%
4%%%  Author : Nicolas Niclausse <nicolas.niclausse@inria.fr>
5%%%  Created: 4 mai 2011 by Nicolas Niclausse <nicolas.niclausse@inria.fr>
6%%%
7%%%  This program is free software; you can redistribute it and/or modify
8%%%  it under the terms of the GNU General Public License as published by
9%%%  the Free Software Foundation; either version 2 of the License, or
10%%%  (at your option) any later version.
11%%%
12%%%  This program is distributed in the hope that it will be useful,
13%%%  but WITHOUT ANY WARRANTY; without even the implied warranty of
14%%%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15%%%  GNU General Public License for more details.
16%%%
17%%%  You should have received a copy of the GNU General Public License
18%%%  along with this program; if not, write to the Free Software
19%%%  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
20%%%
21%%%  In addition, as a special exception, you have the permission to
22%%%  link the code of this program with any library released under
23%%%  the EPL license and distribute linked combinations including
24%%%  the two; the MPL (Mozilla Public License), which EPL (Erlang
25%%%  Public License) is based on, is included in this exception.
26
27-module(ts_config_job).
28-vc('$Id$ ').
29-author('nicolas.niclausse@inria.fr').
30
31-export([parse_config/2]).
32
33-include("ts_profile.hrl").
34-include("ts_http.hrl").
35-include("ts_config.hrl").
36
37-include("xmerl.hrl").
38
39-include("ts_job.hrl").
40
41%% @spec parse_config(#xmlElement{}, Config::term()) -> NewConfig::term()
42%% @doc Parses a tsung.xml configuration file xml element for this
43%% protocol and updates the Config term.
44%% @end
45parse_config(Element = #xmlElement{name=dyn_variable}, Conf = #config{}) ->
46    ts_config:parse(Element,Conf);
47parse_config(Element = #xmlElement{name=job},
48             Config=#config{curid = Id, session_tab = Tab,
49                            sessions = [CurS | _], dynvar=DynVar,
50                            subst    = SubstFlag, match=MatchRegExp}) ->
51    Request = #job{req       = ts_config:getAttr(atom,Element#xmlElement.attributes, req, submit),
52                   type      = ts_config:getAttr(atom,Element#xmlElement.attributes, type, oar),
53                   script    = ts_config:getAttr(string,Element#xmlElement.attributes, script),
54                   notify_script = ts_config:getAttr(string,Element#xmlElement.attributes, notify_script),
55                   walltime  = ts_config:getAttr(string,Element#xmlElement.attributes, walltime, "1:00:00"),
56                   resources = ts_config:getAttr(string,Element#xmlElement.attributes, resources, ""),
57                   queue     = ts_config:getAttr(string,Element#xmlElement.attributes, queue),
58                   notify_port = ts_config:getAttr(integer_or_string,Element#xmlElement.attributes, notify_port),
59                   jobid     = ts_config:getAttr(integer_or_string,Element#xmlElement.attributes, jobid, undefined),
60                   name      = ts_config:getAttr(string,Element#xmlElement.attributes, name, "tsung"),
61                   user      = ts_config:getAttr(string,Element#xmlElement.attributes, user, undefined),
62                   options   = ts_config:getAttr(string,Element#xmlElement.attributes, options),
63                   duration  = ts_config:getAttr(integer_or_string,Element#xmlElement.attributes, duration, 3600)
64                   },
65    Msg= #ts_request{ack     = parse,
66                     endpage = true,
67                     dynvar_specs  = DynVar,
68                     subst   = SubstFlag,
69                     match   = MatchRegExp,
70                     param   = Request},
71
72    ts_config:mark_prev_req(Id-1, Tab, CurS),
73    ets:insert(Tab,{{CurS#session.id, Id},Msg}),
74    lists:foldl( fun(A,B)->ts_config:parse(A,B) end, Config#config{dynvar=[]},
75                 Element#xmlElement.content);
76%% Parsing other elements
77parse_config(Element = #xmlElement{}, Conf = #config{}) ->
78    ts_config:parse(Element,Conf);
79%% Parsing non #xmlElement elements
80parse_config(_, Conf = #config{}) ->
81    Conf.
82