Class: MockNoyesServer

Inherits:
Object
  • Object
show all
Defined in:
lib/common/mock_noyes_server.rb

Defined Under Namespace

Classes: Session

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ MockNoyesServer

Returns a new instance of MockNoyesServer.


16
17
18
19
20
21
22
23
# File 'lib/common/mock_noyes_server.rb', line 16

def initialize options
  @server_socket = TCPServer.new('', options.port)
  @server_socket.setsockopt Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1
  @descriptors = [@server_socket]
  @sessions = {}
  @file_counter = 0
  @verbose = true
end

Instance Method Details

#accept_new_connectionObject


39
40
41
42
43
44
45
# File 'lib/common/mock_noyes_server.rb', line 39

def accept_new_connection
  puts "Accepting new connection."
  newsock = @server_socket.accept
  @descriptors.push newsock
  session = Session.new open("session_#{@file_counter+=1}.raw", 'w')
  @sessions[newsock] = session
end

#close_socket(sock) ⇒ Object


97
98
99
100
101
# File 'lib/common/mock_noyes_server.rb', line 97

def close_socket sock
  @descriptors.delete sock
  @sessions.delete sock
  sock.close
end

#process_available_data(sock) ⇒ Object


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/common/mock_noyes_server.rb', line 47

def process_available_data sock
  msg, from = sock.recvfrom 1024
  return if msg.size == 0
  session = @sessions[sock]
  session.data << msg

  if !session.magic
    if session.data =~ /^#{TMAGIC}/
      session.magic = true
      session.data.slice! 0, TMAGIC.size
    end
  end

  id = session.data.slice(0,4)
  id = session.data.slice!(0,4) if id == TSTART
    
  # We just don't really do anything with the cepstra in the mock server.
  cepstra = []
  while id == TCEPSTRA && session.data.size >=8
    cep_count = 13 * session.data.slice(4,4).unpack('N')[0]
    puts "cep_count = #{cep_count}" if @verbose
    break unless cep_count * 4 + TCEPSTRA.size + 4 <= session.data.size
    session.data.slice!(0,8)
    cepstra.push session.data.slice!(0,cep_count * 4).unpack('g*')
    puts "cepval = #{cepstra.last}" if @verbose
    id = session.data.slice(0,4)
  end
  while (id == TA16_44 || id == TA16_16) && session.data.size >=8
    count = session.data.slice(4,4).unpack('N')[0]
    break unless count * 2 + TA16_44.size + 4 <= session.data.size
    print '.'
    puts "count = #{count}"
    session.data.slice!(0,8)
    audio = session.data.slice!(0,count*2).unpack('n*')
    session.file.write audio.pack 'n*'
    id = session.data.slice(0,4)
  end
  puts "id = #{id.unpack 'N'}" if @verbose
  if id == TEND
    puts "Connection closed."
    session.file.flush
    session.file.close
    session.data.slice!(0,4) 
    sock.puts 'new england patriots'
    close_socket sock
  end
  session.data.slice!(0,4) if id == TBYE
rescue IOError => e
  puts "Connection IOError"
end

#runObject


24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/common/mock_noyes_server.rb', line 24

def run
  while true
    res = select(@descriptors, nil, nil, nil)
    if res
      res[0].each do |sock|
        if sock == @server_socket
          accept_new_connection
        else
          process_available_data sock
        end
      end
    end
  end
end