added hostid function (artf724006)
authorebensza <bence.janos.szabo@ericsson.com>
Thu, 5 May 2016 08:20:05 +0000 (10:20 +0200)
committerebensza <bence.janos.szabo@ericsson.com>
Thu, 5 May 2016 08:20:05 +0000 (10:20 +0200)
Signed-off-by: ebensza <bence.janos.szabo@ericsson.com>
compiler2/Value.cc
compiler2/Value.hh
compiler2/ttcn3/compiler.l
compiler2/ttcn3/compiler.y
core/Communication.hh
core/Runtime.cc
core/Runtime.hh
regression_test/Makefile
regression_test/hostid/Hostid.ttcn [new file with mode: 0644]
regression_test/hostid/Makefile [new file with mode: 0644]

index 2fe448ea9d8dce0c4267b9cd6fb1db9cbba602b1..af36e4ef7d0c8fc179a41af45b293ec11d2583f4 100644 (file)
@@ -211,6 +211,9 @@ namespace Common {
       case OPTYPE_REMOVE_BOM:
         u.expr.v1=p.u.expr.v1->clone();
         break;
+      case OPTYPE_HOSTID: // [v1]
+        u.expr.v1=p.u.expr.v1?p.u.expr.v1->clone():0;
+        break;
       case OPTYPE_ADD: // v1 v2
       case OPTYPE_SUBTRACT:
       case OPTYPE_MULTIPLY:
@@ -528,6 +531,7 @@ namespace Common {
     case OPTYPE_REMOVE_BOM:
     case OPTYPE_GET_STRINGENCODING:
     case OPTYPE_DECODE_BASE64:
+    case OPTYPE_HOSTID:
       delete u.expr.v1;
       break;
     case OPTYPE_ADD: // v1 v2
@@ -922,6 +926,9 @@ namespace Common {
       if(!p_v1) FATAL_ERROR("Value::Value()");
       u.expr.v1=p_v1;
       break;
+    case OPTYPE_HOSTID:
+      u.expr.v1=p_v1;
+      break;
     default:
       FATAL_ERROR("Value::Value()");
     } // switch
@@ -1534,6 +1541,9 @@ namespace Common {
     case OPTYPE_DECODE_BASE64:
       u.expr.v1->set_fullname(p_fullname+".<operand>");
       break;
+    case OPTYPE_HOSTID: // [v1]
+      if(u.expr.v1) u.expr.v1->set_fullname(p_fullname+".<operand>");
+      break;
     case OPTYPE_ADD: // v1 v2
     case OPTYPE_SUBTRACT:
     case OPTYPE_MULTIPLY:
@@ -1729,6 +1739,9 @@ namespace Common {
     case OPTYPE_DECODE_BASE64:
       u.expr.v1->set_my_scope(p_scope);
       break;
+    case OPTYPE_HOSTID: // [v1]
+      if(u.expr.v1) u.expr.v1->set_my_scope(p_scope);
+      break;
     case OPTYPE_ADD: // v1 v2
     case OPTYPE_SUBTRACT:
     case OPTYPE_MULTIPLY:
@@ -2054,6 +2067,9 @@ namespace Common {
       case OPTYPE_REMOVE_BOM:
         u.expr.v1->set_code_section(p_code_section);
         break;
+      case OPTYPE_HOSTID: // [v1]
+        if(u.expr.v1) u.expr.v1->set_code_section(p_code_section);
+        break;
       case OPTYPE_ADD: // v1 v2
       case OPTYPE_SUBTRACT:
       case OPTYPE_MULTIPLY:
@@ -3167,6 +3183,7 @@ namespace Common {
       case OPTYPE_TTCN2STRING:
       case OPTYPE_GET_STRINGENCODING:
       case OPTYPE_ENCODE_BASE64:
+      case OPTYPE_HOSTID:
         return Type::T_CSTR;
       case OPTYPE_INT2UNICHAR:
       case OPTYPE_OCT2UNICHAR:
@@ -3530,6 +3547,8 @@ namespace Common {
       return "encode_base64()";
     case OPTYPE_DECODE_BASE64:
       return "decode_base64()";
+    case OPTYPE_HOSTID: // [v1]
+      return "hostid()";
     case OPTYPE_ADD: // v1 v2
       return "+";
     case OPTYPE_SUBTRACT:
@@ -6592,6 +6611,17 @@ error:
         chk_expr_val_ustr_7bitchars(v1, the, opname);
       }
       break;
+    case OPTYPE_HOSTID:
+      v1=u.expr.v1 ? u.expr.v1 : 0;
+      if (v1)
+      {
+        Error_Context cntxt(this, "In the first operand of operation `%s'", opname);
+        v1->set_lowerid_to_ref();
+        tt1=v1->get_expr_returntype(exp_val);
+        chk_expr_operandtype_cstr(tt1, second, opname, v1);
+        chk_expr_eval_value(v1, t_chk, refch, exp_val);
+      }
+      break;
     case OPTYPE_UNICHAR2OCT: // v1 [v2]
       v1=u.expr.v1;
       {
@@ -7268,6 +7298,7 @@ error:
     case OPTYPE_DECVALUE_UNICHAR:
     case OPTYPE_CHECKSTATE_ANY:
     case OPTYPE_CHECKSTATE_ALL:
+    case OPTYPE_HOSTID:
       break;
     case OPTYPE_TESTCASENAME: { // -
       if (!my_scope) FATAL_ERROR("Value::evaluate_value()");
@@ -8568,6 +8599,7 @@ error:
       case OPTYPE_DECVALUE_UNICHAR:
       case OPTYPE_CHECKSTATE_ANY:
       case OPTYPE_CHECKSTATE_ALL:
+      case OPTYPE_HOSTID:
         return true;
       case OPTYPE_COMP_NULL: // -
         return false;
@@ -9782,6 +9814,13 @@ error:
       u.expr.t1->chk_recursions(refch);
       refch.prev_state();
       break;
+    case OPTYPE_HOSTID: // [v1]
+      if (u.expr.v1) {
+        refch.mark_state();
+        u.expr.v1->chk_recursions(refch);
+        refch.prev_state();
+      }
+      break;
     case OPTYPE_ADD: // v1 v2
     case OPTYPE_SUBTRACT:
     case OPTYPE_MULTIPLY:
@@ -10140,6 +10179,9 @@ error:
     case OPTYPE_REMOVE_BOM:
       self_ref |= chk_expr_self_ref_val(u.expr.v1, lhs);
       break;
+    case OPTYPE_HOSTID: // [v1]
+      if (u.expr.v1) self_ref |= chk_expr_self_ref_val(u.expr.v1, lhs);
+      break;
     case OPTYPE_ADD: // v1 v2
     case OPTYPE_SUBTRACT: // v1 v2
     case OPTYPE_MULTIPLY: // v1 v2
@@ -10520,6 +10562,10 @@ error:
          if (u.expr.v2) return create_stringRepr_predef2("encvalue_unichar");
          else return create_stringRepr_predef1("encvalue_unichar");
       }
+      case OPTYPE_HOSTID: {
+        if (u.expr.v1) return create_stringRepr_predef1("hostid");   
+        else return string("hostid()");
+      }
       case OPTYPE_DECVALUE_UNICHAR: {
          if (u.expr.v3) {
            string ret_val("decvalue_unichar");
@@ -11482,6 +11528,9 @@ error:
         ass = u.expr.r2->get_refd_assignment();
         if (parlist) str = parlist->rearrange_init_code(str, usage_mod);
         break; }
+      case OPTYPE_HOSTID:    
+        if (u.expr.v1) str = u.expr.v1->rearrange_init_code(str, usage_mod);
+        break;
       case OPTYPE_ADD:
       case OPTYPE_SUBTRACT:
       case OPTYPE_MULTIPLY:
@@ -11791,6 +11840,9 @@ error:
     case OPTYPE_DECVALUE_UNICHAR:
         generate_code_expr_decvalue_unichar(expr);
       break;
+    case OPTYPE_HOSTID:
+      generate_code_expr_hostid(expr);
+      break;
     case OPTYPE_OCT2HEX:
       generate_code_expr_predef1(expr, "oct2hex", u.expr.v1);
       break;
@@ -12921,6 +12973,14 @@ void Value::generate_code_expr_encvalue_unichar(expression_struct *expr)
     expr->expr = mputstr(expr->expr, ")");
   }
   
+  void Value::generate_code_expr_hostid(expression_struct *expr)
+  {
+    expr->expr = mputstr(expr->expr, "TTCN_Runtime::get_host_address(");
+    if (u.expr.v1) u.expr.v1->generate_code_expr_mandatory(expr);
+    else expr->expr = mputstr(expr->expr, "CHARSTRING(\"Ipv4orIpv6\")");
+    expr->expr = mputstr(expr->expr, ")");
+  }
+  
   char* Value::generate_code_char_coding_check(expression_struct *expr, Value *v, const char *name)
   {
     expression_struct expr2;
@@ -13599,6 +13659,7 @@ void Value::generate_code_expr_encvalue_unichar(expression_struct *expr)
     case OPTYPE_PROF_RUNNING:
     case OPTYPE_CHECKSTATE_ANY:
     case OPTYPE_CHECKSTATE_ALL:
+    case OPTYPE_HOSTID:
       return true;
     case OPTYPE_ENCODE:
     case OPTYPE_DECODE:
index d3c744fde9f9d5b71853463f8b19558a508dce92..4e0897ee1865b45ad6785ada8d1049341bedcbeb 100644 (file)
@@ -267,6 +267,7 @@ namespace Common {
       OPTYPE_ANY2UNISTR, // logarg, length = 1
       OPTYPE_CHECKSTATE_ANY, // [r1] v2, port or any
       OPTYPE_CHECKSTATE_ALL, // [r1] v2, port or all
+      OPTYPE_HOSTID, // [v1]
 
       NUMBER_OF_OPTYPES // must be last
     };
@@ -400,7 +401,7 @@ namespace Common {
      *  COMP_ALIVE_ALL, COMP_ALIVE_ANY, TMR_RUNNING_ANY, GETVERDICT,
      *  PROF_RUNNING */
     Value(operationtype_t p_optype);
-    /** Constructor used by V_EXPR "v1" */
+    /** Constructor used by V_EXPR "v1" or [v1] */
     Value(operationtype_t p_optype, Value *p_v1);
     /** Constructor used by V_EXPR "ti1": LENGTHOF, SIZEOF, VALUEOF, TTCN2STRING */
     Value(operationtype_t p_optype, TemplateInstance *p_ti1);
@@ -927,6 +928,8 @@ namespace Common {
     
     void generate_code_expr_checkstate(expression_struct *expr);
     
+    void generate_code_expr_hostid(expression_struct *expr);
+    
     char* generate_code_char_coding_check(expression_struct *expr, Value *v, const char *name);
 
     /** Helper function for \a generate_code_init(). It handles the
index 730ab7037e075e0464ab6070ce08f79dd10ca62b..7442e68b77273593e81f126c1e1b6b23dd90f3c4 100644 (file)
@@ -589,6 +589,7 @@ decode_base64       RETURN(decode_base64KeyWord);
 encvalue_unichar RETURN(encvalue_unicharKeyWord);
 decvalue_unichar RETURN(decvalue_unicharKeyWord);
 any2unistr      RETURN(any2unistrKeyWord);
+hostid          RETURN(hostidKeyWord);
 
        /* Values */
 
index 42c3cb2ba1742c2531f807302989c5c6661e9c85..66ef6511efbbb112da8f97cb6506bb079cf7f9d8 100644 (file)
@@ -837,6 +837,7 @@ static const string anyname("anytype");
 %token encvalue_unicharKeyWord
 %token decvalue_unicharKeyWord
 %token any2unistrKeyWord
+%token hostidKeyWord
 
 /* Multi-character operators */
 
@@ -8807,6 +8808,17 @@ PredefinedOps:
     $$ = new Value(Value::V_ERROR);
     $$->set_location(infile, @$);
   }
+| hostidKeyWord '(' ')'
+  {
+    Value *null_value = NULL;
+    $$ = new Value(Value::OPTYPE_HOSTID, null_value);
+    $$->set_location(infile, @$);
+  }
+| hostidKeyWord '(' optError Expression optError ')'
+  {
+    $$ = new Value(Value::OPTYPE_HOSTID, $4);
+    $$->set_location(infile, @$);
+  }
 ;
 
 DecValueArg:
index 2d51295e1966d1420fd7f13e8bcb718e559d7291..1147128f8e09dc8b72b83e09e9d08e34ab26b0c4 100644 (file)
@@ -13,6 +13,7 @@
  *   Forstner, Matyas
  *   Kovacs, Ferenc
  *   Raduly, Csaba
+ *   Szabo, Bence Janos
  *   Szabo, Janos Zoltan – initial implementation
  *   Zalanyi, Balazs Andor
  *
@@ -43,6 +44,7 @@ class TTCN_Communication {
 
 public:
   static const NetworkFamily& get_network_family() { return hcnh.get_family(); }
+  static bool has_local_address() { return local_addr_set; }
   static void set_local_address(const char *host_name);
   static const IPAddress *get_local_address();
   static void set_mc_address(const char *host_name,
index 625004e51026862447a3f43257fb10dc91c14aa8..8536d41d10d4a2d37783d3b43f641186fd95752a 100644 (file)
@@ -16,6 +16,7 @@
  *   Lovassy, Arpad
  *   Raduly, Csaba
  *   Szabados, Kristof
+ *   Szabo, Bence Janos
  *   Szabo, Janos Zoltan – initial implementation
  *   Zalanyi, Balazs Andor
  *   Pandi, Krisztian
@@ -281,6 +282,35 @@ const char *TTCN_Runtime::get_host_name()
   return host_name;
 }
 
+CHARSTRING TTCN_Runtime::get_host_address(const CHARSTRING& type)
+{
+  if (type != "Ipv4orIpv6" && type != "Ipv4" && type != "Ipv6") {
+      TTCN_error("The argument of hostid function must be Ipv4orIpv6 or Ipv4"
+        "or Ipv6. %s is not a valid argument.", (const char*)type);
+  }
+  
+  // Return empty if no host address could be retrieved
+  if (!TTCN_Communication::has_local_address()) {
+    return CHARSTRING("");
+  }
+  const IPAddress *address = TTCN_Communication::get_local_address();
+  
+  // Return empty if the type is not matching the address type
+  if (type == "Ipv4") {
+    const IPv4Address * ipv4 = dynamic_cast<const IPv4Address*>(address);
+    if (ipv4 == NULL) {
+      return CHARSTRING("");
+    }
+  } else if (type == "Ipv6") {
+    const IPv6Address * ipv6 = dynamic_cast<const IPv6Address*>(address);
+    if (ipv6 == NULL) {
+      return CHARSTRING("");
+    }
+  }
+  // Return the string representation of the address
+  return CHARSTRING(address->get_addr_str());
+}
+
 CHARSTRING TTCN_Runtime::get_testcase_id_macro()
 {
   if (in_controlpart()) TTCN_error("Macro %%testcaseId cannot be used from "
index 97e6d30d7c0bf1f719d8d40ccb61d57a667531f5..282a531a52852d0ed3e6c2311919270030313293 100644 (file)
@@ -14,6 +14,7 @@
  *   Kovacs, Ferenc
  *   Raduly, Csaba
  *   Szabados, Kristof
+ *   Szabo, Bence Janos
  *   Szabo, Janos Zoltan – initial implementation
  *   Zalanyi, Balazs Andor
  *
@@ -153,6 +154,8 @@ public:
 
   /// Returns a string which must not be freed.
   static const char *get_host_name();
+  
+  static CHARSTRING get_host_address(const CHARSTRING& type);
 
   static CHARSTRING get_testcase_id_macro();
   static CHARSTRING get_testcasename();
index 97e3cca651445182e659ecd6fb667d2e4bc00060..e359cf66841accd94e209c026636c796aa67525c 100644 (file)
@@ -46,7 +46,7 @@ assignmentNotation omitdef anytype RAW implicitMsgEncoding pattern_quadruples \
 macros visibility hexstrOper ucharstrOper objidOper CRTR00015758 slider \
 XML ipv6 implicitOmit testcase_defparam transparent HQ16404 cfgFile \
 all_from lazyEval tryCatch text2ttcn json junitlogger ttcn2json profiler templateOmit \
-customEncoding makefilegen uidChars checkstate
+customEncoding makefilegen uidChars checkstate hostid
 
 ifdef DYN
 DIRS += loggerplugin
diff --git a/regression_test/hostid/Hostid.ttcn b/regression_test/hostid/Hostid.ttcn
new file mode 100644 (file)
index 0000000..be2a1bc
--- /dev/null
@@ -0,0 +1,69 @@
+/******************************************************************************
+ * Copyright (c) 2000-2016 Ericsson Telecom AB
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Szabo, Bence Janos
+ *
+ ******************************************************************************/
+
+module Hostid
+{
+    type port MyPort message    
+    {
+    inout charstring
+    } with { extension "internal" }
+
+    type component MyMTCType
+    {
+        port MyPort MyPCO1;
+    }
+
+    type component MTCType
+    {
+        port MyPort MyPCO2;
+    }
+
+    testcase tc_hostid() runs on MyMTCType system MTCType
+    {
+        // ip should be "127.0.0.1" aka localhost, I think...
+        var charstring ip := hostid("Ipv4orIpv6");
+        log(ip);
+        if(ip != "") {
+            setverdict(pass);
+        }else {
+            setverdict(fail);
+        }
+
+        // The same as hostid("Ipv4orIpv6")
+        ip := hostid();
+        log(ip);
+        if(ip != "") {
+            setverdict(pass);
+        }else {
+            setverdict(fail);
+        }
+
+        ip := hostid("Ipv4");
+        if(ip != "") {
+            setverdict(pass);
+        }else {
+            setverdict(fail);
+        }
+
+        ip := hostid("Ipv6");
+        if(ip == "") {
+            setverdict(pass);
+        }else {
+            setverdict(fail);
+        }
+    }
+   
+    control
+    {
+        execute(tc_hostid());
+    }
+}
diff --git a/regression_test/hostid/Makefile b/regression_test/hostid/Makefile
new file mode 100644 (file)
index 0000000..614df8f
--- /dev/null
@@ -0,0 +1,56 @@
+##############################################################################
+# Copyright (c) 2000-2016 Ericsson Telecom AB
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Eclipse Public License v1.0
+# which accompanies this distribution, and is available at
+# http://www.eclipse.org/legal/epl-v10.html
+#
+# Contributors:
+#   Szabo, Bence Janos
+#
+##############################################################################
+TOPDIR := ..
+include $(TOPDIR)/Makefile.regression
+
+.SUFFIXES: .ttcn .hh
+.PHONY: all clean dep run
+
+TTCN3_LIB = ttcn3$(RT2_SUFFIX)-parallel$(DYNAMIC_SUFFIX)
+
+TTCN3_MODULES = Hostid.ttcn
+
+GENERATED_SOURCES = $(TTCN3_MODULES:.ttcn=.cc)
+GENERATED_HEADERS = $(GENERATED_SOURCES:.cc=.hh)
+ifdef CODE_SPLIT
+GENERATED_SOURCES := $(foreach file, $(GENERATED_SOURCES:.cc=), $(addprefix $(file), .cc _seq.cc _set.cc  _seqof.cc _setof.cc _union.cc))
+endif
+
+USER_SOURCES =
+
+OBJECTS = $(GENERATED_SOURCES:.cc=.o) $(USER_SOURCES:.cc=.o)
+
+TARGET = Hostid$(EXESUFFIX)
+
+all: $(TARGET)
+
+$(TARGET): $(GENERATED_SOURCES) $(USER_SOURCES)
+       $(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -o $@ $^ -L$(TTCN3_DIR)/lib -l$(TTCN3_LIB) -L$(OPENSSL_DIR)/lib -lcrypto $($(PLATFORM)_LIBS)
+
+$(GENERATED_SOURCES) $(GENERATED_HEADERS): compile
+       @if [ ! -f $@ ]; then $(RM) compile; $(MAKE) compile; fi
+
+compile: $(TTCN3_MODULES) $(ASN1_MODULES)
+       $(filter-out -Nold -E, $(TTCN3_COMPILER)) $(COMPILER_FLAGS) $^ 
+       touch compile
+       
+clean distclean:
+       -rm -f $(TARGET) $(OBJECTS) $(GENERATED_HEADERS) \
+       $(GENERATED_SOURCES) *.log Makefile.bak
+
+dep: $(GENERATED_SOURCES)
+       makedepend $(CPPFLAGS) $(GENERATED_SOURCES)
+
+run: $(TARGET)
+       $(TTCN3_DIR)/bin/ttcn3_start $^
+
+
This page took 0.035364 seconds and 5 git commands to generate.