@@ -2,6 +2,7 @@ import { Texture, Sprite, FederatedPointerEvent, Graphics } from "pixi.js";
22import RouterImage from "../assets/router.svg" ;
33import ServerImage from "../assets/server.svg" ;
44import PcImage from "../assets/pc.svg" ;
5+ import { Packet } from "./packet" ;
56import { ViewGraph } from "./graphs/viewgraph" ;
67import {
78 deselectElement ,
@@ -82,11 +83,40 @@ export class Device extends Sprite {
8283 }
8384
8485 resize ( sprite : Sprite ) : void {
85- // Setup the size of the new element
8686 sprite . width = sprite . width / 70 ;
8787 sprite . height = sprite . height / DEVICE_SIZE ;
8888 }
8989
90+ sendPacket ( packetType : string , destinationId : number ) : void {
91+ console . log (
92+ `Sending ${ packetType } packet from ${ this . id } to ${ destinationId } ` ,
93+ ) ;
94+
95+ const packetColors : Record < string , number > = {
96+ IP : 0x0000ff , // Verde para paquetes IP
97+ ICMP : 0xff0000 , // Rojo para paquetes ICMP
98+ } ;
99+
100+ const color = packetColors [ packetType ] || 0xffffff ; // Color por defecto blanco
101+ const speed = 200 ; // Velocidad en píxeles por segundo
102+
103+ const pathEdgeIds = this . viewgraph . getPathBetween ( this . id , destinationId ) ;
104+
105+ if ( pathEdgeIds . length === 0 ) {
106+ console . warn (
107+ `No se encontró un camino entre ${ this . id } y ${ destinationId } .` ,
108+ ) ;
109+ return ;
110+ }
111+
112+ const pathEdges = pathEdgeIds . map ( ( id ) => this . viewgraph . getEdge ( id ) ) ;
113+
114+ const packet = new Packet ( color , speed ) ;
115+ const stage = this . viewgraph . getViewport ( ) ;
116+ stage . addChild ( packet ) ;
117+ packet . animateAlongPath ( pathEdges , this . id ) ;
118+ }
119+
90120 deleteDevice ( ) : void {
91121 this . viewgraph . removeDevice ( this . id ) ;
92122 // Clear connections
@@ -223,6 +253,52 @@ export class Device extends Sprite {
223253 true ,
224254 ) ;
225255 this . rightbar . addButton ( "Delete device" , ( ) => this . deleteDevice ( ) ) ;
256+
257+ // Dropdown for selecting packet type
258+ this . rightbar . addDropdown (
259+ "Packet Type" ,
260+ [
261+ { value : "IP" , text : "IP" } ,
262+ { value : "ICMP" , text : "ICMP" } ,
263+ ] ,
264+ ( selectedValue ) => {
265+ console . log ( "Selected Packet Type:" , selectedValue ) ;
266+ } ,
267+ "packet-type" ,
268+ ) ;
269+
270+ // Dropdown for selecting destination
271+ const adjacentDevices = this . viewgraph
272+ . getDeviceIds ( )
273+ . filter ( ( id ) => id !== this . id )
274+ . map ( ( id ) => ( { value : id . toString ( ) , text : `Device ${ id } ` } ) ) ;
275+
276+ this . rightbar . addDropdown (
277+ "Destination" ,
278+ adjacentDevices ,
279+ ( selectedValue ) => {
280+ console . log ( "Selected Destination:" , selectedValue ) ;
281+ } ,
282+ "destination" ,
283+ ) ;
284+
285+ // Button to send the packet
286+ this . rightbar . addButton ( "Send Packet" , ( ) => {
287+ // Get the selected packet type and destination ID
288+ const packetType = (
289+ document . getElementById ( "packet-type" ) as HTMLSelectElement
290+ ) ?. value ;
291+ const destinationId = Number (
292+ ( document . getElementById ( "destination" ) as HTMLSelectElement ) ?. value ,
293+ ) ;
294+
295+ // Call the sendPacket method with the selected values
296+ if ( packetType && ! isNaN ( destinationId ) ) {
297+ this . sendPacket ( packetType , destinationId ) ;
298+ } else {
299+ console . warn ( "Please select both a packet type and a destination." ) ;
300+ }
301+ } ) ;
226302 }
227303
228304 showInfo ( ) {
@@ -261,10 +337,6 @@ export class Router extends Device {
261337 ? "[" + Array . from ( this . connections . values ( ) ) . join ( ", " ) + "]"
262338 : "None" ,
263339 } ,
264- { label : "Model" , value : "TP-Link AX6000" } ,
265- { label : "IP Address" , value : "192.168.1.1" } ,
266- { label : "Firmware Version" , value : "1.2.3" } ,
267- { label : "Uptime" , value : "5 days, 4 hours, 23 minutes" } ,
268340 ] ) ;
269341
270342 this . addCommonButtons ( ) ;
@@ -292,11 +364,6 @@ export class Server extends Device {
292364 ? "[" + Array . from ( this . connections . values ( ) ) . join ( ", " ) + "]"
293365 : "None" ,
294366 } ,
295- { label : "Operating System" , value : "Ubuntu 20.04 LTS" } ,
296- { label : "CPU Usage" , value : "42%" } ,
297- { label : "Memory Usage" , value : "8 GB / 16 GB" } ,
298- { label : "Disk Space" , value : "500 GB / 1 TB" } ,
299- { label : "Last Backup" , value : "2024-11-01 02:30 AM" } ,
300367 ] ) ;
301368
302369 this . addCommonButtons ( ) ;
@@ -324,10 +391,6 @@ export class Pc extends Device {
324391 ? "[" + Array . from ( this . connections . values ( ) ) . join ( ", " ) + "]"
325392 : "None" ,
326393 } ,
327- { label : "Operating System" , value : "Windows 10 Pro" } ,
328- { label : "Antivirus Status" , value : "Active" } ,
329- { label : "IP Address" , value : "192.168.1.100" } ,
330- { label : "Storage Available" , value : "250 GB / 512 GB" } ,
331394 ] ) ;
332395
333396 this . addCommonButtons ( ) ;
0 commit comments