CreateSessionWithClient creates a new VNC session for the specified target using a specific client.
(ctx context.Context, client *api.Client, sessionType SessionType, nodeName, vmid, targetName string)
| 336 | |
| 337 | // CreateSessionWithClient creates a new VNC session for the specified target using a specific client. |
| 338 | func (sm *SessionManager) CreateSessionWithClient(ctx context.Context, client *api.Client, sessionType SessionType, nodeName, vmid, targetName string) (*VNCSession, error) { |
| 339 | sm.mutex.Lock() |
| 340 | defer sm.mutex.Unlock() |
| 341 | |
| 342 | targetKey := fmt.Sprintf("%s:%s:%s", sessionType, nodeName, vmid) |
| 343 | |
| 344 | // Check if we can reuse an existing session |
| 345 | if existingSession := sm.findReusableSession(targetKey); existingSession != nil { |
| 346 | existingSession.UpdateLastUsed() |
| 347 | |
| 348 | // If session was disconnected, reset it to active state |
| 349 | if existingSession.State == SessionStateDisconnected { |
| 350 | existingSession.mutex.Lock() |
| 351 | existingSession.State = SessionStateActive |
| 352 | existingSession.mutex.Unlock() |
| 353 | |
| 354 | if sm.logger != nil { |
| 355 | sm.logger.Info("Reactivating disconnected VNC session: session_id=%s, target_type=%s, node=%s, vmid=%s, port=%d", |
| 356 | existingSession.ID, sessionType, nodeName, vmid, existingSession.Port) |
| 357 | } |
| 358 | } else { |
| 359 | if sm.logger != nil { |
| 360 | sm.logger.Info("Reusing existing VNC session: session_id=%s, target_type=%s, node=%s, vmid=%s, port=%d", |
| 361 | existingSession.ID, sessionType, nodeName, vmid, existingSession.Port) |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | return existingSession, nil |
| 366 | } |
| 367 | |
| 368 | // Create new session |
| 369 | sessionID := fmt.Sprintf("vnc_%d_%s", time.Now().Unix(), targetKey) |
| 370 | |
| 371 | // Create the session |
| 372 | session := &VNCSession{ |
| 373 | ID: sessionID, |
| 374 | TargetType: sessionType, |
| 375 | NodeName: nodeName, |
| 376 | VMID: vmid, |
| 377 | TargetName: targetName, |
| 378 | CreatedAt: time.Now(), |
| 379 | LastUsed: time.Now(), |
| 380 | State: SessionStateActive, |
| 381 | activeConnections: 0, |
| 382 | disconnectChan: make(chan struct{}, 1), |
| 383 | sessionManager: sm, |
| 384 | } |
| 385 | |
| 386 | // Create and start the VNC server for this session |
| 387 | server := NewServerWithLogger(sm.logger) |
| 388 | session.Server = server |
| 389 | |
| 390 | var vncURL string |
| 391 | |
| 392 | var err error |
| 393 | |
| 394 | // Start the appropriate server based on session type |
| 395 | switch sessionType { |
no test coverage detected