Class: IDL::RubyServantWriter

Inherits:
RubyWriterBase show all
Defined in:
lib/ridlbe/ruby/walker.rb

Overview

RubyStubWriter

Instance Method Summary collapse

Methods inherited from RubyWriterBase

#indent, #nest, #print, #printi, #printiln, #println, #visit_nodes

Constructor Details

#initialize(output = STDOUT, params = {}, indent = " ") ⇒ RubyServantWriter

Returns a new instance of RubyServantWriter.



1106
1107
1108
1109
# File 'lib/ridlbe/ruby/walker.rb', line 1106

def initialize(output = STDOUT, params = {}, indent = "  ")
  super
  @stub_root = '::'
end

Instance Method Details

#declare_interface(node) ⇒ Object



1177
1178
1179
# File 'lib/ridlbe/ruby/walker.rb', line 1177

def declare_interface(node)
  printiln("class #{node.rubyname} < PortableServer::Servant; end  ## servant forward")
end

#declare_struct(node) ⇒ Object



1553
1554
# File 'lib/ridlbe/ruby/walker.rb', line 1553

def declare_struct(node)
end

#declare_union(node) ⇒ Object



1565
1566
# File 'lib/ridlbe/ruby/walker.rb', line 1565

def declare_union(node)
end

#declare_valuetype(node) ⇒ Object



1229
1230
# File 'lib/ridlbe/ruby/walker.rb', line 1229

def declare_valuetype(node)
end

#enter_exception(node) ⇒ Object



1560
1561
# File 'lib/ridlbe/ruby/walker.rb', line 1560

def enter_exception(node)
end

#enter_include(node) ⇒ Object



1154
1155
1156
1157
1158
# File 'lib/ridlbe/ruby/walker.rb', line 1154

def enter_include(node)
  printiln("## include")
  printiln("CORBA.implement('#{node.filename}', {}, CORBA::IDL::SERVANT_INTF) {")
  println()
end

#enter_interface(node) ⇒ Object



1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
# File 'lib/ridlbe/ruby/walker.rb', line 1180

def enter_interface(node)
  if !node.is_local?
    println
    printiln("class #{node.rubyname} < PortableServer::Servant ## servant")
    println()
    @nest += 1

    printiln("module Intf")
    @nest += 1
    printiln(format("Id = '%s'.freeze", node.repository_id))
    printi("Ids = [ Id")
    if node.is_abstract?
      print(", 'IDL:omg.org/CORBA/AbstractBase:1.0'")
    end
    println(' ]')
    printiln("Operations = {}")
    println()
  end
end

#enter_module(node) ⇒ Object



1166
1167
1168
1169
1170
# File 'lib/ridlbe/ruby/walker.rb', line 1166

def enter_module(node)
  printiln("module " + node.rubyname)
  println()
  @nest += 1
end

#enter_struct(node) ⇒ Object



1555
1556
# File 'lib/ridlbe/ruby/walker.rb', line 1555

def enter_struct(node)
end

#enter_union(node) ⇒ Object



1567
1568
# File 'lib/ridlbe/ruby/walker.rb', line 1567

def enter_union(node)
end

#enter_valuetype(node) ⇒ Object



1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
# File 'lib/ridlbe/ruby/walker.rb', line 1231

def enter_valuetype(node)
  println
  printiln("class #{node.rubyname} < PortableServer::Servant ## servant")
  @nest += 1
  printiln('## valuetype interface')
  printiln("include #{@stub_root}#{node.scoped_rubyname}")
  println
  printiln('## object interfaces')
  node.interfaces.each do |intf|
    #printiln("include #{@stub_root}#{intf.scoped_rubyname}")
    printiln("include #{intf.scoped_rubyname}")
  end
end

#expression_to_s(exp) ⇒ Object



1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
# File 'lib/ridlbe/ruby/walker.rb', line 1475

def expression_to_s(exp)
  case exp
  when Expression::Value
    value_to_s(exp)
  when Expression::Operation
    operation_to_s(exp)
  when Expression::ScopedName
    @stub_root + exp.node.scoped_rubyname
  else
    raise RuntimeError, "unknown expression type: #{exp.class.name}"
  end
end

#get_arg_type(_idl_argtype) ⇒ Object



1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
# File 'lib/ridlbe/ruby/walker.rb', line 1464

def get_arg_type(_idl_argtype)
  case _idl_argtype
  when IDL::AST::Parameter::IN
    "CORBA::ARG_IN"
  when IDL::AST::Parameter::OUT
    "CORBA::ARG_OUT"
  else
    "CORBA::ARG_INOUT"
  end
end

#get_typecode(_type) ⇒ Object



1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
# File 'lib/ridlbe/ruby/walker.rb', line 1405

def get_typecode(_type)
  case _type
  when Type::Octet,
       Type::UShort, Type::Short,
       Type::ULong, Type::Long,
       Type::ULongLong, Type::LongLong,
       Type::Boolean, Type::Char, Type::WChar,
       Type::Float, Type::Double, Type::LongDouble,
       Type::Void, Type::Any
    s = _type.class.name.split("::") # IDL::Type::Octet -> [IDL, Type, Octet]
    s = s[s.length - 1]
    s.downcase!
    format("CORBA._tc_%s",s)

  when Type::Object
    "CORBA._tc_Object"

  when Type::String
    if not _type.length.nil?
      format("CORBA::TypeCode::String.new(%d)", _type.length)
    else
      "CORBA._tc_string"
    end

  when Type::WString
    if not _type.length.nil?
      format("CORBA::TypeCode::WString.new(%d)", _type.length)
    else
      "CORBA._tc_wstring"
    end

  when Type::ScopedName
    @stub_root + _type.node.scoped_rubyname + '._tc'

  when Type::Array
    sep = ""
    tc = "CORBA::TypeCode::Array.new(" +
        get_typecode(_type.basetype) + ", "
    _type.sizes.each do |sz|
      tc += "#{sep}#{sz.to_s}"
      sep = ", "
    end
    tc + ")"

  when Type::Sequence
    if _type.is_recursive?
      "CORBA::TypeCode::Sequence.new(CORBA::TypeCode::Recursive.new('#{_type.basetype.resolved_type.node.repository_id}'))"
    else
      "CORBA::TypeCode::Sequence.new(" +
            get_typecode(_type.basetype) +
            if not _type.length.nil? then format(", %d)", _type.length) else ")" end +
            ".freeze"
    end

  else
    raise RuntimeError, "invalid type for (un)marshalling: #{_type.typename}"
  end
end

#leave_exception(node) ⇒ Object



1562
1563
# File 'lib/ridlbe/ruby/walker.rb', line 1562

def leave_exception(node)
end

#leave_include(node) ⇒ Object



1160
1161
1162
1163
1164
# File 'lib/ridlbe/ruby/walker.rb', line 1160

def leave_include(node)
  println()
  printiln("} ## end of include '#{node.filename}'")
  println()
end

#leave_interface(node) ⇒ Object



1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
# File 'lib/ridlbe/ruby/walker.rb', line 1199

def leave_interface(node)
  if !node.is_local?
    name = node.rubyname

    @nest -= 1
    printiln("end #of Intf")

    println()
    printiln("Id = Intf::Id")
    println()

    if node.bases.size>0
      node.bases.each do |n|
        printiln("include_interface(#{n.scoped_rubyname}::Intf)")
      end
    else
      printiln("include_interface(PortableServer::Servant::Intf)")
    end
    println()

    printiln("include Intf")
    println()

    printiln("def _this; #{@stub_root}#{node.scoped_rubyname}._narrow(super); end")

    @nest -= 1
    printiln("end #of servant #{name}")
  end
end

#leave_module(node) ⇒ Object



1171
1172
1173
1174
1175
# File 'lib/ridlbe/ruby/walker.rb', line 1171

def leave_module(node)
  @nest -= 1
  printiln("end #of module #{node.rubyname}")
  println()
end

#leave_struct(node) ⇒ Object



1557
1558
# File 'lib/ridlbe/ruby/walker.rb', line 1557

def leave_struct(node)
end

#leave_union(node) ⇒ Object



1569
1570
# File 'lib/ridlbe/ruby/walker.rb', line 1569

def leave_union(node)
end

#leave_valuetype(node) ⇒ Object



1245
1246
1247
1248
# File 'lib/ridlbe/ruby/walker.rb', line 1245

def leave_valuetype(node)
  @nest -= 1
  printiln("end #of servant #{node.rubyname}")
end

#operation_to_s(exp) ⇒ Object



1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
# File 'lib/ridlbe/ruby/walker.rb', line 1519

def operation_to_s(exp)
  s = nil
  op = exp.operands
  case exp
  when Expression::UnaryPlus
    s = expression_to_s(op[0])
  when Expression::UnaryMinus
    s = "-" + expression_to_s(op[0])
  when Expression::UnaryNot
    s = "~" + expression_to_s(op[0])
  when Expression::Or
    s = expression_to_s(op[0]) + " | " + expression_to_s(op[1])
  when Expression::And
    s = expression_to_s(op[0]) + " & " + expression_to_s(op[1])
  when Expression::LShift
    s = expression_to_s(op[0]) + " << " + expression_to_s(op[1])
  when Expression::RShift
    s = expression_to_s(op[0]) + " >> " + expression_to_s(op[1])
  when Expression::Add
    s = expression_to_s(op[0]) + " + " + expression_to_s(op[1])
  when Expression::Minus
    s = expression_to_s(op[0]) + " - " + expression_to_s(op[1])
  when Expression::Mult
    s = expression_to_s(op[0]) + " * " + expression_to_s(op[1])
  when Expression::Div
    s = expression_to_s(op[0]) + " / " + expression_to_s(op[1])
  when Expression::Mod
    s = expression_to_s(op[0]) + " % " + expression_to_s(op[1])
  else
    raise RuntimeError, "unknown operation: #{exp.type.name}"
  end
  "(" + s + ")"
end

#post_visit(parser) ⇒ Object



1139
1140
1141
1142
1143
1144
1145
1146
1147
# File 'lib/ridlbe/ruby/walker.rb', line 1139

def post_visit(parser)
  idleval = @params[:idl_eval] || false
  if !idleval
    printiln("} ## end of '#{@params[:idlfile]}'")
  end
  @nest -= 1
  printiln("end #of module POA")
  println("# -*- END -*-")
end

#pre_visit(parser) ⇒ Object



1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
# File 'lib/ridlbe/ruby/walker.rb', line 1111

def pre_visit(parser)
  print(
%Q{# -*- Ruby -*-
#
# ****  Code generated by the R2CORBA IDL Compiler ****
# R2CORBA has been developed by:
#        Remedy IT
#        Nijkerk, GLD
#        The Netherlands
#        http://www.remedy.nl \ http://www.theaceorb.nl
#
})
  idleval = @params[:idl_eval] || false
  println("require 'corba/poa.rb'") if @params[:libinit]
  if !@params[:expand_includes]
    println("require '"+@params[:idlfile].sub(/\.[^\.]*$/,@params[:stub_pfx])+"'")
  end
  println()
  printiln("module POA")
  @nest += 1
  if !idleval
    printiln("CORBA.implement('#{@params[:idlfile]}', {}, CORBA::IDL::SERVANT_INTF) {")
    println()
  end
  ## register explicit (*not* IDL derived) rootnamespace used for client stubs
  @stub_root = "#{parser.root_namespace.rubyname}::" unless parser.root_namespace.nil?
end

#value_to_s(exp) ⇒ Object



1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
# File 'lib/ridlbe/ruby/walker.rb', line 1488

def value_to_s(exp)
  s = nil
  v = exp.value
  case exp.idltype
  when Type::Void
    s = "nil"
  when Type::Char
    s = "'#{v.chr}'"
  when Type::Integer,
    Type::Boolean,
    Type::Octet,
    Type::Float,
    Type::WChar
    s = v.to_s
  when Type::Enum
    s = v.to_s
  when Type::String
    s = "'#{v.to_s}'"
  when Type::WString
    s = "[#{v.join(',')}]"
  #when Type::Fixed
  #when Type::Any
  #when Type::Object
  when Type::ScopedName
    s = value_to_s(Expression::Value.new(exp.idltype.node.idltype, v))
  else
    raise RuntimeError, "#{exp.typename}'s not been supported yet."
  end
  s
end

#visit_attribute(node) ⇒ Object



1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
# File 'lib/ridlbe/ruby/walker.rb', line 1332

def visit_attribute(node)
  _intf = node.enclosure

  ## do nothing for valuetypes or local interfaces
  return nil if _intf.is_a?(IDL::AST::Valuetype) || _intf.is_local?

  printiln("Operations.store(:_get_#{node.name}, {")
  nest {
    nest {
      printiln(":result_type => #{get_typecode(node.idltype)},")
      if node.get_raises.size>0
        printi(":exc_list => [")
        pfx = ""
        nest {
          node.get_raises.each { |ex|
            println(pfx)
            pfx = ","
            printi(get_typecode(ex))
          }
          println("],")
        }
      end
      printiln(":op_sym => :#{node.rubyname} })")
    }
  }
  println()

  printiln("def #{node.rubyname}()")
  nest {
    printiln("raise ::CORBA::NO_IMPLEMENT.new(")
    printiln("         'unimplemented servant attribute get',")
    printiln("         1, ::CORBA::COMPLETED_NO)")
  }
  printiln("end #of attribute get_#{node.name}")
  println()

  if not node.readonly
    printiln("Operations.store(:_set_#{node.name}, {")
    nest {
      nest {
        printiln(":arg_list => [")
        nest {
          printiln("['val', CORBA::ARG_IN, #{get_typecode(node.idltype)}]],");
        }
        printiln(":result_type => CORBA._tc_void,")
        if node.set_raises.size>0
          printi(":exc_list => [")
          pfx = ""
          nest {
            node.set_raises.each { |ex|
              println(pfx)
              pfx = ","
              printi(get_typecode(ex))
            }
            println("],")
          }
        end
        printiln(":op_sym => :#{node.rubyname}= })")
      }
    }
    println()

    printiln("def #{node.rubyname}=(val)")
    nest {
      printiln("raise ::CORBA::NO_IMPLEMENT.new(")
      printiln("         'unimplemented servant attribute set',")
      printiln("         1, ::CORBA::COMPLETED_NO)")
    }
    printiln("end #of attribute set_#{node.name}")
    println()
  end
end

#visit_const(node) ⇒ Object



1253
1254
# File 'lib/ridlbe/ruby/walker.rb', line 1253

def visit_const(node)
end

#visit_enum(node) ⇒ Object



1572
1573
# File 'lib/ridlbe/ruby/walker.rb', line 1572

def visit_enum(node)
end

#visit_enumerator(node) ⇒ Object



1575
1576
# File 'lib/ridlbe/ruby/walker.rb', line 1575

def visit_enumerator(node)
end

#visit_include(node) ⇒ Object



1149
1150
1151
1152
# File 'lib/ridlbe/ruby/walker.rb', line 1149

def visit_include(node)
  printiln(format("require '%s'", node.filename.sub(/\.[^\.]*$/,@params[:srv_pfx])))
  println()
end

#visit_operation(node) ⇒ Object



1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
# File 'lib/ridlbe/ruby/walker.rb', line 1256

def visit_operation(node)
  _parm = node.params
  _in = node.in_params
  _out = node.out_params
  _intf = node.enclosure

  ## do nothing for valuetypes or local interfaces
  return nil if _intf.is_a?(IDL::AST::Valuetype) || _intf.is_local?

  printi("Operations.store(:#{node.name}, {")
  newln = ""
  if _parm.size>0
    println(newln)
    nest do
      printi(":arg_list => [")
      nest {
        pfx = ""
        _parm.each do |p|
          println(pfx)
          printi("['#{p.rubyname}', #{get_arg_type(p.attribute)}, ");
          print(get_typecode(p.idltype))
          print("]")
          pfx = ","
        end
        print("]")
      }
    end
    newln = ","
  end

  if not node.oneway
    println(newln)
    nest { printi(":result_type => #{get_typecode(node.idltype)}") }
    newln = ","
  end

  if node.raises.size>0
    println(newln)
    nest {
      printi(":exc_list => [")
      pfx = ""
      nest {
        node.raises.each { |ex|
          println(pfx)
          pfx = ","
          printi(get_typecode(ex))
        }
        print("]")
      }
    }
    newln = ","
  end

  if node.rubyname != node.name
    println(newln)
    nest { printi(":op_sym => :#{node.rubyname}") }
  end
  println("})")
  println()

  printi("def #{node.rubyname}(")
  print( _in.collect{ |p| p.rubyname }.join(", ") )
  if node.oneway
    println(")    # oneway")
  else
    println(")")
  end
  nest {
    printiln("raise ::CORBA::NO_IMPLEMENT.new(")
    printiln("         'unimplemented servant operation',")
    printiln("         1, ::CORBA::COMPLETED_NO)")
  }
  printiln("end")
  println()
end

#visit_typedef(node) ⇒ Object



1578
1579
# File 'lib/ridlbe/ruby/walker.rb', line 1578

def visit_typedef(node)
end

#visit_valuebox(node) ⇒ Object



1250
1251
# File 'lib/ridlbe/ruby/walker.rb', line 1250

def visit_valuebox(node)
end