Class: ItunesController::ITunesControlServer

Inherits:
GServer
  • Object
show all
Defined in:
lib/itunesController/controllserver.rb

Overview

The TCP Socket server used to listen on connections and process commands whcih control itunes. see ItunesController::CommandName for a list of supported commands

Instance Method Summary collapse

Constructor Details

#initialize(config, port, itunes) ⇒ ITunesControlServer

The constructor

Parameters:



467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# File 'lib/itunesController/controllserver.rb', line 467

def initialize(config,port,itunes)
    super(port,config.interfaceAddress)
    ItunesController::ItunesControllerLogging::info("Started iTunes controll server on port #{port}")    
    @exit=false   
    @jobQueue=Queue.new
    @jobQueueThread=Thread.new {
        loop do                    
            processJobs()
            if (@exit)
                break;
            end
            sleep(1)
        end                
    }             
    cmd=CreateControllerCommand.new(itunes)
    @jobQueue << Job.new(cmd,nil)
    while (cmd.getController()==nil)  
        sleep(1)             
    end
    
    @itunes=cmd.getController()                 
    @state=ServerState.new(config)
    @commands=[
        HelloCommand.new(@state,@itunes),
        QuitCommand.new(@state,@itunes),
        LoginCommand.new(@state,@itunes),
        PasswordCommand.new(@state,@itunes),
        ClearFilesCommand.new(@state,@itunes),
        AddFilesCommand.new(@state,@itunes),
        RemoveFilesCommand.new(@state,@itunes),
        RemoveDeadFilesCommand.new(@state,@itunes),
        FileCommand.new(@state,@itunes),
        RefreshFilesCommand.new(@state,@itunes),
        VersionCommand.new(@state,@itunes)
    ]
   
    Thread.abort_on_exception = true
   
               
    start()                                                 
end

Instance Method Details

#killServerObject



515
516
517
518
519
# File 'lib/itunesController/controllserver.rb', line 515

def killServer()
    @exit=true
    @jobQueueThread.join
    join()
end

#processCommands(io, data) ⇒ Boolean, String

This is used to workout which command is been executed by the client and execute it.

Parameters:

  • io

    A IO Stream that is used to talk to the connected client

  • data

    The data recived from the client

Returns:

  • (Boolean, String)

    The returned status of the command if the command could be found. The first part is false, then the server will disconnect the client. The second part is a string message send to the client. If the command could not be found, then nil,nil is returned.



561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
# File 'lib/itunesController/controllserver.rb', line 561

def processCommands(io,data)
    @commands.each do | cmd |
        if (cmd.requiredLoginState==nil || cmd.requiredLoginState==@state.state)
            begin
                previousState=@state.clone
                ok, op = cmd.processLine(data,io)
                if (ok!=nil)    
                    if (cmd.singleThreaded) 
                        @jobQueue << Job.new(cmd,previousState)
                    end
                    ItunesController::ItunesControllerLogging::debug("Command processed: #{cmd.name}")
                    return ok,op
                end
            rescue => exc                
                ItunesController::ItunesControllerLogging::error("Unable to execute command",exc)                        
                raise exc.exception(exc.message)
            end
        end
    end
    return nil,nil
end

#processJobsObject



521
522
523
524
525
# File 'lib/itunesController/controllserver.rb', line 521

def processJobs()            
    job=@jobQueue.pop
    ItunesController::ItunesControllerLogging::debug("Popped command and executeing #{job}")
    job.execute()
end

#serve(io) ⇒ Object

This method is called when a client is connected and finished when the client disconnects.

Parameters:

  • io

    A IO Stream that is used to talk to the connected client



529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
# File 'lib/itunesController/controllserver.rb', line 529

def serve(io)            
    ItunesController::ItunesControllerLogging::info("Connected")                       
    @state.clean
    io.print "001 hello\r\n"
    loop do
        if IO.select([io], nil, nil, 2)
            data = io.readpartial(4096)
            ok,op = processCommands(io,data)
            if (ok!=nil)
                io.print op
                break unless ok                    
            else
                io.print "500 ERROR\r\n"
            end                                           
        end
        if (@exit)
            break;
        end
    end
    io.print "Send:002 bye\r\n"
    io.close
    @state.clean            
    ItunesController::ItunesControllerLogging::info("Disconnected")
end

#waitForEmptyJobQueueObject



509
510
511
512
513
# File 'lib/itunesController/controllserver.rb', line 509

def waitForEmptyJobQueue
    while (@jobQueue.size()>0)
        sleep(1)
    end
end