aboutsummaryrefslogtreecommitdiffstats
path: root/src/demo_server.rb
blob: e89b69dea94456d747abe8c1fc1270e72d8b1073 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env ruby
# encoding: UTF-8
=begin
This file is part of softSIM.

softSIM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

softSIM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with sofSIM.  If not, see <http://www.gnu.org/licenses/>.

Copyright (C) 2011 Kevin "tsaitgaist" Redon kevredon@mail.tsaitgaist.info
=end
# this program is there to start a server

#=============
#== default ==
#=============

# the server to use (pcsc,sim)
@type = "pcsc"
# which IO to use (tcp,unix)
@socket = "unix"
# tcp port
@port = 1337
# unix socket
@unix = "/tmp/osmocom_sap"
# sim file
@file = "sim.xml"
# the verbosity (from common)
$verbosity = 1

#=============
#== methods ==
#=============

# show help
def print_help
  puts "demo_server.rb [options]"
  puts ""
  puts "demonstration SAP server, using available implementations"
  puts ""
  puts "options :"
  puts " --help,-h\t\tprint this help"
  puts " --type,-t type\tserver type: pcsc,sim (default #{@type})"
  puts " --socket,-s type\tsocket type: tcp,unix (default #{@socket})"
  puts " --port,-p port\t\ttcp listeing port (default #{@port})"
  puts " --unix,-u file\t\tunix socket (default #{@unix})"
  puts " --file,-f file\t\tfile for sim type (default #{@file})"
  puts " --verbosity,-v\t\tdebug verbosity 0..5 (default #{$verbosity})"
end

#==========
#== main ==
#==========

# parse CLI arguments
while arg=ARGV.shift do
  
  case arg
  when "--help","-h"
    print_help
    exit 0
  when "--type","-t"
    param = ARGV.shift
    @type = param if param
  when "--socket","-s"
    param = ARGV.shift
    @socket = param if param
  when "--port","-p"
    param = ARGV.shift.to_i
    @port = param if param
  when "--unix","-u"
    param = ARGV.shift
    @unix = param if param
  when "--file","-f"
    param = ARGV.shift
    @file = param if param
  when "--verbosity","-v"
    param = ARGV.shift.to_i
    $verbosity = param if param
  else
    puts "unknown argument #{arg}"
    exit 0
  end
end

# create IO
case @socket
when "tcp"
  require 'socket'
  socket = TCPServer.new("0.0.0.0",@port)
when "unix"
  require 'socket'
  if File.exists?(@unix)
    puts 'Previous socket exists, deleting it...'
    File.delete(@unix)
  end
  socket = UNIXServer.new(@unix)
else
  raise "please defined which socket to use"
end
# wait for a client to connect
io = socket.accept

case @type
when "pcsc"
  require_relative 'pcsc_server'
  server = PCSCServer.new(io)
when "sim"
  require_relative 'simos_server'
  server = SIMServer.new(io)
else
  raise "unkown server type"
end

# starting the server
server.start

# close IO when finished
io.close
server.close