Skip to content

Commit

Permalink
Added button to reload Terminal (hobbyfarm#191)
Browse files Browse the repository at this point in the history
* Reload Terminal button

* Resize Guacamole Terminal

* Remove unused variable
  • Loading branch information
jggoebel authored Jan 11, 2024
1 parent b1b3c1f commit dce72bc
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 25 deletions.
6 changes: 6 additions & 0 deletions src/app/scenario/guacTerminal.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,12 @@ export class GuacTerminalComponent implements OnChanges {
return { width: width, height: height };
}

reloadConnection() {
this.shellService.setStatus(this.vmname, 'Reconnecting');
this.client.disconnect();
this.connect();
}

resize() {
const elm = this.viewport.nativeElement;
if (!elm || !elm.offsetWidth) {
Expand Down
9 changes: 9 additions & 0 deletions src/app/scenario/step.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ <h4 class="card-title">
<td><b>Private IP:</b> {{ v.value.private_ip }}</td>
<td><b>Hostname:</b> {{ v.value.hostname }}</td>
<td><b>Shell Status:</b> {{ getShellStatus(v.key) }}</td>
<td style="padding: 0">
<button
class="btn btn-icon btn-primary btn-sm"
title="Reload Terminal"
(click)="reloadTerminal(v.key)"
>
<cds-icon shape="refresh"></cds-icon>
</button>
</td>
</tr>
</table>
<app-terminal
Expand Down
45 changes: 20 additions & 25 deletions src/app/scenario/step.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,32 +427,14 @@ export class StepComponent implements OnInit, AfterViewInit, OnDestroy {
}

resizeTerminals() {
let numberOfGuacTabs = 0;
let numberOfTermTabs = 0;
const vmArray: VM[] = [...this.vms.values()];
// For each tab...
this.tabContents.forEach((t: ClrTabContent, i: number) => {
const isGuacTerminal: boolean = this.isGuacamoleTerminal(
vmArray[i].protocol,
);
const isActiveTab: boolean = t.ifActiveService.current === t.id;
if (isGuacTerminal) {
++numberOfGuacTabs;
// If the active tab is the same as the currently scoped ...
// ... resize the terminal that corresponds to the index of the active tab.
// Subtract the number of terminal tabs over which it has already been iterated.
// e.g.:
// - Tab 0 could have been a (regular) terminal, so the index sits now at 1
// - But we need the guacamole terminal at index 0 to retrieve the first one from guacterms
// - Therefore calculate i (index) - numberOfTermTabs (iterated term tabs) ...
// ... to retrieve the current index of guacterms.toArray()
isActiveTab && this.guacterms.toArray()[i - numberOfTermTabs].resize();
} else {
++numberOfTermTabs;
// see above
isActiveTab && this.terms.toArray()[i - numberOfGuacTabs].resize();
}
this.terms.forEach((t: TerminalComponent) => {
t.resize();
});

this.guacterms.forEach((t: GuacTerminalComponent) => {
t.resize();
});

this.calculateMaxInterfaceTabs();
}

Expand Down Expand Up @@ -480,6 +462,19 @@ export class StepComponent implements OnInit, AfterViewInit, OnDestroy {
} as webinterfaceTabIdentifier);
}

reloadTerminal(target: string) {
this.terms.forEach((t: TerminalComponent) => {
if (t.vmname == target) {
t.reloadSocket();
}
});
this.guacterms.forEach((t: GuacTerminalComponent) => {
if (t.vmname == target) {
t.reloadConnection();
}
});
}

calculateMaxInterfaceTabs(reduce: boolean = false) {
const tabs = document.getElementsByTagName('li');
let tabsBarWidth: number | undefined = 0;
Expand Down
11 changes: 11 additions & 0 deletions src/app/scenario/terminal.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,17 @@ export class TerminalComponent implements OnChanges, AfterViewInit, OnDestroy {
this.socket.close(WS_CODE_NORMAL_CLOSURE);
}

reloadSocket() {
// Override the socket.onclose function that was defined in the buildSocket function
// to ensure we reconnect after the old socket has been closed and the terminal is disposed properly
this.socket.onclose = () => {
this.term.dispose(); // destroy the terminal on the page to avoid bad display
this.shellService.setStatus(this.vmname, 'Reconnecting');
this.buildSocket();
};
this.closeSocket();
}

ngOnChanges() {
if (this.vmid != null && this.endpoint != null) {
this.closeSocket();
Expand Down

0 comments on commit dce72bc

Please sign in to comment.