1
/*
2
 * Hurl (https://hurl.dev)
3
 * Copyright (C) 2024 Orange
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *          http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 */
18
use std::io;
19

            
20
use crate::parallel::job::{Job, JobResult};
21
use crate::parallel::worker::WorkerId;
22
use crate::util::term::{Stderr, Stdout};
23

            
24
/// Represents a message sent from the worker to the runner (running on the main thread).
25
pub enum WorkerMessage {
26
    /// Error raised when the file can't be read.
27
    IOError(IOErrorMsg),
28
    /// Error raised when the file isn't a valid Hurl content.
29
    ParsingError(ParsingErrorMsg),
30
    /// Sent when the Hurl file is in progress (file has been parsed and HTTP exchanges have started).
31
    Running(RunningMsg),
32
    /// Sent when the Hurl file is completed, whether successful or failed.
33
    Completed(CompletedMsg),
34
}
35

            
36
/// A message sent from worker to runner when the input file can't be read.
37
pub struct IOErrorMsg {
38
    /// Identifier of the worker sending this message.
39
    #[allow(dead_code)]
40
    pub worker_id: WorkerId,
41
    /// Job originator of this message.
42
    pub job: Job,
43
    /// Inner error that has triggered this message.
44
    pub error: io::Error,
45
}
46

            
47
impl IOErrorMsg {
48
    /// Creates a new I/O error message.
49
3
    pub fn new(worker_id: WorkerId, job: &Job, error: io::Error) -> Self {
50
3
        IOErrorMsg {
51
3
            worker_id,
52
3
            job: job.clone(),
53
3
            error,
54
        }
55
    }
56
}
57

            
58
/// A message sent from worker to runner when the input file can't be parsed.
59
pub struct ParsingErrorMsg {
60
    /// Identifier of the worker sending this message.
61
    #[allow(dead_code)]
62
    pub worker_id: WorkerId,
63
    /// Job originator of this message.
64
    #[allow(dead_code)]
65
    pub job: Job,
66
    /// Standard error of the worker for this job.
67
    pub stderr: Stderr,
68
}
69

            
70
impl ParsingErrorMsg {
71
    /// Creates a new parsing error message.
72
3
    pub fn new(worker_id: WorkerId, job: &Job, stderr: &Stderr) -> Self {
73
3
        ParsingErrorMsg {
74
3
            worker_id,
75
3
            job: job.clone(),
76
3
            stderr: stderr.clone(),
77
        }
78
    }
79
}
80

            
81
/// A message sent from worker to runner at regular time to inform that the job is being run.
82
pub struct RunningMsg {
83
    /// Identifier of the worker sending this message.
84
    pub worker_id: WorkerId,
85
    /// Job originator of this message.
86
    pub job: Job,
87
    /// 0-based index of the current entry.
88
    pub entry_index: usize,
89
    /// Number of entries
90
    pub entry_count: usize,
91
}
92

            
93
impl RunningMsg {
94
    /// Creates a new running message: the job is in progress.
95
1962
    pub fn new(worker_id: WorkerId, job: &Job, entry_index: usize, entry_count: usize) -> Self {
96
1962
        RunningMsg {
97
1962
            worker_id,
98
1962
            job: job.clone(),
99
1962
            entry_index,
100
1962
            entry_count,
101
        }
102
    }
103
}
104

            
105
/// A message sent from worker to runner when a Hurl file has completed, whether successful or not.
106
pub struct CompletedMsg {
107
    /// Identifier of the worker sending this message.
108
    pub worker_id: WorkerId,
109
    /// Result execution of the originator job, can successful or failed.
110
    pub result: JobResult,
111
    /// Standard output of the worker for this job.
112
    pub stdout: Stdout,
113
    /// Standard error of the worker for this job.
114
    pub stderr: Stderr,
115
}
116

            
117
impl CompletedMsg {
118
    /// Creates a new completed message: the job has completed, successfully or not.
119
1080
    pub fn new(worker_id: WorkerId, result: JobResult, stdout: Stdout, stderr: Stderr) -> Self {
120
1080
        CompletedMsg {
121
1080
            worker_id,
122
1080
            result,
123
1080
            stdout,
124
1080
            stderr,
125
        }
126
    }
127
}