<commit_before>import {Http} from "@angular/http";
import {Injectable, Inject} from "@angular/core";
import {DOCUMENT} from "@angular/platform-browser";
import io = require('socket.io-client');

import IProcessManager from "../../../common/ProcessManager";
import {IProcess} from "../../../common/ProcessManager";
import {genericFromBase64, genericToBase64} from "../../../common/utils";
import EventEmitter from "./EventEmitter";

@Injectable()
export class HttpProcessManagerService implements IProcessManager {
    private static resourceToProcess (resource: any): IProcess {
        console.log(resource);
        return {
            programName: genericFromBase64(resource.attributes.programId),
            filePath: resource.attributes.file,
            args: resource.attributes.args || [ ],
            pid: Number(resource.id)
        };
    }

    private eventEmitter = new EventEmitter();
    private io: SocketIOClient.Socket;

    public constructor (private http: Http, @Inject(DOCUMENT) private document) {
        console.log(`${document.location.protocol}//${document.location.hostname}:${document.location.port}`);
        this.io = io(`${document.location.protocol}//${document.location.hostname}:${document.location.port}`);
        this.socketIoRegisterNewProcessHandler();
        this.socketIoRegisterStreamDataHandler('stdout');
        this.socketIoRegisterStreamDataHandler('stderr');
        this.socketIoRegisterProcessExitHandler();
    }

    public async run (programName: string, filePath: string, args: string[] = [ ]) {
        let response = (await this.http.post('/api/processes', {
            data: {
                type: 'process',
                attributes: {
                    programId: genericToBase64(programName),
                    fileId: genericToBase64(filePath),
                    args
                }
            }
        }).toPromise()).json().data;

        return HttpProcessManagerService.resourceToProcess(response);
    }

    public async kill (pid: number) {
        await this.http.delete(`/api/processes/${pid}`).toPromise();
    }

    public async getStdout (pid: number): Promise<string> {
        return (await this.http.get(`/api/processes/${pid}/stdout`).toPromise()).toString();
    }

    public async getStderr (pid: number): Promise<string> {
        return (await this.http.get(`/api/processes/${pid}/stderr`).toPromise()).toString();
    }

    public async writeStdin (pid: number, data: string): Promise<void> {
        await this.http.patch(`/api/processes/${pid}/stderr`, data);
    }

    public async getProcess (pid: number): Promise<IProcess> {
        let response = (await this.http.get(`/api/processes/${pid}`).toPromise()).json().data;
        return HttpProcessManagerService.resourceToProcess(response);
    }

    public on (event: string, handler: Function) {
        this.eventEmitter.on(event, handler);
    }

    private socketIoRegisterNewProcessHandler () {<commit_msg>fix writeStdin<commit_after>import {Http} from "@angular/http";
import {Injectable, Inject} from "@angular/core";
import {DOCUMENT} from "@angular/platform-browser";
import io = require('socket.io-client');

import IProcessManager from "../../../common/ProcessManager";
import {IProcess} from "../../../common/ProcessManager";
import {genericFromBase64, genericToBase64} from "../../../common/utils";
import EventEmitter from "./EventEmitter";

@Injectable()
export class HttpProcessManagerService implements IProcessManager {
    private static resourceToProcess (resource: any): IProcess {
        console.log(resource);
        return {
            programName: genericFromBase64(resource.attributes.programId),
            filePath: resource.attributes.file,
            args: resource.attributes.args || [ ],
            pid: Number(resource.id)
        };
    }

    private eventEmitter = new EventEmitter();
    private io: SocketIOClient.Socket;

    public constructor (private http: Http, @Inject(DOCUMENT) private document) {
        console.log(`${document.location.protocol}//${document.location.hostname}:${document.location.port}`);
        this.io = io(`${document.location.protocol}//${document.location.hostname}:${document.location.port}`);
        this.socketIoRegisterNewProcessHandler();
        this.socketIoRegisterStreamDataHandler('stdout');
        this.socketIoRegisterStreamDataHandler('stderr');
        this.socketIoRegisterProcessExitHandler();
    }

    public async run (programName: string, filePath: string, args: string[] = [ ]) {
        let response = (await this.http.post('/api/processes', {
            data: {
                type: 'process',
                attributes: {
                    programId: genericToBase64(programName),
                    fileId: genericToBase64(filePath),
                    args
                }
            }
        }).toPromise()).json().data;

        return HttpProcessManagerService.resourceToProcess(response);
    }

    public async kill (pid: number) {
        await this.http.delete(`/api/processes/${pid}`).toPromise();
    }

    public async getStdout (pid: number): Promise<string> {
        return (await this.http.get(`/api/processes/${pid}/stdout`).toPromise()).toString();
    }

    public async getStderr (pid: number): Promise<string> {
        return (await this.http.get(`/api/processes/${pid}/stderr`).toPromise()).toString();
    }

    public async writeStdin (pid: number, data: string): Promise<void> {
        await this.http.patch(`/api/processes/${pid}/stdin`, data);
    }

    public async getProcess (pid: number): Promise<IProcess> {
        let response = (await this.http.get(`/api/processes/${pid}`).toPromise()).json().data;
        return HttpProcessManagerService.resourceToProcess(response);
    }

    public on (event: string, handler: Function) {
        this.eventEmitter.on(event, handler);
    }

    private socketIoRegisterNewProcessHandler () {
{'fraction_non_alphanumeric': 0.14175344306348672, 'fraction_numerical': 0.003359086328518643, 'mean_word_length': 3.002016129032258, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5216921', 'n_tokens_mistral': 1595, 'n_tokens_neox': 1602, 'n_words': 321}