-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e8a52cd
commit 1ed09f0
Showing
9 changed files
with
300 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/********************************************************************* | ||
* Copyright (c) Intel Corporation 2023 | ||
* SPDX-License-Identifier: Apache-2.0 | ||
**********************************************************************/ | ||
|
||
import React from 'react' | ||
import { IDER } from './ider' | ||
|
||
export class AttachDiskImage extends React.Component<{ | ||
deviceId: string | null | ||
mpsServer: string | null | ||
authToken?: string | ||
}, { selectedFile: File | null, iderState: number }> { | ||
|
||
constructor(props) { | ||
super(props) | ||
this.state = { | ||
selectedFile: null, | ||
iderState: 0, // State to track the IDER state | ||
} | ||
} | ||
|
||
handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
const file = event.target.files ? event.target.files[0] : null | ||
this.setState({ selectedFile: file }) | ||
} | ||
|
||
updateIderState = (newState) => { | ||
this.setState({ iderState: newState }) | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<IDER | ||
iderState={this.state.iderState} | ||
updateIderState={this.updateIderState} | ||
deviceId={this.props.deviceId} | ||
mpsServer={this.props.mpsServer} | ||
authToken={this.props.authToken} | ||
cdrom={this.state.selectedFile} | ||
floppy={null} | ||
data-testid="ider-component" iderData={null} /> | ||
<input data-testid="file-input" type="file" onChange={this.handleFileChange} /> | ||
<button | ||
onClick={() => this.state.iderState === 0 ? this.updateIderState(1) : this.updateIderState(0)} | ||
disabled={!this.state.selectedFile} | ||
> | ||
{this.state.iderState === 0 ? 'Start IDER' : 'Stop IDER'} | ||
</button> | ||
</div> | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/********************************************************************* | ||
* Copyright (c) Intel Corporation 2023 | ||
* SPDX-License-Identifier: Apache-2.0 | ||
**********************************************************************/ | ||
import { AMTRedirector, Protocol, AMTIDER, RedirectorConfig } from '@open-amt-cloud-toolkit/ui-toolkit/core' | ||
import React from 'react' | ||
|
||
export interface IDERProps { | ||
iderState: number | ||
updateIderState: (newState: number) => void | ||
iderData: IDERData | null | ||
cdrom: File | null | ||
floppy: File | null | ||
mpsServer: string | null | ||
authToken?: string | ||
deviceId: string | null | ||
} | ||
|
||
export interface IDERState { | ||
iderState: number | ||
iderData: IDERData | null | ||
} | ||
|
||
export interface IDERData { | ||
floppyRead: number | ||
floppyWrite: number | ||
cdromRead: number | ||
cdromWrite: number | ||
} | ||
|
||
export class IDER extends React.Component<IDERProps, IDERState> { | ||
redirector: AMTRedirector | null = null | ||
ider: AMTIDER | null = null | ||
|
||
constructor(props: IDERProps) { | ||
super(props) | ||
this.state = { | ||
iderState: 0, | ||
iderData: null, | ||
} | ||
} | ||
|
||
componentDidMount(): void { | ||
const server: string = this.props.mpsServer != null ? this.props.mpsServer.replace('http', 'ws') : '' | ||
const config: RedirectorConfig = { | ||
mode: 'ider', | ||
protocol: Protocol.IDER, | ||
fr: new FileReader(), | ||
host: this.props.deviceId != null ? this.props.deviceId : '', | ||
port: 16994, | ||
user: '', | ||
pass: '', | ||
tls: 0, | ||
tls1only: 0, | ||
authToken: this.props.authToken != null ? this.props.authToken : '', | ||
server: server | ||
} | ||
this.redirector = new AMTRedirector(config) | ||
} | ||
|
||
componentWillUnmount(): void { | ||
this.cleanup() | ||
} | ||
|
||
onConnectionStateChange = (redirector, state: number): void => this.setState({ iderState: state }) | ||
|
||
componentDidUpdate(prevProps) { | ||
// React to changes in props, specifically iderState | ||
if (this.props.iderState !== prevProps.iderState) { | ||
if (this.props.iderState === 1) { | ||
this.startIder() | ||
} else { | ||
this.stopIder() | ||
} | ||
} | ||
} | ||
|
||
startIder = (): void => { | ||
this.props.updateIderState(1) | ||
if (this.redirector) { | ||
this.ider = new AMTIDER(this.redirector, this.props.cdrom, null) | ||
this.redirector.onNewState = this.ider.stateChange.bind(this.ider) | ||
this.redirector.onProcessData = this.ider.processData.bind(this.ider) | ||
this.ider.sectorStats = this.iderSectorStats.bind(this) | ||
this.redirector.onStateChanged = this.onConnectionStateChange.bind(this) | ||
this.redirector.start(WebSocket) | ||
} | ||
} | ||
|
||
stopIder(): void { | ||
this.props.updateIderState(0) | ||
if (this.redirector) { | ||
this.redirector.stop() | ||
this.cleanup() | ||
} | ||
} | ||
|
||
cleanup(): void { | ||
this.redirector = null | ||
this.ider = null | ||
} | ||
|
||
iderSectorStats(mode, dev, total, start, len): void { | ||
if (!this.ider) return | ||
if (mode === 1) { // Read operation | ||
if (dev === 0) { // Floppy | ||
this.ider.floppyRead += len * 512; | ||
} else { // CD-ROM | ||
this.ider.cdromRead += len * 2048; | ||
} | ||
} else { // Write operation | ||
if (dev === 0) { // Floppy | ||
this.ider.floppyWrite += len * 512; | ||
} else { // CD-ROM | ||
this.ider.cdromWrite += len * 2048; | ||
} | ||
} | ||
} | ||
|
||
render() { | ||
return null | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/********************************************************************* | ||
* Copyright (c) Intel Corporation 2023 | ||
* SPDX-License-Identifier: Apache-2.0 | ||
**********************************************************************/ | ||
|
||
import React from 'react' | ||
import { createRoot } from 'react-dom/client' | ||
import { AttachDiskImage } from './AttachDiskImage' | ||
import i18n from '../../i18n' | ||
// Get browser language | ||
i18n.changeLanguage(navigator.language).catch(() => console.info('error occured')) | ||
|
||
const url = new URL(window.location.href) | ||
const params = new URLSearchParams(url.search) | ||
|
||
const rootElement = document.getElementById('sol') | ||
if (rootElement != null){ | ||
const root = createRoot(rootElement) | ||
root.render(<AttachDiskImage deviceId={params.get('deviceId')} authToken="authToken" mpsServer={params.get('mpsServer')} />) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/********************************************************************* | ||
* Copyright (c) Intel Corporation 2023 | ||
* SPDX-License-Identifier: Apache-2.0 | ||
**********************************************************************/ | ||
import React from 'react' | ||
import { AttachDiskImage } from '../reactjs/IDER/AttachDiskImage' | ||
import { render, fireEvent, screen } from '@testing-library/react' | ||
import { IDER } from '../reactjs/IDER/ider' | ||
|
||
describe('AttachDiskImage Component', () => { | ||
test('renders the component', () => { | ||
render(<AttachDiskImage deviceId="123" mpsServer="http://example.com" />) | ||
expect(screen.getByText(/start IDER/i)).toBeInTheDocument() | ||
}) | ||
|
||
test('handles file selection', () => { | ||
render(<AttachDiskImage deviceId="123" mpsServer="http://example.com" />) | ||
const fileInput = screen.getByTestId('file-input') as HTMLInputElement | ||
const file = new File(['dummy content'], 'example.txt', { type: 'text/plain' }) | ||
fireEvent.change(fileInput, { target: { files: [file] } }) | ||
expect(fileInput.files).toHaveLength(1) | ||
}) | ||
|
||
test('handles IDER connect/disconnect', () => { | ||
render(<AttachDiskImage deviceId="123" mpsServer="http://example.com" />) | ||
const button = screen.getByText(/start IDER/i) | ||
fireEvent.click(button) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/********************************************************************* | ||
* Copyright (c) Intel Corporation 2023 | ||
* SPDX-License-Identifier: Apache-2.0 | ||
**********************************************************************/ | ||
import React from 'react' | ||
import { IDER } from '../reactjs/IDER/ider' | ||
import { AMTRedirector } from '@open-amt-cloud-toolkit/ui-toolkit/core' | ||
import { render } from '@testing-library/react' | ||
|
||
// Mocks for external dependencies | ||
jest.mock('@open-amt-cloud-toolkit/ui-toolkit/core', () => ({ | ||
AMTRedirector: jest.fn(), | ||
AMTIDER: jest.fn(), | ||
Protocol: { IDER: 'IDER' }, | ||
})) | ||
describe('IDER Component', () => { | ||
beforeEach(() => { | ||
// Clear all mocks before each test | ||
jest.clearAllMocks() | ||
}) | ||
const props = { | ||
mpsServer: 'http://example.com', | ||
deviceId: 'device123', | ||
authToken: 'token123', | ||
iderState: 0, | ||
updateIderState: jest.fn(), | ||
iderData: null, | ||
cdrom: new File([''], 'cdrom.iso', { type: 'application/octet-stream' }), | ||
floppy: null | ||
} | ||
|
||
it('should initialize and clean up redirector', () => { | ||
const { unmount } = render(<IDER {...props} />) | ||
console.log(AMTRedirector) | ||
expect(AMTRedirector).toHaveBeenCalledWith(expect.anything()) | ||
unmount() | ||
}) | ||
it('renders no visible content', () => { | ||
const { container } = render(<IDER {...props} />) | ||
expect(container).toBeEmptyDOMElement() | ||
}) | ||
}) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters