Blame view

simulators/user/userSimulator.py 3.28 KB
3c179769   Jeremy   userSimulator / a...
1
2
3
4
5
6
7
8
import sys
import os
import datetime
import time
import json
import requests
from requests.auth import HTTPBasicAuth

cd0149f5   Etienne Pallier   progress made for...
9
DEBUG_FILE = True
3224f14a   Jeremy   Fixed some simula...
10

3c179769   Jeremy   userSimulator / a...
11
class UserSimulator():
605b65e5   Jeremy   Update simulators
12
13
    conf_file = "../config/conf.json"
    conf_path = "../config/"
3c179769   Jeremy   userSimulator / a...
14
    request_path = "../resources/"
4816e86b   Jeremy   removed *.sh *.ba...
15
    login = "http://localhost:8000/user_manager/login"
cd0149f5   Etienne Pallier   progress made for...
16
    # This will call src/routine_manager/views.py:import_request()
3c179769   Jeremy   userSimulator / a...
17
18
19
20
21
    url = "http://localhost:8000/routine_manager/import_request"
    get_url = "http://localhost:8000/routine_manager/"
    sims = []
    ended = 0

b6b0fd47   Jeremy   Added parameters ...
22
    def userPrint(self, string: str):
3224f14a   Jeremy   Fixed some simula...
23
24
        if DEBUG_FILE:
            print("UserSimulator : " + string)
3c179769   Jeremy   userSimulator / a...
25

65149de7   Jeremy   Update
26
    def __init__(self, argv):
3c179769   Jeremy   userSimulator / a...
27
28
        if (len(argv) > 1):
            self.conf_file = self.conf_path + argv[1]
3224f14a   Jeremy   Fixed some simula...
29
30
            if DEBUG_FILE:
                print("conf_file = " + self.conf_file)
3c179769   Jeremy   userSimulator / a...
31
32
33
34
35
36
37
38
39
40
        else:
            self.userPrint("Using the default configuration file : conf.json")

    def parse(self):
        try:
            json_data = open(self.conf_file, 'r')
        except IOError:
            self.userPrint("Configuration file not found")
            sys.exit(0)
        json_content = json.loads(json_data.read())
678838ed   Jeremy   Weather ans insid...
41
        for dic in json_content[1:]:
3c179769   Jeremy   userSimulator / a...
42
43
44
45
46
47
            for key, value in dic.items():
                if (key == "userSimulator"):
                    self.sims.append(dic)
                    self.ended = max(dic["time"], self.ended)
        return (0)

b6b0fd47   Jeremy   Added parameters ...
48
    def sendRequest(self, file_name: str):
3c179769   Jeremy   userSimulator / a...
49
        files = {'request_file' : open(self.request_path + file_name, 'rb')}
05bdcc44   Etienne Pallier   BIG DEMO tests (s...
50
        #print("Post request", self.url, "with args", files)
cd0149f5   Etienne Pallier   progress made for...
51
52
        # ex: Post request http://localhost:8000/routine_manager/import_request with args {'request_file': <_io.BufferedReader name='../resources/routine_request_01.xml'>}
        # This calls import_request() action from src/routine_manager/views.py
3c179769   Jeremy   userSimulator / a...
53
54
55
56
57
58
59
60
61
        r = self.client.post(self.url, files=files)
        if (r.status_code == 200):
            self.userPrint("The request has been successfully sent")
        else:
            self.userPrint("Sending request failed %d"%(r.status_code))
        return (0)

    def authenticate(self):
        self.client = requests.session()
4816e86b   Jeremy   removed *.sh *.ba...
62
63
64
65
66
67
68
69
70
71
        try:
            self.client.get("http://localhost:8000/user_manager/login")
            csrftoken = self.client.cookies['csrftoken']
            self.logindata = dict(email="pyros", password="DjangoPyros", csrfmiddlewaretoken=csrftoken)
            resp = self.client.post(self.login, data=self.logindata, headers=dict(Referer=self.login))
            self.userPrint("Connection status : %d"%(resp.status_code))
        except Exception as e:
            print(e, file=sys.stderr)
            return 1
        return 0
3c179769   Jeremy   userSimulator / a...
72
73
74
75

    def run(self):
        i = 0
        self.parse()
4816e86b   Jeremy   removed *.sh *.ba...
76
77
        if self.authenticate():
            return 1
3c179769   Jeremy   userSimulator / a...
78
        self.userPrint("The simulator will end in %d seconds"%(int(self.ended)))
675fb3d5   Jeremy   Update scheduler ...
79
        while (i <= self.ended):
3c179769   Jeremy   userSimulator / a...
80
81
            for dic in self.sims:
                if (int(dic["time"]) == i):
cd0149f5   Etienne Pallier   progress made for...
82
                    # call src/routine_manager/views.py:import_request()
3c179769   Jeremy   userSimulator / a...
83
                    self.sendRequest(dic["userSimulator"])
cd0149f5   Etienne Pallier   progress made for...
84
                    ##break
3c179769   Jeremy   userSimulator / a...
85
86
            i += 1
            time.sleep(1)
cd0149f5   Etienne Pallier   progress made for...
87
            ##if i==3: break
3c179769   Jeremy   userSimulator / a...
88
89
90
91
        return (0)

if (__name__ == "__main__"):
    sim = UserSimulator(sys.argv)
4816e86b   Jeremy   removed *.sh *.ba...
92
    sys.exit(sim.run())