File: vboxcheckpoint.cpp

package info (click to toggle)
boinc 7.14.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 111,132 kB
  • sloc: cpp: 163,589; php: 113,173; ansic: 49,284; pascal: 35,620; xml: 17,864; java: 13,521; python: 6,551; sh: 4,082; perl: 1,843; makefile: 1,796; objc: 1,543; sql: 959; csh: 126; lisp: 47
file content (145 lines) | stat: -rw-r--r-- 3,785 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2010-2012 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BOINC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.

#ifdef _WIN32
#include "boinc_win.h"
#include "win_util.h"
#else
#include <vector>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <cmath>
#include <string>
#include <unistd.h>
#endif

#include "parse.h"
#include "filesys.h"
#include "boinc_api.h"
#include "vboxlogging.h"
#include "vboxcheckpoint.h"


VBOX_CHECKPOINT::VBOX_CHECKPOINT() {
    clear();
}

VBOX_CHECKPOINT::~VBOX_CHECKPOINT() {
    clear();
}

void VBOX_CHECKPOINT::clear() {
    elapsed_time = 0.0;
    cpu_time = 0.0;
    webapi_port = 0;
    remote_desktop_port = 0;
}

int VBOX_CHECKPOINT::parse() {
    MIOFILE mf;

    FILE* f = boinc_fopen(CHECKPOINT_FILENAME, "r");
    if (!f) {
        return ERR_FOPEN;
    }
    mf.init_file(f);
    XML_PARSER xp(&mf);

    if (!xp.parse_start("vbox_checkpoint")) return ERR_XML_PARSE;
    while (!xp.get_tag()) {
        if (!xp.is_tag) {
            vboxlog_msg("VBOX_CHECKPOINT::parse(): unexpected text %s", xp.parsed_tag);
            continue;
        }
        if (xp.match_tag("/vbox_checkpoint")) {
            fclose(f);
            return 0;
        }
        else if (xp.parse_double("elapsed_time", elapsed_time)) continue;
        else if (xp.parse_double("cpu_time", cpu_time)) continue;
        else if (xp.parse_int("webapi_port", webapi_port)) continue;
        else if (xp.parse_int("remote_desktop_port", remote_desktop_port)) continue;
        else {
            vboxlog_msg("VBOX_CHECKPOINT::parse(): unexpected text %s", xp.parsed_tag);
        }
    }
    fclose(f);
    return ERR_XML_PARSE;
}

int VBOX_CHECKPOINT::write() {
    MIOFILE mf;
    FILE* f;

    // Write checkpoint info to disk
    //
    f = boinc_fopen(CHECKPOINT_FILENAME, "w");
    mf.init_file(f);

    mf.printf(
        "<vbox_checkpoint>\n"
        "    <elapsed_time>%f</elapsed_time>\n"
        "    <cpu_time>%f</cpu_time>\n"
        "    <webapi_port>%d</webapi_port>\n"
        "    <remote_desktop_port>%d</remote_desktop_port>\n"
        "</vbox_checkpoint>\n",
        elapsed_time,
        cpu_time,
        webapi_port,
        remote_desktop_port
    );

    fclose(f);

    // Write webapi info to disk
    //
    if (!boinc_file_exists(WEBAPI_FILENAME)) {
        f = boinc_fopen(WEBAPI_FILENAME, "w");
        mf.init_file(f);
        mf.printf(
            "<webapi>\n"
            "  <host_port>%d</host_port>\n"
            "</webapi>\n",
            webapi_port
        );
        fclose(f);
    }

    // Write remote desktop info to disk
    //
    if (!boinc_file_exists(REMOTEDESKTOP_FILENAME)) {
        f = boinc_fopen(REMOTEDESKTOP_FILENAME, "w");
        mf.init_file(f);
        mf.printf(
            "<remote_desktop>\n"
            "  <host_port>%d</host_port>\n"
            "</remote_desktop>\n",
            remote_desktop_port
        );
        fclose(f);
    }

    return 0;
}

int VBOX_CHECKPOINT::update(double _elapsed_time, double _cpu_time) {
    elapsed_time = _elapsed_time;
    cpu_time = _cpu_time;
    return write();
}