m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/pl/edu/mimuw/cloudatlas/fetcher/Fetcher.java
blob: 12d795a15702bdb0042d2d953cceaa52c4b68448 (plain)
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
146
147
148
149
150
package pl.edu.mimuw.cloudatlas.fetcher;

import pl.edu.mimuw.cloudatlas.api.Api;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.google.gson.Gson;
import pl.edu.mimuw.cloudatlas.model.*;

public class Fetcher {
    private static String host;
    private static int port;

    private static final List<String> fetcherAttributeNames = Arrays.asList(
            "avg_load",
            "free_disk",
            "total_disk",
            "free_ram",
            "total_ram",
            "free_swap",
            "total_swap",
            "num_processes",
            "num_cores",
            "kernel_ver",
            "logged_users",
            "dns_names"
    );

    private static final List<Type.PrimaryType> fetcherAttributeTypes = Arrays.asList(
            Type.PrimaryType.DOUBLE,
            Type.PrimaryType.INT,
            Type.PrimaryType.INT,
            Type.PrimaryType.INT,
            Type.PrimaryType.INT,
            Type.PrimaryType.INT,
            Type.PrimaryType.INT,
            Type.PrimaryType.INT,
            Type.PrimaryType.INT,
            Type.PrimaryType.STRING,
            Type.PrimaryType.INT,
            Type.PrimaryType.LIST
    );

    private static Api api;
    private static Process pythonProcess;

    private static Value packAttributeValue(Object rawValue, Type.PrimaryType valueType) {
        Value val = null;
        ArrayList<Value> contacts = new ArrayList<Value>();

        if (valueType.equals(Type.PrimaryType.STRING)) {
            val = new ValueString((String) rawValue);
        } else if (valueType.equals(Type.PrimaryType.INT)) {
            val = new ValueInt(((Double) rawValue).longValue());
        } else if (valueType.equals(Type.PrimaryType.DOUBLE)) {
            val = new ValueDouble((Double) rawValue);
        } else if (valueType.equals(Type.PrimaryType.LIST)) {
            for (Object c : (ArrayList) rawValue) {
                contacts.add(new ValueString((String) c));
            }
            val = new ValueList(contacts, TypePrimitive.STRING);
        } else {
            throw new UnsupportedOperationException();
        }

        return val;
    }
    
    private static void initializeApiStub() throws RemoteException, NotBoundException {
        Registry registry = LocateRegistry.getRegistry(host, port);
        api = (Api) registry.lookup("Api");
        System.out.println("Fetcher runs with registry");
    }

    private static void initializePythonProcess() throws IOException {
        String pythonScript = Fetcher.class.getResource("data_fetcher.py").getFile();
        String pythonCmd = "/usr/bin/python3 " + pythonScript;
        System.out.println("Run cmd: " + pythonCmd);
        pythonProcess = Runtime.getRuntime().exec(pythonCmd);
    }

    private static ArrayList deserializeAttribs(String serializedAttribs) {
        Gson g = new Gson();
        return g.fromJson(serializedAttribs, ArrayList.class);
    }

    // https://jj09.net/interprocess-communication-python-java/
    private static void fetchData(String zonePath) {
        BufferedReader bufferRead;
        ArrayList deserializedAttribs;
        String jsonAttribs;

        System.out.println(System.getProperty("user.dir"));

        try {
            initializeApiStub();
            initializePythonProcess();

            bufferRead = new BufferedReader( new InputStreamReader(pythonProcess.getInputStream()));

            while((jsonAttribs = bufferRead.readLine()) != null) {
                System.out.println(jsonAttribs);
                System.out.flush();
                deserializedAttribs = deserializeAttribs(jsonAttribs);
                for (int i = 0; i < fetcherAttributeNames.size(); i++) {
                    api.setAttributeValue(
                            zonePath,
                            fetcherAttributeNames.get(i),
                            packAttributeValue(
                                    deserializedAttribs.get(i),
                                    fetcherAttributeTypes.get(i)));
                }
            }

            bufferRead.close();

        } catch (Exception e) {
            System.err.println("Fetcher exception:");
            e.printStackTrace();
        }
    }

    private static void parseArgs(String[] args) {
        System.out.println("args length: " + args.length);
        if (args.length < 2) {
            port = 1099;
        } else {
            port = Integer.parseInt(args[1]);
        }

        if (args.length < 1) {
            host = "localhost";
        } else {
            host = args[0];
        }
    }

    public static void main(String[] args) {
        String zonePath = System.getProperty("zone_path");
        parseArgs(args);
        fetchData(zonePath);
    }
}