diff options
Diffstat (limited to 'lib/jinterface')
74 files changed, 8488 insertions, 6443 deletions
diff --git a/lib/jinterface/.classpath b/lib/jinterface/.classpath new file mode 100644 index 0000000000..9785e55986 --- /dev/null +++ b/lib/jinterface/.classpath @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" path="java_src"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> + <classpathentry kind="output" path="priv"/> +</classpath> diff --git a/lib/jinterface/.gitignore b/lib/jinterface/.gitignore new file mode 100644 index 0000000000..3418d354a9 --- /dev/null +++ b/lib/jinterface/.gitignore @@ -0,0 +1,2 @@ +priv/ + diff --git a/lib/jinterface/.project b/lib/jinterface/.project new file mode 100644 index 0000000000..450b96dc12 --- /dev/null +++ b/lib/jinterface/.project @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>jinterface</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> diff --git a/lib/jinterface/.settings/org.eclipse.jdt.core.prefs b/lib/jinterface/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..e8b3772a8a --- /dev/null +++ b/lib/jinterface/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,296 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.5 +org.eclipse.jdt.core.formatter.align_type_members_on_columns=false +org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression=16 +org.eclipse.jdt.core.formatter.alignment_for_arguments_in_annotation=0 +org.eclipse.jdt.core.formatter.alignment_for_arguments_in_enum_constant=16 +org.eclipse.jdt.core.formatter.alignment_for_arguments_in_explicit_constructor_call=16 +org.eclipse.jdt.core.formatter.alignment_for_arguments_in_method_invocation=16 +org.eclipse.jdt.core.formatter.alignment_for_arguments_in_qualified_allocation_expression=16 +org.eclipse.jdt.core.formatter.alignment_for_assignment=0 +org.eclipse.jdt.core.formatter.alignment_for_binary_expression=16 +org.eclipse.jdt.core.formatter.alignment_for_compact_if=16 +org.eclipse.jdt.core.formatter.alignment_for_conditional_expression=80 +org.eclipse.jdt.core.formatter.alignment_for_enum_constants=0 +org.eclipse.jdt.core.formatter.alignment_for_expressions_in_array_initializer=16 +org.eclipse.jdt.core.formatter.alignment_for_method_declaration=0 +org.eclipse.jdt.core.formatter.alignment_for_multiple_fields=16 +org.eclipse.jdt.core.formatter.alignment_for_parameters_in_constructor_declaration=16 +org.eclipse.jdt.core.formatter.alignment_for_parameters_in_method_declaration=16 +org.eclipse.jdt.core.formatter.alignment_for_resources_in_try=80 +org.eclipse.jdt.core.formatter.alignment_for_selector_in_method_invocation=16 +org.eclipse.jdt.core.formatter.alignment_for_superclass_in_type_declaration=16 +org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_enum_declaration=16 +org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_type_declaration=16 +org.eclipse.jdt.core.formatter.alignment_for_throws_clause_in_constructor_declaration=16 +org.eclipse.jdt.core.formatter.alignment_for_throws_clause_in_method_declaration=16 +org.eclipse.jdt.core.formatter.alignment_for_union_type_in_multicatch=16 +org.eclipse.jdt.core.formatter.blank_lines_after_imports=1 +org.eclipse.jdt.core.formatter.blank_lines_after_package=1 +org.eclipse.jdt.core.formatter.blank_lines_before_field=0 +org.eclipse.jdt.core.formatter.blank_lines_before_first_class_body_declaration=0 +org.eclipse.jdt.core.formatter.blank_lines_before_imports=1 +org.eclipse.jdt.core.formatter.blank_lines_before_member_type=1 +org.eclipse.jdt.core.formatter.blank_lines_before_method=1 +org.eclipse.jdt.core.formatter.blank_lines_before_new_chunk=1 +org.eclipse.jdt.core.formatter.blank_lines_before_package=0 +org.eclipse.jdt.core.formatter.blank_lines_between_import_groups=1 +org.eclipse.jdt.core.formatter.blank_lines_between_type_declarations=1 +org.eclipse.jdt.core.formatter.brace_position_for_annotation_type_declaration=end_of_line +org.eclipse.jdt.core.formatter.brace_position_for_anonymous_type_declaration=end_of_line +org.eclipse.jdt.core.formatter.brace_position_for_array_initializer=end_of_line +org.eclipse.jdt.core.formatter.brace_position_for_block=end_of_line +org.eclipse.jdt.core.formatter.brace_position_for_block_in_case=end_of_line +org.eclipse.jdt.core.formatter.brace_position_for_constructor_declaration=end_of_line +org.eclipse.jdt.core.formatter.brace_position_for_enum_constant=end_of_line +org.eclipse.jdt.core.formatter.brace_position_for_enum_declaration=end_of_line +org.eclipse.jdt.core.formatter.brace_position_for_lambda_body=end_of_line +org.eclipse.jdt.core.formatter.brace_position_for_method_declaration=end_of_line +org.eclipse.jdt.core.formatter.brace_position_for_switch=end_of_line +org.eclipse.jdt.core.formatter.brace_position_for_type_declaration=end_of_line +org.eclipse.jdt.core.formatter.comment.clear_blank_lines_in_block_comment=false +org.eclipse.jdt.core.formatter.comment.clear_blank_lines_in_javadoc_comment=false +org.eclipse.jdt.core.formatter.comment.format_block_comments=true +org.eclipse.jdt.core.formatter.comment.format_header=false +org.eclipse.jdt.core.formatter.comment.format_html=true +org.eclipse.jdt.core.formatter.comment.format_javadoc_comments=true +org.eclipse.jdt.core.formatter.comment.format_line_comments=true +org.eclipse.jdt.core.formatter.comment.format_source_code=true +org.eclipse.jdt.core.formatter.comment.indent_parameter_description=true +org.eclipse.jdt.core.formatter.comment.indent_root_tags=true +org.eclipse.jdt.core.formatter.comment.insert_new_line_before_root_tags=insert +org.eclipse.jdt.core.formatter.comment.insert_new_line_for_parameter=insert +org.eclipse.jdt.core.formatter.comment.line_length=80 +org.eclipse.jdt.core.formatter.comment.new_lines_at_block_boundaries=true +org.eclipse.jdt.core.formatter.comment.new_lines_at_javadoc_boundaries=true +org.eclipse.jdt.core.formatter.comment.preserve_white_space_between_code_and_line_comments=true +org.eclipse.jdt.core.formatter.compact_else_if=true +org.eclipse.jdt.core.formatter.continuation_indentation=2 +org.eclipse.jdt.core.formatter.continuation_indentation_for_array_initializer=2 +org.eclipse.jdt.core.formatter.disabling_tag=@formatter\:off +org.eclipse.jdt.core.formatter.enabling_tag=@formatter\:on +org.eclipse.jdt.core.formatter.format_guardian_clause_on_one_line=false +org.eclipse.jdt.core.formatter.format_line_comment_starting_on_first_column=true +org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_annotation_declaration_header=true +org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_enum_constant_header=true +org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_enum_declaration_header=true +org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_type_header=true +org.eclipse.jdt.core.formatter.indent_breaks_compare_to_cases=true +org.eclipse.jdt.core.formatter.indent_empty_lines=false +org.eclipse.jdt.core.formatter.indent_statements_compare_to_block=true +org.eclipse.jdt.core.formatter.indent_statements_compare_to_body=true +org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_cases=true +org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_switch=false +org.eclipse.jdt.core.formatter.indentation.size=4 +org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_field=insert +org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_local_variable=insert +org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_method=insert +org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_package=insert +org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_parameter=do not insert +org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_type=insert +org.eclipse.jdt.core.formatter.insert_new_line_after_label=do not insert +org.eclipse.jdt.core.formatter.insert_new_line_after_opening_brace_in_array_initializer=do not insert +org.eclipse.jdt.core.formatter.insert_new_line_after_type_annotation=do not insert +org.eclipse.jdt.core.formatter.insert_new_line_at_end_of_file_if_missing=insert +org.eclipse.jdt.core.formatter.insert_new_line_before_catch_in_try_statement=do not insert +org.eclipse.jdt.core.formatter.insert_new_line_before_closing_brace_in_array_initializer=do not insert +org.eclipse.jdt.core.formatter.insert_new_line_before_else_in_if_statement=do not insert +org.eclipse.jdt.core.formatter.insert_new_line_before_finally_in_try_statement=do not insert +org.eclipse.jdt.core.formatter.insert_new_line_before_while_in_do_statement=do not insert +org.eclipse.jdt.core.formatter.insert_new_line_in_empty_annotation_declaration=insert +org.eclipse.jdt.core.formatter.insert_new_line_in_empty_anonymous_type_declaration=insert +org.eclipse.jdt.core.formatter.insert_new_line_in_empty_block=insert +org.eclipse.jdt.core.formatter.insert_new_line_in_empty_enum_constant=insert +org.eclipse.jdt.core.formatter.insert_new_line_in_empty_enum_declaration=insert +org.eclipse.jdt.core.formatter.insert_new_line_in_empty_method_body=insert +org.eclipse.jdt.core.formatter.insert_new_line_in_empty_type_declaration=insert +org.eclipse.jdt.core.formatter.insert_space_after_and_in_type_parameter=insert +org.eclipse.jdt.core.formatter.insert_space_after_assignment_operator=insert +org.eclipse.jdt.core.formatter.insert_space_after_at_in_annotation=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_at_in_annotation_type_declaration=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_binary_operator=insert +org.eclipse.jdt.core.formatter.insert_space_after_closing_angle_bracket_in_type_arguments=insert +org.eclipse.jdt.core.formatter.insert_space_after_closing_angle_bracket_in_type_parameters=insert +org.eclipse.jdt.core.formatter.insert_space_after_closing_brace_in_block=insert +org.eclipse.jdt.core.formatter.insert_space_after_closing_paren_in_cast=insert +org.eclipse.jdt.core.formatter.insert_space_after_colon_in_assert=insert +org.eclipse.jdt.core.formatter.insert_space_after_colon_in_case=insert +org.eclipse.jdt.core.formatter.insert_space_after_colon_in_conditional=insert +org.eclipse.jdt.core.formatter.insert_space_after_colon_in_for=insert +org.eclipse.jdt.core.formatter.insert_space_after_colon_in_labeled_statement=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_allocation_expression=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_annotation=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_array_initializer=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_constructor_declaration_parameters=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_constructor_declaration_throws=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_enum_constant_arguments=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_enum_declarations=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_explicitconstructorcall_arguments=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_for_increments=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_for_inits=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_method_declaration_parameters=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_method_declaration_throws=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_method_invocation_arguments=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_multiple_field_declarations=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_multiple_local_declarations=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_parameterized_type_reference=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_superinterfaces=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_type_arguments=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_type_parameters=insert +org.eclipse.jdt.core.formatter.insert_space_after_ellipsis=insert +org.eclipse.jdt.core.formatter.insert_space_after_lambda_arrow=insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_parameterized_type_reference=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_type_arguments=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_type_parameters=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_brace_in_array_initializer=insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_bracket_in_array_allocation_expression=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_bracket_in_array_reference=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_annotation=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_cast=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_catch=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_constructor_declaration=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_enum_constant=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_for=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_if=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_method_declaration=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_method_invocation=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_parenthesized_expression=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_switch=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_synchronized=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_try=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_while=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_postfix_operator=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_prefix_operator=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_question_in_conditional=insert +org.eclipse.jdt.core.formatter.insert_space_after_question_in_wildcard=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_semicolon_in_for=insert +org.eclipse.jdt.core.formatter.insert_space_after_semicolon_in_try_resources=insert +org.eclipse.jdt.core.formatter.insert_space_after_unary_operator=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_and_in_type_parameter=insert +org.eclipse.jdt.core.formatter.insert_space_before_assignment_operator=insert +org.eclipse.jdt.core.formatter.insert_space_before_at_in_annotation_type_declaration=insert +org.eclipse.jdt.core.formatter.insert_space_before_binary_operator=insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_angle_bracket_in_parameterized_type_reference=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_angle_bracket_in_type_arguments=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_angle_bracket_in_type_parameters=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_brace_in_array_initializer=insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_bracket_in_array_allocation_expression=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_bracket_in_array_reference=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_annotation=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_cast=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_catch=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_constructor_declaration=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_enum_constant=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_for=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_if=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_method_declaration=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_method_invocation=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_parenthesized_expression=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_switch=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_synchronized=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_try=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_while=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_colon_in_assert=insert +org.eclipse.jdt.core.formatter.insert_space_before_colon_in_case=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_colon_in_conditional=insert +org.eclipse.jdt.core.formatter.insert_space_before_colon_in_default=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_colon_in_for=insert +org.eclipse.jdt.core.formatter.insert_space_before_colon_in_labeled_statement=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_allocation_expression=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_annotation=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_array_initializer=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_constructor_declaration_parameters=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_constructor_declaration_throws=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_enum_constant_arguments=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_enum_declarations=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_explicitconstructorcall_arguments=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_for_increments=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_for_inits=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_method_declaration_parameters=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_method_declaration_throws=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_method_invocation_arguments=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_multiple_field_declarations=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_multiple_local_declarations=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_parameterized_type_reference=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_superinterfaces=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_type_arguments=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_type_parameters=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_ellipsis=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_lambda_arrow=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_parameterized_type_reference=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_type_arguments=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_type_parameters=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_annotation_type_declaration=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_anonymous_type_declaration=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_array_initializer=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_block=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_constructor_declaration=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_enum_constant=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_enum_declaration=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_method_declaration=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_switch=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_type_declaration=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_bracket_in_array_allocation_expression=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_bracket_in_array_reference=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_bracket_in_array_type_reference=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_annotation=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_annotation_type_member_declaration=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_catch=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_constructor_declaration=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_enum_constant=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_for=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_if=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_method_declaration=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_method_invocation=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_parenthesized_expression=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_switch=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_synchronized=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_try=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_while=insert +org.eclipse.jdt.core.formatter.insert_space_before_parenthesized_expression_in_return=insert +org.eclipse.jdt.core.formatter.insert_space_before_parenthesized_expression_in_throw=insert +org.eclipse.jdt.core.formatter.insert_space_before_postfix_operator=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_prefix_operator=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_question_in_conditional=insert +org.eclipse.jdt.core.formatter.insert_space_before_question_in_wildcard=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_semicolon=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_semicolon_in_for=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_semicolon_in_try_resources=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_unary_operator=do not insert +org.eclipse.jdt.core.formatter.insert_space_between_brackets_in_array_type_reference=do not insert +org.eclipse.jdt.core.formatter.insert_space_between_empty_braces_in_array_initializer=do not insert +org.eclipse.jdt.core.formatter.insert_space_between_empty_brackets_in_array_allocation_expression=do not insert +org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_annotation_type_member_declaration=do not insert +org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_constructor_declaration=do not insert +org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_enum_constant=do not insert +org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_declaration=do not insert +org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_invocation=do not insert +org.eclipse.jdt.core.formatter.join_lines_in_comments=true +org.eclipse.jdt.core.formatter.join_wrapped_lines=true +org.eclipse.jdt.core.formatter.keep_else_statement_on_same_line=false +org.eclipse.jdt.core.formatter.keep_empty_array_initializer_on_one_line=false +org.eclipse.jdt.core.formatter.keep_imple_if_on_one_line=false +org.eclipse.jdt.core.formatter.keep_then_statement_on_same_line=false +org.eclipse.jdt.core.formatter.lineSplit=80 +org.eclipse.jdt.core.formatter.never_indent_block_comments_on_first_column=false +org.eclipse.jdt.core.formatter.never_indent_line_comments_on_first_column=false +org.eclipse.jdt.core.formatter.number_of_blank_lines_at_beginning_of_method_body=0 +org.eclipse.jdt.core.formatter.number_of_empty_lines_to_preserve=1 +org.eclipse.jdt.core.formatter.put_empty_statement_on_new_line=true +org.eclipse.jdt.core.formatter.tabulation.char=space +org.eclipse.jdt.core.formatter.tabulation.size=4 +org.eclipse.jdt.core.formatter.use_on_off_tags=true +org.eclipse.jdt.core.formatter.use_tabs_only_for_leading_indentations=false +org.eclipse.jdt.core.formatter.wrap_before_binary_operator=true +org.eclipse.jdt.core.formatter.wrap_before_or_operator_multicatch=true +org.eclipse.jdt.core.formatter.wrap_outer_expressions_when_nested=true diff --git a/lib/jinterface/.settings/org.eclipse.jdt.ui.prefs b/lib/jinterface/.settings/org.eclipse.jdt.ui.prefs new file mode 100644 index 0000000000..9190d818bc --- /dev/null +++ b/lib/jinterface/.settings/org.eclipse.jdt.ui.prefs @@ -0,0 +1,121 @@ +cleanup.add_default_serial_version_id=true +cleanup.add_generated_serial_version_id=false +cleanup.add_missing_annotations=true +cleanup.add_missing_deprecated_annotations=true +cleanup.add_missing_methods=false +cleanup.add_missing_nls_tags=false +cleanup.add_missing_override_annotations=true +cleanup.add_missing_override_annotations_interface_methods=true +cleanup.add_serial_version_id=false +cleanup.always_use_blocks=true +cleanup.always_use_parentheses_in_expressions=false +cleanup.always_use_this_for_non_static_field_access=false +cleanup.always_use_this_for_non_static_method_access=false +cleanup.convert_functional_interfaces=false +cleanup.convert_to_enhanced_for_loop=false +cleanup.correct_indentation=false +cleanup.format_source_code=true +cleanup.format_source_code_changes_only=false +cleanup.insert_inferred_type_arguments=false +cleanup.make_local_variable_final=true +cleanup.make_parameters_final=true +cleanup.make_private_fields_final=true +cleanup.make_type_abstract_if_missing_method=false +cleanup.make_variable_declarations_final=true +cleanup.never_use_blocks=false +cleanup.never_use_parentheses_in_expressions=true +cleanup.organize_imports=true +cleanup.qualify_static_field_accesses_with_declaring_class=false +cleanup.qualify_static_member_accesses_through_instances_with_declaring_class=true +cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class=true +cleanup.qualify_static_member_accesses_with_declaring_class=true +cleanup.qualify_static_method_accesses_with_declaring_class=false +cleanup.remove_private_constructors=true +cleanup.remove_redundant_type_arguments=true +cleanup.remove_trailing_whitespaces=true +cleanup.remove_trailing_whitespaces_all=true +cleanup.remove_trailing_whitespaces_ignore_empty=false +cleanup.remove_unnecessary_casts=true +cleanup.remove_unnecessary_nls_tags=true +cleanup.remove_unused_imports=true +cleanup.remove_unused_local_variables=false +cleanup.remove_unused_private_fields=true +cleanup.remove_unused_private_members=false +cleanup.remove_unused_private_methods=true +cleanup.remove_unused_private_types=true +cleanup.sort_members=false +cleanup.sort_members_all=false +cleanup.use_anonymous_class_creation=false +cleanup.use_blocks=true +cleanup.use_blocks_only_for_return_and_throw=false +cleanup.use_lambda=true +cleanup.use_parentheses_in_expressions=true +cleanup.use_this_for_non_static_field_access=true +cleanup.use_this_for_non_static_field_access_only_if_necessary=true +cleanup.use_this_for_non_static_method_access=true +cleanup.use_this_for_non_static_method_access_only_if_necessary=true +cleanup.use_type_arguments=false +cleanup_profile=_jinterface +cleanup_settings_version=2 +eclipse.preferences.version=1 +editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true +formatter_profile=_jinterface +formatter_settings_version=12 +sp_cleanup.add_default_serial_version_id=true +sp_cleanup.add_generated_serial_version_id=false +sp_cleanup.add_missing_annotations=true +sp_cleanup.add_missing_deprecated_annotations=true +sp_cleanup.add_missing_methods=false +sp_cleanup.add_missing_nls_tags=false +sp_cleanup.add_missing_override_annotations=true +sp_cleanup.add_missing_override_annotations_interface_methods=true +sp_cleanup.add_serial_version_id=false +sp_cleanup.always_use_blocks=true +sp_cleanup.always_use_parentheses_in_expressions=false +sp_cleanup.always_use_this_for_non_static_field_access=false +sp_cleanup.always_use_this_for_non_static_method_access=false +sp_cleanup.convert_functional_interfaces=false +sp_cleanup.convert_to_enhanced_for_loop=false +sp_cleanup.correct_indentation=false +sp_cleanup.format_source_code=true +sp_cleanup.format_source_code_changes_only=false +sp_cleanup.insert_inferred_type_arguments=false +sp_cleanup.make_local_variable_final=true +sp_cleanup.make_parameters_final=true +sp_cleanup.make_private_fields_final=true +sp_cleanup.make_type_abstract_if_missing_method=false +sp_cleanup.make_variable_declarations_final=true +sp_cleanup.never_use_blocks=false +sp_cleanup.never_use_parentheses_in_expressions=true +sp_cleanup.on_save_use_additional_actions=true +sp_cleanup.organize_imports=true +sp_cleanup.qualify_static_field_accesses_with_declaring_class=false +sp_cleanup.qualify_static_member_accesses_through_instances_with_declaring_class=true +sp_cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class=true +sp_cleanup.qualify_static_member_accesses_with_declaring_class=true +sp_cleanup.qualify_static_method_accesses_with_declaring_class=false +sp_cleanup.remove_private_constructors=true +sp_cleanup.remove_redundant_type_arguments=false +sp_cleanup.remove_trailing_whitespaces=true +sp_cleanup.remove_trailing_whitespaces_all=true +sp_cleanup.remove_trailing_whitespaces_ignore_empty=false +sp_cleanup.remove_unnecessary_casts=true +sp_cleanup.remove_unnecessary_nls_tags=true +sp_cleanup.remove_unused_imports=true +sp_cleanup.remove_unused_local_variables=false +sp_cleanup.remove_unused_private_fields=true +sp_cleanup.remove_unused_private_members=false +sp_cleanup.remove_unused_private_methods=true +sp_cleanup.remove_unused_private_types=true +sp_cleanup.sort_members=false +sp_cleanup.sort_members_all=false +sp_cleanup.use_anonymous_class_creation=false +sp_cleanup.use_blocks=true +sp_cleanup.use_blocks_only_for_return_and_throw=false +sp_cleanup.use_lambda=false +sp_cleanup.use_parentheses_in_expressions=true +sp_cleanup.use_this_for_non_static_field_access=false +sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=true +sp_cleanup.use_this_for_non_static_method_access=false +sp_cleanup.use_this_for_non_static_method_access_only_if_necessary=true +sp_cleanup.use_type_arguments=false diff --git a/lib/jinterface/doc/src/jinterface_users_guide.xml b/lib/jinterface/doc/src/jinterface_users_guide.xml index 5dfe5c0c6d..238f90ce38 100644 --- a/lib/jinterface/doc/src/jinterface_users_guide.xml +++ b/lib/jinterface/doc/src/jinterface_users_guide.xml @@ -223,6 +223,14 @@ OtpNode node = new OtpNode("gurka"); </code> </section> <section> + <title>Transport Factory</title> + <p>All necessary connections are made using methods of + <seealso marker="java/com/ericsson/otp/erlang/OtpTransportFactory">OtpTransportFactory</seealso> + interface. Default OtpTransportFactory implementation is based on standard Socket class. + User may provide custom transport factory as needed. See java doc for details.</p> + </section> + + <section> <title>Sending and Receiving Messages</title> <p>Messages sent with this package must be instances of <seealso marker="java/com/ericsson/otp/erlang/OtpErlangObject">OtpErlangObject</seealso> diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/AbstractConnection.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/AbstractConnection.java index b8a973753a..ab8fa06c1b 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/AbstractConnection.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/AbstractConnection.java @@ -1,52 +1,52 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2010. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; import java.io.IOException; -import java.net.Socket; +import java.io.OutputStream; import java.util.Random; /** * Maintains a connection between a Java process and a remote Erlang, Java or C * node. The object maintains connection state and allows data to be sent to and * received from the peer. - * + * * <p> * This abstract class provides the neccesary methods to maintain the actual * connection and encode the messages and headers in the proper format according * to the Erlang distribution protocol. Subclasses can use these methods to * provide a more or less transparent communication channel as desired. * </p> - * + * * <p> * Note that no receive methods are provided. Subclasses must provide methods * for message delivery, and may implement their own receive methods. * <p> - * + * * <p> * If an exception occurs in any of the methods in this class, the connection * will be closed and must be reopened in order to resume communication with the * peer. This will be indicated to the subclass by passing the exception to its * delivery() method. * </p> - * + * * <p> * The System property OtpConnection.trace can be used to change the initial * trace level setting for all connections. Normally the initial trace level is @@ -84,7 +84,7 @@ public abstract class AbstractConnection extends Thread { private volatile boolean done = false; protected boolean connected = false; // connection status - protected Socket socket; // communication channel + protected OtpTransport socket; // communication channel protected OtpPeer peer; // who are we connected to protected OtpLocalNode localNode; // this nodes id String name; // local name of this connection @@ -106,104 +106,103 @@ public abstract class AbstractConnection extends Thread { private int flags = 0; static { - // trace this connection? - final String trace = System.getProperties().getProperty( - "OtpConnection.trace"); - try { - if (trace != null) { - defaultLevel = Integer.valueOf(trace).intValue(); - } - } catch (final NumberFormatException e) { - defaultLevel = 0; - } - random = new Random(); + // trace this connection? + final String trace = System.getProperties().getProperty( + "OtpConnection.trace"); + try { + if (trace != null) { + defaultLevel = Integer.valueOf(trace).intValue(); + } + } catch (final NumberFormatException e) { + defaultLevel = 0; + } + random = new Random(); } // private AbstractConnection() { // } /** - * Accept an incoming connection from a remote node. Used by {@link - * OtpSelf#accept() OtpSelf.accept()} to create a connection based on data - * received when handshaking with the peer node, when the remote node is the - * connection intitiator. - * - * @exception java.io.IOException if it was not possible to connect to the - * peer. - * - * @exception OtpAuthException if handshake resulted in an authentication - * error + * Accept an incoming connection from a remote node. Used by + * {@link OtpSelf#accept() OtpSelf.accept()} to create a connection based on + * data received when handshaking with the peer node, when the remote node + * is the connection initiator. + * + * @exception java.io.IOException + * if it was not possible to connect to the peer. + * + * @exception OtpAuthException + * if handshake resulted in an authentication error */ - protected AbstractConnection(final OtpLocalNode self, final Socket s) - throws IOException, OtpAuthException { - this.localNode = self; - peer = new OtpPeer(); - socket = s; - - socket.setTcpNoDelay(true); - - traceLevel = defaultLevel; - setDaemon(true); - - if (traceLevel >= handshakeThreshold) { - System.out.println("<- ACCEPT FROM " + s.getInetAddress() + ":" - + s.getPort()); - } - - // get his info - recvName(peer); - - // now find highest common dist value - if (peer.proto != self.proto || self.distHigh < peer.distLow - || self.distLow > peer.distHigh) { - close(); - throw new IOException( - "No common protocol found - cannot accept connection"); - } - // highest common version: min(peer.distHigh, self.distHigh) - peer.distChoose = peer.distHigh > self.distHigh ? self.distHigh - : peer.distHigh; - - doAccept(); - name = peer.node(); + protected AbstractConnection(final OtpLocalNode self, final OtpTransport s) + throws IOException, OtpAuthException { + localNode = self; + peer = new OtpPeer(self.transportFactory); + socket = s; + + traceLevel = defaultLevel; + setDaemon(true); + + if (traceLevel >= handshakeThreshold) { + System.out.println("<- ACCEPT FROM " + s); + } + + // get his info + recvName(peer); + + // now find highest common dist value + if (peer.proto != self.proto || self.distHigh < peer.distLow + || self.distLow > peer.distHigh) { + close(); + throw new IOException( + "No common protocol found - cannot accept connection"); + } + // highest common version: min(peer.distHigh, self.distHigh) + peer.distChoose = peer.distHigh > self.distHigh ? self.distHigh + : peer.distHigh; + + doAccept(); + name = peer.node(); } /** * Intiate and open a connection to a remote node. - * - * @exception java.io.IOException if it was not possible to connect to the - * peer. - * - * @exception OtpAuthException if handshake resulted in an authentication - * error. + * + * @exception java.io.IOException + * if it was not possible to connect to the peer. + * + * @exception OtpAuthException + * if handshake resulted in an authentication error. */ protected AbstractConnection(final OtpLocalNode self, final OtpPeer other) - throws IOException, OtpAuthException { - peer = other; - this.localNode = self; - socket = null; - int port; - - traceLevel = defaultLevel; - setDaemon(true); - - // now get a connection between the two... - port = OtpEpmd.lookupPort(peer); - - // now find highest common dist value - if (peer.proto != self.proto || self.distHigh < peer.distLow - || self.distLow > peer.distHigh) { - throw new IOException("No common protocol found - cannot connect"); - } - - // highest common version: min(peer.distHigh, self.distHigh) - peer.distChoose = peer.distHigh > self.distHigh ? self.distHigh - : peer.distHigh; - - doConnect(port); - - name = peer.node(); - connected = true; + throws IOException, OtpAuthException { + peer = other; + localNode = self; + socket = null; + int port; + + traceLevel = defaultLevel; + setDaemon(true); + + // now get a connection between the two... + port = OtpEpmd.lookupPort(peer); + if (port == 0) + throw new IOException("No remote node found - cannot connect"); + + // now find highest common dist value + if (peer.proto != self.proto || self.distHigh < peer.distLow + || self.distLow > peer.distHigh) { + throw new IOException("No common protocol found - cannot connect"); + } + + // highest common version: min(peer.distHigh, self.distHigh) + peer.distChoose = peer.distHigh > self.distHigh ? self.distHigh + : peer.distHigh; + + doConnect(port); + + name = peer.node(); + connected = true; } /** @@ -218,91 +217,91 @@ public abstract class AbstractConnection extends Thread { /** * Send a pre-encoded message to a named process on a remote node. - * + * * @param dest * the name of the remote process. * @param payload * the encoded message to send. - * + * * @exception java.io.IOException * if the connection is not active or a communication error * occurs. */ protected void sendBuf(final OtpErlangPid from, final String dest, - final OtpOutputStream payload) throws IOException { - if (!connected) { - throw new IOException("Not connected"); - } - @SuppressWarnings("resource") - final OtpOutputStream header = new OtpOutputStream(headerLen); - - // preamble: 4 byte length + "passthrough" tag + version - header.write4BE(0); // reserve space for length - header.write1(passThrough); - header.write1(version); - - // header info - header.write_tuple_head(4); - header.write_long(regSendTag); - header.write_any(from); - if (sendCookie) { - header.write_atom(localNode.cookie()); - } else { - header.write_atom(""); - } - header.write_atom(dest); - - // version for payload - header.write1(version); - - // fix up length in preamble - header.poke4BE(0, header.size() + payload.size() - 4); - - do_send(header, payload); + final OtpOutputStream payload) throws IOException { + if (!connected) { + throw new IOException("Not connected"); + } + @SuppressWarnings("resource") + final OtpOutputStream header = new OtpOutputStream(headerLen); + + // preamble: 4 byte length + "passthrough" tag + version + header.write4BE(0); // reserve space for length + header.write1(passThrough); + header.write1(version); + + // header info + header.write_tuple_head(4); + header.write_long(regSendTag); + header.write_any(from); + if (sendCookie) { + header.write_atom(localNode.cookie()); + } else { + header.write_atom(""); + } + header.write_atom(dest); + + // version for payload + header.write1(version); + + // fix up length in preamble + header.poke4BE(0, header.size() + payload.size() - 4); + + do_send(header, payload); } /** * Send a pre-encoded message to a process on a remote node. - * + * * @param dest * the Erlang PID of the remote process. * @param payload * the encoded message to send. - * + * * @exception java.io.IOException * if the connection is not active or a communication error * occurs. */ protected void sendBuf(final OtpErlangPid from, final OtpErlangPid dest, - final OtpOutputStream payload) throws IOException { - if (!connected) { - throw new IOException("Not connected"); - } - @SuppressWarnings("resource") - final OtpOutputStream header = new OtpOutputStream(headerLen); - - // preamble: 4 byte length + "passthrough" tag + version - header.write4BE(0); // reserve space for length - header.write1(passThrough); - header.write1(version); - - // header info - header.write_tuple_head(3); - header.write_long(sendTag); - if (sendCookie) { - header.write_atom(localNode.cookie()); - } else { - header.write_atom(""); - } - header.write_any(dest); - - // version for payload - header.write1(version); - - // fix up length in preamble - header.poke4BE(0, header.size() + payload.size() - 4); - - do_send(header, payload); + final OtpOutputStream payload) throws IOException { + if (!connected) { + throw new IOException("Not connected"); + } + @SuppressWarnings("resource") + final OtpOutputStream header = new OtpOutputStream(headerLen); + + // preamble: 4 byte length + "passthrough" tag + version + header.write4BE(0); // reserve space for length + header.write1(passThrough); + header.write1(version); + + // header info + header.write_tuple_head(3); + header.write_long(sendTag); + if (sendCookie) { + header.write_atom(localNode.cookie()); + } else { + header.write_atom(""); + } + header.write_any(dest); + + // version for payload + header.write1(version); + + // fix up length in preamble + header.poke4BE(0, header.size() + payload.size() - 4); + + do_send(header, payload); } /* @@ -311,60 +310,60 @@ public abstract class AbstractConnection extends Thread { * otherwise */ private void cookieError(final OtpLocalNode local, - final OtpErlangAtom cookie) throws OtpAuthException { - try { - @SuppressWarnings("resource") - final OtpOutputStream header = new OtpOutputStream(headerLen); - - // preamble: 4 byte length + "passthrough" tag + version - header.write4BE(0); // reserve space for length - header.write1(passThrough); - header.write1(version); - - header.write_tuple_head(4); - header.write_long(regSendTag); - header.write_any(local.createPid()); // disposable pid - header.write_atom(cookie.atomValue()); // important: his cookie, - // not mine... - header.write_atom("auth"); - - // version for payload - header.write1(version); - - // the payload - - // the no_auth message (copied from Erlang) Don't change this - // (Erlang will crash) - // {$gen_cast, {print, "~n** Unauthorized cookie ~w **~n", - // [foo@aule]}} - final OtpErlangObject[] msg = new OtpErlangObject[2]; - final OtpErlangObject[] msgbody = new OtpErlangObject[3]; - - msgbody[0] = new OtpErlangAtom("print"); - msgbody[1] = new OtpErlangString("~n** Bad cookie sent to " + local - + " **~n"); - // Erlang will crash and burn if there is no third argument here... - msgbody[2] = new OtpErlangList(); // empty list - - msg[0] = new OtpErlangAtom("$gen_cast"); - msg[1] = new OtpErlangTuple(msgbody); - - @SuppressWarnings("resource") - final OtpOutputStream payload = new OtpOutputStream( - new OtpErlangTuple(msg)); - - // fix up length in preamble - header.poke4BE(0, header.size() + payload.size() - 4); - - try { - do_send(header, payload); - } catch (final IOException e) { - } // ignore - } finally { - close(); - } - throw new OtpAuthException("Remote cookie not authorized: " - + cookie.atomValue()); + final OtpErlangAtom cookie) throws OtpAuthException { + try { + @SuppressWarnings("resource") + final OtpOutputStream header = new OtpOutputStream(headerLen); + + // preamble: 4 byte length + "passthrough" tag + version + header.write4BE(0); // reserve space for length + header.write1(passThrough); + header.write1(version); + + header.write_tuple_head(4); + header.write_long(regSendTag); + header.write_any(local.createPid()); // disposable pid + header.write_atom(cookie.atomValue()); // important: his cookie, + // not mine... + header.write_atom("auth"); + + // version for payload + header.write1(version); + + // the payload + + // the no_auth message (copied from Erlang) Don't change this + // (Erlang will crash) + // {$gen_cast, {print, "~n** Unauthorized cookie ~w **~n", + // [foo@aule]}} + final OtpErlangObject[] msg = new OtpErlangObject[2]; + final OtpErlangObject[] msgbody = new OtpErlangObject[3]; + + msgbody[0] = new OtpErlangAtom("print"); + msgbody[1] = new OtpErlangString("~n** Bad cookie sent to " + local + + " **~n"); + // Erlang will crash and burn if there is no third argument here... + msgbody[2] = new OtpErlangList(); // empty list + + msg[0] = new OtpErlangAtom("$gen_cast"); + msg[1] = new OtpErlangTuple(msgbody); + + @SuppressWarnings("resource") + final OtpOutputStream payload = new OtpOutputStream( + new OtpErlangTuple(msg)); + + // fix up length in preamble + header.poke4BE(0, header.size() + payload.size() - 4); + + try { + do_send(header, payload); + } catch (final IOException e) { + } // ignore + } finally { + close(); + } + throw new OtpAuthException("Remote cookie not authorized: " + + cookie.atomValue()); } // link to pid @@ -374,364 +373,366 @@ public abstract class AbstractConnection extends Thread { * remote node. If the link is still active when the remote process * terminates, an exit signal will be sent to this connection. Use * {@link #sendUnlink unlink()} to remove the link. - * + * * @param dest * the Erlang PID of the remote process. - * + * * @exception java.io.IOException * if the connection is not active or a communication error * occurs. */ protected void sendLink(final OtpErlangPid from, final OtpErlangPid dest) - throws IOException { - if (!connected) { - throw new IOException("Not connected"); - } - @SuppressWarnings("resource") - final OtpOutputStream header = new OtpOutputStream(headerLen); + throws IOException { + if (!connected) { + throw new IOException("Not connected"); + } + @SuppressWarnings("resource") + final OtpOutputStream header = new OtpOutputStream(headerLen); - // preamble: 4 byte length + "passthrough" tag - header.write4BE(0); // reserve space for length - header.write1(passThrough); - header.write1(version); + // preamble: 4 byte length + "passthrough" tag + header.write4BE(0); // reserve space for length + header.write1(passThrough); + header.write1(version); - // header - header.write_tuple_head(3); - header.write_long(linkTag); - header.write_any(from); - header.write_any(dest); + // header + header.write_tuple_head(3); + header.write_long(linkTag); + header.write_any(from); + header.write_any(dest); - // fix up length in preamble - header.poke4BE(0, header.size() - 4); + // fix up length in preamble + header.poke4BE(0, header.size() - 4); - do_send(header); + do_send(header); } /** * Remove a link between the local node and the specified process on the * remote node. This method deactivates links created with {@link #sendLink * link()}. - * + * * @param dest * the Erlang PID of the remote process. - * + * * @exception java.io.IOException * if the connection is not active or a communication error * occurs. */ protected void sendUnlink(final OtpErlangPid from, final OtpErlangPid dest) - throws IOException { - if (!connected) { - throw new IOException("Not connected"); - } - @SuppressWarnings("resource") - final OtpOutputStream header = new OtpOutputStream(headerLen); + throws IOException { + if (!connected) { + throw new IOException("Not connected"); + } + @SuppressWarnings("resource") + final OtpOutputStream header = new OtpOutputStream(headerLen); - // preamble: 4 byte length + "passthrough" tag - header.write4BE(0); // reserve space for length - header.write1(passThrough); - header.write1(version); + // preamble: 4 byte length + "passthrough" tag + header.write4BE(0); // reserve space for length + header.write1(passThrough); + header.write1(version); - // header - header.write_tuple_head(3); - header.write_long(unlinkTag); - header.write_any(from); - header.write_any(dest); + // header + header.write_tuple_head(3); + header.write_long(unlinkTag); + header.write_any(from); + header.write_any(dest); - // fix up length in preamble - header.poke4BE(0, header.size() - 4); + // fix up length in preamble + header.poke4BE(0, header.size() - 4); - do_send(header); + do_send(header); } /* used internally when "processes" terminate */ protected void sendExit(final OtpErlangPid from, final OtpErlangPid dest, - final OtpErlangObject reason) throws IOException { - sendExit(exitTag, from, dest, reason); + final OtpErlangObject reason) throws IOException { + sendExit(exitTag, from, dest, reason); } /** * Send an exit signal to a remote process. - * + * * @param dest * the Erlang PID of the remote process. * @param reason * an Erlang term describing the exit reason. - * + * * @exception java.io.IOException * if the connection is not active or a communication error * occurs. */ protected void sendExit2(final OtpErlangPid from, final OtpErlangPid dest, - final OtpErlangObject reason) throws IOException { - sendExit(exit2Tag, from, dest, reason); + final OtpErlangObject reason) throws IOException { + sendExit(exit2Tag, from, dest, reason); } private void sendExit(final int tag, final OtpErlangPid from, - final OtpErlangPid dest, final OtpErlangObject reason) - throws IOException { - if (!connected) { - throw new IOException("Not connected"); - } - @SuppressWarnings("resource") - final OtpOutputStream header = new OtpOutputStream(headerLen); + final OtpErlangPid dest, final OtpErlangObject reason) + throws IOException { + if (!connected) { + throw new IOException("Not connected"); + } + @SuppressWarnings("resource") + final OtpOutputStream header = new OtpOutputStream(headerLen); - // preamble: 4 byte length + "passthrough" tag - header.write4BE(0); // reserve space for length - header.write1(passThrough); - header.write1(version); + // preamble: 4 byte length + "passthrough" tag + header.write4BE(0); // reserve space for length + header.write1(passThrough); + header.write1(version); - // header - header.write_tuple_head(4); - header.write_long(tag); - header.write_any(from); - header.write_any(dest); - header.write_any(reason); + // header + header.write_tuple_head(4); + header.write_long(tag); + header.write_any(from); + header.write_any(dest); + header.write_any(reason); - // fix up length in preamble - header.poke4BE(0, header.size() - 4); + // fix up length in preamble + header.poke4BE(0, header.size() - 4); - do_send(header); + do_send(header); } @SuppressWarnings("resource") @Override public void run() { - if (!connected) { - deliver(new IOException("Not connected")); - return; - } - - final byte[] lbuf = new byte[4]; - OtpInputStream ibuf; - OtpErlangObject traceobj; - int len; - final byte[] tock = { 0, 0, 0, 0 }; - - try { - receive_loop: while (!done) { - // don't return until we get a real message - // or a failure of some kind (e.g. EXIT) - // read length and read buffer must be atomic! - do { - // read 4 bytes - get length of incoming packet - // socket.getInputStream().read(lbuf); - readSock(socket, lbuf); - ibuf = new OtpInputStream(lbuf, flags); - len = ibuf.read4BE(); - - // received tick? send tock! - if (len == 0) { - synchronized (this) { - socket.getOutputStream().write(tock); - } - } - - } while (len == 0); // tick_loop - - // got a real message (maybe) - read len bytes - final byte[] tmpbuf = new byte[len]; - // i = socket.getInputStream().read(tmpbuf); - readSock(socket, tmpbuf); - ibuf.close(); - ibuf = new OtpInputStream(tmpbuf, flags); - - if (ibuf.read1() != passThrough) { - break receive_loop; - } - - // got a real message (really) - OtpErlangObject reason = null; - OtpErlangAtom cookie = null; - OtpErlangObject tmp = null; - OtpErlangTuple head = null; - OtpErlangAtom toName; - OtpErlangPid to; - OtpErlangPid from; - int tag; - - // decode the header - tmp = ibuf.read_any(); - if (!(tmp instanceof OtpErlangTuple)) { - break receive_loop; - } - - head = (OtpErlangTuple) tmp; - if (!(head.elementAt(0) instanceof OtpErlangLong)) { - break receive_loop; - } - - // lets see what kind of message this is - tag = (int) ((OtpErlangLong) head.elementAt(0)).longValue(); - - switch (tag) { - case sendTag: // { SEND, Cookie, ToPid } - case sendTTTag: // { SEND, Cookie, ToPid, TraceToken } - if (!cookieOk) { - // we only check this once, he can send us bad cookies - // later if he likes - if (!(head.elementAt(1) instanceof OtpErlangAtom)) { - break receive_loop; - } - cookie = (OtpErlangAtom) head.elementAt(1); - if (sendCookie) { - if (!cookie.atomValue().equals(localNode.cookie())) { - cookieError(localNode, cookie); - } - } else { - if (!cookie.atomValue().equals("")) { - cookieError(localNode, cookie); - } - } - cookieOk = true; - } - - if (traceLevel >= sendThreshold) { - System.out.println("<- " + headerType(head) + " " - + head); - - /* show received payload too */ - ibuf.mark(0); - traceobj = ibuf.read_any(); - - if (traceobj != null) { - System.out.println(" " + traceobj); - } else { - System.out.println(" (null)"); - } - ibuf.reset(); - } - - to = (OtpErlangPid) head.elementAt(2); - - deliver(new OtpMsg(to, ibuf)); - break; - - case regSendTag: // { REG_SEND, FromPid, Cookie, ToName } - case regSendTTTag: // { REG_SEND, FromPid, Cookie, ToName, - // TraceToken } - if (!cookieOk) { - // we only check this once, he can send us bad cookies - // later if he likes - if (!(head.elementAt(2) instanceof OtpErlangAtom)) { - break receive_loop; - } - cookie = (OtpErlangAtom) head.elementAt(2); - if (sendCookie) { - if (!cookie.atomValue().equals(localNode.cookie())) { - cookieError(localNode, cookie); - } - } else { - if (!cookie.atomValue().equals("")) { - cookieError(localNode, cookie); - } - } - cookieOk = true; - } - - if (traceLevel >= sendThreshold) { - System.out.println("<- " + headerType(head) + " " - + head); - - /* show received payload too */ - ibuf.mark(0); - traceobj = ibuf.read_any(); - - if (traceobj != null) { - System.out.println(" " + traceobj); - } else { - System.out.println(" (null)"); - } - ibuf.reset(); - } - - from = (OtpErlangPid) head.elementAt(1); - toName = (OtpErlangAtom) head.elementAt(3); - - deliver(new OtpMsg(from, toName.atomValue(), ibuf)); - break; - - case exitTag: // { EXIT, FromPid, ToPid, Reason } - case exit2Tag: // { EXIT2, FromPid, ToPid, Reason } - if (head.elementAt(3) == null) { - break receive_loop; - } - if (traceLevel >= ctrlThreshold) { - System.out.println("<- " + headerType(head) + " " - + head); - } - - from = (OtpErlangPid) head.elementAt(1); - to = (OtpErlangPid) head.elementAt(2); - reason = head.elementAt(3); - - deliver(new OtpMsg(tag, from, to, reason)); - break; - - case exitTTTag: // { EXIT, FromPid, ToPid, TraceToken, Reason } - case exit2TTTag: // { EXIT2, FromPid, ToPid, TraceToken, - // Reason - // } - // as above, but bifferent element number - if (head.elementAt(4) == null) { - break receive_loop; - } - if (traceLevel >= ctrlThreshold) { - System.out.println("<- " + headerType(head) + " " - + head); - } - - from = (OtpErlangPid) head.elementAt(1); - to = (OtpErlangPid) head.elementAt(2); - reason = head.elementAt(4); - - deliver(new OtpMsg(tag, from, to, reason)); - break; - - case linkTag: // { LINK, FromPid, ToPid} - case unlinkTag: // { UNLINK, FromPid, ToPid} - if (traceLevel >= ctrlThreshold) { - System.out.println("<- " + headerType(head) + " " - + head); - } - - from = (OtpErlangPid) head.elementAt(1); - to = (OtpErlangPid) head.elementAt(2); - - deliver(new OtpMsg(tag, from, to)); - break; - - // absolutely no idea what to do with these, so we ignore - // them... - case groupLeaderTag: // { GROUPLEADER, FromPid, ToPid} - // (just show trace) - if (traceLevel >= ctrlThreshold) { - System.out.println("<- " + headerType(head) + " " - + head); - } - break; - - default: - // garbage? - break receive_loop; - } - } // end receive_loop - - // this section reachable only with break - // we have received garbage from peer - deliver(new OtpErlangExit("Remote is sending garbage")); - - } // try - - catch (final OtpAuthException e) { - deliver(e); - } catch (final OtpErlangDecodeException e) { - deliver(new OtpErlangExit("Remote is sending garbage")); - } catch (final IOException e) { - deliver(new OtpErlangExit("Remote has closed connection")); - } finally { - close(); - } + if (!connected) { + deliver(new IOException("Not connected")); + return; + } + + final byte[] lbuf = new byte[4]; + OtpInputStream ibuf; + OtpErlangObject traceobj; + int len; + final byte[] tock = { 0, 0, 0, 0 }; + + try { + receive_loop: while (!done) { + // don't return until we get a real message + // or a failure of some kind (e.g. EXIT) + // read length and read buffer must be atomic! + do { + // read 4 bytes - get length of incoming packet + // socket.getInputStream().read(lbuf); + readSock(socket, lbuf); + ibuf = new OtpInputStream(lbuf, flags); + len = ibuf.read4BE(); + + // received tick? send tock! + if (len == 0) { + synchronized (this) { + OutputStream out = socket.getOutputStream(); + out.write(tock); + out.flush(); + } + } + + } while (len == 0); // tick_loop + + // got a real message (maybe) - read len bytes + final byte[] tmpbuf = new byte[len]; + // i = socket.getInputStream().read(tmpbuf); + readSock(socket, tmpbuf); + ibuf.close(); + ibuf = new OtpInputStream(tmpbuf, flags); + + if (ibuf.read1() != passThrough) { + break receive_loop; + } + + // got a real message (really) + OtpErlangObject reason = null; + OtpErlangAtom cookie = null; + OtpErlangObject tmp = null; + OtpErlangTuple head = null; + OtpErlangAtom toName; + OtpErlangPid to; + OtpErlangPid from; + int tag; + + // decode the header + tmp = ibuf.read_any(); + if (!(tmp instanceof OtpErlangTuple)) { + break receive_loop; + } + + head = (OtpErlangTuple) tmp; + if (!(head.elementAt(0) instanceof OtpErlangLong)) { + break receive_loop; + } + + // lets see what kind of message this is + tag = (int) ((OtpErlangLong) head.elementAt(0)).longValue(); + + switch (tag) { + case sendTag: // { SEND, Cookie, ToPid } + case sendTTTag: // { SEND, Cookie, ToPid, TraceToken } + if (!cookieOk) { + // we only check this once, he can send us bad cookies + // later if he likes + if (!(head.elementAt(1) instanceof OtpErlangAtom)) { + break receive_loop; + } + cookie = (OtpErlangAtom) head.elementAt(1); + if (sendCookie) { + if (!cookie.atomValue().equals(localNode.cookie())) { + cookieError(localNode, cookie); + } + } else { + if (!cookie.atomValue().equals("")) { + cookieError(localNode, cookie); + } + } + cookieOk = true; + } + + if (traceLevel >= sendThreshold) { + System.out.println("<- " + headerType(head) + " " + + head); + + /* show received payload too */ + ibuf.mark(0); + traceobj = ibuf.read_any(); + + if (traceobj != null) { + System.out.println(" " + traceobj); + } else { + System.out.println(" (null)"); + } + ibuf.reset(); + } + + to = (OtpErlangPid) head.elementAt(2); + + deliver(new OtpMsg(to, ibuf)); + break; + + case regSendTag: // { REG_SEND, FromPid, Cookie, ToName } + case regSendTTTag: // { REG_SEND, FromPid, Cookie, ToName, + // TraceToken } + if (!cookieOk) { + // we only check this once, he can send us bad cookies + // later if he likes + if (!(head.elementAt(2) instanceof OtpErlangAtom)) { + break receive_loop; + } + cookie = (OtpErlangAtom) head.elementAt(2); + if (sendCookie) { + if (!cookie.atomValue().equals(localNode.cookie())) { + cookieError(localNode, cookie); + } + } else { + if (!cookie.atomValue().equals("")) { + cookieError(localNode, cookie); + } + } + cookieOk = true; + } + + if (traceLevel >= sendThreshold) { + System.out.println("<- " + headerType(head) + " " + + head); + + /* show received payload too */ + ibuf.mark(0); + traceobj = ibuf.read_any(); + + if (traceobj != null) { + System.out.println(" " + traceobj); + } else { + System.out.println(" (null)"); + } + ibuf.reset(); + } + + from = (OtpErlangPid) head.elementAt(1); + toName = (OtpErlangAtom) head.elementAt(3); + + deliver(new OtpMsg(from, toName.atomValue(), ibuf)); + break; + + case exitTag: // { EXIT, FromPid, ToPid, Reason } + case exit2Tag: // { EXIT2, FromPid, ToPid, Reason } + if (head.elementAt(3) == null) { + break receive_loop; + } + if (traceLevel >= ctrlThreshold) { + System.out.println("<- " + headerType(head) + " " + + head); + } + + from = (OtpErlangPid) head.elementAt(1); + to = (OtpErlangPid) head.elementAt(2); + reason = head.elementAt(3); + + deliver(new OtpMsg(tag, from, to, reason)); + break; + + case exitTTTag: // { EXIT, FromPid, ToPid, TraceToken, Reason } + case exit2TTTag: // { EXIT2, FromPid, ToPid, TraceToken, + // Reason + // } + // as above, but bifferent element number + if (head.elementAt(4) == null) { + break receive_loop; + } + if (traceLevel >= ctrlThreshold) { + System.out.println("<- " + headerType(head) + " " + + head); + } + + from = (OtpErlangPid) head.elementAt(1); + to = (OtpErlangPid) head.elementAt(2); + reason = head.elementAt(4); + + deliver(new OtpMsg(tag, from, to, reason)); + break; + + case linkTag: // { LINK, FromPid, ToPid} + case unlinkTag: // { UNLINK, FromPid, ToPid} + if (traceLevel >= ctrlThreshold) { + System.out.println("<- " + headerType(head) + " " + + head); + } + + from = (OtpErlangPid) head.elementAt(1); + to = (OtpErlangPid) head.elementAt(2); + + deliver(new OtpMsg(tag, from, to)); + break; + + // absolutely no idea what to do with these, so we ignore + // them... + case groupLeaderTag: // { GROUPLEADER, FromPid, ToPid} + // (just show trace) + if (traceLevel >= ctrlThreshold) { + System.out.println("<- " + headerType(head) + " " + + head); + } + break; + + default: + // garbage? + break receive_loop; + } + } // end receive_loop + + // this section reachable only with break + // we have received garbage from peer + deliver(new OtpErlangExit("Remote is sending garbage")); + + } // try + + catch (final OtpAuthException e) { + deliver(e); + } catch (final OtpErlangDecodeException e) { + deliver(new OtpErlangExit("Remote is sending garbage")); + } catch (final IOException e) { + deliver(new OtpErlangExit("Remote has closed connection")); + } finally { + close(); + } } /** @@ -739,7 +740,7 @@ public abstract class AbstractConnection extends Thread { * Set the trace level for this connection. Normally tracing is off by * default unless System property OtpConnection.trace was set. * </p> - * + * * <p> * The following levels are valid: 0 turns off tracing completely, 1 shows * ordinary send and receive messages, 2 shows control messages such as link @@ -747,632 +748,643 @@ public abstract class AbstractConnection extends Thread { * communication with Epmd. Each level includes the information shown by the * lower ones. * </p> - * + * * @param level * the level to set. - * + * * @return the previous trace level. */ - public int setTraceLevel(int level) { - final int oldLevel = traceLevel; + public int setTraceLevel(final int level) { + final int oldLevel = traceLevel; - // pin the value - int theLevel = level; - if (level < 0) { - theLevel = 0; - } else if (level > 4) { - theLevel = 4; - } + // pin the value + int theLevel = level; + if (level < 0) { + theLevel = 0; + } else if (level > 4) { + theLevel = 4; + } - traceLevel = theLevel; + traceLevel = theLevel; - return oldLevel; + return oldLevel; } /** * Get the trace level for this connection. - * + * * @return the current trace level. */ public int getTraceLevel() { - return traceLevel; + return traceLevel; } /** * Close the connection to the remote node. */ public void close() { - done = true; - connected = false; - synchronized (this) { - try { - if (socket != null) { - if (traceLevel >= ctrlThreshold) { - System.out.println("-> CLOSE"); - } - socket.close(); - } - } catch (final IOException e) { /* ignore socket close errors */ - } finally { - socket = null; - } - } + done = true; + connected = false; + synchronized (this) { + try { + if (socket != null) { + if (traceLevel >= ctrlThreshold) { + System.out.println("-> CLOSE"); + } + socket.close(); + } + } catch (final IOException e) { /* ignore socket close errors */ + } finally { + socket = null; + } + } } @Override protected void finalize() { - close(); + close(); } /** * Determine if the connection is still alive. Note that this method only * reports the status of the connection, and that it is possible that there * are unread messages waiting in the receive queue. - * + * * @return true if the connection is alive. */ public boolean isConnected() { - return connected; + return connected; } // used by send and send_reg (message types with payload) protected synchronized void do_send(final OtpOutputStream header, - final OtpOutputStream payload) throws IOException { - try { - if (traceLevel >= sendThreshold) { - // Need to decode header and output buffer to show trace - // message! - // First make OtpInputStream, then decode. - try { - final OtpErlangObject h = header.getOtpInputStream(5) - .read_any(); - System.out.println("-> " + headerType(h) + " " + h); - - OtpErlangObject o = payload.getOtpInputStream(0).read_any(); - System.out.println(" " + o); - o = null; - } catch (final OtpErlangDecodeException e) { - System.out.println(" " + "can't decode output buffer:" - + e); - } - } - - header.writeTo(socket.getOutputStream()); - payload.writeTo(socket.getOutputStream()); - } catch (final IOException e) { - close(); - throw e; - } + final OtpOutputStream payload) throws IOException { + try { + if (traceLevel >= sendThreshold) { + // Need to decode header and output buffer to show trace + // message! + // First make OtpInputStream, then decode. + try { + final OtpErlangObject h = header.getOtpInputStream(5) + .read_any(); + System.out.println("-> " + headerType(h) + " " + h); + + OtpErlangObject o = payload.getOtpInputStream(0).read_any(); + System.out.println(" " + o); + o = null; + } catch (final OtpErlangDecodeException e) { + System.out.println(" " + "can't decode output buffer:" + + e); + } + } + + // group flush op in favour of possible ssh-tunneled stream + OutputStream out = socket.getOutputStream(); + header.writeTo(out); + payload.writeTo(out); + out.flush(); + } catch (final IOException e) { + close(); + throw e; + } } // used by the other message types protected synchronized void do_send(final OtpOutputStream header) - throws IOException { - try { - if (traceLevel >= ctrlThreshold) { - try { - final OtpErlangObject h = header.getOtpInputStream(5) - .read_any(); - System.out.println("-> " + headerType(h) + " " + h); - } catch (final OtpErlangDecodeException e) { - System.out.println(" " + "can't decode output buffer: " - + e); - } - } - header.writeTo(socket.getOutputStream()); - } catch (final IOException e) { - close(); - throw e; - } + throws IOException { + try { + if (traceLevel >= ctrlThreshold) { + try { + final OtpErlangObject h = header.getOtpInputStream(5) + .read_any(); + System.out.println("-> " + headerType(h) + " " + h); + } catch (final OtpErlangDecodeException e) { + System.out.println(" " + "can't decode output buffer: " + + e); + } + } + header.writeToAndFlush(socket.getOutputStream()); + } catch (final IOException e) { + close(); + throw e; + } } protected String headerType(final OtpErlangObject h) { - int tag = -1; + int tag = -1; - if (h instanceof OtpErlangTuple) { - tag = (int) ((OtpErlangLong) ((OtpErlangTuple) h).elementAt(0)) - .longValue(); - } + if (h instanceof OtpErlangTuple) { + tag = (int) ((OtpErlangLong) ((OtpErlangTuple) h).elementAt(0)) + .longValue(); + } - switch (tag) { - case linkTag: - return "LINK"; + switch (tag) { + case linkTag: + return "LINK"; - case sendTag: - return "SEND"; + case sendTag: + return "SEND"; - case exitTag: - return "EXIT"; + case exitTag: + return "EXIT"; - case unlinkTag: - return "UNLINK"; + case unlinkTag: + return "UNLINK"; - case regSendTag: - return "REG_SEND"; + case regSendTag: + return "REG_SEND"; - case groupLeaderTag: - return "GROUP_LEADER"; + case groupLeaderTag: + return "GROUP_LEADER"; - case exit2Tag: - return "EXIT2"; + case exit2Tag: + return "EXIT2"; - case sendTTTag: - return "SEND_TT"; + case sendTTTag: + return "SEND_TT"; - case exitTTTag: - return "EXIT_TT"; + case exitTTTag: + return "EXIT_TT"; - case regSendTTTag: - return "REG_SEND_TT"; + case regSendTTTag: + return "REG_SEND_TT"; - case exit2TTTag: - return "EXIT2_TT"; - } + case exit2TTTag: + return "EXIT2_TT"; + } - return "(unknown type)"; + return "(unknown type)"; } /* this method now throws exception if we don't get full read */ - protected int readSock(final Socket s, final byte[] b) throws IOException { - int got = 0; - final int len = b.length; - int i; - - synchronized (this) { - if (s == null) { - throw new IOException("expected " + len - + " bytes, socket was closed"); - } - } - - while (got < len) { - i = s.getInputStream().read(b, got, len - got); - - if (i < 0) { - throw new IOException("expected " + len - + " bytes, got EOF after " + got + " bytes"); - } else if (i == 0 && len != 0) { - /* - * This is a corner case. According to - * http://java.sun.com/j2se/1.4.2/docs/api/ class InputStream - * is.read(,,l) can only return 0 if l==0. In other words it - * should not happen, but apparently did. - */ - throw new IOException("Remote connection closed"); - } else { - got += i; - } - } - return got; + protected int readSock(final OtpTransport s, final byte[] b) + throws IOException { + int got = 0; + final int len = b.length; + int i; + + synchronized (this) { + if (s == null) { + throw new IOException("expected " + len + + " bytes, socket was closed"); + } + } + + while (got < len) { + i = s.getInputStream().read(b, got, len - got); + + if (i < 0) { + throw new IOException("expected " + len + + " bytes, got EOF after " + got + " bytes"); + } else if (i == 0 && len != 0) { + /* + * This is a corner case. According to + * http://java.sun.com/j2se/1.4.2/docs/api/ class InputStream + * is.read(,,l) can only return 0 if l==0. In other words it + * should not happen, but apparently did. + */ + throw new IOException("Remote connection closed"); + } else { + got += i; + } + } + return got; } protected void doAccept() throws IOException, OtpAuthException { - try { - sendStatus("ok"); - final int our_challenge = genChallenge(); - sendChallenge(peer.distChoose, localNode.flags, our_challenge); - final int her_challenge = recvChallengeReply(our_challenge); - final byte[] our_digest = genDigest(her_challenge, localNode.cookie()); - sendChallengeAck(our_digest); - connected = true; - cookieOk = true; - sendCookie = false; - } catch (final IOException ie) { - close(); - throw ie; - } catch (final OtpAuthException ae) { - close(); - throw ae; - } catch (final Exception e) { - final String nn = peer.node(); - close(); - IOException ioe = new IOException("Error accepting connection from " + nn); - ioe.initCause(e); - throw ioe; - } - if (traceLevel >= handshakeThreshold) { - System.out.println("<- MD5 ACCEPTED " + peer.host()); - } + try { + sendStatus("ok"); + final int our_challenge = genChallenge(); + sendChallenge(peer.distChoose, localNode.flags, our_challenge); + final int her_challenge = recvChallengeReply(our_challenge); + final byte[] our_digest = genDigest(her_challenge, + localNode.cookie()); + sendChallengeAck(our_digest); + connected = true; + cookieOk = true; + sendCookie = false; + } catch (final IOException ie) { + close(); + throw ie; + } catch (final OtpAuthException ae) { + close(); + throw ae; + } catch (final Exception e) { + final String nn = peer.node(); + close(); + final IOException ioe = new IOException( + "Error accepting connection from " + nn); + ioe.initCause(e); + throw ioe; + } + if (traceLevel >= handshakeThreshold) { + System.out.println("<- MD5 ACCEPTED " + peer.host()); + } } protected void doConnect(final int port) throws IOException, - OtpAuthException { - try { - socket = new Socket(peer.host(), port); - socket.setTcpNoDelay(true); - - if (traceLevel >= handshakeThreshold) { - System.out.println("-> MD5 CONNECT TO " + peer.host() + ":" - + port); - } - sendName(peer.distChoose, localNode.flags); - recvStatus(); - final int her_challenge = recvChallenge(); - final byte[] our_digest = genDigest(her_challenge, localNode.cookie()); - final int our_challenge = genChallenge(); - sendChallengeReply(our_challenge, our_digest); - recvChallengeAck(our_challenge); - cookieOk = true; - sendCookie = false; - } catch (final OtpAuthException ae) { - close(); - throw ae; - } catch (final Exception e) { - close(); - IOException ioe = new IOException("Cannot connect to peer node"); - ioe.initCause(e); - throw ioe; - } + OtpAuthException { + try { + socket = peer.createTransport(peer.host(), port); + + if (traceLevel >= handshakeThreshold) { + System.out.println("-> MD5 CONNECT TO " + peer.host() + ":" + + port); + } + sendName(peer.distChoose, localNode.flags); + recvStatus(); + final int her_challenge = recvChallenge(); + final byte[] our_digest = genDigest(her_challenge, + localNode.cookie()); + final int our_challenge = genChallenge(); + sendChallengeReply(our_challenge, our_digest); + recvChallengeAck(our_challenge); + cookieOk = true; + sendCookie = false; + } catch (final OtpAuthException ae) { + close(); + throw ae; + } catch (final Exception e) { + close(); + final IOException ioe = new IOException( + "Cannot connect to peer node"); + ioe.initCause(e); + throw ioe; + } } // This is nooo good as a challenge, // XXX fix me. static protected int genChallenge() { - return random.nextInt(); + return random.nextInt(); } // Used to debug print a message digest static String hex0(final byte x) { - final char tab[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', - 'a', 'b', 'c', 'd', 'e', 'f' }; - int uint; - if (x < 0) { - uint = x & 0x7F; - uint |= 1 << 7; - } else { - uint = x; - } - return "" + tab[uint >>> 4] + tab[uint & 0xF]; + final char tab[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + 'a', 'b', 'c', 'd', 'e', 'f' }; + int uint; + if (x < 0) { + uint = x & 0x7F; + uint |= 1 << 7; + } else { + uint = x; + } + return "" + tab[uint >>> 4] + tab[uint & 0xF]; } static String hex(final byte[] b) { - final StringBuffer sb = new StringBuffer(); - try { - int i; - for (i = 0; i < b.length; ++i) { - sb.append(hex0(b[i])); - } - } catch (final Exception e) { - // Debug function, ignore errors. - } - return sb.toString(); + final StringBuffer sb = new StringBuffer(); + try { + int i; + for (i = 0; i < b.length; ++i) { + sb.append(hex0(b[i])); + } + } catch (final Exception e) { + // Debug function, ignore errors. + } + return sb.toString(); } protected byte[] genDigest(final int challenge, final String cookie) { - int i; - long ch2; - - if (challenge < 0) { - ch2 = 1L << 31; - ch2 |= challenge & 0x7FFFFFFF; - } else { - ch2 = challenge; - } - final OtpMD5 context = new OtpMD5(); - context.update(cookie); - context.update("" + ch2); - - final int[] tmp = context.final_bytes(); - final byte[] res = new byte[tmp.length]; - for (i = 0; i < tmp.length; ++i) { - res[i] = (byte) (tmp[i] & 0xFF); - } - return res; + int i; + long ch2; + + if (challenge < 0) { + ch2 = 1L << 31; + ch2 |= challenge & 0x7FFFFFFF; + } else { + ch2 = challenge; + } + final OtpMD5 context = new OtpMD5(); + context.update(cookie); + context.update("" + ch2); + + final int[] tmp = context.final_bytes(); + final byte[] res = new byte[tmp.length]; + for (i = 0; i < tmp.length; ++i) { + res[i] = (byte) (tmp[i] & 0xFF); + } + return res; } - protected void sendName(final int dist, final int aflags) throws IOException { + protected void sendName(final int dist, final int aflags) + throws IOException { - @SuppressWarnings("resource") - final OtpOutputStream obuf = new OtpOutputStream(); - final String str = localNode.node(); - obuf.write2BE(str.length() + 7); // 7 bytes + nodename - obuf.write1(AbstractNode.NTYPE_R6); - obuf.write2BE(dist); - obuf.write4BE(aflags); - obuf.write(str.getBytes()); - - obuf.writeTo(socket.getOutputStream()); - - if (traceLevel >= handshakeThreshold) { - System.out.println("-> " + "HANDSHAKE sendName" + " flags=" + aflags - + " dist=" + dist + " local=" + localNode); - } + @SuppressWarnings("resource") + final OtpOutputStream obuf = new OtpOutputStream(); + final String str = localNode.node(); + obuf.write2BE(str.length() + 7); // 7 bytes + nodename + obuf.write1(AbstractNode.NTYPE_R6); + obuf.write2BE(dist); + obuf.write4BE(aflags); + obuf.write(str.getBytes()); + + obuf.writeToAndFlush(socket.getOutputStream()); + + if (traceLevel >= handshakeThreshold) { + System.out.println("-> " + "HANDSHAKE sendName" + " flags=" + + aflags + " dist=" + dist + " local=" + localNode); + } } protected void sendChallenge(final int dist, final int aflags, - final int challenge) throws IOException { + final int challenge) throws IOException { - @SuppressWarnings("resource") - final OtpOutputStream obuf = new OtpOutputStream(); - final String str = localNode.node(); - obuf.write2BE(str.length() + 11); // 11 bytes + nodename - obuf.write1(AbstractNode.NTYPE_R6); - obuf.write2BE(dist); - obuf.write4BE(aflags); - obuf.write4BE(challenge); - obuf.write(str.getBytes()); - - obuf.writeTo(socket.getOutputStream()); - - if (traceLevel >= handshakeThreshold) { - System.out.println("-> " + "HANDSHAKE sendChallenge" + " flags=" - + aflags + " dist=" + dist + " challenge=" + challenge - + " local=" + localNode); - } + @SuppressWarnings("resource") + final OtpOutputStream obuf = new OtpOutputStream(); + final String str = localNode.node(); + obuf.write2BE(str.length() + 11); // 11 bytes + nodename + obuf.write1(AbstractNode.NTYPE_R6); + obuf.write2BE(dist); + obuf.write4BE(aflags); + obuf.write4BE(challenge); + obuf.write(str.getBytes()); + + obuf.writeToAndFlush(socket.getOutputStream()); + + if (traceLevel >= handshakeThreshold) { + System.out.println("-> " + "HANDSHAKE sendChallenge" + " flags=" + + aflags + " dist=" + dist + " challenge=" + challenge + + " local=" + localNode); + } } protected byte[] read2BytePackage() throws IOException, - OtpErlangDecodeException { + OtpErlangDecodeException { - final byte[] lbuf = new byte[2]; - byte[] tmpbuf; + final byte[] lbuf = new byte[2]; + byte[] tmpbuf; - readSock(socket, lbuf); - @SuppressWarnings("resource") - final OtpInputStream ibuf = new OtpInputStream(lbuf, 0); - final int len = ibuf.read2BE(); - tmpbuf = new byte[len]; - readSock(socket, tmpbuf); - return tmpbuf; + readSock(socket, lbuf); + @SuppressWarnings("resource") + final OtpInputStream ibuf = new OtpInputStream(lbuf, 0); + final int len = ibuf.read2BE(); + tmpbuf = new byte[len]; + readSock(socket, tmpbuf); + return tmpbuf; } protected void recvName(final OtpPeer apeer) throws IOException { - String hisname = ""; - - try { - final byte[] tmpbuf = read2BytePackage(); - @SuppressWarnings("resource") - final OtpInputStream ibuf = new OtpInputStream(tmpbuf, 0); - byte[] tmpname; - final int len = tmpbuf.length; - apeer.ntype = ibuf.read1(); - if (apeer.ntype != AbstractNode.NTYPE_R6) { - throw new IOException("Unknown remote node type"); - } - apeer.distLow = apeer.distHigh = ibuf.read2BE(); - if (apeer.distLow < 5) { - throw new IOException("Unknown remote node type"); - } - apeer.flags = ibuf.read4BE(); - tmpname = new byte[len - 7]; - ibuf.readN(tmpname); - hisname = OtpErlangString.newString(tmpname); - // Set the old nodetype parameter to indicate hidden/normal status - // When the old handshake is removed, the ntype should also be. - if ((apeer.flags & AbstractNode.dFlagPublished) != 0) { - apeer.ntype = AbstractNode.NTYPE_R4_ERLANG; - } else { - apeer.ntype = AbstractNode.NTYPE_R4_HIDDEN; - } - - if ((apeer.flags & AbstractNode.dFlagExtendedReferences) == 0) { - throw new IOException( - "Handshake failed - peer cannot handle extended references"); - } - - if ((apeer.flags & AbstractNode.dFlagExtendedPidsPorts) == 0) { - throw new IOException( - "Handshake failed - peer cannot handle extended pids and ports"); - } - - } catch (final OtpErlangDecodeException e) { - throw new IOException("Handshake failed - not enough data"); - } - - final int i = hisname.indexOf('@', 0); - apeer.node = hisname; - apeer.alive = hisname.substring(0, i); - apeer.host = hisname.substring(i + 1, hisname.length()); - - if (traceLevel >= handshakeThreshold) { - System.out.println("<- " + "HANDSHAKE" + " ntype=" + apeer.ntype - + " dist=" + apeer.distHigh + " remote=" + apeer); - } + String hisname = ""; + + try { + final byte[] tmpbuf = read2BytePackage(); + @SuppressWarnings("resource") + final OtpInputStream ibuf = new OtpInputStream(tmpbuf, 0); + byte[] tmpname; + final int len = tmpbuf.length; + apeer.ntype = ibuf.read1(); + if (apeer.ntype != AbstractNode.NTYPE_R6) { + throw new IOException("Unknown remote node type"); + } + apeer.distLow = apeer.distHigh = ibuf.read2BE(); + if (apeer.distLow < 5) { + throw new IOException("Unknown remote node type"); + } + apeer.flags = ibuf.read4BE(); + tmpname = new byte[len - 7]; + ibuf.readN(tmpname); + hisname = OtpErlangString.newString(tmpname); + // Set the old nodetype parameter to indicate hidden/normal status + // When the old handshake is removed, the ntype should also be. + if ((apeer.flags & AbstractNode.dFlagPublished) != 0) { + apeer.ntype = AbstractNode.NTYPE_R4_ERLANG; + } else { + apeer.ntype = AbstractNode.NTYPE_R4_HIDDEN; + } + + if ((apeer.flags & AbstractNode.dFlagExtendedReferences) == 0) { + throw new IOException( + "Handshake failed - peer cannot handle extended references"); + } + + if ((apeer.flags & AbstractNode.dFlagExtendedPidsPorts) == 0) { + throw new IOException( + "Handshake failed - peer cannot handle extended pids and ports"); + } + + } catch (final OtpErlangDecodeException e) { + throw new IOException("Handshake failed - not enough data"); + } + + final int i = hisname.indexOf('@', 0); + apeer.node = hisname; + apeer.alive = hisname.substring(0, i); + apeer.host = hisname.substring(i + 1, hisname.length()); + + if (traceLevel >= handshakeThreshold) { + System.out.println("<- " + "HANDSHAKE" + " ntype=" + apeer.ntype + + " dist=" + apeer.distHigh + " remote=" + apeer); + } } protected int recvChallenge() throws IOException { - int challenge; - - try { - final byte[] buf = read2BytePackage(); - @SuppressWarnings("resource") - final OtpInputStream ibuf = new OtpInputStream(buf, 0); - peer.ntype = ibuf.read1(); - if (peer.ntype != AbstractNode.NTYPE_R6) { - throw new IOException("Unexpected peer type"); - } - peer.distLow = peer.distHigh = ibuf.read2BE(); - peer.flags = ibuf.read4BE(); - challenge = ibuf.read4BE(); - final byte[] tmpname = new byte[buf.length - 11]; - ibuf.readN(tmpname); - final String hisname = OtpErlangString.newString(tmpname); - if (!hisname.equals(peer.node)) { - throw new IOException( - "Handshake failed - peer has wrong name: " + hisname); - } - - if ((peer.flags & AbstractNode.dFlagExtendedReferences) == 0) { - throw new IOException( - "Handshake failed - peer cannot handle extended references"); - } - - if ((peer.flags & AbstractNode.dFlagExtendedPidsPorts) == 0) { - throw new IOException( - "Handshake failed - peer cannot handle extended pids and ports"); - } - - } catch (final OtpErlangDecodeException e) { - throw new IOException("Handshake failed - not enough data"); - } - - if (traceLevel >= handshakeThreshold) { - System.out.println("<- " + "HANDSHAKE recvChallenge" + " from=" - + peer.node + " challenge=" + challenge + " local=" + localNode); - } - - return challenge; + int challenge; + + try { + final byte[] buf = read2BytePackage(); + @SuppressWarnings("resource") + final OtpInputStream ibuf = new OtpInputStream(buf, 0); + peer.ntype = ibuf.read1(); + if (peer.ntype != AbstractNode.NTYPE_R6) { + throw new IOException("Unexpected peer type"); + } + peer.distLow = peer.distHigh = ibuf.read2BE(); + peer.flags = ibuf.read4BE(); + challenge = ibuf.read4BE(); + final byte[] tmpname = new byte[buf.length - 11]; + ibuf.readN(tmpname); + final String hisname = OtpErlangString.newString(tmpname); + if (!hisname.equals(peer.node)) { + throw new IOException( + "Handshake failed - peer has wrong name: " + hisname); + } + + if ((peer.flags & AbstractNode.dFlagExtendedReferences) == 0) { + throw new IOException( + "Handshake failed - peer cannot handle extended references"); + } + + if ((peer.flags & AbstractNode.dFlagExtendedPidsPorts) == 0) { + throw new IOException( + "Handshake failed - peer cannot handle extended pids and ports"); + } + + } catch (final OtpErlangDecodeException e) { + throw new IOException("Handshake failed - not enough data"); + } + + if (traceLevel >= handshakeThreshold) { + System.out.println("<- " + "HANDSHAKE recvChallenge" + " from=" + + peer.node + " challenge=" + challenge + " local=" + + localNode); + } + + return challenge; } protected void sendChallengeReply(final int challenge, final byte[] digest) - throws IOException { + throws IOException { - @SuppressWarnings("resource") - final OtpOutputStream obuf = new OtpOutputStream(); - obuf.write2BE(21); - obuf.write1(ChallengeReply); - obuf.write4BE(challenge); - obuf.write(digest); - obuf.writeTo(socket.getOutputStream()); - - if (traceLevel >= handshakeThreshold) { - System.out.println("-> " + "HANDSHAKE sendChallengeReply" - + " challenge=" + challenge + " digest=" + hex(digest) - + " local=" + localNode); - } + @SuppressWarnings("resource") + final OtpOutputStream obuf = new OtpOutputStream(); + obuf.write2BE(21); + obuf.write1(ChallengeReply); + obuf.write4BE(challenge); + obuf.write(digest); + obuf.writeToAndFlush(socket.getOutputStream()); + + if (traceLevel >= handshakeThreshold) { + System.out.println("-> " + "HANDSHAKE sendChallengeReply" + + " challenge=" + challenge + " digest=" + hex(digest) + + " local=" + localNode); + } } // Would use Array.equals in newer JDK... private boolean digests_equals(final byte[] a, final byte[] b) { - int i; - for (i = 0; i < 16; ++i) { - if (a[i] != b[i]) { - return false; - } - } - return true; + int i; + for (i = 0; i < 16; ++i) { + if (a[i] != b[i]) { + return false; + } + } + return true; } protected int recvChallengeReply(final int our_challenge) - throws IOException, OtpAuthException { - - int challenge; - final byte[] her_digest = new byte[16]; - - try { - final byte[] buf = read2BytePackage(); - @SuppressWarnings("resource") - final OtpInputStream ibuf = new OtpInputStream(buf, 0); - final int tag = ibuf.read1(); - if (tag != ChallengeReply) { - throw new IOException("Handshake protocol error"); - } - challenge = ibuf.read4BE(); - ibuf.readN(her_digest); - final byte[] our_digest = genDigest(our_challenge, localNode.cookie()); - if (!digests_equals(her_digest, our_digest)) { - throw new OtpAuthException("Peer authentication error."); - } - } catch (final OtpErlangDecodeException e) { - throw new IOException("Handshake failed - not enough data"); - } - - if (traceLevel >= handshakeThreshold) { - System.out.println("<- " + "HANDSHAKE recvChallengeReply" - + " from=" + peer.node + " challenge=" + challenge - + " digest=" + hex(her_digest) + " local=" + localNode); - } - - return challenge; + throws IOException, OtpAuthException { + + int challenge; + final byte[] her_digest = new byte[16]; + + try { + final byte[] buf = read2BytePackage(); + @SuppressWarnings("resource") + final OtpInputStream ibuf = new OtpInputStream(buf, 0); + final int tag = ibuf.read1(); + if (tag != ChallengeReply) { + throw new IOException("Handshake protocol error"); + } + challenge = ibuf.read4BE(); + ibuf.readN(her_digest); + final byte[] our_digest = genDigest(our_challenge, + localNode.cookie()); + if (!digests_equals(her_digest, our_digest)) { + throw new OtpAuthException("Peer authentication error."); + } + } catch (final OtpErlangDecodeException e) { + throw new IOException("Handshake failed - not enough data"); + } + + if (traceLevel >= handshakeThreshold) { + System.out.println("<- " + "HANDSHAKE recvChallengeReply" + + " from=" + peer.node + " challenge=" + challenge + + " digest=" + hex(her_digest) + " local=" + localNode); + } + + return challenge; } protected void sendChallengeAck(final byte[] digest) throws IOException { - @SuppressWarnings("resource") - final OtpOutputStream obuf = new OtpOutputStream(); - obuf.write2BE(17); - obuf.write1(ChallengeAck); - obuf.write(digest); + @SuppressWarnings("resource") + final OtpOutputStream obuf = new OtpOutputStream(); + obuf.write2BE(17); + obuf.write1(ChallengeAck); + obuf.write(digest); - obuf.writeTo(socket.getOutputStream()); + obuf.writeToAndFlush(socket.getOutputStream()); - if (traceLevel >= handshakeThreshold) { - System.out.println("-> " + "HANDSHAKE sendChallengeAck" - + " digest=" + hex(digest) + " local=" + localNode); - } + if (traceLevel >= handshakeThreshold) { + System.out.println("-> " + "HANDSHAKE sendChallengeAck" + + " digest=" + hex(digest) + " local=" + localNode); + } } protected void recvChallengeAck(final int our_challenge) - throws IOException, OtpAuthException { - - final byte[] her_digest = new byte[16]; - try { - final byte[] buf = read2BytePackage(); - @SuppressWarnings("resource") - final OtpInputStream ibuf = new OtpInputStream(buf, 0); - final int tag = ibuf.read1(); - if (tag != ChallengeAck) { - throw new IOException("Handshake protocol error"); - } - ibuf.readN(her_digest); - final byte[] our_digest = genDigest(our_challenge, localNode.cookie()); - if (!digests_equals(her_digest, our_digest)) { - throw new OtpAuthException("Peer authentication error."); - } - } catch (final OtpErlangDecodeException e) { - throw new IOException("Handshake failed - not enough data"); - } catch (final Exception e) { - throw new OtpAuthException("Peer authentication error."); - } - - if (traceLevel >= handshakeThreshold) { - System.out.println("<- " + "HANDSHAKE recvChallengeAck" + " from=" - + peer.node + " digest=" + hex(her_digest) + " local=" - + localNode); - } + throws IOException, OtpAuthException { + + final byte[] her_digest = new byte[16]; + try { + final byte[] buf = read2BytePackage(); + @SuppressWarnings("resource") + final OtpInputStream ibuf = new OtpInputStream(buf, 0); + final int tag = ibuf.read1(); + if (tag != ChallengeAck) { + throw new IOException("Handshake protocol error"); + } + ibuf.readN(her_digest); + final byte[] our_digest = genDigest(our_challenge, + localNode.cookie()); + if (!digests_equals(her_digest, our_digest)) { + throw new OtpAuthException("Peer authentication error."); + } + } catch (final OtpErlangDecodeException e) { + throw new IOException("Handshake failed - not enough data"); + } catch (final Exception e) { + throw new OtpAuthException("Peer authentication error."); + } + + if (traceLevel >= handshakeThreshold) { + System.out.println("<- " + "HANDSHAKE recvChallengeAck" + " from=" + + peer.node + " digest=" + hex(her_digest) + " local=" + + localNode); + } } protected void sendStatus(final String status) throws IOException { - @SuppressWarnings("resource") - final OtpOutputStream obuf = new OtpOutputStream(); - obuf.write2BE(status.length() + 1); - obuf.write1(ChallengeStatus); - obuf.write(status.getBytes()); + @SuppressWarnings("resource") + final OtpOutputStream obuf = new OtpOutputStream(); + obuf.write2BE(status.length() + 1); + obuf.write1(ChallengeStatus); + obuf.write(status.getBytes()); - obuf.writeTo(socket.getOutputStream()); + obuf.writeToAndFlush(socket.getOutputStream()); - if (traceLevel >= handshakeThreshold) { - System.out.println("-> " + "HANDSHAKE sendStatus" + " status=" - + status + " local=" + localNode); - } + if (traceLevel >= handshakeThreshold) { + System.out.println("-> " + "HANDSHAKE sendStatus" + " status=" + + status + " local=" + localNode); + } } protected void recvStatus() throws IOException { - try { - final byte[] buf = read2BytePackage(); - @SuppressWarnings("resource") - final OtpInputStream ibuf = new OtpInputStream(buf, 0); - final int tag = ibuf.read1(); - if (tag != ChallengeStatus) { - throw new IOException("Handshake protocol error"); - } - final byte[] tmpbuf = new byte[buf.length - 1]; - ibuf.readN(tmpbuf); - final String status = OtpErlangString.newString(tmpbuf); - - if (status.compareTo("ok") != 0) { - throw new IOException("Peer replied with status '" + status - + "' instead of 'ok'"); - } - } catch (final OtpErlangDecodeException e) { - throw new IOException("Handshake failed - not enough data"); - } - if (traceLevel >= handshakeThreshold) { - System.out.println("<- " + "HANDSHAKE recvStatus (ok)" + " local=" - + localNode); - } + try { + final byte[] buf = read2BytePackage(); + @SuppressWarnings("resource") + final OtpInputStream ibuf = new OtpInputStream(buf, 0); + final int tag = ibuf.read1(); + if (tag != ChallengeStatus) { + throw new IOException("Handshake protocol error"); + } + final byte[] tmpbuf = new byte[buf.length - 1]; + ibuf.readN(tmpbuf); + final String status = OtpErlangString.newString(tmpbuf); + + if (status.compareTo("ok") != 0) { + throw new IOException("Peer replied with status '" + status + + "' instead of 'ok'"); + } + } catch (final OtpErlangDecodeException e) { + throw new IOException("Handshake failed - not enough data"); + } + if (traceLevel >= handshakeThreshold) { + System.out.println("<- " + "HANDSHAKE recvStatus (ok)" + " local=" + + localNode); + } } public void setFlags(final int flags) { - this.flags = flags; + this.flags = flags; } public int getFlags() { - return flags; + return flags; } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/AbstractNode.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/AbstractNode.java index 3bb1bbbd18..0a33984b31 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/AbstractNode.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/AbstractNode.java @@ -1,20 +1,20 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2014. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * - * %CopyrightEnd% + * + * %CopyrightEnd% */ package com.ericsson.otp.erlang; @@ -29,7 +29,7 @@ import java.net.UnknownHostException; * <p> * Represents an OTP node. * </p> - * + * * <p> * About nodenames: Erlang nodenames consist of two components, an alivename and * a hostname separated by '@'. Additionally, there are two nodename formats: @@ -40,7 +40,7 @@ import java.net.UnknownHostException; * however Jinterface makes no distinction. See the Erlang documentation for * more information about nodenames. * </p> - * + * * <p> * The constructors for the AbstractNode classes will create names exactly as * you provide them as long as the name contains '@'. If the string you provide @@ -48,7 +48,7 @@ import java.net.UnknownHostException; * host will be appended, resulting in a shortname. Nodenames longer than 255 * characters will be truncated without warning. * </p> - * + * * <p> * Upon initialization, this class attempts to read the file .erlang.cookie in * the user's home directory, and uses the trimmed first line of the file as the @@ -58,19 +58,20 @@ import java.net.UnknownHostException; * using the system property "user.home", which may not be automatically set on * all platforms. * </p> - * + * * <p> * Instances of this class cannot be created directly, use one of the subclasses * instead. * </p> */ -public class AbstractNode { +public class AbstractNode implements OtpTransportFactory { static String localHost = null; String node; String host; String alive; String cookie; static String defaultCookie = null; + final OtpTransportFactory transportFactory; // Node types static final int NTYPE_R6 = 110; // 'n' post-r5, all nodes @@ -100,172 +101,207 @@ public class AbstractNode { int distLow = 5; // Cannot talk to nodes before R6 int creation = 0; int flags = dFlagExtendedReferences | dFlagExtendedPidsPorts - | dFlagBitBinaries | dFlagNewFloats | dFlagFunTags - | dflagNewFunTags | dFlagUtf8Atoms | dFlagMapTag; + | dFlagBitBinaries | dFlagNewFloats | dFlagFunTags + | dflagNewFunTags | dFlagUtf8Atoms | dFlagMapTag; /* initialize hostname and default cookie */ static { - try { - localHost = InetAddress.getLocalHost().getHostName(); - /* - * Make sure it's a short name, i.e. strip of everything after first - * '.' - */ - final int dot = localHost.indexOf("."); - if (dot != -1) { - localHost = localHost.substring(0, dot); - } - } catch (final UnknownHostException e) { - localHost = "localhost"; - } - - final String homeDir = getHomeDir(); - final String dotCookieFilename = homeDir + File.separator + try { + localHost = InetAddress.getLocalHost().getHostName(); + /* + * Make sure it's a short name, i.e. strip of everything after first + * '.' + */ + final int dot = localHost.indexOf("."); + if (dot != -1) { + localHost = localHost.substring(0, dot); + } + } catch (final UnknownHostException e) { + localHost = "localhost"; + } + + final String homeDir = getHomeDir(); + final String dotCookieFilename = homeDir + File.separator + ".erlang.cookie"; - BufferedReader br = null; - - try { - final File dotCookieFile = new File(dotCookieFilename); - - br = new BufferedReader(new FileReader(dotCookieFile)); - final String line = br.readLine(); - if (line == null) { - defaultCookie = ""; - } else { - defaultCookie = line.trim(); - } - } catch (final IOException e) { - defaultCookie = ""; - } finally { - try { - if (br != null) { - br.close(); - } - } catch (final IOException e) { - } - } + BufferedReader br = null; + + try { + final File dotCookieFile = new File(dotCookieFilename); + + br = new BufferedReader(new FileReader(dotCookieFile)); + final String line = br.readLine(); + if (line == null) { + defaultCookie = ""; + } else { + defaultCookie = line.trim(); + } + } catch (final IOException e) { + defaultCookie = ""; + } finally { + try { + if (br != null) { + br.close(); + } + } catch (final IOException e) { + } + } } - protected AbstractNode() { + protected AbstractNode(final OtpTransportFactory transportFactory) { + this.transportFactory = transportFactory; } /** - * Create a node with the given name and the default cookie. + * Create a node with the given name and default cookie and transport + * factory. */ protected AbstractNode(final String node) { - this(node, defaultCookie); + this(node, defaultCookie, new OtpSocketTransportFactory()); + } + + /** + * Create a node with the given name, transport factory and the default + * cookie. + */ + protected AbstractNode(final String node, + final OtpTransportFactory transportFactory) { + this(node, defaultCookie, transportFactory); } /** - * Create a node with the given name and cookie. + * Create a node with the given name, cookie and default transport factory. */ protected AbstractNode(final String name, final String cookie) { - this.cookie = cookie; - - final int i = name.indexOf('@', 0); - if (i < 0) { - alive = name; - host = localHost; - } else { - alive = name.substring(0, i); - host = name.substring(i + 1, name.length()); - } - - if (alive.length() > 0xff) { - alive = alive.substring(0, 0xff); - } - - node = alive + "@" + host; + this(name, cookie, new OtpSocketTransportFactory()); + } + + /** + * Create a node with the given name, cookie and transport factory. + */ + protected AbstractNode(final String name, final String cookie, + final OtpTransportFactory transportFactory) { + this.cookie = cookie; + this.transportFactory = transportFactory; + + final int i = name.indexOf('@', 0); + if (i < 0) { + alive = name; + host = localHost; + } else { + alive = name.substring(0, i); + host = name.substring(i + 1, name.length()); + } + + if (alive.length() > 0xff) { + alive = alive.substring(0, 0xff); + } + + node = alive + "@" + host; } /** * Get the name of this node. - * + * * @return the name of the node represented by this object. */ public String node() { - return node; + return node; } /** * Get the hostname part of the nodename. Nodenames are composed of two * parts, an alivename and a hostname, separated by '@'. This method returns * the part of the nodename following the '@'. - * + * * @return the hostname component of the nodename. */ public String host() { - return host; + return host; } /** * Get the alivename part of the hostname. Nodenames are composed of two * parts, an alivename and a hostname, separated by '@'. This method returns * the part of the nodename preceding the '@'. - * + * * @return the alivename component of the nodename. */ public String alive() { - return alive; + return alive; } /** * Get the authorization cookie used by this node. - * + * * @return the authorization cookie used by this node. */ public String cookie() { - return cookie; + return cookie; } // package scope int type() { - return ntype; + return ntype; } // package scope int distHigh() { - return distHigh; + return distHigh; } // package scope int distLow() { - return distLow; + return distLow; } // package scope: useless information? int proto() { - return proto; + return proto; } // package scope int creation() { - return creation; + return creation; } /** * Set the authorization cookie used by this node. - * + * * @return the previous authorization cookie used by this node. */ public String setCookie(final String cookie) { - final String prev = this.cookie; - this.cookie = cookie; - return prev; + final String prev = this.cookie; + this.cookie = cookie; + return prev; } @Override public String toString() { - return node(); + return node(); } private static String getHomeDir() { - final String home = System.getProperty("user.home"); - if (System.getProperty("os.name").toLowerCase().contains("windows")) { - final String drive = System.getenv("HOMEDRIVE"); - final String path = System.getenv("HOMEPATH"); - return (drive != null && path != null) ? drive + path : home; - } - return home; + final String home = System.getProperty("user.home"); + if (System.getProperty("os.name").toLowerCase().contains("windows")) { + final String drive = System.getenv("HOMEDRIVE"); + final String path = System.getenv("HOMEPATH"); + return drive != null && path != null ? drive + path : home; + } + return home; + } + + public OtpTransport createTransport(final String addr, final int port) + throws IOException { + return transportFactory.createTransport(addr, port); + } + + public OtpTransport createTransport(final InetAddress addr, final int port) + throws IOException { + return transportFactory.createTransport(addr, port); + } + + public OtpServerTransport createServerTransport(final int port) + throws IOException { + return transportFactory.createServerTransport(port); } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/GenericQueue.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/GenericQueue.java index 80bb02f16c..8a66190e6f 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/GenericQueue.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/GenericQueue.java @@ -1,19 +1,19 @@ -/* +/* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; @@ -34,128 +34,128 @@ public class GenericQueue { private int count; private void init() { - head = null; - tail = null; - count = 0; + head = null; + tail = null; + count = 0; } /** Create an empty queue */ public GenericQueue() { - init(); - status = open; + init(); + status = open; } /** Clear a queue */ public void flush() { - init(); + init(); } public void close() { - status = closing; + status = closing; } /** * Add an object to the tail of the queue. - * + * * @param o - * Object to insert in the queue + * Object to insert in the queue */ public synchronized void put(final Object o) { - final Bucket b = new Bucket(o); - - if (tail != null) { - tail.setNext(b); - tail = b; - } else { - // queue was empty but has one element now - head = tail = b; - } - count++; - - // notify any waiting tasks - notify(); + final Bucket b = new Bucket(o); + + if (tail != null) { + tail.setNext(b); + tail = b; + } else { + // queue was empty but has one element now + head = tail = b; + } + count++; + + // notify any waiting tasks + notify(); } /** * Retrieve an object from the head of the queue, or block until one * arrives. - * + * * @return The object at the head of the queue. */ public synchronized Object get() { - Object o = null; - - while ((o = tryGet()) == null) { - try { - this.wait(); - } catch (final InterruptedException e) { - } - } - return o; + Object o = null; + + while ((o = tryGet()) == null) { + try { + this.wait(); + } catch (final InterruptedException e) { + } + } + return o; } /** * Retrieve an object from the head of the queue, blocking until one arrives * or until timeout occurs. - * + * * @param timeout - * Maximum time to block on queue, in ms. Use 0 to poll the - * queue. - * + * Maximum time to block on queue, in ms. Use 0 to poll the + * queue. + * * @exception InterruptedException - * if the operation times out. - * + * if the operation times out. + * * @return The object at the head of the queue, or null if none arrived in * time. */ public synchronized Object get(final long timeout) - throws InterruptedException { - if (status == closed) { - return null; - } - - long currentTime = System.currentTimeMillis(); - final long stopTime = currentTime + timeout; - Object o = null; - - while (true) { - if ((o = tryGet()) != null) { - return o; - } - - currentTime = System.currentTimeMillis(); - if (stopTime <= currentTime) { - throw new InterruptedException("Get operation timed out"); - } - - try { - this.wait(stopTime - currentTime); - } catch (final InterruptedException e) { - // ignore, but really should retry operation instead - } - } + throws InterruptedException { + if (status == closed) { + return null; + } + + long currentTime = System.currentTimeMillis(); + final long stopTime = currentTime + timeout; + Object o = null; + + while (true) { + if ((o = tryGet()) != null) { + return o; + } + + currentTime = System.currentTimeMillis(); + if (stopTime <= currentTime) { + throw new InterruptedException("Get operation timed out"); + } + + try { + this.wait(stopTime - currentTime); + } catch (final InterruptedException e) { + // ignore, but really should retry operation instead + } + } } // attempt to retrieve message from queue head public Object tryGet() { - Object o = null; + Object o = null; - if (head != null) { - o = head.getContents(); - head = head.getNext(); - count--; + if (head != null) { + o = head.getContents(); + head = head.getNext(); + count--; - if (head == null) { - tail = null; - count = 0; - } - } + if (head == null) { + tail = null; + count = 0; + } + } - return o; + return o; } public synchronized int getCount() { - return count; + return count; } /* @@ -163,24 +163,24 @@ public class GenericQueue { * The container holds the queued object and a reference to the next Bucket. */ class Bucket { - private Bucket next; - private final Object contents; + private Bucket next; + private final Object contents; - public Bucket(final Object o) { - next = null; - contents = o; - } + public Bucket(final Object o) { + next = null; + contents = o; + } - public void setNext(final Bucket newNext) { - next = newNext; - } + public void setNext(final Bucket newNext) { + next = newNext; + } - public Bucket getNext() { - return next; - } + public Bucket getNext() { + return next; + } - public Object getContents() { - return contents; - } + public Object getContents() { + return contents; + } } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/Link.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/Link.java index c8b4fcebde..33ba94e53f 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/Link.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/Link.java @@ -1,19 +1,19 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; @@ -25,34 +25,34 @@ class Link { private int hashCodeValue = 0; public Link(final OtpErlangPid local, final OtpErlangPid remote) { - this.local = local; - this.remote = remote; + this.local = local; + this.remote = remote; } public OtpErlangPid local() { - return local; + return local; } public OtpErlangPid remote() { - return remote; + return remote; } public boolean contains(final OtpErlangPid pid) { - return local.equals(pid) || remote.equals(pid); + return local.equals(pid) || remote.equals(pid); } public boolean equals(final OtpErlangPid alocal, final OtpErlangPid aremote) { - return local.equals(alocal) && remote.equals(aremote) - || local.equals(aremote) && remote.equals(alocal); + return local.equals(alocal) && remote.equals(aremote) + || local.equals(aremote) && remote.equals(alocal); } - + @Override public int hashCode() { - if (hashCodeValue == 0) { - OtpErlangObject.Hash hash = new OtpErlangObject.Hash(5); - hash.combine(local.hashCode() + remote.hashCode()); - hashCodeValue = hash.valueOf(); - } - return hashCodeValue; + if (hashCodeValue == 0) { + final OtpErlangObject.Hash hash = new OtpErlangObject.Hash(5); + hash.combine(local.hashCode() + remote.hashCode()); + hashCodeValue = hash.valueOf(); + } + return hashCodeValue; } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/Links.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/Links.java index 0bb4a708a3..38517860ed 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/Links.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/Links.java @@ -1,19 +1,19 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; @@ -24,100 +24,100 @@ class Links { int count; Links() { - this(10); + this(10); } Links(final int initialSize) { - links = new Link[initialSize]; - count = 0; + links = new Link[initialSize]; + count = 0; } synchronized void addLink(final OtpErlangPid local, - final OtpErlangPid remote) { - if (find(local, remote) == -1) { - if (count >= links.length) { - final Link[] tmp = new Link[count * 2]; - System.arraycopy(links, 0, tmp, 0, count); - links = tmp; - } - links[count++] = new Link(local, remote); - } + final OtpErlangPid remote) { + if (find(local, remote) == -1) { + if (count >= links.length) { + final Link[] tmp = new Link[count * 2]; + System.arraycopy(links, 0, tmp, 0, count); + links = tmp; + } + links[count++] = new Link(local, remote); + } } synchronized void removeLink(final OtpErlangPid local, - final OtpErlangPid remote) { - int i; + final OtpErlangPid remote) { + int i; - if ((i = find(local, remote)) != -1) { - count--; - links[i] = links[count]; - links[count] = null; - } + if ((i = find(local, remote)) != -1) { + count--; + links[i] = links[count]; + links[count] = null; + } } synchronized boolean exists(final OtpErlangPid local, - final OtpErlangPid remote) { - return find(local, remote) != -1; + final OtpErlangPid remote) { + return find(local, remote) != -1; } synchronized int find(final OtpErlangPid local, final OtpErlangPid remote) { - for (int i = 0; i < count; i++) { - if (links[i].equals(local, remote)) { - return i; - } - } - return -1; + for (int i = 0; i < count; i++) { + if (links[i].equals(local, remote)) { + return i; + } + } + return -1; } int count() { - return count; + return count; } /* all local pids get notified about broken connection */ synchronized OtpErlangPid[] localPids() { - OtpErlangPid[] ret = null; - if (count != 0) { - ret = new OtpErlangPid[count]; - for (int i = 0; i < count; i++) { - ret[i] = links[i].local(); - } - } - return ret; + OtpErlangPid[] ret = null; + if (count != 0) { + ret = new OtpErlangPid[count]; + for (int i = 0; i < count; i++) { + ret[i] = links[i].local(); + } + } + return ret; } /* all remote pids get notified about failed pid */ synchronized OtpErlangPid[] remotePids() { - OtpErlangPid[] ret = null; - if (count != 0) { - ret = new OtpErlangPid[count]; - for (int i = 0; i < count; i++) { - ret[i] = links[i].remote(); - } - } - return ret; + OtpErlangPid[] ret = null; + if (count != 0) { + ret = new OtpErlangPid[count]; + for (int i = 0; i < count; i++) { + ret[i] = links[i].remote(); + } + } + return ret; } /* clears the link table, returns a copy */ synchronized Link[] clearLinks() { - Link[] ret = null; - if (count != 0) { - ret = new Link[count]; - for (int i = 0; i < count; i++) { - ret[i] = links[i]; - links[i] = null; - } - count = 0; - } - return ret; + Link[] ret = null; + if (count != 0) { + ret = new Link[count]; + for (int i = 0; i < count; i++) { + ret[i] = links[i]; + links[i] = null; + } + count = 0; + } + return ret; } /* returns a copy of the link table */ synchronized Link[] links() { - Link[] ret = null; - if (count != 0) { - ret = new Link[count]; - System.arraycopy(links, 0, ret, 0, count); - } - return ret; + Link[] ret = null; + if (count != 0) { + ret = new Link[count]; + System.arraycopy(links, 0, ret, 0, count); + } + return ret; } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpAuthException.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpAuthException.java index 39d254d9fa..47646121c3 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpAuthException.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpAuthException.java @@ -1,19 +1,19 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; @@ -22,7 +22,7 @@ package com.ericsson.otp.erlang; * Exception raised when a node attempts to establish a communication channel * when it is not authorized to do so, or when a node sends a message containing * an invalid cookie on an established channel. - * + * * @see OtpConnection */ public class OtpAuthException extends OtpException { @@ -32,6 +32,6 @@ public class OtpAuthException extends OtpException { * Provides a detailed message. */ public OtpAuthException(final String s) { - super(s); + super(s); } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpConnection.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpConnection.java index 9ad02506fd..af0926f939 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpConnection.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpConnection.java @@ -1,46 +1,45 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; import java.io.IOException; -import java.net.Socket; /** * Maintains a connection between a Java process and a remote Erlang, Java or C * node. The object maintains connection state and allows data to be sent to and * received from the peer. - * + * * <p> * Once a connection is established between the local node and a remote node, * the connection object can be used to send and receive messages between the * nodes and make rpc calls (assuming that the remote node is a real Erlang * node). - * + * * <p> * The various receive methods are all blocking and will return only when a * valid message has been received or an exception is raised. - * + * * <p> * If an exception occurs in any of the methods in this class, the connection * will be closed and must be explicitely reopened in order to resume * communication with the peer. - * + * * <p> * It is not possible to create an instance of this class directly. * OtpConnection objects are returned by {@link OtpSelf#connect(OtpPeer) @@ -55,66 +54,66 @@ public class OtpConnection extends AbstractConnection { * OtpSelf#accept() OtpSelf.accept()} to create a connection based on data * received when handshaking with the peer node, when the remote node is the * connection intitiator. - * + * * @exception java.io.IOException if it was not possible to connect to the * peer. - * + * * @exception OtpAuthException if handshake resulted in an authentication * error */ // package scope - OtpConnection(final OtpSelf self, final Socket s) throws IOException, - OtpAuthException { - super(self, s); - this.self = self; - queue = new GenericQueue(); - start(); + OtpConnection(final OtpSelf self, final OtpTransport s) + throws IOException, OtpAuthException { + super(self, s); + this.self = self; + queue = new GenericQueue(); + start(); } /* * Intiate and open a connection to a remote node. - * + * * @exception java.io.IOException if it was not possible to connect to the * peer. - * + * * @exception OtpAuthException if handshake resulted in an authentication * error. */ // package scope OtpConnection(final OtpSelf self, final OtpPeer other) throws IOException, - OtpAuthException { - super(self, other); - this.self = self; - queue = new GenericQueue(); - start(); + OtpAuthException { + super(self, other); + this.self = self; + queue = new GenericQueue(); + start(); } @Override public void deliver(final Exception e) { - queue.put(e); + queue.put(e); } @Override public void deliver(final OtpMsg msg) { - queue.put(msg); + queue.put(msg); } /** * Get information about the node at the peer end of this connection. - * + * * @return the {@link OtpPeer Node} representing the peer node. */ public OtpPeer peer() { - return peer; + return peer; } /** * Get information about the node at the local end of this connection. - * + * * @return the {@link OtpSelf Node} representing the local node. */ public OtpSelf self() { - return self; + return self; } /** @@ -122,416 +121,412 @@ public class OtpConnection extends AbstractConnection { * this connection. */ public int msgCount() { - return queue.getCount(); + return queue.getCount(); } /** * Receive a message from a remote process. This method blocks until a valid * message is received or an exception is raised. - * + * * <p> * If the remote node sends a message that cannot be decoded properly, the * connection is closed and the method throws an exception. - * + * * @return an object containing a single Erlang term. - * + * * @exception java.io.IOException - * if the connection is not active or a communication - * error occurs. - * + * if the connection is not active or a communication error + * occurs. + * * @exception OtpErlangExit - * if an exit signal is received from a process on the - * peer node. - * + * if an exit signal is received from a process on the peer + * node. + * * @exception OtpAuthException - * if the remote node sends a message containing an - * invalid cookie. + * if the remote node sends a message containing an invalid + * cookie. */ public OtpErlangObject receive() throws IOException, OtpErlangExit, - OtpAuthException { - try { - return receiveMsg().getMsg(); - } catch (final OtpErlangDecodeException e) { - close(); - throw new IOException(e.getMessage()); - } + OtpAuthException { + try { + return receiveMsg().getMsg(); + } catch (final OtpErlangDecodeException e) { + close(); + throw new IOException(e.getMessage()); + } } /** * Receive a message from a remote process. This method blocks at most for * the specified time, until a valid message is received or an exception is * raised. - * + * * <p> * If the remote node sends a message that cannot be decoded properly, the * connection is closed and the method throws an exception. - * + * * @param timeout - * the time in milliseconds that this operation will block. - * Specify 0 to poll the queue. - * + * the time in milliseconds that this operation will block. + * Specify 0 to poll the queue. + * * @return an object containing a single Erlang term. - * + * * @exception java.io.IOException - * if the connection is not active or a communication - * error occurs. - * + * if the connection is not active or a communication error + * occurs. + * * @exception OtpErlangExit - * if an exit signal is received from a process on the - * peer node. - * + * if an exit signal is received from a process on the peer + * node. + * * @exception OtpAuthException - * if the remote node sends a message containing an - * invalid cookie. - * + * if the remote node sends a message containing an invalid + * cookie. + * * @exception InterruptedException - * if no message if the method times out before a message - * becomes available. + * if no message if the method times out before a message + * becomes available. */ public OtpErlangObject receive(final long timeout) - throws InterruptedException, IOException, OtpErlangExit, - OtpAuthException { - try { - return receiveMsg(timeout).getMsg(); - } catch (final OtpErlangDecodeException e) { - close(); - throw new IOException(e.getMessage()); - } + throws InterruptedException, IOException, OtpErlangExit, + OtpAuthException { + try { + return receiveMsg(timeout).getMsg(); + } catch (final OtpErlangDecodeException e) { + close(); + throw new IOException(e.getMessage()); + } } /** * Receive a raw (still encoded) message from a remote process. This message * blocks until a valid message is received or an exception is raised. - * + * * <p> * If the remote node sends a message that cannot be decoded properly, the * connection is closed and the method throws an exception. - * + * * @return an object containing a raw (still encoded) Erlang term. - * + * * @exception java.io.IOException - * if the connection is not active or a communication - * error occurs. - * + * if the connection is not active or a communication error + * occurs. + * * @exception OtpErlangExit - * if an exit signal is received from a process on the - * peer node, or if the connection is lost for any - * reason. - * + * if an exit signal is received from a process on the peer + * node, or if the connection is lost for any reason. + * * @exception OtpAuthException - * if the remote node sends a message containing an - * invalid cookie. + * if the remote node sends a message containing an invalid + * cookie. */ public OtpInputStream receiveBuf() throws IOException, OtpErlangExit, - OtpAuthException { - return receiveMsg().getMsgBuf(); + OtpAuthException { + return receiveMsg().getMsgBuf(); } /** * Receive a raw (still encoded) message from a remote process. This message * blocks at most for the specified time until a valid message is received * or an exception is raised. - * + * * <p> * If the remote node sends a message that cannot be decoded properly, the * connection is closed and the method throws an exception. - * + * * @param timeout - * the time in milliseconds that this operation will block. - * Specify 0 to poll the queue. - * + * the time in milliseconds that this operation will block. + * Specify 0 to poll the queue. + * * @return an object containing a raw (still encoded) Erlang term. - * + * * @exception java.io.IOException - * if the connection is not active or a communication - * error occurs. - * + * if the connection is not active or a communication error + * occurs. + * * @exception OtpErlangExit - * if an exit signal is received from a process on the - * peer node, or if the connection is lost for any - * reason. - * + * if an exit signal is received from a process on the peer + * node, or if the connection is lost for any reason. + * * @exception OtpAuthException - * if the remote node sends a message containing an - * invalid cookie. - * + * if the remote node sends a message containing an invalid + * cookie. + * * @exception InterruptedException - * if no message if the method times out before a message - * becomes available. + * if no message if the method times out before a message + * becomes available. */ public OtpInputStream receiveBuf(final long timeout) - throws InterruptedException, IOException, OtpErlangExit, - OtpAuthException { - return receiveMsg(timeout).getMsgBuf(); + throws InterruptedException, IOException, OtpErlangExit, + OtpAuthException { + return receiveMsg(timeout).getMsgBuf(); } /** * Receive a messge complete with sender and recipient information. - * + * * @return an {@link OtpMsg OtpMsg} containing the header information about * the sender and recipient, as well as the actual message contents. - * + * * @exception java.io.IOException - * if the connection is not active or a communication - * error occurs. - * + * if the connection is not active or a communication error + * occurs. + * * @exception OtpErlangExit - * if an exit signal is received from a process on the - * peer node, or if the connection is lost for any - * reason. - * + * if an exit signal is received from a process on the peer + * node, or if the connection is lost for any reason. + * * @exception OtpAuthException - * if the remote node sends a message containing an - * invalid cookie. + * if the remote node sends a message containing an invalid + * cookie. */ public OtpMsg receiveMsg() throws IOException, OtpErlangExit, - OtpAuthException { - final Object o = queue.get(); - - if (o instanceof OtpMsg) { - return (OtpMsg) o; - } else if (o instanceof IOException) { - throw (IOException) o; - } else if (o instanceof OtpErlangExit) { - throw (OtpErlangExit) o; - } else if (o instanceof OtpAuthException) { - throw (OtpAuthException) o; - } - - return null; + OtpAuthException { + final Object o = queue.get(); + + if (o instanceof OtpMsg) { + return (OtpMsg) o; + } else if (o instanceof IOException) { + throw (IOException) o; + } else if (o instanceof OtpErlangExit) { + throw (OtpErlangExit) o; + } else if (o instanceof OtpAuthException) { + throw (OtpAuthException) o; + } + + return null; } /** * Receive a messge complete with sender and recipient information. This * method blocks at most for the specified time. - * + * * @param timeout - * the time in milliseconds that this operation will block. - * Specify 0 to poll the queue. - * + * the time in milliseconds that this operation will block. + * Specify 0 to poll the queue. + * * @return an {@link OtpMsg OtpMsg} containing the header information about * the sender and recipient, as well as the actual message contents. - * + * * @exception java.io.IOException - * if the connection is not active or a communication - * error occurs. - * + * if the connection is not active or a communication error + * occurs. + * * @exception OtpErlangExit - * if an exit signal is received from a process on the - * peer node, or if the connection is lost for any - * reason. - * + * if an exit signal is received from a process on the peer + * node, or if the connection is lost for any reason. + * * @exception OtpAuthException - * if the remote node sends a message containing an - * invalid cookie. - * + * if the remote node sends a message containing an invalid + * cookie. + * * @exception InterruptedException - * if no message if the method times out before a message - * becomes available. + * if no message if the method times out before a message + * becomes available. */ public OtpMsg receiveMsg(final long timeout) throws InterruptedException, - IOException, OtpErlangExit, OtpAuthException { - final Object o = queue.get(timeout); - - if (o instanceof OtpMsg) { - return (OtpMsg) o; - } else if (o instanceof IOException) { - throw (IOException) o; - } else if (o instanceof OtpErlangExit) { - throw (OtpErlangExit) o; - } else if (o instanceof OtpAuthException) { - throw (OtpAuthException) o; - } - - return null; + IOException, OtpErlangExit, OtpAuthException { + final Object o = queue.get(timeout); + + if (o instanceof OtpMsg) { + return (OtpMsg) o; + } else if (o instanceof IOException) { + throw (IOException) o; + } else if (o instanceof OtpErlangExit) { + throw (OtpErlangExit) o; + } else if (o instanceof OtpAuthException) { + throw (OtpAuthException) o; + } + + return null; } /** * Send a message to a process on a remote node. - * + * * @param dest - * the Erlang PID of the remote process. + * the Erlang PID of the remote process. * @param msg - * the message to send. - * + * the message to send. + * * @exception java.io.IOException - * if the connection is not active or a communication - * error occurs. + * if the connection is not active or a communication error + * occurs. */ @SuppressWarnings("resource") public void send(final OtpErlangPid dest, final OtpErlangObject msg) - throws IOException { - // encode and send the message - super.sendBuf(self.pid(), dest, new OtpOutputStream(msg)); + throws IOException { + // encode and send the message + super.sendBuf(self.pid(), dest, new OtpOutputStream(msg)); } /** * Send a message to a named process on a remote node. - * + * * @param dest - * the name of the remote process. + * the name of the remote process. * @param msg - * the message to send. - * + * the message to send. + * * @exception java.io.IOException - * if the connection is not active or a communication - * error occurs. + * if the connection is not active or a communication error + * occurs. */ @SuppressWarnings("resource") public void send(final String dest, final OtpErlangObject msg) - throws IOException { - // encode and send the message - super.sendBuf(self.pid(), dest, new OtpOutputStream(msg)); + throws IOException { + // encode and send the message + super.sendBuf(self.pid(), dest, new OtpOutputStream(msg)); } /** * Send a pre-encoded message to a named process on a remote node. - * + * * @param dest - * the name of the remote process. + * the name of the remote process. * @param payload - * the encoded message to send. - * + * the encoded message to send. + * * @exception java.io.IOException - * if the connection is not active or a communication - * error occurs. + * if the connection is not active or a communication error + * occurs. */ public void sendBuf(final String dest, final OtpOutputStream payload) - throws IOException { - super.sendBuf(self.pid(), dest, payload); + throws IOException { + super.sendBuf(self.pid(), dest, payload); } /** * Send a pre-encoded message to a process on a remote node. - * + * * @param dest - * the Erlang PID of the remote process. + * the Erlang PID of the remote process. * @param payload - * the encoded message to send. - * + * the encoded message to send. + * * @exception java.io.IOException - * if the connection is not active or a communication - * error occurs. + * if the connection is not active or a communication error + * occurs. */ public void sendBuf(final OtpErlangPid dest, final OtpOutputStream payload) - throws IOException { - super.sendBuf(self.pid(), dest, payload); + throws IOException { + super.sendBuf(self.pid(), dest, payload); } /** * Send an RPC request to the remote Erlang node. This convenience function * creates the following message and sends it to 'rex' on the remote node: - * + * * <pre> * { self, { call, Mod, Fun, Args, user } } * </pre> - * + * * <p> * Note that this method has unpredicatble results if the remote node is not * an Erlang node. * </p> - * + * * @param mod - * the name of the Erlang module containing the function to - * be called. + * the name of the Erlang module containing the function to be + * called. * @param fun - * the name of the function to call. + * the name of the function to call. * @param args - * an array of Erlang terms, to be used as arguments to the - * function. - * + * an array of Erlang terms, to be used as arguments to the + * function. + * * @exception java.io.IOException - * if the connection is not active or a communication - * error occurs. + * if the connection is not active or a communication error + * occurs. */ public void sendRPC(final String mod, final String fun, - final OtpErlangObject[] args) throws IOException { - sendRPC(mod, fun, new OtpErlangList(args)); + final OtpErlangObject[] args) throws IOException { + sendRPC(mod, fun, new OtpErlangList(args)); } /** * Send an RPC request to the remote Erlang node. This convenience function * creates the following message and sends it to 'rex' on the remote node: - * + * * <pre> * { self, { call, Mod, Fun, Args, user } } * </pre> - * + * * <p> * Note that this method has unpredicatble results if the remote node is not * an Erlang node. * </p> - * + * * @param mod - * the name of the Erlang module containing the function to - * be called. + * the name of the Erlang module containing the function to be + * called. * @param fun - * the name of the function to call. + * the name of the function to call. * @param args - * a list of Erlang terms, to be used as arguments to the - * function. - * + * a list of Erlang terms, to be used as arguments to the + * function. + * * @exception java.io.IOException - * if the connection is not active or a communication - * error occurs. + * if the connection is not active or a communication error + * occurs. */ public void sendRPC(final String mod, final String fun, - final OtpErlangList args) throws IOException { - final OtpErlangObject[] rpc = new OtpErlangObject[2]; - final OtpErlangObject[] call = new OtpErlangObject[5]; + final OtpErlangList args) throws IOException { + final OtpErlangObject[] rpc = new OtpErlangObject[2]; + final OtpErlangObject[] call = new OtpErlangObject[5]; - /* {self, { call, Mod, Fun, Args, user}} */ + /* {self, { call, Mod, Fun, Args, user}} */ - call[0] = new OtpErlangAtom("call"); - call[1] = new OtpErlangAtom(mod); - call[2] = new OtpErlangAtom(fun); - call[3] = args; - call[4] = new OtpErlangAtom("user"); + call[0] = new OtpErlangAtom("call"); + call[1] = new OtpErlangAtom(mod); + call[2] = new OtpErlangAtom(fun); + call[3] = args; + call[4] = new OtpErlangAtom("user"); - rpc[0] = self.pid(); - rpc[1] = new OtpErlangTuple(call); + rpc[0] = self.pid(); + rpc[1] = new OtpErlangTuple(call); - send("rex", new OtpErlangTuple(rpc)); + send("rex", new OtpErlangTuple(rpc)); } /** * Receive an RPC reply from the remote Erlang node. This convenience * function receives a message from the remote node, and expects it to have * the following format: - * + * * <pre> * { rex, Term } * </pre> - * + * * @return the second element of the tuple if the received message is a * two-tuple, otherwise null. No further error checking is * performed. - * + * * @exception java.io.IOException - * if the connection is not active or a communication - * error occurs. - * + * if the connection is not active or a communication error + * occurs. + * * @exception OtpErlangExit - * if an exit signal is received from a process on the - * peer node. - * + * if an exit signal is received from a process on the peer + * node. + * * @exception OtpAuthException - * if the remote node sends a message containing an - * invalid cookie. + * if the remote node sends a message containing an invalid + * cookie. */ public OtpErlangObject receiveRPC() throws IOException, OtpErlangExit, - OtpAuthException { + OtpAuthException { - final OtpErlangObject msg = receive(); + final OtpErlangObject msg = receive(); - if (msg instanceof OtpErlangTuple) { - final OtpErlangTuple t = (OtpErlangTuple) msg; - if (t.arity() == 2) { - return t.elementAt(1); // obs: second element - } - } + if (msg instanceof OtpErlangTuple) { + final OtpErlangTuple t = (OtpErlangTuple) msg; + if (t.arity() == 2) { + return t.elementAt(1); // obs: second element + } + } - return null; + return null; } /** @@ -539,48 +534,48 @@ public class OtpConnection extends AbstractConnection { * remote node. If the link is still active when the remote process * terminates, an exit signal will be sent to this connection. Use * {@link #unlink unlink()} to remove the link. - * + * * @param dest - * the Erlang PID of the remote process. - * + * the Erlang PID of the remote process. + * * @exception java.io.IOException - * if the connection is not active or a communication - * error occurs. + * if the connection is not active or a communication error + * occurs. */ public void link(final OtpErlangPid dest) throws IOException { - super.sendLink(self.pid(), dest); + super.sendLink(self.pid(), dest); } /** * Remove a link between the local node and the specified process on the - * remote node. This method deactivates links created with - * {@link #link link()}. - * + * remote node. This method deactivates links created with {@link #link + * link()}. + * * @param dest - * the Erlang PID of the remote process. - * + * the Erlang PID of the remote process. + * * @exception java.io.IOException - * if the connection is not active or a communication - * error occurs. + * if the connection is not active or a communication error + * occurs. */ public void unlink(final OtpErlangPid dest) throws IOException { - super.sendUnlink(self.pid(), dest); + super.sendUnlink(self.pid(), dest); } /** * Send an exit signal to a remote process. - * + * * @param dest - * the Erlang PID of the remote process. + * the Erlang PID of the remote process. * @param reason - * an Erlang term describing the exit reason. - * + * an Erlang term describing the exit reason. + * * @exception java.io.IOException - * if the connection is not active or a communication - * error occurs. + * if the connection is not active or a communication error + * occurs. */ public void exit(final OtpErlangPid dest, final OtpErlangObject reason) - throws IOException { - super.sendExit2(self.pid(), dest, reason); + throws IOException { + super.sendExit2(self.pid(), dest, reason); } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpCookedConnection.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpCookedConnection.java index 43b0cad222..b0e3e81fca 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpCookedConnection.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpCookedConnection.java @@ -1,25 +1,24 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; import java.io.IOException; -import java.net.Socket; /** * <p> @@ -27,29 +26,29 @@ import java.net.Socket; * node. The object maintains connection state and allows data to be sent to and * received from the peer. * </p> - * + * * <p> * Once a connection is established between the local node and a remote node, * the connection object can be used to send and receive messages between the * nodes. * </p> - * + * * <p> * The various receive methods are all blocking and will return only when a * valid message has been received or an exception is raised. * </p> - * + * * <p> * If an exception occurs in any of the methods in this class, the connection * will be closed and must be reopened in order to resume communication with the * peer. * </p> - * + * * <p> * The message delivery methods in this class deliver directly to * {@link OtpMbox mailboxes} in the {@link OtpNode OtpNode} class. * </p> - * + * * <p> * It is not possible to create an instance of this class directly. * OtpCookedConnection objects are created as needed by the underlying mailbox @@ -70,45 +69,45 @@ public class OtpCookedConnection extends AbstractConnection { * OtpSelf#accept() OtpSelf.accept()} to create a connection based on data * received when handshaking with the peer node, when the remote node is the * connection intitiator. - * + * * @exception java.io.IOException if it was not possible to connect to the * peer. - * + * * @exception OtpAuthException if handshake resulted in an authentication * error */ // package scope - OtpCookedConnection(final OtpNode self, final Socket s) throws IOException, - OtpAuthException { - super(self, s); - this.self = self; - links = new Links(25); - start(); + OtpCookedConnection(final OtpNode self, final OtpTransport s) + throws IOException, OtpAuthException { + super(self, s); + this.self = self; + links = new Links(25); + start(); } /* * Intiate and open a connection to a remote node. - * + * * @exception java.io.IOException if it was not possible to connect to the * peer. - * + * * @exception OtpAuthException if handshake resulted in an authentication * error. */ // package scope OtpCookedConnection(final OtpNode self, final OtpPeer other) - throws IOException, OtpAuthException { - super(self, other); - this.self = self; - links = new Links(25); - start(); + throws IOException, OtpAuthException { + super(self, other); + this.self = self; + links = new Links(25); + start(); } // pass the error to the node @Override public void deliver(final Exception e) { - self.deliverError(this, e); - return; + self.deliverError(this, e); + return; } /* @@ -118,32 +117,32 @@ public class OtpCookedConnection extends AbstractConnection { */ @Override public void deliver(final OtpMsg msg) { - final boolean delivered = self.deliver(msg); - - switch (msg.type()) { - case OtpMsg.linkTag: - if (delivered) { - links.addLink(msg.getRecipientPid(), msg.getSenderPid()); - } else { - try { - // no such pid - send exit to sender - super.sendExit(msg.getRecipientPid(), msg.getSenderPid(), - new OtpErlangAtom("noproc")); - } catch (final IOException e) { - } - } - break; - - case OtpMsg.unlinkTag: - case OtpMsg.exitTag: - links.removeLink(msg.getRecipientPid(), msg.getSenderPid()); - break; - - case OtpMsg.exit2Tag: - break; - } - - return; + final boolean delivered = self.deliver(msg); + + switch (msg.type()) { + case OtpMsg.linkTag: + if (delivered) { + links.addLink(msg.getRecipientPid(), msg.getSenderPid()); + } else { + try { + // no such pid - send exit to sender + super.sendExit(msg.getRecipientPid(), msg.getSenderPid(), + new OtpErlangAtom("noproc")); + } catch (final IOException e) { + } + } + break; + + case OtpMsg.unlinkTag: + case OtpMsg.exitTag: + links.removeLink(msg.getRecipientPid(), msg.getSenderPid()); + break; + + case OtpMsg.exit2Tag: + break; + } + + return; } /* @@ -151,9 +150,9 @@ public class OtpCookedConnection extends AbstractConnection { */ @SuppressWarnings("resource") void send(final OtpErlangPid from, final OtpErlangPid dest, - final OtpErlangObject msg) throws IOException { - // encode and send the message - sendBuf(from, dest, new OtpOutputStream(msg)); + final OtpErlangObject msg) throws IOException { + // encode and send the message + sendBuf(from, dest, new OtpOutputStream(msg)); } /* @@ -162,66 +161,66 @@ public class OtpCookedConnection extends AbstractConnection { */ @SuppressWarnings("resource") void send(final OtpErlangPid from, final String dest, - final OtpErlangObject msg) throws IOException { - // encode and send the message - sendBuf(from, dest, new OtpOutputStream(msg)); + final OtpErlangObject msg) throws IOException { + // encode and send the message + sendBuf(from, dest, new OtpOutputStream(msg)); } @Override public void close() { - super.close(); - breakLinks(); + super.close(); + breakLinks(); } @Override protected void finalize() { - close(); + close(); } /* * this one called by dying/killed process */ void exit(final OtpErlangPid from, final OtpErlangPid to, - final OtpErlangObject reason) { - try { - super.sendExit(from, to, reason); - } catch (final Exception e) { - } + final OtpErlangObject reason) { + try { + super.sendExit(from, to, reason); + } catch (final Exception e) { + } } /* * this one called explicitely by user code => use exit2 */ void exit2(final OtpErlangPid from, final OtpErlangPid to, - final OtpErlangObject reason) { - try { - super.sendExit2(from, to, reason); - } catch (final Exception e) { - } + final OtpErlangObject reason) { + try { + super.sendExit2(from, to, reason); + } catch (final Exception e) { + } } /* * snoop for outgoing links and update own table */ synchronized void link(final OtpErlangPid from, final OtpErlangPid to) - throws OtpErlangExit { - try { - super.sendLink(from, to); - links.addLink(from, to); - } catch (final IOException e) { - throw new OtpErlangExit("noproc", to); - } + throws OtpErlangExit { + try { + super.sendLink(from, to); + links.addLink(from, to); + } catch (final IOException e) { + throw new OtpErlangExit("noproc", to); + } } /* * snoop for outgoing unlinks and update own table */ synchronized void unlink(final OtpErlangPid from, final OtpErlangPid to) { - links.removeLink(from, to); - try { - super.sendUnlink(from, to); - } catch (final IOException e) { - } + links.removeLink(from, to); + try { + super.sendUnlink(from, to); + } catch (final IOException e) { + } } /* @@ -229,18 +228,18 @@ public class OtpCookedConnection extends AbstractConnection { * through this connection */ synchronized void breakLinks() { - if (links != null) { - final Link[] l = links.clearLinks(); - - if (l != null) { - final int len = l.length; - - for (int i = 0; i < len; i++) { - // send exit "from" remote pids to local ones - self.deliver(new OtpMsg(OtpMsg.exitTag, l[i].remote(), l[i] - .local(), new OtpErlangAtom("noconnection"))); - } - } - } + if (links != null) { + final Link[] l = links.clearLinks(); + + if (l != null) { + final int len = l.length; + + for (int i = 0; i < len; i++) { + // send exit "from" remote pids to local ones + self.deliver(new OtpMsg(OtpMsg.exitTag, l[i].remote(), l[i] + .local(), new OtpErlangAtom("noconnection"))); + } + } + } } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpEpmd.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpEpmd.java index 8a8ba785d9..6c7c8fe951 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpEpmd.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpEpmd.java @@ -1,19 +1,19 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2013. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; @@ -21,20 +21,19 @@ package com.ericsson.otp.erlang; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.net.InetAddress; -import java.net.Socket; /** * Provides methods for registering, unregistering and looking up nodes with the * Erlang portmapper daemon (Epmd). For each registered node, Epmd maintains * information about the port on which incoming connections are accepted, as - * well as which versions of the Erlang communication protocolt the node + * well as which versions of the Erlang communication protocol the node * supports. - * + * * <p> * Nodes wishing to contact other nodes must first request information from Epmd * before a connection can be set up, however this is done automatically by * {@link OtpSelf#connect(OtpPeer) OtpSelf.connect()} when necessary. - * + * * <p> * The methods {@link #publishPort(OtpLocalNode) publishPort()} and * {@link #unPublishPort(OtpLocalNode) unPublishPort()} will fail if an Epmd @@ -42,32 +41,33 @@ import java.net.Socket; * {@link #lookupPort(AbstractNode) lookupPort()} will fail if there is no Epmd * process running on the host where the specified node is running. See the * Erlang documentation for information about starting Epmd. - * + * * <p> * This class contains only static methods, there are no constructors. */ public class OtpEpmd { private static class EpmdPort { - private static int epmdPort = 0; - - public static int get() { - if (epmdPort == 0) { - String env; - try { - env = System.getenv("ERL_EPMD_PORT"); - } - catch (java.lang.SecurityException e) { - env = null; - } - epmdPort = (env != null) ? Integer.parseInt(env) : 4369; - } - return epmdPort; - } - public static void set(int port) { - epmdPort = port; - } + private static int epmdPort = 0; + + public static int get() { + if (epmdPort == 0) { + String env; + try { + env = System.getenv("ERL_EPMD_PORT"); + } catch (final java.lang.SecurityException e) { + env = null; + } + epmdPort = env != null ? Integer.parseInt(env) : 4369; + } + return epmdPort; + } + + public static void set(final int port) { + epmdPort = port; + } } + // common values private static final byte stopReq = (byte) 115; @@ -81,16 +81,16 @@ public class OtpEpmd { private static final int traceThreshold = 4; static { - // debug this connection? - final String trace = System.getProperties().getProperty( - "OtpConnection.trace"); - try { - if (trace != null) { - traceLevel = Integer.valueOf(trace).intValue(); - } - } catch (final NumberFormatException e) { - traceLevel = 0; - } + // debug this connection? + final String trace = System.getProperties().getProperty( + "OtpConnection.trace"); + try { + if (trace != null) { + traceLevel = Integer.valueOf(trace).intValue(); + } + } catch (final NumberFormatException e) { + traceLevel = 0; + } } // only static methods: no public constructors @@ -98,51 +98,50 @@ public class OtpEpmd { private OtpEpmd() { } - /** - * Set the port number to be used to contact the epmd process. - * Only needed when the default port is not desired and system environment - * variable ERL_EPMD_PORT can not be read (applet). + * Set the port number to be used to contact the epmd process. Only needed + * when the default port is not desired and system environment variable + * ERL_EPMD_PORT can not be read (applet). */ - public static void useEpmdPort(int port) { - EpmdPort.set(port); + public static void useEpmdPort(final int port) { + EpmdPort.set(port); } /** * Determine what port a node listens for incoming connections on. - * + * * @return the listen port for the specified node, or 0 if the node was not * registered with Epmd. - * + * * @exception java.io.IOException * if there was no response from the name server. */ public static int lookupPort(final AbstractNode node) throws IOException { - return r4_lookupPort(node); + return r4_lookupPort(node); } /** * Register with Epmd, so that other nodes are able to find and connect to * it. - * + * * @param node * the server node that should be registered with Epmd. - * + * * @return true if the operation was successful. False if the node was * already registered. - * + * * @exception java.io.IOException * if there was no response from the name server. */ public static boolean publishPort(final OtpLocalNode node) - throws IOException { - Socket s = null; + throws IOException { + OtpTransport s = null; - s = r4_publish(node); + s = r4_publish(node); - node.setEpmd(s); + node.setEpmd(s); - return s != null; + return s != null; } // Ask epmd to close his end of the connection. @@ -151,275 +150,285 @@ public class OtpEpmd { /** * Unregister from Epmd. Other nodes wishing to connect will no longer be * able to. - * + * * <p> * This method does not report any failures. */ public static void unPublishPort(final OtpLocalNode node) { - Socket s = null; - - try { - s = new Socket((String) null, EpmdPort.get()); - @SuppressWarnings("resource") - final OtpOutputStream obuf = new OtpOutputStream(); - obuf.write2BE(node.alive().length() + 1); - obuf.write1(stopReq); - obuf.writeN(node.alive().getBytes()); - obuf.writeTo(s.getOutputStream()); - // don't even wait for a response (is there one?) - if (traceLevel >= traceThreshold) { - System.out.println("-> UNPUBLISH " + node + " port=" - + node.port()); - System.out.println("<- OK (assumed)"); - } - } catch (final Exception e) {/* ignore all failures */ - } finally { - try { - if (s != null) { - s.close(); - } - } catch (final IOException e) { /* ignore close failure */ - } - s = null; - } + OtpTransport s = null; + + try { + s = node.createTransport((String) null, EpmdPort.get()); + @SuppressWarnings("resource") + final OtpOutputStream obuf = new OtpOutputStream(); + obuf.write2BE(node.alive().length() + 1); + obuf.write1(stopReq); + obuf.writeN(node.alive().getBytes()); + obuf.writeToAndFlush(s.getOutputStream()); + // don't even wait for a response (is there one?) + if (traceLevel >= traceThreshold) { + System.out.println("-> UNPUBLISH " + node + " port=" + + node.port()); + System.out.println("<- OK (assumed)"); + } + } catch (final Exception e) {/* ignore all failures */ + } finally { + try { + if (s != null) { + s.close(); + } + } catch (final IOException e) { /* ignore close failure */ + } + s = null; + } } private static int r4_lookupPort(final AbstractNode node) - throws IOException { - int port = 0; - Socket s = null; - - try { - @SuppressWarnings("resource") - final OtpOutputStream obuf = new OtpOutputStream(); - s = new Socket(node.host(), EpmdPort.get()); - - // build and send epmd request - // length[2], tag[1], alivename[n] (length = n+1) - obuf.write2BE(node.alive().length() + 1); - obuf.write1(port4req); - obuf.writeN(node.alive().getBytes()); - - // send request - obuf.writeTo(s.getOutputStream()); - - if (traceLevel >= traceThreshold) { - System.out.println("-> LOOKUP (r4) " + node); - } - - // receive and decode reply - // resptag[1], result[1], port[2], ntype[1], proto[1], - // disthigh[2], distlow[2], nlen[2], alivename[n], - // elen[2], edata[m] - final byte[] tmpbuf = new byte[100]; - - final int n = s.getInputStream().read(tmpbuf); - - if (n < 0) { - s.close(); - throw new IOException("Nameserver not responding on " - + node.host() + " when looking up " + node.alive()); - } - - @SuppressWarnings("resource") - final OtpInputStream ibuf = new OtpInputStream(tmpbuf, 0); - - final int response = ibuf.read1(); - if (response == port4resp) { - final int result = ibuf.read1(); - if (result == 0) { - port = ibuf.read2BE(); - - node.ntype = ibuf.read1(); - node.proto = ibuf.read1(); - node.distHigh = ibuf.read2BE(); - node.distLow = ibuf.read2BE(); - // ignore rest of fields - } - } - } catch (final IOException e) { - if (traceLevel >= traceThreshold) { - System.out.println("<- (no response)"); - } - throw new IOException("Nameserver not responding on " + node.host() - + " when looking up " + node.alive()); - } catch (final OtpErlangDecodeException e) { - if (traceLevel >= traceThreshold) { - System.out.println("<- (invalid response)"); - } - throw new IOException("Nameserver not responding on " + node.host() - + " when looking up " + node.alive()); - } finally { - try { - if (s != null) { - s.close(); - } - } catch (final IOException e) { /* ignore close errors */ - } - s = null; - } - - if (traceLevel >= traceThreshold) { - if (port == 0) { - System.out.println("<- NOT FOUND"); - } else { - System.out.println("<- PORT " + port); - } - } - return port; + throws IOException { + int port = 0; + OtpTransport s = null; + + try { + @SuppressWarnings("resource") + final OtpOutputStream obuf = new OtpOutputStream(); + s = node.createTransport(node.host(), EpmdPort.get()); + + // build and send epmd request + // length[2], tag[1], alivename[n] (length = n+1) + obuf.write2BE(node.alive().length() + 1); + obuf.write1(port4req); + obuf.writeN(node.alive().getBytes()); + + // send request + obuf.writeToAndFlush(s.getOutputStream()); + + if (traceLevel >= traceThreshold) { + System.out.println("-> LOOKUP (r4) " + node); + } + + // receive and decode reply + // resptag[1], result[1], port[2], ntype[1], proto[1], + // disthigh[2], distlow[2], nlen[2], alivename[n], + // elen[2], edata[m] + final byte[] tmpbuf = new byte[100]; + + final int n = s.getInputStream().read(tmpbuf); + + if (n < 0) { + s.close(); + throw new IOException("Nameserver not responding on " + + node.host() + " when looking up " + node.alive()); + } + + @SuppressWarnings("resource") + final OtpInputStream ibuf = new OtpInputStream(tmpbuf, 0); + + final int response = ibuf.read1(); + if (response == port4resp) { + final int result = ibuf.read1(); + if (result == 0) { + port = ibuf.read2BE(); + + node.ntype = ibuf.read1(); + node.proto = ibuf.read1(); + node.distHigh = ibuf.read2BE(); + node.distLow = ibuf.read2BE(); + // ignore rest of fields + } + } + } catch (final IOException e) { + if (traceLevel >= traceThreshold) { + System.out.println("<- (no response)"); + } + throw new IOException("Nameserver not responding on " + node.host() + + " when looking up " + node.alive(), e); + } catch (final OtpErlangDecodeException e) { + if (traceLevel >= traceThreshold) { + System.out.println("<- (invalid response)"); + } + throw new IOException("Nameserver not responding on " + node.host() + + " when looking up " + node.alive()); + } finally { + try { + if (s != null) { + s.close(); + } + } catch (final IOException e) { /* ignore close errors */ + } + s = null; + } + + if (traceLevel >= traceThreshold) { + if (port == 0) { + System.out.println("<- NOT FOUND"); + } else { + System.out.println("<- PORT " + port); + } + } + return port; } /* - * this function will get an exception if it tries to talk to a - * very old epmd, or if something else happens that it cannot - * forsee. In both cases we return an exception. We no longer - * support r3, so the exception is fatal. If we manage to - * successfully communicate with an r4 epmd, we return either the - * socket, or null, depending on the result. + * this function will get an exception if it tries to talk to a very old + * epmd, or if something else happens that it cannot forsee. In both cases + * we return an exception. We no longer support r3, so the exception is + * fatal. If we manage to successfully communicate with an r4 epmd, we + * return either the socket, or null, depending on the result. */ - private static Socket r4_publish(final OtpLocalNode node) - throws IOException { - Socket s = null; - - try { - @SuppressWarnings("resource") - final OtpOutputStream obuf = new OtpOutputStream(); - s = new Socket((String) null, EpmdPort.get()); - - obuf.write2BE(node.alive().length() + 13); - - obuf.write1(publish4req); - obuf.write2BE(node.port()); - - obuf.write1(node.type()); - - obuf.write1(node.proto()); - obuf.write2BE(node.distHigh()); - obuf.write2BE(node.distLow()); - - obuf.write2BE(node.alive().length()); - obuf.writeN(node.alive().getBytes()); - obuf.write2BE(0); // No extra - - // send request - obuf.writeTo(s.getOutputStream()); - - if (traceLevel >= traceThreshold) { - System.out.println("-> PUBLISH (r4) " + node + " port=" - + node.port()); - } - - // get reply - final byte[] tmpbuf = new byte[100]; - final int n = s.getInputStream().read(tmpbuf); - - if (n < 0) { - s.close(); - throw new IOException("Nameserver not responding on " - + node.host() + " when publishing " + node.alive()); - } - - @SuppressWarnings("resource") - final OtpInputStream ibuf = new OtpInputStream(tmpbuf, 0); - - final int response = ibuf.read1(); - if (response == publish4resp) { - final int result = ibuf.read1(); - if (result == 0) { - node.creation = ibuf.read2BE(); - if (traceLevel >= traceThreshold) { - System.out.println("<- OK"); - } - return s; // success - } - } - } catch (final IOException e) { - // epmd closed the connection = fail - if (s != null) { - s.close(); - } - if (traceLevel >= traceThreshold) { - System.out.println("<- (no response)"); - } - throw new IOException("Nameserver not responding on " + node.host() - + " when publishing " + node.alive()); - } catch (final OtpErlangDecodeException e) { - s.close(); - if (traceLevel >= traceThreshold) { - System.out.println("<- (invalid response)"); - } - throw new IOException("Nameserver not responding on " + node.host() - + " when publishing " + node.alive()); - } - - s.close(); - return null; + private static OtpTransport r4_publish(final OtpLocalNode node) + throws IOException { + OtpTransport s = null; + + try { + @SuppressWarnings("resource") + final OtpOutputStream obuf = new OtpOutputStream(); + s = node.createTransport((String) null, EpmdPort.get()); + + obuf.write2BE(node.alive().length() + 13); + + obuf.write1(publish4req); + obuf.write2BE(node.port()); + + obuf.write1(node.type()); + + obuf.write1(node.proto()); + obuf.write2BE(node.distHigh()); + obuf.write2BE(node.distLow()); + + obuf.write2BE(node.alive().length()); + obuf.writeN(node.alive().getBytes()); + obuf.write2BE(0); // No extra + + // send request + obuf.writeToAndFlush(s.getOutputStream()); + + if (traceLevel >= traceThreshold) { + System.out.println("-> PUBLISH (r4) " + node + " port=" + + node.port()); + } + + // get reply + final byte[] tmpbuf = new byte[100]; + final int n = s.getInputStream().read(tmpbuf); + + if (n < 0) { + s.close(); + throw new IOException("Nameserver not responding on " + + node.host() + " when publishing " + node.alive()); + } + + @SuppressWarnings("resource") + final OtpInputStream ibuf = new OtpInputStream(tmpbuf, 0); + + final int response = ibuf.read1(); + if (response == publish4resp) { + final int result = ibuf.read1(); + if (result == 0) { + node.creation = ibuf.read2BE(); + if (traceLevel >= traceThreshold) { + System.out.println("<- OK"); + } + return s; // success + } + } + } catch (final IOException e) { + // epmd closed the connection = fail + if (s != null) { + s.close(); + } + if (traceLevel >= traceThreshold) { + System.out.println("<- (no response)"); + } + throw new IOException("Nameserver not responding on " + node.host() + + " when publishing " + node.alive()); + } catch (final OtpErlangDecodeException e) { + s.close(); + if (traceLevel >= traceThreshold) { + System.out.println("<- (invalid response)"); + } + throw new IOException("Nameserver not responding on " + node.host() + + " when publishing " + node.alive()); + } + + s.close(); + return null; } public static String[] lookupNames() throws IOException { - return lookupNames(InetAddress.getByName(null)); + return lookupNames(InetAddress.getByName(null), + new OtpSocketTransportFactory()); + } + + public static String[] lookupNames( + final OtpTransportFactory transportFactory) throws IOException { + return lookupNames(InetAddress.getByName(null), transportFactory); } public static String[] lookupNames(final InetAddress address) - throws IOException { - Socket s = null; - - try { - @SuppressWarnings("resource") - final OtpOutputStream obuf = new OtpOutputStream(); - try { - s = new Socket(address, EpmdPort.get()); - - obuf.write2BE(1); - obuf.write1(names4req); - // send request - obuf.writeTo(s.getOutputStream()); - - if (traceLevel >= traceThreshold) { - System.out.println("-> NAMES (r4) "); - } - - // get reply - final byte[] buffer = new byte[256]; - final ByteArrayOutputStream out = new ByteArrayOutputStream(256); - while (true) { - final int bytesRead = s.getInputStream().read(buffer); - if (bytesRead == -1) { - break; - } - out.write(buffer, 0, bytesRead); - } - final byte[] tmpbuf = out.toByteArray(); - @SuppressWarnings("resource") - final OtpInputStream ibuf = new OtpInputStream(tmpbuf, 0); - ibuf.read4BE(); // read port int - // final int port = ibuf.read4BE(); - // check if port = epmdPort - - final int n = tmpbuf.length; - final byte[] buf = new byte[n - 4]; - System.arraycopy(tmpbuf, 4, buf, 0, n - 4); - final String all = OtpErlangString.newString(buf); - return all.split("\n"); - } finally { - if (s != null) { - s.close(); - } - } - - } catch (final IOException e) { - if (traceLevel >= traceThreshold) { - System.out.println("<- (no response)"); - } - throw new IOException( - "Nameserver not responding when requesting names"); - } catch (final OtpErlangDecodeException e) { - if (traceLevel >= traceThreshold) { - System.out.println("<- (invalid response)"); - } - throw new IOException( - "Nameserver not responding when requesting names"); - } + throws IOException { + return lookupNames(address, new OtpSocketTransportFactory()); + } + + public static String[] lookupNames(final InetAddress address, + final OtpTransportFactory transportFactory) throws IOException { + OtpTransport s = null; + + try { + @SuppressWarnings("resource") + final OtpOutputStream obuf = new OtpOutputStream(); + try { + s = transportFactory.createTransport(address, EpmdPort.get()); + + obuf.write2BE(1); + obuf.write1(names4req); + // send request + obuf.writeToAndFlush(s.getOutputStream()); + + if (traceLevel >= traceThreshold) { + System.out.println("-> NAMES (r4) "); + } + + // get reply + final byte[] buffer = new byte[256]; + final ByteArrayOutputStream out = new ByteArrayOutputStream(256); + while (true) { + final int bytesRead = s.getInputStream().read(buffer); + if (bytesRead == -1) { + break; + } + out.write(buffer, 0, bytesRead); + } + final byte[] tmpbuf = out.toByteArray(); + @SuppressWarnings("resource") + final OtpInputStream ibuf = new OtpInputStream(tmpbuf, 0); + ibuf.read4BE(); // read port int + // final int port = ibuf.read4BE(); + // check if port = epmdPort + + final int n = tmpbuf.length; + final byte[] buf = new byte[n - 4]; + System.arraycopy(tmpbuf, 4, buf, 0, n - 4); + final String all = OtpErlangString.newString(buf); + return all.split("\n"); + } finally { + if (s != null) { + s.close(); + } + } + + } catch (final IOException e) { + if (traceLevel >= traceThreshold) { + System.out.println("<- (no response)"); + } + throw new IOException( + "Nameserver not responding when requesting names"); + } catch (final OtpErlangDecodeException e) { + if (traceLevel >= traceThreshold) { + System.out.println("<- (invalid response)"); + } + throw new IOException( + "Nameserver not responding when requesting names"); + } } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangAtom.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangAtom.java index bff3e2c0e3..5b2a2baad5 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangAtom.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangAtom.java @@ -1,24 +1,23 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2013. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; - /** * Provides a Java representation of Erlang atoms. Atoms can be created from * strings whose length is not more than {@link #maxAtomLength maxAtomLength} @@ -35,72 +34,71 @@ public class OtpErlangAtom extends OtpErlangObject { /** * Create an atom from the given string. - * + * * @param atom - * the string to create the atom from. - * + * the string to create the atom from. + * * @exception java.lang.IllegalArgumentException - * if the string is null or contains more than - * {@link #maxAtomLength maxAtomLength} characters. + * if the string is null or contains more than + * {@link #maxAtomLength maxAtomLength} characters. */ public OtpErlangAtom(final String atom) { - if (atom == null) { - throw new java.lang.IllegalArgumentException( - "null string value"); - } - - if (atom.codePointCount(0, atom.length()) > maxAtomLength) { - throw new java.lang.IllegalArgumentException("Atom may not exceed " - + maxAtomLength + " characters: " + atom); - } - this.atom = atom; + if (atom == null) { + throw new java.lang.IllegalArgumentException("null string value"); + } + + if (atom.codePointCount(0, atom.length()) > maxAtomLength) { + throw new java.lang.IllegalArgumentException("Atom may not exceed " + + maxAtomLength + " characters: " + atom); + } + this.atom = atom; } /** * Create an atom from a stream containing an atom encoded in Erlang * external format. - * + * * @param buf - * the stream containing the encoded atom. - * + * the stream containing the encoded atom. + * * @exception OtpErlangDecodeException - * if the buffer does not contain a valid external - * representation of an Erlang atom. + * if the buffer does not contain a valid external + * representation of an Erlang atom. */ public OtpErlangAtom(final OtpInputStream buf) - throws OtpErlangDecodeException { - atom = buf.read_atom(); + throws OtpErlangDecodeException { + atom = buf.read_atom(); } /** * Create an atom whose value is "true" or "false". */ public OtpErlangAtom(final boolean t) { - atom = String.valueOf(t); + atom = String.valueOf(t); } /** * Get the actual string contained in this object. - * + * * @return the raw string contained in this object, without regard to Erlang * quoting rules. - * + * * @see #toString */ public String atomValue() { - return atom; + return atom; } /** * The boolean value of this atom. - * + * * @return the value of this atom expressed as a boolean value. If the atom * consists of the characters "true" (independent of case) the value * will be true. For any other values, the value will be false. - * + * */ public boolean booleanValue() { - return Boolean.valueOf(atomValue()).booleanValue(); + return Boolean.valueOf(atomValue()).booleanValue(); } /** @@ -108,92 +106,91 @@ public class OtpErlangAtom extends OtpErlangObject { * between this method and {link #atomValue atomValue()} is that the * printname is quoted and escaped where necessary, according to the Erlang * rules for atom naming. - * + * * @return the printname representation of this atom object. - * + * * @see #atomValue */ @Override public String toString() { - if (atomNeedsQuoting(atom)) { - return "'" + escapeSpecialChars(atom) + "'"; - } - return atom; + if (atomNeedsQuoting(atom)) { + return "'" + escapeSpecialChars(atom) + "'"; + } + return atom; } /** * Determine if two atoms are equal. - * + * * @param o - * the other object to compare to. - * + * the other object to compare to. + * * @return true if the atoms are equal, false otherwise. */ @Override public boolean equals(final Object o) { - if (!(o instanceof OtpErlangAtom)) { - return false; - } + if (!(o instanceof OtpErlangAtom)) { + return false; + } - final OtpErlangAtom other = (OtpErlangAtom) o; - return this.atom.compareTo(other.atom) == 0; + final OtpErlangAtom other = (OtpErlangAtom) o; + return atom.compareTo(other.atom) == 0; } - + @Override protected int doHashCode() { - return atom.hashCode(); + return atom.hashCode(); } /** * Convert this atom to the equivalent Erlang external representation. - * + * * @param buf - * an output stream to which the encoded atom should be - * written. + * an output stream to which the encoded atom should be written. */ @Override public void encode(final OtpOutputStream buf) { - buf.write_atom(atom); + buf.write_atom(atom); } /* the following four predicates are helpers for the toString() method */ private boolean isErlangDigit(final char c) { - return c >= '0' && c <= '9'; + return c >= '0' && c <= '9'; } private boolean isErlangUpper(final char c) { - return c >= 'A' && c <= 'Z' || c == '_'; + return c >= 'A' && c <= 'Z' || c == '_'; } private boolean isErlangLower(final char c) { - return c >= 'a' && c <= 'z'; + return c >= 'a' && c <= 'z'; } private boolean isErlangLetter(final char c) { - return isErlangLower(c) || isErlangUpper(c); + return isErlangLower(c) || isErlangUpper(c); } // true if the atom should be displayed with quotation marks private boolean atomNeedsQuoting(final String s) { - char c; - - if (s.length() == 0) { - return true; - } - if (!isErlangLower(s.charAt(0))) { - return true; - } - - final int len = s.length(); - for (int i = 1; i < len; i++) { - c = s.charAt(i); - - if (!isErlangLetter(c) && !isErlangDigit(c) && c != '@') { - return true; - } - } - return false; + char c; + + if (s.length() == 0) { + return true; + } + if (!isErlangLower(s.charAt(0))) { + return true; + } + + final int len = s.length(); + for (int i = 1; i < len; i++) { + c = s.charAt(i); + + if (!isErlangLetter(c) && !isErlangDigit(c) && c != '@') { + return true; + } + } + return false; } /* @@ -202,80 +199,80 @@ public class OtpErlangAtom extends OtpErlangObject { * printable. */ private String escapeSpecialChars(final String s) { - char c; - final StringBuffer so = new StringBuffer(); - - final int len = s.length(); - for (int i = 0; i < len; i++) { - c = s.charAt(i); - - /* - * note that some of these escape sequences are unique to Erlang, - * which is why the corresponding 'case' values use octal. The - * resulting string is, of course, in Erlang format. - */ - - switch (c) { - // some special escape sequences - case '\b': - so.append("\\b"); - break; - - case 0177: - so.append("\\d"); - break; - - case 033: - so.append("\\e"); - break; - - case '\f': - so.append("\\f"); - break; - - case '\n': - so.append("\\n"); - break; - - case '\r': - so.append("\\r"); - break; - - case '\t': - so.append("\\t"); - break; - - case 013: - so.append("\\v"); - break; - - case '\\': - so.append("\\\\"); - break; - - case '\'': - so.append("\\'"); - break; - - case '\"': - so.append("\\\""); - break; - - default: - // some other character classes - if (c < 027) { - // control chars show as "\^@", "\^A" etc - so.append("\\^" + (char) ('A' - 1 + c)); - } else if (c > 126) { - // 8-bit chars show as \345 \344 \366 etc - so.append("\\" + Integer.toOctalString(c)); - } else { - // character is printable without modification! - so.append(c); - } - } - } - return new String(so); + char c; + final StringBuffer so = new StringBuffer(); + + final int len = s.length(); + for (int i = 0; i < len; i++) { + c = s.charAt(i); + + /* + * note that some of these escape sequences are unique to Erlang, + * which is why the corresponding 'case' values use octal. The + * resulting string is, of course, in Erlang format. + */ + + switch (c) { + // some special escape sequences + case '\b': + so.append("\\b"); + break; + + case 0177: + so.append("\\d"); + break; + + case 033: + so.append("\\e"); + break; + + case '\f': + so.append("\\f"); + break; + + case '\n': + so.append("\\n"); + break; + + case '\r': + so.append("\\r"); + break; + + case '\t': + so.append("\\t"); + break; + + case 013: + so.append("\\v"); + break; + + case '\\': + so.append("\\\\"); + break; + + case '\'': + so.append("\\'"); + break; + + case '\"': + so.append("\\\""); + break; + + default: + // some other character classes + if (c < 027) { + // control chars show as "\^@", "\^A" etc + so.append("\\^" + (char) ('A' - 1 + c)); + } else if (c > 126) { + // 8-bit chars show as \345 \344 \366 etc + so.append("\\" + Integer.toOctalString(c)); + } else { + // character is printable without modification! + so.append(c); + } + } + } + return new String(so); } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangBinary.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangBinary.java index 0891781f8d..c86a7bb05b 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangBinary.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangBinary.java @@ -1,24 +1,23 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; - /** * Provides a Java representation of Erlang binaries. Anything that can be * represented as a sequence of bytes can be made into an Erlang binary. @@ -29,58 +28,58 @@ public class OtpErlangBinary extends OtpErlangBitstr { /** * Create a binary from a byte array - * + * * @param bin - * the array of bytes from which to create the binary. + * the array of bytes from which to create the binary. */ public OtpErlangBinary(final byte[] bin) { - super(bin); + super(bin); } /** * Create a binary from a stream containing a binary encoded in Erlang * external format. - * + * * @param buf - * the stream containing the encoded binary. - * + * the stream containing the encoded binary. + * * @exception OtpErlangDecodeException - * if the buffer does not contain a valid external - * representation of an Erlang binary. + * if the buffer does not contain a valid external + * representation of an Erlang binary. */ public OtpErlangBinary(final OtpInputStream buf) - throws OtpErlangDecodeException { - super(new byte[0]); - bin = buf.read_binary(); - pad_bits = 0; + throws OtpErlangDecodeException { + super(new byte[0]); + bin = buf.read_binary(); + pad_bits = 0; } /** * Create a binary from an arbitrary Java Object. The object must implement * java.io.Serializable or java.io.Externalizable. - * + * * @param o - * the object to serialize and create this binary from. + * the object to serialize and create this binary from. */ public OtpErlangBinary(final Object o) { - super(o); + super(o); } /** * Convert this binary to the equivalent Erlang external representation. - * + * * @param buf - * an output stream to which the encoded binary should be - * written. + * an output stream to which the encoded binary should be + * written. */ @Override public void encode(final OtpOutputStream buf) { - buf.write_binary(bin); + buf.write_binary(bin); } @Override public Object clone() { - final OtpErlangBinary that = (OtpErlangBinary) super.clone(); - return that; + final OtpErlangBinary that = (OtpErlangBinary) super.clone(); + return that; } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangBitstr.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangBitstr.java index 8cb4e0e685..7724892bd3 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangBitstr.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangBitstr.java @@ -1,19 +1,19 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2007-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; @@ -35,249 +35,249 @@ public class OtpErlangBitstr extends OtpErlangObject { /** * Create a bitstr from a byte array - * + * * @param bin - * the array of bytes from which to create the bitstr. + * the array of bytes from which to create the bitstr. */ public OtpErlangBitstr(final byte[] bin) { - this.bin = new byte[bin.length]; - System.arraycopy(bin, 0, this.bin, 0, bin.length); - pad_bits = 0; + this.bin = new byte[bin.length]; + System.arraycopy(bin, 0, this.bin, 0, bin.length); + pad_bits = 0; } /** * Create a bitstr with pad bits from a byte array. - * + * * @param bin - * the array of bytes from which to create the bitstr. + * the array of bytes from which to create the bitstr. * @param pad_bits - * the number of unused bits in the low end of the last byte. + * the number of unused bits in the low end of the last byte. */ public OtpErlangBitstr(final byte[] bin, final int pad_bits) { - this.bin = new byte[bin.length]; - System.arraycopy(bin, 0, this.bin, 0, bin.length); - this.pad_bits = pad_bits; + this.bin = new byte[bin.length]; + System.arraycopy(bin, 0, this.bin, 0, bin.length); + this.pad_bits = pad_bits; - check_bitstr(this.bin, this.pad_bits); + check_bitstr(this.bin, this.pad_bits); } private void check_bitstr(final byte[] abin, final int a_pad_bits) { - if (a_pad_bits < 0 || 7 < a_pad_bits) { - throw new java.lang.IllegalArgumentException( - "Padding must be in range 0..7"); - } - if (a_pad_bits != 0 && abin.length == 0) { - throw new java.lang.IllegalArgumentException( - "Padding on zero length bitstr"); - } - if (abin.length != 0) { - // Make sure padding is zero - abin[abin.length - 1] &= ~((1 << a_pad_bits) - 1); - } + if (a_pad_bits < 0 || 7 < a_pad_bits) { + throw new java.lang.IllegalArgumentException( + "Padding must be in range 0..7"); + } + if (a_pad_bits != 0 && abin.length == 0) { + throw new java.lang.IllegalArgumentException( + "Padding on zero length bitstr"); + } + if (abin.length != 0) { + // Make sure padding is zero + abin[abin.length - 1] &= ~((1 << a_pad_bits) - 1); + } } /** * Create a bitstr from a stream containing a bitstr encoded in Erlang * external format. - * + * * @param buf - * the stream containing the encoded bitstr. - * + * the stream containing the encoded bitstr. + * * @exception OtpErlangDecodeException - * if the buffer does not contain a valid external - * representation of an Erlang bitstr. + * if the buffer does not contain a valid external + * representation of an Erlang bitstr. */ public OtpErlangBitstr(final OtpInputStream buf) - throws OtpErlangDecodeException { - final int pbs[] = { 0 }; // This is ugly just to get a value-result - // parameter - bin = buf.read_bitstr(pbs); - pad_bits = pbs[0]; + throws OtpErlangDecodeException { + final int pbs[] = { 0 }; // This is ugly just to get a value-result + // parameter + bin = buf.read_bitstr(pbs); + pad_bits = pbs[0]; - check_bitstr(bin, pad_bits); + check_bitstr(bin, pad_bits); } /** * Create a bitstr from an arbitrary Java Object. The object must implement * java.io.Serializable or java.io.Externalizable. - * + * * @param o - * the object to serialize and create this bitstr from. + * the object to serialize and create this bitstr from. */ public OtpErlangBitstr(final Object o) { - try { - bin = toByteArray(o); - pad_bits = 0; - } catch (final IOException e) { - throw new java.lang.IllegalArgumentException( - "Object must implement Serializable"); - } + try { + bin = toByteArray(o); + pad_bits = 0; + } catch (final IOException e) { + throw new java.lang.IllegalArgumentException( + "Object must implement Serializable"); + } } private static byte[] toByteArray(final Object o) - throws java.io.IOException { + throws java.io.IOException { - if (o == null) { - return null; - } + if (o == null) { + return null; + } - /* need to synchronize use of the shared baos */ - final java.io.ByteArrayOutputStream baos = new ByteArrayOutputStream(); - final java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream( - baos); + /* need to synchronize use of the shared baos */ + final java.io.ByteArrayOutputStream baos = new ByteArrayOutputStream(); + final java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream( + baos); - oos.writeObject(o); - oos.flush(); + oos.writeObject(o); + oos.flush(); - return baos.toByteArray(); + return baos.toByteArray(); } private static Object fromByteArray(final byte[] buf) { - if (buf == null) { - return null; - } + if (buf == null) { + return null; + } - try { - final java.io.ByteArrayInputStream bais = new java.io.ByteArrayInputStream( - buf); - final java.io.ObjectInputStream ois = new java.io.ObjectInputStream( - bais); - return ois.readObject(); - } catch (final java.lang.ClassNotFoundException e) { - } catch (final java.io.IOException e) { - } + try { + final java.io.ByteArrayInputStream bais = new java.io.ByteArrayInputStream( + buf); + final java.io.ObjectInputStream ois = new java.io.ObjectInputStream( + bais); + return ois.readObject(); + } catch (final java.lang.ClassNotFoundException e) { + } catch (final java.io.IOException e) { + } - return null; + return null; } /** * Get the byte array from a bitstr, padded with zero bits in the little end * of the last byte. - * + * * @return the byte array containing the bytes for this bitstr. */ public byte[] binaryValue() { - return bin; + return bin; } /** * Get the size in whole bytes of the bitstr, rest bits in the last byte not * counted. - * + * * @return the number of bytes contained in the bintstr. */ public int size() { - if (pad_bits == 0) { - return bin.length; - } - if (bin.length == 0) { - throw new java.lang.IllegalStateException("Impossible length"); - } - return bin.length - 1; + if (pad_bits == 0) { + return bin.length; + } + if (bin.length == 0) { + throw new java.lang.IllegalStateException("Impossible length"); + } + return bin.length - 1; } /** * Get the number of pad bits in the last byte of the bitstr. The pad bits * are zero and in the little end. - * + * * @return the number of pad bits in the bitstr. */ public int pad_bits() { - return pad_bits; + return pad_bits; } /** * Get the java Object from the bitstr. If the bitstr contains a serialized * Java object, then this method will recreate the object. - * - * + * + * * @return the java Object represented by this bitstr, or null if the bitstr * does not represent a Java Object. */ public Object getObject() { - if (pad_bits != 0) { - return null; - } - return fromByteArray(bin); + if (pad_bits != 0) { + return null; + } + return fromByteArray(bin); } /** * Get the string representation of this bitstr object. A bitstr is printed * as #Bin<N>, where N is the number of bytes contained in the object * or #bin<N-M> if there are M pad bits. - * + * * @return the Erlang string representation of this bitstr. */ @Override public String toString() { - if (pad_bits == 0) { - return "#Bin<" + bin.length + ">"; - } - if (bin.length == 0) { - throw new java.lang.IllegalStateException("Impossible length"); - } - return "#Bin<" + bin.length + "-" + pad_bits + ">"; + if (pad_bits == 0) { + return "#Bin<" + bin.length + ">"; + } + if (bin.length == 0) { + throw new java.lang.IllegalStateException("Impossible length"); + } + return "#Bin<" + bin.length + "-" + pad_bits + ">"; } /** * Convert this bitstr to the equivalent Erlang external representation. - * + * * @param buf - * an output stream to which the encoded bitstr should be - * written. + * an output stream to which the encoded bitstr should be + * written. */ @Override public void encode(final OtpOutputStream buf) { - buf.write_bitstr(bin, pad_bits); + buf.write_bitstr(bin, pad_bits); } /** * Determine if two bitstrs are equal. Bitstrs are equal if they have the * same byte length and tail length, and the array of bytes is identical. - * + * * @param o - * the bitstr to compare to. - * + * the bitstr to compare to. + * * @return true if the bitstrs contain the same bits, false otherwise. */ @Override public boolean equals(final Object o) { - if (!(o instanceof OtpErlangBitstr)) { - return false; - } + if (!(o instanceof OtpErlangBitstr)) { + return false; + } - final OtpErlangBitstr that = (OtpErlangBitstr) o; - if (pad_bits != that.pad_bits) { - return false; - } + final OtpErlangBitstr that = (OtpErlangBitstr) o; + if (pad_bits != that.pad_bits) { + return false; + } - final int len = bin.length; - if (len != that.bin.length) { - return false; - } + final int len = bin.length; + if (len != that.bin.length) { + return false; + } - for (int i = 0; i < len; i++) { - if (bin[i] != that.bin[i]) { - return false; // early exit - } - } + for (int i = 0; i < len; i++) { + if (bin[i] != that.bin[i]) { + return false; // early exit + } + } - return true; + return true; } - + @Override protected int doHashCode() { - OtpErlangObject.Hash hash = new OtpErlangObject.Hash(15); - hash.combine(bin); - hash.combine(pad_bits); - return hash.valueOf(); + final OtpErlangObject.Hash hash = new OtpErlangObject.Hash(15); + hash.combine(bin); + hash.combine(pad_bits); + return hash.valueOf(); } - + @Override public Object clone() { - final OtpErlangBitstr that = (OtpErlangBitstr) super.clone(); - that.bin = bin.clone(); - that.pad_bits = pad_bits; - return that; + final OtpErlangBitstr that = (OtpErlangBitstr) super.clone(); + that.bin = bin.clone(); + that.pad_bits = pad_bits; + return that; } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangBoolean.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangBoolean.java index eecd2ea288..3f15317a94 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangBoolean.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangBoolean.java @@ -1,24 +1,23 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; - /** * Provides a Java representation of Erlang booleans, which are special cases of * atoms with values 'true' and 'false'. @@ -29,12 +28,12 @@ public class OtpErlangBoolean extends OtpErlangAtom { /** * Create a boolean from the given value - * + * * @param t - * the boolean value to represent as an atom. + * the boolean value to represent as an atom. */ public OtpErlangBoolean(final boolean t) { - super(t); + super(t); } /** @@ -42,13 +41,13 @@ public class OtpErlangBoolean extends OtpErlangAtom { * external format. The value of the boolean will be true if the atom * represented by the stream is "true" without regard to case. For other * atom values, the boolean will have the value false. - * + * * @exception OtpErlangDecodeException - * if the buffer does not contain a valid external - * representation of an Erlang atom. + * if the buffer does not contain a valid external + * representation of an Erlang atom. */ public OtpErlangBoolean(final OtpInputStream buf) - throws OtpErlangDecodeException { - super(buf); + throws OtpErlangDecodeException { + super(buf); } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangByte.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangByte.java index eb6f3d8aba..622e31fa3b 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangByte.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangByte.java @@ -1,24 +1,23 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; - /** * Provides a Java representation of Erlang integral types. */ @@ -28,32 +27,32 @@ public class OtpErlangByte extends OtpErlangLong { /** * Create an Erlang integer from the given value. - * + * * @param b - * the byte value to use. + * the byte value to use. */ public OtpErlangByte(final byte b) { - super(b); + super(b); } /** * Create an Erlang integer from a stream containing an integer encoded in * Erlang external format. - * + * * @param buf - * the stream containing the encoded value. - * + * the stream containing the encoded value. + * * @exception OtpErlangDecodeException - * if the buffer does not contain a valid external - * representation of an Erlang integer. - * + * if the buffer does not contain a valid external + * representation of an Erlang integer. + * * @exception OtpErlangRangeException - * if the value is too large to be represented as a byte. + * if the value is too large to be represented as a byte. */ public OtpErlangByte(final OtpInputStream buf) - throws OtpErlangRangeException, OtpErlangDecodeException { - super(buf); + throws OtpErlangRangeException, OtpErlangDecodeException { + super(buf); - byteValue(); + byteValue(); } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangChar.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangChar.java index e7c6dd8ad4..1401716839 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangChar.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangChar.java @@ -1,24 +1,23 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; - /** * Provides a Java representation of Erlang integral types. */ @@ -28,32 +27,32 @@ public class OtpErlangChar extends OtpErlangLong { /** * Create an Erlang integer from the given value. - * + * * @param c - * the char value to use. + * the char value to use. */ public OtpErlangChar(final char c) { - super(c); + super(c); } /** * Create an Erlang integer from a stream containing an integer encoded in * Erlang external format. - * + * * @param buf - * the stream containing the encoded value. - * + * the stream containing the encoded value. + * * @exception OtpErlangDecodeException - * if the buffer does not contain a valid external - * representation of an Erlang integer. - * + * if the buffer does not contain a valid external + * representation of an Erlang integer. + * * @exception OtpErlangRangeException - * if the value is too large to be represented as a char. + * if the value is too large to be represented as a char. */ public OtpErlangChar(final OtpInputStream buf) - throws OtpErlangRangeException, OtpErlangDecodeException { - super(buf); + throws OtpErlangRangeException, OtpErlangDecodeException { + super(buf); - charValue(); + charValue(); } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangDecodeException.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangDecodeException.java index 6986e26908..a7a9e71a08 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangDecodeException.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangDecodeException.java @@ -1,19 +1,19 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; @@ -22,7 +22,7 @@ package com.ericsson.otp.erlang; * Exception raised when an attempt is made to create an Erlang term by decoding * a sequence of bytes that does not represent the type of term that was * requested. - * + * * @see OtpInputStream */ public class OtpErlangDecodeException extends OtpErlangException { @@ -32,6 +32,6 @@ public class OtpErlangDecodeException extends OtpErlangException { * Provides a detailed message. */ public OtpErlangDecodeException(final String msg) { - super(msg); + super(msg); } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangDouble.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangDouble.java index e92ce11431..bf0b7d5c11 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangDouble.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangDouble.java @@ -1,24 +1,23 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; - /** * Provides a Java representation of Erlang floats and doubles. Erlang defines * only one floating point numeric type, however this class and its subclass @@ -35,96 +34,95 @@ public class OtpErlangDouble extends OtpErlangObject { * Create an Erlang float from the given double value. */ public OtpErlangDouble(final double d) { - this.d = d; + this.d = d; } /** * Create an Erlang float from a stream containing a double encoded in * Erlang external format. - * + * * @param buf - * the stream containing the encoded value. - * + * the stream containing the encoded value. + * * @exception OtpErlangDecodeException - * if the buffer does not contain a valid external - * representation of an Erlang float. + * if the buffer does not contain a valid external + * representation of an Erlang float. */ public OtpErlangDouble(final OtpInputStream buf) - throws OtpErlangDecodeException { - d = buf.read_double(); + throws OtpErlangDecodeException { + d = buf.read_double(); } /** * Get the value, as a double. - * + * * @return the value of this object, as a double. */ public double doubleValue() { - return d; + return d; } /** * Get the value, as a float. - * + * * @return the value of this object, as a float. - * + * * @exception OtpErlangRangeException - * if the value cannot be represented as a float. + * if the value cannot be represented as a float. */ public float floatValue() throws OtpErlangRangeException { - final float f = (float) d; + final float f = (float) d; - if (f != d) { - throw new OtpErlangRangeException("Value too large for float: " + d); - } + if (f != d) { + throw new OtpErlangRangeException("Value too large for float: " + d); + } - return f; + return f; } /** * Get the string representation of this double. - * + * * @return the string representation of this double. */ @Override public String toString() { - return "" + d; + return "" + d; } /** * Convert this double to the equivalent Erlang external representation. - * + * * @param buf - * an output stream to which the encoded value should be - * written. + * an output stream to which the encoded value should be written. */ @Override public void encode(final OtpOutputStream buf) { - buf.write_double(d); + buf.write_double(d); } /** * Determine if two floats are equal. Floats are equal if they contain the * same value. - * + * * @param o - * the float to compare to. - * + * the float to compare to. + * * @return true if the floats have the same value. */ @Override public boolean equals(final Object o) { - if (!(o instanceof OtpErlangDouble)) { - return false; - } + if (!(o instanceof OtpErlangDouble)) { + return false; + } - final OtpErlangDouble other = (OtpErlangDouble) o; - return this.d == other.d; + final OtpErlangDouble other = (OtpErlangDouble) o; + return d == other.d; } - + @Override protected int doHashCode() { - Double v = new Double(d); - return v.hashCode(); + final Double v = new Double(d); + return v.hashCode(); } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangException.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangException.java index 5b111a56a8..2e250488fa 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangException.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangException.java @@ -1,19 +1,19 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; @@ -28,13 +28,13 @@ public class OtpErlangException extends OtpException { * Provides no message. */ public OtpErlangException() { - super(); + super(); } /** * Provides a detailed message. */ public OtpErlangException(final String msg) { - super(msg); + super(msg); } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangExit.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangExit.java index 6b9015c0e5..f4c6f21207 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangExit.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangExit.java @@ -1,19 +1,19 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; @@ -21,13 +21,13 @@ package com.ericsson.otp.erlang; /** * Exception raised when a communication channel is broken. This can be caused * for a number of reasons, for example: - * + * * <ul> - * <li> an error in communication has occurred - * <li> a remote process has sent an exit signal - * <li> a linked process has exited + * <li>an error in communication has occurred + * <li>a remote process has sent an exit signal + * <li>a linked process has exited * </ul> - * + * * @see OtpConnection */ @@ -39,13 +39,13 @@ public class OtpErlangExit extends OtpErlangException { /** * Create an OtpErlangExit exception with the given reason. - * + * * @param reason - * the reason this exit signal has been sent. + * the reason this exit signal has been sent. */ public OtpErlangExit(final OtpErlangObject reason) { - super(reason.toString()); - this.reason = reason; + super(reason.toString()); + this.reason = reason; } /** @@ -53,29 +53,29 @@ public class OtpErlangExit extends OtpErlangException { * Equivalent to <code>OtpErlangExit(new * OtpErlangAtom(reason)</code>. * </p> - * + * * @param reason - * the reason this exit signal has been sent. - * + * the reason this exit signal has been sent. + * * @see #OtpErlangExit(OtpErlangObject) */ public OtpErlangExit(final String reason) { - this(new OtpErlangAtom(reason)); + this(new OtpErlangAtom(reason)); } /** * Create an OtpErlangExit exception with the given reason and sender pid. - * + * * @param reason - * the reason this exit signal has been sent. - * + * the reason this exit signal has been sent. + * * @param pid - * the pid that sent this exit. + * the pid that sent this exit. */ public OtpErlangExit(final OtpErlangObject reason, final OtpErlangPid pid) { - super(reason.toString()); - this.reason = reason; - this.pid = pid; + super(reason.toString()); + this.reason = reason; + this.pid = pid; } /** @@ -83,30 +83,30 @@ public class OtpErlangExit extends OtpErlangException { * Equivalent to <code>OtpErlangExit(new OtpErlangAtom(reason), * pid)</code>. * </p> - * + * * @param reason - * the reason this exit signal has been sent. - * + * the reason this exit signal has been sent. + * * @param pid - * the pid that sent this exit. - * + * the pid that sent this exit. + * * @see #OtpErlangExit(OtpErlangObject, OtpErlangPid) */ public OtpErlangExit(final String reason, final OtpErlangPid pid) { - this(new OtpErlangAtom(reason), pid); + this(new OtpErlangAtom(reason), pid); } /** * Get the reason associated with this exit signal. */ public OtpErlangObject reason() { - return reason; + return reason; } /** * Get the pid that sent this exit. */ public OtpErlangPid pid() { - return pid; + return pid; } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangExternalFun.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangExternalFun.java index 09f36b1ff4..80751cae53 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangExternalFun.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangExternalFun.java @@ -1,20 +1,20 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * - * %CopyrightEnd% + * + * %CopyrightEnd% */ package com.ericsson.otp.erlang; @@ -27,47 +27,47 @@ public class OtpErlangExternalFun extends OtpErlangObject { private final int arity; public OtpErlangExternalFun(final String module, final String function, - final int arity) { - super(); - this.module = module; - this.function = function; - this.arity = arity; + final int arity) { + super(); + this.module = module; + this.function = function; + this.arity = arity; } public OtpErlangExternalFun(final OtpInputStream buf) - throws OtpErlangDecodeException { - final OtpErlangExternalFun f = buf.read_external_fun(); - module = f.module; - function = f.function; - arity = f.arity; + throws OtpErlangDecodeException { + final OtpErlangExternalFun f = buf.read_external_fun(); + module = f.module; + function = f.function; + arity = f.arity; } @Override public void encode(final OtpOutputStream buf) { - buf.write_external_fun(module, function, arity); + buf.write_external_fun(module, function, arity); } @Override public boolean equals(final Object o) { - if (!(o instanceof OtpErlangExternalFun)) { - return false; - } - final OtpErlangExternalFun f = (OtpErlangExternalFun) o; - return module.equals(f.module) && function.equals(f.function) - && arity == f.arity; + if (!(o instanceof OtpErlangExternalFun)) { + return false; + } + final OtpErlangExternalFun f = (OtpErlangExternalFun) o; + return module.equals(f.module) && function.equals(f.function) + && arity == f.arity; } @Override protected int doHashCode() { - OtpErlangObject.Hash hash = new OtpErlangObject.Hash(14); - hash.combine(module.hashCode(), function.hashCode()); - hash.combine(arity); - return hash.valueOf(); + final OtpErlangObject.Hash hash = new OtpErlangObject.Hash(14); + hash.combine(module.hashCode(), function.hashCode()); + hash.combine(arity); + return hash.valueOf(); } - + @Override public String toString() { - return "#Fun<" + module + "." + function + "." + arity + ">"; + return "#Fun<" + module + "." + function + "." + arity + ">"; } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangFloat.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangFloat.java index 7d48f848f0..6dcf3e7c3a 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangFloat.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangFloat.java @@ -1,24 +1,23 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; - /** * Provides a Java representation of Erlang floats and doubles. */ @@ -30,27 +29,27 @@ public class OtpErlangFloat extends OtpErlangDouble { * Create an Erlang float from the given float value. */ public OtpErlangFloat(final float f) { - super(f); + super(f); } /** * Create an Erlang float from a stream containing a float encoded in Erlang * external format. - * + * * @param buf - * the stream containing the encoded value. - * + * the stream containing the encoded value. + * * @exception OtpErlangDecodeException - * if the buffer does not contain a valid external - * representation of an Erlang float. - * + * if the buffer does not contain a valid external + * representation of an Erlang float. + * * @exception OtpErlangRangeException - * if the value cannot be represented as a Java float. + * if the value cannot be represented as a Java float. */ public OtpErlangFloat(final OtpInputStream buf) - throws OtpErlangDecodeException, OtpErlangRangeException { - super(buf); + throws OtpErlangDecodeException, OtpErlangRangeException { + super(buf); - floatValue(); + floatValue(); } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangFun.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangFun.java index 05fa0cbb23..2de284029b 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangFun.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangFun.java @@ -1,20 +1,20 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * - * %CopyrightEnd% + * + * %CopyrightEnd% */ package com.ericsson.otp.erlang; @@ -34,97 +34,97 @@ public class OtpErlangFun extends OtpErlangObject { private final byte[] md5; public OtpErlangFun(final OtpInputStream buf) - throws OtpErlangDecodeException { - final OtpErlangFun f = buf.read_fun(); - pid = f.pid; - module = f.module; - arity = f.arity; - md5 = f.md5; - index = f.index; - old_index = f.old_index; - uniq = f.uniq; - freeVars = f.freeVars; + throws OtpErlangDecodeException { + final OtpErlangFun f = buf.read_fun(); + pid = f.pid; + module = f.module; + arity = f.arity; + md5 = f.md5; + index = f.index; + old_index = f.old_index; + uniq = f.uniq; + freeVars = f.freeVars; } public OtpErlangFun(final OtpErlangPid pid, final String module, - final long index, final long uniq, final OtpErlangObject[] freeVars) { - this.pid = pid; - this.module = module; - arity = -1; - md5 = null; - this.index = index; - old_index = 0; - this.uniq = uniq; - this.freeVars = freeVars; + final long index, final long uniq, final OtpErlangObject[] freeVars) { + this.pid = pid; + this.module = module; + arity = -1; + md5 = null; + this.index = index; + old_index = 0; + this.uniq = uniq; + this.freeVars = freeVars; } public OtpErlangFun(final OtpErlangPid pid, final String module, - final int arity, final byte[] md5, final int index, - final long old_index, final long uniq, - final OtpErlangObject[] freeVars) { - this.pid = pid; - this.module = module; - this.arity = arity; - this.md5 = md5; - this.index = index; - this.old_index = old_index; - this.uniq = uniq; - this.freeVars = freeVars; + final int arity, final byte[] md5, final int index, + final long old_index, final long uniq, + final OtpErlangObject[] freeVars) { + this.pid = pid; + this.module = module; + this.arity = arity; + this.md5 = md5; + this.index = index; + this.old_index = old_index; + this.uniq = uniq; + this.freeVars = freeVars; } @Override public void encode(final OtpOutputStream buf) { - buf - .write_fun(pid, module, old_index, arity, md5, index, uniq, - freeVars); + buf.write_fun(pid, module, old_index, arity, md5, index, uniq, freeVars); } @Override public boolean equals(final Object o) { - if (!(o instanceof OtpErlangFun)) { - return false; - } - final OtpErlangFun f = (OtpErlangFun) o; - if (!pid.equals(f.pid) || !module.equals(f.module) || arity != f.arity) { - return false; - } - if (md5 == null) { - if (f.md5 != null) { - return false; - } - } else { - if (!Arrays.equals(md5, f.md5)) { - return false; - } - } - if (index != f.index || uniq != f.uniq) { - return false; - } - if (freeVars == null) { - return f.freeVars == null; - } - return Arrays.equals(freeVars, f.freeVars); + if (!(o instanceof OtpErlangFun)) { + return false; + } + final OtpErlangFun f = (OtpErlangFun) o; + if (!pid.equals(f.pid) || !module.equals(f.module) || arity != f.arity) { + return false; + } + if (md5 == null) { + if (f.md5 != null) { + return false; + } + } else { + if (!Arrays.equals(md5, f.md5)) { + return false; + } + } + if (index != f.index || uniq != f.uniq) { + return false; + } + if (freeVars == null) { + return f.freeVars == null; + } + return Arrays.equals(freeVars, f.freeVars); } - + @Override protected int doHashCode() { - OtpErlangObject.Hash hash = new OtpErlangObject.Hash(1); - hash.combine(pid.hashCode(), module.hashCode()); - hash.combine(arity); - if (md5 != null) hash.combine(md5); - hash.combine(index); - hash.combine(uniq); - if (freeVars != null) { - for (OtpErlangObject o: freeVars) { - hash.combine(o.hashCode(), 1); - } - } - return hash.valueOf(); + final OtpErlangObject.Hash hash = new OtpErlangObject.Hash(1); + hash.combine(pid.hashCode(), module.hashCode()); + hash.combine(arity); + if (md5 != null) { + hash.combine(md5); + } + hash.combine(index); + hash.combine(uniq); + if (freeVars != null) { + for (final OtpErlangObject o : freeVars) { + hash.combine(o.hashCode(), 1); + } + } + return hash.valueOf(); } - + @Override public String toString() { - return "#Fun<" + module + "." + old_index + "." + uniq + ">"; + return "#Fun<" + module + "." + old_index + "." + uniq + ">"; } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangInt.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangInt.java index 741fc29dd0..628e3f6e6e 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangInt.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangInt.java @@ -1,24 +1,23 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; - /** * Provides a Java representation of Erlang integral types. */ @@ -28,32 +27,32 @@ public class OtpErlangInt extends OtpErlangLong { /** * Create an Erlang integer from the given value. - * + * * @param i - * the int value to use. + * the int value to use. */ public OtpErlangInt(final int i) { - super(i); + super(i); } /** * Create an Erlang integer from a stream containing an integer encoded in * Erlang external format. - * + * * @param buf - * the stream containing the encoded value. - * + * the stream containing the encoded value. + * * @exception OtpErlangDecodeException - * if the buffer does not contain a valid external - * representation of an Erlang integer. - * + * if the buffer does not contain a valid external + * representation of an Erlang integer. + * * @exception OtpErlangRangeException - * if the value is too large to be represented as an int. + * if the value is too large to be represented as an int. */ public OtpErlangInt(final OtpInputStream buf) - throws OtpErlangRangeException, OtpErlangDecodeException { - super(buf); + throws OtpErlangRangeException, OtpErlangDecodeException { + super(buf); - intValue(); + intValue(); } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangList.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangList.java index 9f7c5f5499..268261ec10 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangList.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangList.java @@ -1,19 +1,19 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; @@ -24,12 +24,12 @@ import java.util.NoSuchElementException; /** * Provides a Java representation of Erlang lists. Lists are created from zero * or more arbitrary Erlang terms. - * + * * <p> * The arity of the list is the number of elements it contains. */ public class OtpErlangList extends OtpErlangObject implements - Iterable<OtpErlangObject> { + Iterable<OtpErlangObject> { // don't change this! private static final long serialVersionUID = 5999112769036676548L; @@ -43,69 +43,69 @@ public class OtpErlangList extends OtpErlangObject implements * Create an empty list. */ public OtpErlangList() { - elems = NO_ELEMENTS; + elems = NO_ELEMENTS; } /** - * Create a list of Erlang integers representing Unicode codePoints. - * This method does not check if the string contains valid code points. - * + * Create a list of Erlang integers representing Unicode codePoints. This + * method does not check if the string contains valid code points. + * * @param str * the characters from which to create the list. */ public OtpErlangList(final String str) { - if (str == null || str.length() == 0) { - elems = NO_ELEMENTS; - } else { - final int[] codePoints = OtpErlangString.stringToCodePoints(str); - elems = new OtpErlangObject[codePoints.length]; - for (int i = 0; i < elems.length; i++) { - elems[i] = new OtpErlangInt(codePoints[i]); - } - } + if (str == null || str.length() == 0) { + elems = NO_ELEMENTS; + } else { + final int[] codePoints = OtpErlangString.stringToCodePoints(str); + elems = new OtpErlangObject[codePoints.length]; + for (int i = 0; i < elems.length; i++) { + elems[i] = new OtpErlangInt(codePoints[i]); + } + } } /** * Create a list containing one element. - * + * * @param elem * the elememet to make the list from. */ public OtpErlangList(final OtpErlangObject elem) { - elems = new OtpErlangObject[] { elem }; + elems = new OtpErlangObject[] { elem }; } /** * Create a list from an array of arbitrary Erlang terms. - * + * * @param elems * the array of terms from which to create the list. */ public OtpErlangList(final OtpErlangObject[] elems) { - this(elems, 0, elems.length); + this(elems, 0, elems.length); } /** * Create a list from an array of arbitrary Erlang terms. Tail can be * specified, if not null, the list will not be proper. - * + * * @param elems * array of terms from which to create the list * @param lastTail * @throws OtpErlangException */ public OtpErlangList(final OtpErlangObject[] elems, - final OtpErlangObject lastTail) throws OtpErlangException { - this(elems, 0, elems.length); - if (elems.length == 0 && lastTail != null) { - throw new OtpErlangException("Bad list, empty head, non-empty tail"); - } - this.lastTail = lastTail; + final OtpErlangObject lastTail) throws OtpErlangException { + this(elems, 0, elems.length); + if (elems.length == 0 && lastTail != null) { + throw new OtpErlangException("Bad list, empty head, non-empty tail"); + } + this.lastTail = lastTail; } /** * Create a list from an array of arbitrary Erlang terms. - * + * * @param elems * the array of terms from which to create the list. * @param start @@ -114,152 +114,152 @@ public class OtpErlangList extends OtpErlangObject implements * the number of terms to insert. */ public OtpErlangList(final OtpErlangObject[] elems, final int start, - final int count) { - if (elems != null && count > 0) { - this.elems = new OtpErlangObject[count]; - System.arraycopy(elems, start, this.elems, 0, count); - } else { - this.elems = NO_ELEMENTS; - } + final int count) { + if (elems != null && count > 0) { + this.elems = new OtpErlangObject[count]; + System.arraycopy(elems, start, this.elems, 0, count); + } else { + this.elems = NO_ELEMENTS; + } } /** * Create a list from a stream containing an list encoded in Erlang external * format. - * + * * @param buf * the stream containing the encoded list. - * + * * @exception OtpErlangDecodeException * if the buffer does not contain a valid external * representation of an Erlang list. */ public OtpErlangList(final OtpInputStream buf) - throws OtpErlangDecodeException { - final int arity = buf.read_list_head(); - if (arity > 0) { - elems = new OtpErlangObject[arity]; - for (int i = 0; i < arity; i++) { - elems[i] = buf.read_any(); - } - /* discard the terminating nil (empty list) or read tail */ - if (buf.peek1() == OtpExternal.nilTag) { - buf.read_nil(); - } else { - lastTail = buf.read_any(); - } - } else { - elems = NO_ELEMENTS; - } + throws OtpErlangDecodeException { + final int arity = buf.read_list_head(); + if (arity > 0) { + elems = new OtpErlangObject[arity]; + for (int i = 0; i < arity; i++) { + elems[i] = buf.read_any(); + } + /* discard the terminating nil (empty list) or read tail */ + if (buf.peek1() == OtpExternal.nilTag) { + buf.read_nil(); + } else { + lastTail = buf.read_any(); + } + } else { + elems = NO_ELEMENTS; + } } /** * Get the arity of the list. - * + * * @return the number of elements contained in the list. */ public int arity() { - return elems.length; + return elems.length; } /** * Get the specified element from the list. - * + * * @param i * the index of the requested element. List elements are numbered * as array elements, starting at 0. - * + * * @return the requested element, of null if i is not a valid element index. */ public OtpErlangObject elementAt(final int i) { - if (i >= arity() || i < 0) { - return null; - } - return elems[i]; + if (i >= arity() || i < 0) { + return null; + } + return elems[i]; } /** * Get all the elements from the list as an array. - * + * * @return an array containing all of the list's elements. */ public OtpErlangObject[] elements() { - if (arity() == 0) { - return NO_ELEMENTS; + if (arity() == 0) { + return NO_ELEMENTS; + } + final OtpErlangObject[] res = new OtpErlangObject[arity()]; + System.arraycopy(elems, 0, res, 0, res.length); + return res; } - final OtpErlangObject[] res = new OtpErlangObject[arity()]; - System.arraycopy(elems, 0, res, 0, res.length); - return res; - } /** * Get the string representation of the list. - * + * * @return the string representation of the list. */ @Override public String toString() { - return toString(0); + return toString(0); } protected String toString(final int start) { - final StringBuffer s = new StringBuffer(); - s.append("["); - - for (int i = start; i < arity(); i++) { - if (i > start) { - s.append(","); - } - s.append(elems[i].toString()); - } - if (lastTail != null) { - s.append("|").append(lastTail.toString()); - } - s.append("]"); - - return s.toString(); + final StringBuffer s = new StringBuffer(); + s.append("["); + + for (int i = start; i < arity(); i++) { + if (i > start) { + s.append(","); + } + s.append(elems[i].toString()); + } + if (lastTail != null) { + s.append("|").append(lastTail.toString()); + } + s.append("]"); + + return s.toString(); } /** * Convert this list to the equivalent Erlang external representation. Note * that this method never encodes lists as strings, even when it is possible * to do so. - * + * * @param buf * An output stream to which the encoded list should be written. - * + * */ @Override public void encode(final OtpOutputStream buf) { - encode(buf, 0); + encode(buf, 0); } protected void encode(final OtpOutputStream buf, final int start) { - final int arity = arity() - start; - - if (arity > 0) { - buf.write_list_head(arity); - - for (int i = start; i < arity + start; i++) { - buf.write_any(elems[i]); - } - } - if (lastTail == null) { - buf.write_nil(); - } else { - buf.write_any(lastTail); - } + final int arity = arity() - start; + + if (arity > 0) { + buf.write_list_head(arity); + + for (int i = start; i < arity + start; i++) { + buf.write_any(elems[i]); + } + } + if (lastTail == null) { + buf.write_nil(); + } else { + buf.write_any(lastTail); + } } /** * Determine if two lists are equal. Lists are equal if they have the same * arity and all of the elements are equal. - * + * * @param o * the list to compare to. - * + * * @return true if the lists have the same arity and all the elements are * equal. */ @@ -267,236 +267,282 @@ public class OtpErlangList extends OtpErlangObject implements @Override public boolean equals(final Object o) { - /* - * Be careful to use methods even for "this", so that equals work also - * for sublists - */ - - if (!(o instanceof OtpErlangList)) { - return false; - } - - final OtpErlangList l = (OtpErlangList) o; - - final int a = arity(); - if (a != l.arity()) { - return false; - } - for (int i = 0; i < a; i++) { - if (!elementAt(i).equals(l.elementAt(i))) { - return false; // early exit - } - } - final OtpErlangObject otherTail = l.getLastTail(); - if (getLastTail() == null && otherTail == null) { - return true; - } - if (getLastTail() == null) { - return false; - } - return getLastTail().equals(l.getLastTail()); + /* + * Be careful to use methods even for "this", so that equals work also + * for sublists + */ + + if (!(o instanceof OtpErlangList)) { + return false; + } + + final OtpErlangList l = (OtpErlangList) o; + + final int a = arity(); + if (a != l.arity()) { + return false; + } + for (int i = 0; i < a; i++) { + if (!elementAt(i).equals(l.elementAt(i))) { + return false; // early exit + } + } + final OtpErlangObject otherTail = l.getLastTail(); + if (getLastTail() == null && otherTail == null) { + return true; + } + if (getLastTail() == null) { + return false; + } + return getLastTail().equals(l.getLastTail()); + } + + @Override + public <T> boolean match(final OtpErlangObject term, final T bindings) { + if (!(term instanceof OtpErlangList)) { + return false; + } + final OtpErlangList that = (OtpErlangList) term; + + final int thisArity = this.arity(); + final int thatArity = that.arity(); + final OtpErlangObject thisTail = this.getLastTail(); + final OtpErlangObject thatTail = that.getLastTail(); + + if (thisTail == null) { + if (thisArity != thatArity || thatTail != null) { + return false; + } + } else { + if (thisArity > thatArity) { + return false; + } + } + for (int i = 0; i < thisArity; i++) { + if (!elementAt(i).match(that.elementAt(i), bindings)) { + return false; + } + } + if (thisTail == null) { + return true; + } + return thisTail.match(that.getNthTail(thisArity), bindings); + } + + @Override + public <T> OtpErlangObject bind(final T binds) throws OtpErlangException { + final OtpErlangList list = (OtpErlangList) this.clone(); + + final int a = list.elems.length; + for (int i = 0; i < a; i++) { + list.elems[i] = list.elems[i].bind(binds); + } + + if (list.lastTail != null) { + list.lastTail = list.lastTail.bind(binds); + } + + return list; } public OtpErlangObject getLastTail() { - return lastTail; + return lastTail; } - + @Override protected int doHashCode() { - OtpErlangObject.Hash hash = new OtpErlangObject.Hash(4); - final int a = arity(); - if (a == 0) { - return (int)3468870702L; - } - for (int i = 0; i < a; i++) { - hash.combine(elementAt(i).hashCode()); - } - final OtpErlangObject t = getLastTail(); - if (t != null) { - int h = t.hashCode(); - hash.combine(h, h); - } - return hash.valueOf(); + final OtpErlangObject.Hash hash = new OtpErlangObject.Hash(4); + final int a = arity(); + if (a == 0) { + return (int) 3468870702L; + } + for (int i = 0; i < a; i++) { + hash.combine(elementAt(i).hashCode()); + } + final OtpErlangObject t = getLastTail(); + if (t != null) { + final int h = t.hashCode(); + hash.combine(h, h); + } + return hash.valueOf(); } - + @Override public Object clone() { - try { - return new OtpErlangList(elements(), getLastTail()); - } catch (final OtpErlangException e) { - throw new AssertionError(this); - } + try { + return new OtpErlangList(elements(), getLastTail()); + } catch (final OtpErlangException e) { + throw new AssertionError(this); + } } public Iterator<OtpErlangObject> iterator() { - return iterator(0); + return iterator(0); } private Iterator<OtpErlangObject> iterator(final int start) { - return new Itr(start); + return new Itr(start); } /** * @return true if the list is proper, i.e. the last tail is nil */ public boolean isProper() { - return lastTail == null; + return lastTail == null; } public OtpErlangObject getHead() { - if (arity() > 0) { - return elems[0]; - } - return null; + if (arity() > 0) { + return elems[0]; + } + return null; } public OtpErlangObject getTail() { - return getNthTail(1); + return getNthTail(1); } public OtpErlangObject getNthTail(final int n) { - final int arity = arity(); - if (arity >= n) { - if (arity == n && lastTail != null) { - return lastTail; - } - return new SubList(this, n); - } - return null; + final int arity = arity(); + if (arity >= n) { + if (arity == n && lastTail != null) { + return lastTail; + } + return new SubList(this, n); + } + return null; } /** - * Convert a list of integers into a Unicode string, - * interpreting each integer as a Unicode code point value. - * - * @return A java.lang.String object created through its - * constructor String(int[], int, int). + * Convert a list of integers into a Unicode string, interpreting each + * integer as a Unicode code point value. + * + * @return A java.lang.String object created through its constructor + * String(int[], int, int). * * @exception OtpErlangException - * for non-proper and non-integer lists. + * for non-proper and non-integer lists. * * @exception OtpErlangRangeException - * if any integer does not fit into a Java int. + * if any integer does not fit into a Java int. * * @exception java.security.InvalidParameterException - * if any integer is not within the Unicode range. + * if any integer is not within the Unicode range. * * @see String#String(int[], int, int) * */ public String stringValue() throws OtpErlangException { - if (! isProper()) { - throw new OtpErlangException("Non-proper list: " + this); - } - final int[] values = new int[arity()]; - for (int i = 0; i < values.length; ++i) { - final OtpErlangObject o = elementAt(i); - if (! (o instanceof OtpErlangLong)) { - throw new OtpErlangException("Non-integer term: " + o); - } - final OtpErlangLong l = (OtpErlangLong) o; - values[i] = l.intValue(); - } - return new String(values, 0, values.length); + if (!isProper()) { + throw new OtpErlangException("Non-proper list: " + this); + } + final int[] values = new int[arity()]; + for (int i = 0; i < values.length; ++i) { + final OtpErlangObject o = elementAt(i); + if (!(o instanceof OtpErlangLong)) { + throw new OtpErlangException("Non-integer term: " + o); + } + final OtpErlangLong l = (OtpErlangLong) o; + values[i] = l.intValue(); + } + return new String(values, 0, values.length); } - - public static class SubList extends OtpErlangList { - private static final long serialVersionUID = OtpErlangList.serialVersionUID; - - private final int start; - - private final OtpErlangList parent; - - private SubList(final OtpErlangList parent, final int start) { - super(); - this.parent = parent; - this.start = start; - } - - @Override - public int arity() { - return parent.arity() - start; - } - - @Override - public OtpErlangObject elementAt(final int i) { - return parent.elementAt(i + start); - } - - @Override - public OtpErlangObject[] elements() { - final int n = parent.arity() - start; - final OtpErlangObject[] res = new OtpErlangObject[n]; - for (int i = 0; i < res.length; i++) { - res[i] = parent.elementAt(i + start); - } - return res; - } - - @Override - public boolean isProper() { - return parent.isProper(); - } - - @Override - public OtpErlangObject getHead() { - return parent.elementAt(start); - } - - @Override - public OtpErlangObject getNthTail(final int n) { - return parent.getNthTail(n + start); - } - - @Override - public String toString() { - return parent.toString(start); - } - - @Override - public void encode(final OtpOutputStream stream) { - parent.encode(stream, start); - } - - @Override - public OtpErlangObject getLastTail() { - return parent.getLastTail(); - } - - @Override - public Iterator<OtpErlangObject> iterator() { - return parent.iterator(start); - } + private static final long serialVersionUID = OtpErlangList.serialVersionUID; + + private final int start; + + private final OtpErlangList parent; + + private SubList(final OtpErlangList parent, final int start) { + super(); + this.parent = parent; + this.start = start; + } + + @Override + public int arity() { + return parent.arity() - start; + } + + @Override + public OtpErlangObject elementAt(final int i) { + return parent.elementAt(i + start); + } + + @Override + public OtpErlangObject[] elements() { + final int n = parent.arity() - start; + final OtpErlangObject[] res = new OtpErlangObject[n]; + for (int i = 0; i < res.length; i++) { + res[i] = parent.elementAt(i + start); + } + return res; + } + + @Override + public boolean isProper() { + return parent.isProper(); + } + + @Override + public OtpErlangObject getHead() { + return parent.elementAt(start); + } + + @Override + public OtpErlangObject getNthTail(final int n) { + return parent.getNthTail(n + start); + } + + @Override + public String toString() { + return parent.toString(start); + } + + @Override + public void encode(final OtpOutputStream stream) { + parent.encode(stream, start); + } + + @Override + public OtpErlangObject getLastTail() { + return parent.getLastTail(); + } + + @Override + public Iterator<OtpErlangObject> iterator() { + return parent.iterator(start); + } } private class Itr implements Iterator<OtpErlangObject> { - /** - * Index of element to be returned by subsequent call to next. - */ - private int cursor; - - private Itr(final int cursor) { - this.cursor = cursor; - } - - public boolean hasNext() { - return cursor < elems.length; - } - - public OtpErlangObject next() { - try { - return elems[cursor++]; - } catch (final IndexOutOfBoundsException e) { - throw new NoSuchElementException(); - } - } - - public void remove() { - throw new UnsupportedOperationException( - "OtpErlangList cannot be modified!"); - } + /** + * Index of element to be returned by subsequent call to next. + */ + private int cursor; + + private Itr(final int cursor) { + this.cursor = cursor; + } + + public boolean hasNext() { + return cursor < elems.length; + } + + public OtpErlangObject next() { + try { + return elems[cursor++]; + } catch (final IndexOutOfBoundsException e) { + throw new NoSuchElementException(); + } + } + + public void remove() { + throw new UnsupportedOperationException( + "OtpErlangList cannot be modified!"); + } } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangLong.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangLong.java index c6021a6ae1..47a691224b 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangLong.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangLong.java @@ -1,19 +1,19 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; @@ -23,11 +23,11 @@ import java.math.BigInteger; /** * Provides a Java representation of Erlang integral types. Erlang does not * distinguish between different integral types, however this class and its - * subclasses {@link OtpErlangByte}, {@link OtpErlangChar}, - * {@link OtpErlangInt}, and {@link OtpErlangShort} attempt to map the Erlang - * types onto the various Java integral types. Two additional classes, - * {@link OtpErlangUInt} and {@link OtpErlangUShort} are provided for Corba - * compatibility. See the documentation for IC for more information. + * subclasses {@link OtpErlangByte}, {@link OtpErlangChar}, {@link OtpErlangInt} + * , and {@link OtpErlangShort} attempt to map the Erlang types onto the various + * Java integral types. Two additional classes, {@link OtpErlangUInt} and + * {@link OtpErlangUShort} are provided for Corba compatibility. See the + * documentation for IC for more information. */ public class OtpErlangLong extends OtpErlangObject { // don't change this! @@ -38,354 +38,353 @@ public class OtpErlangLong extends OtpErlangObject { /** * Create an Erlang integer from the given value. - * + * * @param l - * the long value to use. + * the long value to use. */ public OtpErlangLong(final long l) { - val = l; + val = l; } /** * Create an Erlang integer from the given value. - * + * * @param v - * the big integer value to use. + * the big integer value to use. */ public OtpErlangLong(final BigInteger v) { - if (v == null) { - throw new java.lang.NullPointerException(); - } - if (v.bitLength() < 64) { - val = v.longValue(); - } else { - bigVal = v; - } + if (v == null) { + throw new java.lang.NullPointerException(); + } + if (v.bitLength() < 64) { + val = v.longValue(); + } else { + bigVal = v; + } } /** * Create an Erlang integer from a stream containing an integer encoded in * Erlang external format. - * + * * @param buf - * the stream containing the encoded value. - * + * the stream containing the encoded value. + * * @exception OtpErlangDecodeException - * if the buffer does not contain a valid external - * representation of an Erlang integer. + * if the buffer does not contain a valid external + * representation of an Erlang integer. */ public OtpErlangLong(final OtpInputStream buf) - throws OtpErlangDecodeException { - final byte[] b = buf.read_integer_byte_array(); - try { - val = OtpInputStream.byte_array_to_long(b, false); - } catch (final OtpErlangDecodeException e) { - bigVal = new BigInteger(b); - } + throws OtpErlangDecodeException { + final byte[] b = buf.read_integer_byte_array(); + try { + val = OtpInputStream.byte_array_to_long(b, false); + } catch (final OtpErlangDecodeException e) { + bigVal = new BigInteger(b); + } } /** * Get this number as a BigInteger. - * + * * @return the value of this number, as a BigInteger. */ public BigInteger bigIntegerValue() { - if (bigVal != null) { - return bigVal; - } - return BigInteger.valueOf(val); + if (bigVal != null) { + return bigVal; + } + return BigInteger.valueOf(val); } /** * Get this number as a long, or rather truncate all but the least * significant 64 bits from the 2's complement representation of this number * and return them as a long. - * + * * @return the value of this number, as a long. */ public long longValue() { - if (bigVal != null) { - return bigVal.longValue(); - } - return val; + if (bigVal != null) { + return bigVal.longValue(); + } + return val; } /** * Determine if this value can be represented as a long without truncation. - * + * * @return true if this value fits in a long, false otherwise. */ public boolean isLong() { - // To just chech this.bigVal is a wee bit to simple, since - // there just might have be a mean bignum that arrived on - // a stream, and was a long disguised as more than 8 byte integer. - if (bigVal != null) { - return bigVal.bitLength() < 64; - } - return true; + // To just chech this.bigVal is a wee bit to simple, since + // there just might have be a mean bignum that arrived on + // a stream, and was a long disguised as more than 8 byte integer. + if (bigVal != null) { + return bigVal.bitLength() < 64; + } + return true; } /** * Determine if this value can be represented as an unsigned long without * truncation, that is if the value is non-negative and its bit pattern * completely fits in a long. - * + * * @return true if this value is non-negative and fits in a long false * otherwise. */ public boolean isULong() { - // Here we have the same problem as for isLong(), plus - // the whole range 1<<63 .. (1<<64-1) is allowed. - if (bigVal != null) { - return bigVal.signum() >= 0 && bigVal.bitLength() <= 64; - } - return val >= 0; + // Here we have the same problem as for isLong(), plus + // the whole range 1<<63 .. (1<<64-1) is allowed. + if (bigVal != null) { + return bigVal.signum() >= 0 && bigVal.bitLength() <= 64; + } + return val >= 0; } /** * Returns the number of bits in the minimal two's-complement representation * of this BigInteger, excluding a sign bit. - * + * * @return number of bits in the minimal two's-complement representation of * this BigInteger, excluding a sign bit. */ public int bitLength() { - if (bigVal != null) { - return bigVal.bitLength(); - } - if (val == 0 || val == -1) { - return 0; + if (bigVal != null) { + return bigVal.bitLength(); + } + if (val == 0 || val == -1) { + return 0; + } + // Binary search for bit length + int i = 32; // mask length + long m = (1L << i) - 1; // AND mask with ones in little end + if (val < 0) { + m = ~m; // OR mask with ones in big end + for (int j = i >> 1; j > 0; j >>= 1) { // mask delta + if ((val | m) == val) { // mask >= enough + i -= j; + m >>= j; // try less bits + } else { + i += j; + m <<= j; // try more bits + } + } + if ((val | m) != val) { + i++; // mask < enough + } + } else { + for (int j = i >> 1; j > 0; j >>= 1) { // mask delta + if ((val & m) == val) { // mask >= enough + i -= j; + m >>= j; // try less bits + } else { + i += j; + m = m << j | m; // try more bits + } + } + if ((val & m) != val) { + i++; // mask < enough + } + } + return i; } - // Binary search for bit length - int i = 32; // mask length - long m = (1L << i) - 1; // AND mask with ones in little end - if (val < 0) { - m = ~m; // OR mask with ones in big end - for (int j = i >> 1; j > 0; j >>= 1) { // mask delta - if ((val | m) == val) { // mask >= enough - i -= j; - m >>= j; // try less bits - } else { - i += j; - m <<= j; // try more bits - } - } - if ((val | m) != val) { - i++; // mask < enough - } - } else { - for (int j = i >> 1; j > 0; j >>= 1) { // mask delta - if ((val & m) == val) { // mask >= enough - i -= j; - m >>= j; // try less bits - } else { - i += j; - m = m << j | m; // try more bits - } - } - if ((val & m) != val) { - i++; // mask < enough - } - } - return i; - } /** * Return the signum function of this object. - * + * * @return -1, 0 or 1 as the value is negative, zero or positive. */ public int signum() { - if (bigVal != null) { - return bigVal.signum(); - } - return val > 0 ? 1 : val < 0 ? -1 : 0; + if (bigVal != null) { + return bigVal.signum(); + } + return val > 0 ? 1 : val < 0 ? -1 : 0; } /** * Get this number as an int. - * + * * @return the value of this number, as an int. - * + * * @exception OtpErlangRangeException - * if the value is too large to be represented as an int. + * if the value is too large to be represented as an int. */ public int intValue() throws OtpErlangRangeException { - final long l = longValue(); - final int i = (int) l; + final long l = longValue(); + final int i = (int) l; - if (i != l) { - throw new OtpErlangRangeException("Value too large for int: " + val); - } + if (i != l) { + throw new OtpErlangRangeException("Value too large for int: " + val); + } - return i; + return i; } /** * Get this number as a non-negative int. - * + * * @return the value of this number, as an int. - * + * * @exception OtpErlangRangeException - * if the value is too large to be represented as an int, - * or if the value is negative. + * if the value is too large to be represented as an int, or + * if the value is negative. */ public int uIntValue() throws OtpErlangRangeException { - final long l = longValue(); - final int i = (int) l; + final long l = longValue(); + final int i = (int) l; - if (i != l) { - throw new OtpErlangRangeException("Value too large for int: " + val); - } else if (i < 0) { - throw new OtpErlangRangeException("Value not positive: " + val); - } + if (i != l) { + throw new OtpErlangRangeException("Value too large for int: " + val); + } else if (i < 0) { + throw new OtpErlangRangeException("Value not positive: " + val); + } - return i; + return i; } /** * Get this number as a short. - * + * * @return the value of this number, as a short. - * + * * @exception OtpErlangRangeException - * if the value is too large to be represented as a - * short. + * if the value is too large to be represented as a short. */ public short shortValue() throws OtpErlangRangeException { - final long l = longValue(); - final short i = (short) l; + final long l = longValue(); + final short i = (short) l; - if (i != l) { - throw new OtpErlangRangeException("Value too large for short: " - + val); - } + if (i != l) { + throw new OtpErlangRangeException("Value too large for short: " + + val); + } - return i; + return i; } /** * Get this number as a non-negative short. - * + * * @return the value of this number, as a short. - * + * * @exception OtpErlangRangeException - * if the value is too large to be represented as a - * short, or if the value is negative. + * if the value is too large to be represented as a short, or + * if the value is negative. */ public short uShortValue() throws OtpErlangRangeException { - final long l = longValue(); - final short i = (short) l; + final long l = longValue(); + final short i = (short) l; - if (i != l) { - throw new OtpErlangRangeException("Value too large for short: " - + val); - } else if (i < 0) { - throw new OtpErlangRangeException("Value not positive: " + val); - } + if (i != l) { + throw new OtpErlangRangeException("Value too large for short: " + + val); + } else if (i < 0) { + throw new OtpErlangRangeException("Value not positive: " + val); + } - return i; + return i; } /** * Get this number as a char. - * + * * @return the char value of this number. - * + * * @exception OtpErlangRangeException - * if the value is too large to be represented as a char. + * if the value is too large to be represented as a char. */ public char charValue() throws OtpErlangRangeException { - final long l = longValue(); - final char i = (char) l; + final long l = longValue(); + final char i = (char) l; - if (i != l) { - throw new OtpErlangRangeException("Value too large for char: " - + val); - } + if (i != l) { + throw new OtpErlangRangeException("Value too large for char: " + + val); + } - return i; + return i; } /** * Get this number as a byte. - * + * * @return the byte value of this number. - * + * * @exception OtpErlangRangeException - * if the value is too large to be represented as a byte. + * if the value is too large to be represented as a byte. */ public byte byteValue() throws OtpErlangRangeException { - final long l = longValue(); - final byte i = (byte) l; + final long l = longValue(); + final byte i = (byte) l; - if (i != l) { - throw new OtpErlangRangeException("Value too large for byte: " - + val); - } + if (i != l) { + throw new OtpErlangRangeException("Value too large for byte: " + + val); + } - return i; + return i; } /** * Get the string representation of this number. - * + * * @return the string representation of this number. */ @Override public String toString() { - if (bigVal != null) { - return "" + bigVal; - } - return "" + val; + if (bigVal != null) { + return "" + bigVal; + } + return "" + val; } /** * Convert this number to the equivalent Erlang external representation. - * + * * @param buf - * an output stream to which the encoded number should be - * written. + * an output stream to which the encoded number should be + * written. */ @Override public void encode(final OtpOutputStream buf) { - if (bigVal != null) { - buf.write_big_integer(bigVal); - } else { - buf.write_long(val); - } + if (bigVal != null) { + buf.write_big_integer(bigVal); + } else { + buf.write_long(val); + } } /** * Determine if two numbers are equal. Numbers are equal if they contain the * same value. - * + * * @param o - * the number to compare to. - * + * the number to compare to. + * * @return true if the numbers have the same value. */ @Override public boolean equals(final Object o) { - if (!(o instanceof OtpErlangLong)) { - return false; - } - - final OtpErlangLong that = (OtpErlangLong) o; - - if (bigVal != null && that.bigVal != null) { - return bigVal.equals(that.bigVal); - } else if (bigVal == null && that.bigVal == null) { - return val == that.val; - } - return false; + if (!(o instanceof OtpErlangLong)) { + return false; + } + + final OtpErlangLong that = (OtpErlangLong) o; + + if (bigVal != null && that.bigVal != null) { + return bigVal.equals(that.bigVal); + } else if (bigVal == null && that.bigVal == null) { + return val == that.val; + } + return false; } - + @Override protected int doHashCode() { - if (bigVal != null) { - return bigVal.hashCode(); - } - return BigInteger.valueOf(val).hashCode(); + if (bigVal != null) { + return bigVal.hashCode(); + } + return BigInteger.valueOf(val).hashCode(); } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangMap.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangMap.java index 7f1a64b87d..a8cd9d5392 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangMap.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangMap.java @@ -18,44 +18,52 @@ */ package com.ericsson.otp.erlang; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; /** * Provides a Java representation of Erlang maps. Maps are created from one or * more arbitrary Erlang terms. - * + * * <p> * The arity of the map is the number of elements it contains. The keys and * values can be retrieved as arrays and the value for a key can be queried. - * + * */ public class OtpErlangMap extends OtpErlangObject { // don't change this! private static final long serialVersionUID = -6410770117696198497L; - private static final OtpErlangObject[] NO_ELEMENTS = new OtpErlangObject[0]; + private HashMap<OtpErlangObject, OtpErlangObject> map; - private OtpErlangObject[] keys = NO_ELEMENTS; - private OtpErlangObject[] values = NO_ELEMENTS; + /** + * Create an empty map. + */ + public OtpErlangMap() { + map = new HashMap<OtpErlangObject, OtpErlangObject>(); + } /** * Create a map from an array of keys and an array of values. - * + * * @param keys * the array of terms to create the map keys from. * @param values * the array of terms to create the map values from. - * + * * @exception java.lang.IllegalArgumentException * if any array is empty (null) or contains null elements. */ public OtpErlangMap(final OtpErlangObject[] keys, - final OtpErlangObject[] values) { - this(keys, 0, keys.length, values, 0, values.length); + final OtpErlangObject[] values) { + this(keys, 0, keys.length, values, 0, values.length); } /** * Create a map from an array of terms. - * + * * @param keys * the array of terms to create the map from. * @param kstart @@ -68,228 +76,280 @@ public class OtpErlangMap extends OtpErlangObject { * the offset of the first value to insert. * @param vcount * the number of values to insert. - * + * * @exception java.lang.IllegalArgumentException * if any array is empty (null) or contains null elements. * @exception java.lang.IllegalArgumentException * if kcount and vcount differ. */ public OtpErlangMap(final OtpErlangObject[] keys, final int kstart, - final int kcount, final OtpErlangObject[] values, final int vstart, - final int vcount) { - if (keys == null || values == null) { - throw new java.lang.IllegalArgumentException( - "Map content can't be null"); - } else if (kcount != vcount) { - throw new java.lang.IllegalArgumentException( - "Map keys and values must have same arity"); - } else if (vcount < 1) { - this.keys = NO_ELEMENTS; - this.values = NO_ELEMENTS; - } else { - this.keys = new OtpErlangObject[vcount]; - for (int i = 0; i < vcount; i++) { - if (keys[kstart + i] != null) { - this.keys[i] = keys[kstart + i]; - } else { - throw new java.lang.IllegalArgumentException( - "Map key cannot be null (element" + (kstart + i) - + ")"); - } - } - this.values = new OtpErlangObject[vcount]; - for (int i = 0; i < vcount; i++) { - if (values[vstart + i] != null) { - this.values[i] = values[vstart + i]; - } else { - throw new java.lang.IllegalArgumentException( - "Map value cannot be null (element" + (vstart + i) - + ")"); - } - } - } + final int kcount, final OtpErlangObject[] values, final int vstart, + final int vcount) { + if (keys == null || values == null) { + throw new java.lang.IllegalArgumentException( + "Map content can't be null"); + } else if (kcount != vcount) { + throw new java.lang.IllegalArgumentException( + "Map keys and values must have same arity"); + } + map = new HashMap<OtpErlangObject, OtpErlangObject>(vcount); + OtpErlangObject key, val; + for (int i = 0; i < vcount; i++) { + if ((key = keys[kstart + i]) == null) { + throw new java.lang.IllegalArgumentException( + "Map key cannot be null (element" + (kstart + i) + ")"); + } + if ((val = values[vstart + i]) == null) { + throw new java.lang.IllegalArgumentException( + "Map value cannot be null (element" + (vstart + i) + + ")"); + } + put(key, val); + } } /** * Create a map from a stream containing a map encoded in Erlang external * format. - * + * * @param buf * the stream containing the encoded map. - * + * * @exception OtpErlangDecodeException * if the buffer does not contain a valid external * representation of an Erlang map. */ public OtpErlangMap(final OtpInputStream buf) - throws OtpErlangDecodeException { - final int arity = buf.read_map_head(); - - if (arity > 0) { - keys = new OtpErlangObject[arity]; - values = new OtpErlangObject[arity]; - - for (int i = 0; i < arity; i++) { - keys[i] = buf.read_any(); - values[i] = buf.read_any(); - } - } else { - keys = NO_ELEMENTS; - values = NO_ELEMENTS; - } + throws OtpErlangDecodeException { + final int arity = buf.read_map_head(); + + if (arity > 0) { + map = new HashMap<OtpErlangObject, OtpErlangObject>(arity); + for (int i = 0; i < arity; i++) { + OtpErlangObject key, val; + key = buf.read_any(); + val = buf.read_any(); + put(key, val); + } + } else { + map = new HashMap<OtpErlangObject, OtpErlangObject>(); + } } /** * Get the arity of the map. - * + * * @return the number of elements contained in the map. */ public int arity() { - return keys.length; + return map.size(); + } + + /** + * Put value corresponding to key into the map. For detailed behavior + * description see {@link Map#put(Object, Object)}. + * + * @param key + * key to associate value with + * @param value + * value to associate with key + * @return previous value associated with key or null + */ + public OtpErlangObject put(final OtpErlangObject key, + final OtpErlangObject value) { + return map.put(key, value); + } + + /** + * removes mapping for the key if present. + * + * @param key + * key for which mapping is to be remove + * @return value associated with key or null + */ + public OtpErlangObject remove(final OtpErlangObject key) { + return map.remove(key); } /** * Get the specified value from the map. - * + * * @param key * the key of the requested value. - * + * * @return the requested value, of null if key is not a valid key. */ public OtpErlangObject get(final OtpErlangObject key) { - if (key == null) { - return null; - } - for (int i = 0; i < keys.length; i++) { - if (key.equals(keys[i])) { - return values[i]; - } - } - return null; + return map.get(key); } /** * Get all the keys from the map as an array. - * + * * @return an array containing all of the map's keys. */ public OtpErlangObject[] keys() { - final OtpErlangObject[] res = new OtpErlangObject[arity()]; - System.arraycopy(keys, 0, res, 0, res.length); - return res; + return map.keySet().toArray(new OtpErlangObject[arity()]); } /** * Get all the values from the map as an array. - * + * * @return an array containing all of the map's values. */ public OtpErlangObject[] values() { - final OtpErlangObject[] res = new OtpErlangObject[arity()]; - System.arraycopy(values, 0, res, 0, res.length); - return res; + return map.values().toArray(new OtpErlangObject[arity()]); + } + + /** + * make Set view of the map key-value pairs + * + * @return a set containing key-value pairs + */ + public Set<Entry<OtpErlangObject, OtpErlangObject>> entrySet() { + return map.entrySet(); } /** * Get the string representation of the map. - * + * * @return the string representation of the map. */ @Override public String toString() { - int i; - final StringBuffer s = new StringBuffer(); - final int arity = values.length; + final StringBuffer s = new StringBuffer(); - s.append("#{"); + s.append("#{"); - for (i = 0; i < arity; i++) { - if (i > 0) { - s.append(","); - } - s.append(keys[i].toString()); - s.append(" => "); - s.append(values[i].toString()); - } + boolean first = true; + for (final Map.Entry<OtpErlangObject, OtpErlangObject> e : entrySet()) { + if (first) { + first = false; + } else { + s.append(","); + } + s.append(e.getKey().toString()); + s.append(" => "); + s.append(e.getValue().toString()); + } - s.append("}"); + s.append("}"); - return s.toString(); + return s.toString(); } /** * Convert this map to the equivalent Erlang external representation. - * + * * @param buf * an output stream to which the encoded map should be written. */ @Override public void encode(final OtpOutputStream buf) { - final int arity = values.length; + final int arity = arity(); - buf.write_map_head(arity); + buf.write_map_head(arity); - for (int i = 0; i < arity; i++) { - buf.write_any(keys[i]); - buf.write_any(values[i]); - } + for (final Map.Entry<OtpErlangObject, OtpErlangObject> e : entrySet()) { + buf.write_any(e.getKey()); + buf.write_any(e.getValue()); + } } /** * Determine if two maps are equal. Maps are equal if they have the same * arity and all of the elements are equal. - * + * * @param o * the map to compare to. - * + * * @return true if the maps have the same arity and all the elements are * equal. */ @Override public boolean equals(final Object o) { - if (!(o instanceof OtpErlangMap)) { - return false; - } - - final OtpErlangMap t = (OtpErlangMap) o; - final int a = arity(); - - if (a != t.arity()) { - return false; - } - - for (int i = 0; i < a; i++) { - if (!keys[i].equals(t.keys[i])) { - return false; // early exit - } - } - for (int i = 0; i < a; i++) { - if (!values[i].equals(t.values[i])) { - return false; // early exit - } - } - - return true; + if (!(o instanceof OtpErlangMap)) { + return false; + } + + final OtpErlangMap t = (OtpErlangMap) o; + final int a = arity(); + + if (a != t.arity()) { + return false; + } + if (a == 0) { + return true; + } + + OtpErlangObject key, val; + for (final Map.Entry<OtpErlangObject, OtpErlangObject> e : entrySet()) { + key = e.getKey(); + val = e.getValue(); + final OtpErlangObject v = t.get(key); + if (v == null || !val.equals(v)) { + return false; + } + } + + return true; + } + + @Override + public <T> boolean match(final OtpErlangObject term, final T binds) { + if (!(term instanceof OtpErlangMap)) { + return false; + } + + final OtpErlangMap t = (OtpErlangMap) term; + final int a = arity(); + + if (a > t.arity()) { + return false; + } + if (a == 0) { + return true; + } + + OtpErlangObject key, val; + for (final Map.Entry<OtpErlangObject, OtpErlangObject> e : entrySet()) { + key = e.getKey(); + val = e.getValue(); + final OtpErlangObject v = t.get(key); + if (v == null || !val.match(v, binds)) { + return false; + } + } + + return true; + } + + @Override + public <T> OtpErlangObject bind(final T binds) throws OtpErlangException { + final OtpErlangMap ret = new OtpErlangMap(); + + OtpErlangObject key, val; + for (final Map.Entry<OtpErlangObject, OtpErlangObject> e : entrySet()) { + key = e.getKey(); + val = e.getValue(); + ret.put(key, val.bind(binds)); + } + + return ret; } @Override protected int doHashCode() { - final OtpErlangObject.Hash hash = new OtpErlangObject.Hash(9); - final int a = arity(); - hash.combine(a); - for (int i = 0; i < a; i++) { - hash.combine(keys[i].hashCode()); - } - for (int i = 0; i < a; i++) { - hash.combine(values[i].hashCode()); - } - return hash.valueOf(); + final OtpErlangObject.Hash hash = new OtpErlangObject.Hash(9); + hash.combine(map.hashCode()); + return hash.valueOf(); } @Override + @SuppressWarnings("unchecked") public Object clone() { - final OtpErlangMap newMap = (OtpErlangMap) super.clone(); - newMap.values = values.clone(); - return newMap; + final OtpErlangMap newMap = (OtpErlangMap) super.clone(); + newMap.map = (HashMap<OtpErlangObject, OtpErlangObject>) map.clone(); + return newMap; } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangObject.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangObject.java index 5215e5887b..9339d3749b 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangObject.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangObject.java @@ -1,19 +1,19 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; @@ -26,7 +26,7 @@ import java.io.Serializable; */ public abstract class OtpErlangObject implements Serializable, Cloneable { protected int hashCodeValue = 0; - + // don't change this! static final long serialVersionUID = -8435938572339430044L; @@ -42,10 +42,9 @@ public abstract class OtpErlangObject implements Serializable, Cloneable { * Convert the object according to the rules of the Erlang external format. * This is mainly used for sending Erlang terms in messages, however it can * also be used for storing terms to disk. - * + * * @param buf - * an output stream to which the encoded term should be - * written. + * an output stream to which the encoded term should be written. */ public abstract void encode(OtpOutputStream buf); @@ -54,137 +53,176 @@ public abstract class OtpErlangObject implements Serializable, Cloneable { * corresponding Erlang data type object. This method is normally used when * Erlang terms are received in messages, however it can also be used for * reading terms from disk. - * + * * @param buf - * an input stream containing one or more encoded Erlang - * terms. - * + * an input stream containing one or more encoded Erlang terms. + * * @return an object representing one of the Erlang data types. - * + * * @exception OtpErlangDecodeException - * if the stream does not contain a valid representation - * of an Erlang term. + * if the stream does not contain a valid representation of + * an Erlang term. */ public static OtpErlangObject decode(final OtpInputStream buf) - throws OtpErlangDecodeException { - return buf.read_any(); + throws OtpErlangDecodeException { + return buf.read_any(); } /** * Determine if two Erlang objects are equal. In general, Erlang objects are * equal if the components they consist of are equal. - * + * * @param o - * the object to compare to. - * + * the object to compare to. + * * @return true if the objects are identical. */ @Override public abstract boolean equals(Object o); - + + /** + * Perform match operation against given term. + * + * @param term + * the object to match + * @param binds + * variable bindings + * @return true if match succeeded + */ + public <T> boolean match(final OtpErlangObject term, final T binds) { + return equals(term); + } + + /** + * Make new Erlang term replacing variables with the respective values from + * bindings argument(s). + * + * @param binds + * variable bindings + * @return new term + * @throws OtpErlangException + */ + public <T> OtpErlangObject bind(final T binds) throws OtpErlangException { + return this; + } + @Override public int hashCode() { - if (hashCodeValue == 0) { - hashCodeValue = doHashCode(); - } - return hashCodeValue; + if (hashCodeValue == 0) { + hashCodeValue = doHashCode(); + } + return hashCodeValue; } - + protected int doHashCode() { - return super.hashCode(); + return super.hashCode(); } - + @Override public Object clone() { - try { - return super.clone(); - } catch (final CloneNotSupportedException e) { - /* cannot happen */ - throw new InternalError(e.toString()); - } + try { + return super.clone(); + } catch (final CloneNotSupportedException e) { + /* cannot happen */ + throw new InternalError(e.toString()); + } } protected final static class Hash { - int abc[] = {0, 0, 0}; - - /* Hash function suggested by Bob Jenkins. - * The same as in the Erlang VM (beam); utils.c. - */ - - private final static int HASH_CONST[] = { - 0, // not used - 0x9e3779b9, // the golden ratio; an arbitrary value - 0x3c6ef372, // (hashHConst[1] * 2) % (1<<32) - 0xdaa66d2b, // 1 3 - 0x78dde6e4, // 1 4 - 0x1715609d, // 1 5 - 0xb54cda56, // 1 6 - 0x5384540f, // 1 7 - 0xf1bbcdc8, // 1 8 - 0x8ff34781, // 1 9 - 0x2e2ac13a, // 1 10 - 0xcc623af3, // 1 11 - 0x6a99b4ac, // 1 12 - 0x08d12e65, // 1 13 - 0xa708a81e, // 1 14 - 0x454021d7, // 1 15 - }; - - protected Hash(int i) { - abc[0] = abc[1] = HASH_CONST[i]; - abc[2] = 0; - } - - //protected Hash() { - // Hash(1); - //} - - private void mix() { - abc[0] -= abc[1]; abc[0] -= abc[2]; abc[0] ^= (abc[2]>>>13); - abc[1] -= abc[2]; abc[1] -= abc[0]; abc[1] ^= (abc[0]<<8); - abc[2] -= abc[0]; abc[2] -= abc[1]; abc[2] ^= (abc[1]>>>13); - abc[0] -= abc[1]; abc[0] -= abc[2]; abc[0] ^= (abc[2]>>>12); - abc[1] -= abc[2]; abc[1] -= abc[0]; abc[1] ^= (abc[0]<<16); - abc[2] -= abc[0]; abc[2] -= abc[1]; abc[2] ^= (abc[1]>>>5); - abc[0] -= abc[1]; abc[0] -= abc[2]; abc[0] ^= (abc[2]>>>3); - abc[1] -= abc[2]; abc[1] -= abc[0]; abc[1] ^= (abc[0]<<10); - abc[2] -= abc[0]; abc[2] -= abc[1]; abc[2] ^= (abc[1]>>>15); - } - - protected void combine(int a) { - abc[0] += a; - mix(); - } - - protected void combine(long a) { - combine((int)(a >>> 32), (int) a); - } - - protected void combine(int a, int b) { - abc[0] += a; - abc[1] += b; - mix(); - } - - protected void combine(byte b[]) { - int j, k; - for (j = 0, k = 0; - j + 4 < b.length; - j += 4, k += 1, k %= 3) { - abc[k] += (b[j+0] & 0xFF) + (b[j+1]<<8 & 0xFF00) - + (b[j+2]<<16 & 0xFF0000) + (b[j+3]<<24); - mix(); - } - for (int n = 0, m = 0xFF; - j < b.length; - j++, n += 8, m <<= 8) { - abc[k] += b[j]<<n & m; - } - mix(); - } - - protected int valueOf() { - return abc[2]; - } + int abc[] = { 0, 0, 0 }; + + /* + * Hash function suggested by Bob Jenkins. The same as in the Erlang VM + * (beam); utils.c. + */ + + private final static int HASH_CONST[] = { 0, // not used + 0x9e3779b9, // the golden ratio; an arbitrary value + 0x3c6ef372, // (hashHConst[1] * 2) % (1<<32) + 0xdaa66d2b, // 1 3 + 0x78dde6e4, // 1 4 + 0x1715609d, // 1 5 + 0xb54cda56, // 1 6 + 0x5384540f, // 1 7 + 0xf1bbcdc8, // 1 8 + 0x8ff34781, // 1 9 + 0x2e2ac13a, // 1 10 + 0xcc623af3, // 1 11 + 0x6a99b4ac, // 1 12 + 0x08d12e65, // 1 13 + 0xa708a81e, // 1 14 + 0x454021d7, // 1 15 + }; + + protected Hash(final int i) { + abc[0] = abc[1] = HASH_CONST[i]; + abc[2] = 0; + } + + // protected Hash() { + // Hash(1); + // } + + private void mix() { + abc[0] -= abc[1]; + abc[0] -= abc[2]; + abc[0] ^= abc[2] >>> 13; + abc[1] -= abc[2]; + abc[1] -= abc[0]; + abc[1] ^= abc[0] << 8; + abc[2] -= abc[0]; + abc[2] -= abc[1]; + abc[2] ^= abc[1] >>> 13; + abc[0] -= abc[1]; + abc[0] -= abc[2]; + abc[0] ^= abc[2] >>> 12; + abc[1] -= abc[2]; + abc[1] -= abc[0]; + abc[1] ^= abc[0] << 16; + abc[2] -= abc[0]; + abc[2] -= abc[1]; + abc[2] ^= abc[1] >>> 5; + abc[0] -= abc[1]; + abc[0] -= abc[2]; + abc[0] ^= abc[2] >>> 3; + abc[1] -= abc[2]; + abc[1] -= abc[0]; + abc[1] ^= abc[0] << 10; + abc[2] -= abc[0]; + abc[2] -= abc[1]; + abc[2] ^= abc[1] >>> 15; + } + + protected void combine(final int a) { + abc[0] += a; + mix(); + } + + protected void combine(final long a) { + combine((int) (a >>> 32), (int) a); + } + + protected void combine(final int a, final int b) { + abc[0] += a; + abc[1] += b; + mix(); + } + + protected void combine(final byte b[]) { + int j, k; + for (j = 0, k = 0; j + 4 < b.length; j += 4, k += 1, k %= 3) { + abc[k] += (b[j + 0] & 0xFF) + (b[j + 1] << 8 & 0xFF00) + + (b[j + 2] << 16 & 0xFF0000) + (b[j + 3] << 24); + mix(); + } + for (int n = 0, m = 0xFF; j < b.length; j++, n += 8, m <<= 8) { + abc[k] += b[j] << n & m; + } + mix(); + } + + protected int valueOf() { + return abc[2]; + } } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangPid.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangPid.java index 4c9f5c78a3..0f6ba8c538 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangPid.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangPid.java @@ -1,24 +1,23 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; - /** * Provides a Java representation of Erlang PIDs. PIDs represent Erlang * processes and consist of a nodename and a number of integers. @@ -34,172 +33,170 @@ public class OtpErlangPid extends OtpErlangObject implements Comparable<Object> /** * Create a unique Erlang PID belonging to the local node. - * + * * @param self - * the local node. - * + * the local node. + * * @deprecated use OtpLocalNode:createPid() instead */ @Deprecated public OtpErlangPid(final OtpLocalNode self) { - final OtpErlangPid p = self.createPid(); + final OtpErlangPid p = self.createPid(); - id = p.id; - serial = p.serial; - creation = p.creation; - node = p.node; + id = p.id; + serial = p.serial; + creation = p.creation; + node = p.node; } /** * Create an Erlang PID from a stream containing a PID encoded in Erlang * external format. - * + * * @param buf - * the stream containing the encoded PID. - * + * the stream containing the encoded PID. + * * @exception OtpErlangDecodeException - * if the buffer does not contain a valid external - * representation of an Erlang PID. + * if the buffer does not contain a valid external + * representation of an Erlang PID. */ public OtpErlangPid(final OtpInputStream buf) - throws OtpErlangDecodeException { - final OtpErlangPid p = buf.read_pid(); + throws OtpErlangDecodeException { + final OtpErlangPid p = buf.read_pid(); - node = p.node(); - id = p.id(); - serial = p.serial(); - creation = p.creation(); + node = p.node(); + id = p.id(); + serial = p.serial(); + creation = p.creation(); } /** * Create an Erlang pid from its components. - * + * * @param node - * the nodename. - * + * the nodename. + * * @param id - * an arbitrary number. Only the low order 15 bits will be - * used. - * + * an arbitrary number. Only the low order 15 bits will be used. + * * @param serial - * another arbitrary number. Only the low order 13 bits will - * be used. - * + * another arbitrary number. Only the low order 13 bits will be + * used. + * * @param creation - * yet another arbitrary number. Only the low order 2 bits - * will be used. + * yet another arbitrary number. Only the low order 2 bits will + * be used. */ public OtpErlangPid(final String node, final int id, final int serial, - final int creation) { - this.node = node; - this.id = id & 0x7fff; // 15 bits - this.serial = serial & 0x1fff; // 13 bits - this.creation = creation & 0x03; // 2 bits + final int creation) { + this.node = node; + this.id = id & 0x7fff; // 15 bits + this.serial = serial & 0x1fff; // 13 bits + this.creation = creation & 0x03; // 2 bits } /** * Get the serial number from the PID. - * + * * @return the serial number from the PID. */ public int serial() { - return serial; + return serial; } /** * Get the id number from the PID. - * + * * @return the id number from the PID. */ public int id() { - return id; + return id; } /** * Get the creation number from the PID. - * + * * @return the creation number from the PID. */ public int creation() { - return creation; + return creation; } /** * Get the node name from the PID. - * + * * @return the node name from the PID. */ public String node() { - return node; + return node; } /** * Get the string representation of the PID. Erlang PIDs are printed as * #Pid<node.id.serial> - * + * * @return the string representation of the PID. */ @Override public String toString() { - return "#Pid<" + node.toString() + "." + id + "." + serial + ">"; + return "#Pid<" + node.toString() + "." + id + "." + serial + ">"; } /** * Convert this PID to the equivalent Erlang external representation. - * + * * @param buf - * an output stream to which the encoded PID should be - * written. + * an output stream to which the encoded PID should be written. */ @Override public void encode(final OtpOutputStream buf) { - buf.write_pid(node, id, serial, creation); + buf.write_pid(node, id, serial, creation); } /** * Determine if two PIDs are equal. PIDs are equal if their components are * equal. - * + * * @param o - * the other PID to compare to. - * + * the other PID to compare to. + * * @return true if the PIDs are equal, false otherwise. */ @Override public boolean equals(final Object o) { - if (!(o instanceof OtpErlangPid)) { - return false; - } + if (!(o instanceof OtpErlangPid)) { + return false; + } - final OtpErlangPid pid = (OtpErlangPid) o; + final OtpErlangPid pid = (OtpErlangPid) o; - return creation == pid.creation && serial == pid.serial && id == pid.id - && node.compareTo(pid.node) == 0; + return creation == pid.creation && serial == pid.serial && id == pid.id + && node.compareTo(pid.node) == 0; } - + @Override protected int doHashCode() { - OtpErlangObject.Hash hash = new OtpErlangObject.Hash(5); - hash.combine(creation, serial); - hash.combine(id, node.hashCode()); - return hash.valueOf(); + final OtpErlangObject.Hash hash = new OtpErlangObject.Hash(5); + hash.combine(creation, serial); + hash.combine(id, node.hashCode()); + return hash.valueOf(); } - + public int compareTo(final Object o) { - if (!(o instanceof OtpErlangPid)) { - return -1; - } - - final OtpErlangPid pid = (OtpErlangPid) o; - if (creation == pid.creation) { - if (serial == pid.serial) { - if (id == pid.id) { - return node.compareTo(pid.node); + if (!(o instanceof OtpErlangPid)) { + return -1; + } + + final OtpErlangPid pid = (OtpErlangPid) o; + if (creation == pid.creation) { + if (serial == pid.serial) { + if (id == pid.id) { + return node.compareTo(pid.node); + } + return id - pid.id; + } + return serial - pid.serial; } - return id - pid.id; - } - return serial - pid.serial; - } - return creation - pid.creation; - } + return creation - pid.creation; } +} diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangPort.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangPort.java index 8557e17325..fc7345aaff 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangPort.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangPort.java @@ -1,24 +1,23 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; - /** * Provides a Java representation of Erlang ports. */ @@ -33,136 +32,134 @@ public class OtpErlangPort extends OtpErlangObject { /* * Create a unique Erlang port belonging to the local node. Since it isn't * meaninful to do so, this constructor is private... - * + * * @param self the local node. - * + * * @deprecated use OtpLocalNode:createPort() instead */ @SuppressWarnings("unused") private OtpErlangPort(final OtpSelf self) { - final OtpErlangPort p = self.createPort(); + final OtpErlangPort p = self.createPort(); - id = p.id; - creation = p.creation; - node = p.node; + id = p.id; + creation = p.creation; + node = p.node; } /** * Create an Erlang port from a stream containing a port encoded in Erlang * external format. - * + * * @param buf - * the stream containing the encoded port. - * + * the stream containing the encoded port. + * * @exception OtpErlangDecodeException - * if the buffer does not contain a valid external - * representation of an Erlang port. + * if the buffer does not contain a valid external + * representation of an Erlang port. */ public OtpErlangPort(final OtpInputStream buf) - throws OtpErlangDecodeException { - final OtpErlangPort p = buf.read_port(); + throws OtpErlangDecodeException { + final OtpErlangPort p = buf.read_port(); - node = p.node(); - id = p.id(); - creation = p.creation(); + node = p.node(); + id = p.id(); + creation = p.creation(); } /** * Create an Erlang port from its components. - * + * * @param node - * the nodename. - * + * the nodename. + * * @param id - * an arbitrary number. Only the low order 28 bits will be - * used. - * + * an arbitrary number. Only the low order 28 bits will be used. + * * @param creation - * another arbitrary number. Only the low order 2 bits will - * be used. + * another arbitrary number. Only the low order 2 bits will be + * used. */ public OtpErlangPort(final String node, final int id, final int creation) { - this.node = node; - this.id = id & 0xfffffff; // 28 bits - this.creation = creation & 0x03; // 2 bits + this.node = node; + this.id = id & 0xfffffff; // 28 bits + this.creation = creation & 0x03; // 2 bits } /** * Get the id number from the port. - * + * * @return the id number from the port. */ public int id() { - return id; + return id; } /** * Get the creation number from the port. - * + * * @return the creation number from the port. */ public int creation() { - return creation; + return creation; } /** * Get the node name from the port. - * + * * @return the node name from the port. */ public String node() { - return node; + return node; } /** * Get the string representation of the port. Erlang ports are printed as * #Port<node.id>. - * + * * @return the string representation of the port. */ @Override public String toString() { - return "#Port<" + node + "." + id + ">"; + return "#Port<" + node + "." + id + ">"; } /** * Convert this port to the equivalent Erlang external representation. - * + * * @param buf - * an output stream to which the encoded port should be - * written. + * an output stream to which the encoded port should be written. */ @Override public void encode(final OtpOutputStream buf) { - buf.write_port(node, id, creation); + buf.write_port(node, id, creation); } /** * Determine if two ports are equal. Ports are equal if their components are * equal. - * + * * @param o - * the other port to compare to. - * + * the other port to compare to. + * * @return true if the ports are equal, false otherwise. */ @Override public boolean equals(final Object o) { - if (!(o instanceof OtpErlangPort)) { - return false; - } + if (!(o instanceof OtpErlangPort)) { + return false; + } - final OtpErlangPort port = (OtpErlangPort) o; + final OtpErlangPort port = (OtpErlangPort) o; - return creation == port.creation && id == port.id - && node.compareTo(port.node) == 0; + return creation == port.creation && id == port.id + && node.compareTo(port.node) == 0; } - + @Override protected int doHashCode() { - OtpErlangObject.Hash hash = new OtpErlangObject.Hash(6); - hash.combine(creation); - hash.combine(id, node.hashCode()); - return hash.valueOf(); + final OtpErlangObject.Hash hash = new OtpErlangObject.Hash(6); + hash.combine(creation); + hash.combine(id, node.hashCode()); + return hash.valueOf(); } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangRangeException.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangRangeException.java index a78b6df6ef..21732717d3 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangRangeException.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangRangeException.java @@ -1,19 +1,19 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; @@ -21,7 +21,7 @@ package com.ericsson.otp.erlang; /** * Exception raised when an attempt is made to create an Erlang term with data * that is out of range for the term in question. - * + * * @see OtpErlangByte * @see OtpErlangChar * @see OtpErlangInt @@ -37,6 +37,6 @@ public class OtpErlangRangeException extends OtpErlangException { * Provides a detailed message. */ public OtpErlangRangeException(final String msg) { - super(msg); + super(msg); } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangRef.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangRef.java index 13a83333fa..f8031fb2e6 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangRef.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangRef.java @@ -1,24 +1,23 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; - /** * Provides a Java representation of Erlang refs. There are two styles of Erlang * refs, old style (one id value) and new style (array of id values). This class @@ -37,203 +36,201 @@ public class OtpErlangRef extends OtpErlangObject { /** * Create a unique Erlang ref belonging to the local node. - * + * * @param self - * the local node. - * + * the local node. + * * @deprecated use OtpLocalNode:createRef() instead */ @Deprecated public OtpErlangRef(final OtpLocalNode self) { - final OtpErlangRef r = self.createRef(); + final OtpErlangRef r = self.createRef(); - ids = r.ids; - creation = r.creation; - node = r.node; + ids = r.ids; + creation = r.creation; + node = r.node; } /** * Create an Erlang ref from a stream containing a ref encoded in Erlang * external format. - * + * * @param buf - * the stream containing the encoded ref. - * + * the stream containing the encoded ref. + * * @exception OtpErlangDecodeException - * if the buffer does not contain a valid external - * representation of an Erlang ref. + * if the buffer does not contain a valid external + * representation of an Erlang ref. */ public OtpErlangRef(final OtpInputStream buf) - throws OtpErlangDecodeException { - final OtpErlangRef r = buf.read_ref(); + throws OtpErlangDecodeException { + final OtpErlangRef r = buf.read_ref(); - node = r.node(); - creation = r.creation(); + node = r.node(); + creation = r.creation(); - ids = r.ids(); + ids = r.ids(); } /** * Create an old style Erlang ref from its components. - * + * * @param node - * the nodename. - * + * the nodename. + * * @param id - * an arbitrary number. Only the low order 18 bits will be - * used. - * + * an arbitrary number. Only the low order 18 bits will be used. + * * @param creation - * another arbitrary number. Only the low order 2 bits will - * be used. + * another arbitrary number. Only the low order 2 bits will be + * used. */ public OtpErlangRef(final String node, final int id, final int creation) { - this.node = node; - ids = new int[1]; - ids[0] = id & 0x3ffff; // 18 bits - this.creation = creation & 0x03; // 2 bits + this.node = node; + ids = new int[1]; + ids[0] = id & 0x3ffff; // 18 bits + this.creation = creation & 0x03; // 2 bits } /** * Create a new style Erlang ref from its components. - * + * * @param node - * the nodename. - * + * the nodename. + * * @param ids - * an array of arbitrary numbers. Only the low order 18 bits - * of the first number will be used. If the array contains - * only one number, an old style ref will be written instead. - * At most three numbers will be read from the array. - * + * an array of arbitrary numbers. Only the low order 18 bits of + * the first number will be used. If the array contains only one + * number, an old style ref will be written instead. At most + * three numbers will be read from the array. + * * @param creation - * another arbitrary number. Only the low order 2 bits will - * be used. + * another arbitrary number. Only the low order 2 bits will be + * used. */ public OtpErlangRef(final String node, final int[] ids, final int creation) { - this.node = node; - this.creation = creation & 0x03; // 2 bits + this.node = node; + this.creation = creation & 0x03; // 2 bits - // use at most 82 bits (18 + 32 + 32) - int len = ids.length; - this.ids = new int[3]; - this.ids[0] = 0; - this.ids[1] = 0; - this.ids[2] = 0; + // use at most 82 bits (18 + 32 + 32) + int len = ids.length; + this.ids = new int[3]; + this.ids[0] = 0; + this.ids[1] = 0; + this.ids[2] = 0; - if (len > 3) { - len = 3; - } - System.arraycopy(ids, 0, this.ids, 0, len); - this.ids[0] &= 0x3ffff; // only 18 significant bits in first number + if (len > 3) { + len = 3; + } + System.arraycopy(ids, 0, this.ids, 0, len); + this.ids[0] &= 0x3ffff; // only 18 significant bits in first number } /** * Get the id number from the ref. Old style refs have only one id number. * If this is a new style ref, the first id number is returned. - * + * * @return the id number from the ref. */ public int id() { - return ids[0]; + return ids[0]; } /** * Get the array of id numbers from the ref. If this is an old style ref, * the array is of length 1. If this is a new style ref, the array has * length 3. - * + * * @return the array of id numbers from the ref. */ public int[] ids() { - return ids; + return ids; } /** * Determine whether this is a new style ref. - * + * * @return true if this ref is a new style ref, false otherwise. */ public boolean isNewRef() { - return ids.length > 1; + return ids.length > 1; } /** * Get the creation number from the ref. - * + * * @return the creation number from the ref. */ public int creation() { - return creation; + return creation; } /** * Get the node name from the ref. - * + * * @return the node name from the ref. */ public String node() { - return node; + return node; } /** * Get the string representation of the ref. Erlang refs are printed as * #Ref<node.id> - * + * * @return the string representation of the ref. */ @Override public String toString() { - String s = "#Ref<" + node; + String s = "#Ref<" + node; - for (int i = 0; i < ids.length; i++) { - s += "." + ids[i]; - } + for (int i = 0; i < ids.length; i++) { + s += "." + ids[i]; + } - s += ">"; + s += ">"; - return s; + return s; } /** * Convert this ref to the equivalent Erlang external representation. - * + * * @param buf - * an output stream to which the encoded ref should be - * written. + * an output stream to which the encoded ref should be written. */ @Override public void encode(final OtpOutputStream buf) { - buf.write_ref(node, ids, creation); + buf.write_ref(node, ids, creation); } /** * Determine if two refs are equal. Refs are equal if their components are * equal. New refs and old refs are considered equal if the node, creation * and first id numnber are equal. - * + * * @param o - * the other ref to compare to. - * + * the other ref to compare to. + * * @return true if the refs are equal, false otherwise. */ @Override public boolean equals(final Object o) { - if (!(o instanceof OtpErlangRef)) { - return false; - } + if (!(o instanceof OtpErlangRef)) { + return false; + } - final OtpErlangRef ref = (OtpErlangRef) o; + final OtpErlangRef ref = (OtpErlangRef) o; - if (!(node.equals(ref.node()) && creation == ref.creation())) { - return false; - } + if (!(node.equals(ref.node()) && creation == ref.creation())) { + return false; + } - if (isNewRef() && ref.isNewRef()) { - return ids[0] == ref.ids[0] && ids[1] == ref.ids[1] - && ids[2] == ref.ids[2]; - } - return ids[0] == ref.ids[0]; + if (isNewRef() && ref.isNewRef()) { + return ids[0] == ref.ids[0] && ids[1] == ref.ids[1] + && ids[2] == ref.ids[2]; + } + return ids[0] == ref.ids[0]; } /** @@ -245,18 +242,18 @@ public class OtpErlangRef extends OtpErlangObject { @Override protected int doHashCode() { - OtpErlangObject.Hash hash = new OtpErlangObject.Hash(7); - hash.combine(creation, ids[0]); - if (isNewRef()) { - hash.combine(ids[1], ids[2]); - } - return hash.valueOf(); + final OtpErlangObject.Hash hash = new OtpErlangObject.Hash(7); + hash.combine(creation, ids[0]); + if (isNewRef()) { + hash.combine(ids[1], ids[2]); + } + return hash.valueOf(); } - + @Override public Object clone() { - final OtpErlangRef newRef = (OtpErlangRef) super.clone(); - newRef.ids = ids.clone(); - return newRef; + final OtpErlangRef newRef = (OtpErlangRef) super.clone(); + newRef.ids = ids.clone(); + return newRef; } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangShort.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangShort.java index 6ef56defbd..0083066141 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangShort.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangShort.java @@ -1,24 +1,23 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; - /** * Provides a Java representation of Erlang integral types. */ @@ -28,34 +27,33 @@ public class OtpErlangShort extends OtpErlangLong { /** * Create an Erlang integer from the given value. - * + * * @param s - * the short value to use. + * the short value to use. */ public OtpErlangShort(final short s) { - super(s); + super(s); } /** * Create an Erlang integer from a stream containing an integer encoded in * Erlang external format. - * + * * @param buf - * the stream containing the encoded value. - * + * the stream containing the encoded value. + * * @exception OtpErlangDecodeException - * if the buffer does not contain a valid external - * representation of an Erlang integer. - * + * if the buffer does not contain a valid external + * representation of an Erlang integer. + * * @exception OtpErlangRangeException - * if the value is too large to be represented as a - * short. + * if the value is too large to be represented as a short. */ public OtpErlangShort(final OtpInputStream buf) - throws OtpErlangRangeException, OtpErlangDecodeException { - super(buf); + throws OtpErlangRangeException, OtpErlangDecodeException { + super(buf); - shortValue(); + shortValue(); } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangString.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangString.java index 1bccfcc567..9e5450ca75 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangString.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangString.java @@ -1,19 +1,19 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2012. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; @@ -33,75 +33,74 @@ public class OtpErlangString extends OtpErlangObject { * Create an Erlang string from the given string. */ public OtpErlangString(final String str) { - this.str = str; + this.str = str; } /** * Create an Erlang string from a list of integers. * * @throws OtpErlangException - * for non-proper and non-integer lists. + * for non-proper and non-integer lists. * @throws OtpErlangRangeException - * if an integer in the list is not - * a valid Unicode code point according to Erlang. + * if an integer in the list is not a valid Unicode code point + * according to Erlang. */ - public OtpErlangString(final OtpErlangList list) - throws OtpErlangException { - String s = list.stringValue(); - final int n = s.length(); - for (int i = 0; i < n; i = s.offsetByCodePoints(i, 1)) { - int cp = s.codePointAt(i); - if (! isValidCodePoint(cp)) { - throw new OtpErlangRangeException("Invalid CodePoint: " + cp); - } - } - str = s; + public OtpErlangString(final OtpErlangList list) throws OtpErlangException { + final String s = list.stringValue(); + final int n = s.length(); + for (int i = 0; i < n; i = s.offsetByCodePoints(i, 1)) { + final int cp = s.codePointAt(i); + if (!isValidCodePoint(cp)) { + throw new OtpErlangRangeException("Invalid CodePoint: " + cp); + } + } + str = s; } /** * Create an Erlang string from a stream containing a string encoded in * Erlang external format. - * + * * @param buf * the stream containing the encoded string. - * + * * @exception OtpErlangDecodeException * if the buffer does not contain a valid external * representation of an Erlang string. */ public OtpErlangString(final OtpInputStream buf) - throws OtpErlangDecodeException { - str = buf.read_string(); + throws OtpErlangDecodeException { + str = buf.read_string(); } /** * Get the actual string contained in this object. - * + * * @return the raw string contained in this object, without regard to Erlang * quoting rules. - * + * * @see #toString */ public String stringValue() { - return str; + return str; } /** * Get the printable version of the string contained in this object. - * + * * @return the string contained in this object, quoted. - * + * * @see #stringValue */ @Override public String toString() { - return "\"" + str + "\""; + return "\"" + str + "\""; } /** * Convert this string to the equivalent Erlang external representation. - * + * * @param buf * an output stream to which the encoded string should be * written. @@ -109,48 +108,48 @@ public class OtpErlangString extends OtpErlangObject { @Override public void encode(final OtpOutputStream buf) { - buf.write_string(str); + buf.write_string(str); } /** * Determine if two strings are equal. They are equal if they represent the * same sequence of characters. This method can be used to compare * OtpErlangStrings with each other and with Strings. - * + * * @param o * the OtpErlangString or String to compare to. - * + * * @return true if the strings consist of the same sequence of characters, * false otherwise. */ @Override public boolean equals(final Object o) { - if (o instanceof String) { - return str.compareTo((String) o) == 0; - } else if (o instanceof OtpErlangString) { - return str.compareTo(((OtpErlangString) o).str) == 0; - } + if (o instanceof String) { + return str.compareTo((String) o) == 0; + } else if (o instanceof OtpErlangString) { + return str.compareTo(((OtpErlangString) o).str) == 0; + } - return false; + return false; } - + @Override protected int doHashCode() { - return str.hashCode(); + return str.hashCode(); } /** * Create Unicode code points from a String. - * - * @param s - * a String to convert to an Unicode code point array * - * @return the corresponding array of integers representing - * Unicode code points + * @param s + * a String to convert to an Unicode code point array + * + * @return the corresponding array of integers representing Unicode code + * points */ - public static int[] stringToCodePoints(final String s) { + public static int[] stringToCodePoints(final String s) { final int m = s.codePointCount(0, s.length()); final int[] codePoints = new int[m]; int j = 0; @@ -163,34 +162,34 @@ public class OtpErlangString extends OtpErlangObject { } /** - * Validate a code point according to Erlang definition; Unicode 3.0. - * That is; valid in the range U+0..U+10FFFF, but not in the range - * U+D800..U+DFFF (surrogat pairs). + * Validate a code point according to Erlang definition; Unicode 3.0. That + * is; valid in the range U+0..U+10FFFF, but not in the range U+D800..U+DFFF + * (surrogat pairs). * - * @param cp - * the code point value to validate + * @param cp + * the code point value to validate * - * @return true if the code point is valid, - * false otherwise. + * @return true if the code point is valid, false otherwise. */ public static boolean isValidCodePoint(final int cp) { - // Erlang definition of valid Unicode code points; - // Unicode 3.0, XML, et.al. - return (cp>>>16) <= 0x10 // in 0..10FFFF; Unicode range - && (cp & ~0x7FF) != 0xD800; // not in D800..DFFF; surrogate range + // Erlang definition of valid Unicode code points; + // Unicode 3.0, XML, et.al. + return cp >>> 16 <= 0x10 // in 0..10FFFF; Unicode range + && (cp & ~0x7FF) != 0xD800; // not in D800..DFFF; surrogate + // range } /** - * Construct a String from a Latin-1 (ISO-8859-1) encoded byte array, - * if Latin-1 is available, otherwise use the default encoding. + * Construct a String from a Latin-1 (ISO-8859-1) encoded byte array, if + * Latin-1 is available, otherwise use the default encoding. * */ public static String newString(final byte[] bytes) { - try { - return new String(bytes, "ISO-8859-1"); - } catch (final UnsupportedEncodingException e) { - } - return new String(bytes); + try { + return new String(bytes, "ISO-8859-1"); + } catch (final UnsupportedEncodingException e) { + } + return new String(bytes); } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangTuple.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangTuple.java index dffaa530cd..ef0a453de1 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangTuple.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangTuple.java @@ -1,28 +1,27 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2013. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; - /** * Provides a Java representation of Erlang tuples. Tuples are created from one * or more arbitrary Erlang terms. - * + * * <p> * The arity of the tuple is the number of elements it contains. Elements are * indexed from 0 to (arity-1) and can be retrieved individually by using the @@ -38,222 +37,248 @@ public class OtpErlangTuple extends OtpErlangObject { /** * Create a unary tuple containing the given element. - * + * * @param elem - * the element to create the tuple from. - * + * the element to create the tuple from. + * * @exception java.lang.IllegalArgumentException - * if the element is null. + * if the element is null. */ public OtpErlangTuple(final OtpErlangObject elem) { - if (elem == null) { - throw new java.lang.IllegalArgumentException( - "Tuple element cannot be null"); - } - elems = new OtpErlangObject[] { elem }; + if (elem == null) { + throw new java.lang.IllegalArgumentException( + "Tuple element cannot be null"); + } + elems = new OtpErlangObject[] { elem }; } /** * Create a tuple from an array of terms. - * + * * @param elems - * the array of terms to create the tuple from. - * + * the array of terms to create the tuple from. + * * @exception java.lang.IllegalArgumentException - * if the array is empty (null) or contains null - * elements. + * if the array is empty (null) or contains null elements. */ public OtpErlangTuple(final OtpErlangObject[] elems) { - this(elems, 0, elems.length); + this(elems, 0, elems.length); } /** * Create a tuple from an array of terms. - * + * * @param elems - * the array of terms to create the tuple from. + * the array of terms to create the tuple from. * @param start - * the offset of the first term to insert. + * the offset of the first term to insert. * @param count - * the number of terms to insert. - * + * the number of terms to insert. + * * @exception java.lang.IllegalArgumentException - * if the array is empty (null) or contains null - * elements. + * if the array is empty (null) or contains null elements. */ public OtpErlangTuple(final OtpErlangObject[] elems, final int start, - final int count) { - if (elems == null) { - throw new java.lang.IllegalArgumentException( - "Tuple content can't be null"); - } else if (count < 1) { - this.elems = NO_ELEMENTS; - } else { - this.elems = new OtpErlangObject[count]; - for (int i = 0; i < count; i++) { - if (elems[start + i] != null) { - this.elems[i] = elems[start + i]; - } else { - throw new java.lang.IllegalArgumentException( - "Tuple element cannot be null (element" - + (start + i) + ")"); - } - } - } + final int count) { + if (elems == null) { + throw new java.lang.IllegalArgumentException( + "Tuple content can't be null"); + } else if (count < 1) { + this.elems = NO_ELEMENTS; + } else { + this.elems = new OtpErlangObject[count]; + for (int i = 0; i < count; i++) { + if (elems[start + i] != null) { + this.elems[i] = elems[start + i]; + } else { + throw new java.lang.IllegalArgumentException( + "Tuple element cannot be null (element" + + (start + i) + ")"); + } + } + } } /** * Create a tuple from a stream containing an tuple encoded in Erlang * external format. - * + * * @param buf - * the stream containing the encoded tuple. - * + * the stream containing the encoded tuple. + * * @exception OtpErlangDecodeException - * if the buffer does not contain a valid external - * representation of an Erlang tuple. + * if the buffer does not contain a valid external + * representation of an Erlang tuple. */ public OtpErlangTuple(final OtpInputStream buf) - throws OtpErlangDecodeException { - final int arity = buf.read_tuple_head(); + throws OtpErlangDecodeException { + final int arity = buf.read_tuple_head(); - if (arity > 0) { - elems = new OtpErlangObject[arity]; + if (arity > 0) { + elems = new OtpErlangObject[arity]; - for (int i = 0; i < arity; i++) { - elems[i] = buf.read_any(); - } - } else { - elems = NO_ELEMENTS; - } + for (int i = 0; i < arity; i++) { + elems[i] = buf.read_any(); + } + } else { + elems = NO_ELEMENTS; + } } /** * Get the arity of the tuple. - * + * * @return the number of elements contained in the tuple. */ public int arity() { - return elems.length; + return elems.length; } /** * Get the specified element from the tuple. - * + * * @param i - * the index of the requested element. Tuple elements are - * numbered as array elements, starting at 0. - * + * the index of the requested element. Tuple elements are + * numbered as array elements, starting at 0. + * * @return the requested element, of null if i is not a valid element index. */ public OtpErlangObject elementAt(final int i) { - if (i >= arity() || i < 0) { - return null; - } - return elems[i]; + if (i >= arity() || i < 0) { + return null; + } + return elems[i]; } /** * Get all the elements from the tuple as an array. - * + * * @return an array containing all of the tuple's elements. */ public OtpErlangObject[] elements() { - final OtpErlangObject[] res = new OtpErlangObject[arity()]; - System.arraycopy(elems, 0, res, 0, res.length); - return res; + final OtpErlangObject[] res = new OtpErlangObject[arity()]; + System.arraycopy(elems, 0, res, 0, res.length); + return res; } /** * Get the string representation of the tuple. - * + * * @return the string representation of the tuple. */ @Override public String toString() { - int i; - final StringBuffer s = new StringBuffer(); - final int arity = elems.length; + int i; + final StringBuffer s = new StringBuffer(); + final int arity = elems.length; - s.append("{"); + s.append("{"); - for (i = 0; i < arity; i++) { - if (i > 0) { - s.append(","); - } - s.append(elems[i].toString()); - } + for (i = 0; i < arity; i++) { + if (i > 0) { + s.append(","); + } + s.append(elems[i].toString()); + } - s.append("}"); + s.append("}"); - return s.toString(); + return s.toString(); } /** * Convert this tuple to the equivalent Erlang external representation. - * + * * @param buf - * an output stream to which the encoded tuple should be - * written. + * an output stream to which the encoded tuple should be written. */ @Override public void encode(final OtpOutputStream buf) { - final int arity = elems.length; + final int arity = elems.length; - buf.write_tuple_head(arity); + buf.write_tuple_head(arity); - for (int i = 0; i < arity; i++) { - buf.write_any(elems[i]); - } + for (int i = 0; i < arity; i++) { + buf.write_any(elems[i]); + } } /** * Determine if two tuples are equal. Tuples are equal if they have the same * arity and all of the elements are equal. - * + * * @param o - * the tuple to compare to. - * + * the tuple to compare to. + * * @return true if the tuples have the same arity and all the elements are * equal. */ @Override public boolean equals(final Object o) { - if (!(o instanceof OtpErlangTuple)) { - return false; - } + if (!(o instanceof OtpErlangTuple)) { + return false; + } - final OtpErlangTuple t = (OtpErlangTuple) o; - final int a = arity(); + final OtpErlangTuple t = (OtpErlangTuple) o; + final int a = arity(); - if (a != t.arity()) { - return false; - } + if (a != t.arity()) { + return false; + } - for (int i = 0; i < a; i++) { - if (!elems[i].equals(t.elems[i])) { - return false; // early exit - } - } + for (int i = 0; i < a; i++) { + if (!elems[i].equals(t.elems[i])) { + return false; // early exit + } + } - return true; + return true; } - + + @Override + public <T> boolean match(final OtpErlangObject term, final T bindings) { + if (!(term instanceof OtpErlangTuple)) { + return false; + } + final OtpErlangTuple t = (OtpErlangTuple) term; + final int a = elems.length; + if (a != t.elems.length) { + return false; + } + for (int i = 0; i < a; i++) { + if (!elems[i].match(t.elems[i], bindings)) { + return false; + } + } + return true; + } + + @Override + public <T> OtpErlangObject bind(final T binds) throws OtpErlangException { + final OtpErlangTuple tuple = (OtpErlangTuple) this.clone(); + final int a = tuple.elems.length; + for (int i = 0; i < a; i++) { + final OtpErlangObject e = tuple.elems[i]; + tuple.elems[i] = e.bind(binds); + } + return tuple; + } + @Override protected int doHashCode() { - OtpErlangObject.Hash hash = new OtpErlangObject.Hash(9); - final int a = arity(); - hash.combine(a); - for (int i = 0; i < a; i++) { - hash.combine(elems[i].hashCode()); - } - return hash.valueOf(); + final OtpErlangObject.Hash hash = new OtpErlangObject.Hash(9); + final int a = arity(); + hash.combine(a); + for (int i = 0; i < a; i++) { + hash.combine(elems[i].hashCode()); + } + return hash.valueOf(); } - + @Override public Object clone() { - final OtpErlangTuple newTuple = (OtpErlangTuple) super.clone(); - newTuple.elems = elems.clone(); - return newTuple; + final OtpErlangTuple newTuple = (OtpErlangTuple) super.clone(); + newTuple.elems = elems.clone(); + return newTuple; } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangUInt.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangUInt.java index a02996e437..f45cce87b2 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangUInt.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangUInt.java @@ -1,24 +1,23 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; - /** * Provides a Java representation of Erlang integral types. */ @@ -28,38 +27,38 @@ public class OtpErlangUInt extends OtpErlangLong { /** * Create an Erlang integer from the given value. - * + * * @param i - * the non-negative int value to use. - * + * the non-negative int value to use. + * * @exception OtpErlangRangeException - * if the value is negative. + * if the value is negative. */ public OtpErlangUInt(final int i) throws OtpErlangRangeException { - super(i); + super(i); - uIntValue(); + uIntValue(); } /** * Create an Erlang integer from a stream containing an integer encoded in * Erlang external format. - * + * * @param buf - * the stream containing the encoded value. - * + * the stream containing the encoded value. + * * @exception OtpErlangDecodeException - * if the buffer does not contain a valid external - * representation of an Erlang integer. - * + * if the buffer does not contain a valid external + * representation of an Erlang integer. + * * @exception OtpErlangRangeException - * if the value is too large to be represented as an int, - * or the value is negative. + * if the value is too large to be represented as an int, or + * the value is negative. */ public OtpErlangUInt(final OtpInputStream buf) - throws OtpErlangRangeException, OtpErlangDecodeException { - super(buf); + throws OtpErlangRangeException, OtpErlangDecodeException { + super(buf); - uIntValue(); + uIntValue(); } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangUShort.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangUShort.java index e9d251f815..96f6ed807b 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangUShort.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangUShort.java @@ -1,24 +1,23 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; - /** * Provides a Java representation of Erlang integral types. */ @@ -28,38 +27,38 @@ public class OtpErlangUShort extends OtpErlangLong { /** * Create an Erlang integer from the given value. - * + * * @param s - * the non-negative short value to use. - * + * the non-negative short value to use. + * * @exception OtpErlangRangeException - * if the value is negative. + * if the value is negative. */ public OtpErlangUShort(final short s) throws OtpErlangRangeException { - super(s); + super(s); - uShortValue(); + uShortValue(); } /** * Create an Erlang integer from a stream containing an integer encoded in * Erlang external format. - * + * * @param buf - * the stream containing the encoded value. - * + * the stream containing the encoded value. + * * @exception OtpErlangDecodeException - * if the buffer does not contain a valid external - * representation of an Erlang integer. - * + * if the buffer does not contain a valid external + * representation of an Erlang integer. + * * @exception OtpErlangRangeException - * if the value is too large to be represented as a - * short, or the value is negative. + * if the value is too large to be represented as a short, or + * the value is negative. */ public OtpErlangUShort(final OtpInputStream buf) - throws OtpErlangRangeException, OtpErlangDecodeException { - super(buf); + throws OtpErlangRangeException, OtpErlangDecodeException { + super(buf); - uShortValue(); + uShortValue(); } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpException.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpException.java index 874c7da104..0a8323c635 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpException.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpException.java @@ -1,19 +1,19 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; @@ -28,13 +28,13 @@ public abstract class OtpException extends Exception { * Provides no message. */ public OtpException() { - super(); + super(); } /** * Provides a detailed message. */ public OtpException(final String msg) { - super(msg); + super(msg); } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpExternal.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpExternal.java index fa0fe18e95..eeb40462dc 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpExternal.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpExternal.java @@ -1,19 +1,19 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2013. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpInputStream.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpInputStream.java index bab0629382..2762c83494 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpInputStream.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpInputStream.java @@ -1,19 +1,19 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2013. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; @@ -25,7 +25,7 @@ import java.util.Arrays; /** * Provides a stream for decoding Erlang terms from external format. - * + * * <p> * Note that this class is not synchronized, if you need synchronization you * must provide it yourself. @@ -40,1211 +40,1211 @@ public class OtpInputStream extends ByteArrayInputStream { * @param buf */ public OtpInputStream(final byte[] buf) { - this(buf, 0); + this(buf, 0); } /** * Create a stream from a buffer containing encoded Erlang terms. - * + * * @param flags */ public OtpInputStream(final byte[] buf, final int flags) { - super(buf); - this.flags = flags; + super(buf); + this.flags = flags; } /** * Create a stream from a buffer containing encoded Erlang terms at the * given offset and length. - * + * * @param flags */ public OtpInputStream(final byte[] buf, final int offset, final int length, - final int flags) { - super(buf, offset, length); - this.flags = flags; + final int flags) { + super(buf, offset, length); + this.flags = flags; } /** * Get the current position in the stream. - * + * * @return the current position in the stream. */ public int getPos() { - return super.pos; + return super.pos; } /** * Set the current position in the stream. - * + * * @param pos * the position to move to in the stream. If pos indicates a * position beyond the end of the stream, the position is move to * the end of the stream instead. If pos is negative, the * position is moved to the beginning of the stream instead. - * + * * @return the previous position in the stream. */ public int setPos(final int pos) { - final int oldpos = super.pos; + final int oldpos = super.pos; - int apos = pos; - if (pos > super.count) { - apos = super.count; - } else if (pos < 0) { - apos = 0; - } + int apos = pos; + if (pos > super.count) { + apos = super.count; + } else if (pos < 0) { + apos = 0; + } - super.pos = apos; + super.pos = apos; - return oldpos; + return oldpos; } /** * Read an array of bytes from the stream. The method reads at most * buf.length bytes from the input stream. - * + * * @return the number of bytes read. - * + * * @exception OtpErlangDecodeException * if the next byte cannot be read. */ public int readN(final byte[] abuf) throws OtpErlangDecodeException { - return this.readN(abuf, 0, abuf.length); + return this.readN(abuf, 0, abuf.length); } /** * Read an array of bytes from the stream. The method reads at most len * bytes from the input stream into offset off of the buffer. - * + * * @return the number of bytes read. - * + * * @exception OtpErlangDecodeException * if the next byte cannot be read. */ public int readN(final byte[] abuf, final int off, final int len) - throws OtpErlangDecodeException { - if (len == 0 && available() == 0) { - return 0; - } - final int i = super.read(abuf, off, len); - if (i < 0) { - throw new OtpErlangDecodeException("Cannot read from input stream"); - } - return i; + throws OtpErlangDecodeException { + if (len == 0 && available() == 0) { + return 0; + } + final int i = super.read(abuf, off, len); + if (i < 0) { + throw new OtpErlangDecodeException("Cannot read from input stream"); + } + return i; } /** * Alias for peek1() */ public int peek() throws OtpErlangDecodeException { - return peek1(); + return peek1(); } /** * Look ahead one position in the stream without consuming the byte found * there. - * + * * @return the next byte in the stream, as an integer. - * + * * @exception OtpErlangDecodeException * if the next byte cannot be read. */ public int peek1() throws OtpErlangDecodeException { - int i; - try { - i = super.buf[super.pos]; - if (i < 0) { - i += 256; - } - - return i; - } catch (final Exception e) { - throw new OtpErlangDecodeException("Cannot read from input stream"); - } + int i; + try { + i = super.buf[super.pos]; + if (i < 0) { + i += 256; + } + + return i; + } catch (final Exception e) { + throw new OtpErlangDecodeException("Cannot read from input stream"); + } } public int peek1skip_version() throws OtpErlangDecodeException { - int i = peek1(); - if (i == OtpExternal.versionTag) { - read1(); - i = peek1(); - } - return i; + int i = peek1(); + if (i == OtpExternal.versionTag) { + read1(); + i = peek1(); + } + return i; } /** * Read a one byte integer from the stream. - * + * * @return the byte read, as an integer. - * + * * @exception OtpErlangDecodeException * if the next byte cannot be read. */ public int read1() throws OtpErlangDecodeException { - int i; - i = super.read(); + int i; + i = super.read(); - if (i < 0) { - throw new OtpErlangDecodeException("Cannot read from input stream"); - } + if (i < 0) { + throw new OtpErlangDecodeException("Cannot read from input stream"); + } - return i; + return i; } public int read1skip_version() throws OtpErlangDecodeException { - int tag = read1(); - if (tag == OtpExternal.versionTag) { - tag = read1(); - } - return tag; + int tag = read1(); + if (tag == OtpExternal.versionTag) { + tag = read1(); + } + return tag; } /** * Read a two byte big endian integer from the stream. - * + * * @return the bytes read, converted from big endian to an integer. - * + * * @exception OtpErlangDecodeException * if the next byte cannot be read. */ public int read2BE() throws OtpErlangDecodeException { - final byte[] b = new byte[2]; - try { - super.read(b); - } catch (final IOException e) { - throw new OtpErlangDecodeException("Cannot read from input stream"); - } - return (b[0] << 8 & 0xff00) + (b[1] & 0xff); + final byte[] b = new byte[2]; + try { + super.read(b); + } catch (final IOException e) { + throw new OtpErlangDecodeException("Cannot read from input stream"); + } + return (b[0] << 8 & 0xff00) + (b[1] & 0xff); } /** * Read a four byte big endian integer from the stream. - * + * * @return the bytes read, converted from big endian to an integer. - * + * * @exception OtpErlangDecodeException * if the next byte cannot be read. */ public int read4BE() throws OtpErlangDecodeException { - final byte[] b = new byte[4]; - try { - super.read(b); - } catch (final IOException e) { - throw new OtpErlangDecodeException("Cannot read from input stream"); - } - return (b[0] << 24 & 0xff000000) + (b[1] << 16 & 0xff0000) - + (b[2] << 8 & 0xff00) + (b[3] & 0xff); + final byte[] b = new byte[4]; + try { + super.read(b); + } catch (final IOException e) { + throw new OtpErlangDecodeException("Cannot read from input stream"); + } + return (b[0] << 24 & 0xff000000) + (b[1] << 16 & 0xff0000) + + (b[2] << 8 & 0xff00) + (b[3] & 0xff); } /** * Read a two byte little endian integer from the stream. - * + * * @return the bytes read, converted from little endian to an integer. - * + * * @exception OtpErlangDecodeException * if the next byte cannot be read. */ public int read2LE() throws OtpErlangDecodeException { - final byte[] b = new byte[2]; - try { - super.read(b); - } catch (final IOException e) { - throw new OtpErlangDecodeException("Cannot read from input stream"); - } - return (b[1] << 8 & 0xff00) + (b[0] & 0xff); + final byte[] b = new byte[2]; + try { + super.read(b); + } catch (final IOException e) { + throw new OtpErlangDecodeException("Cannot read from input stream"); + } + return (b[1] << 8 & 0xff00) + (b[0] & 0xff); } /** * Read a four byte little endian integer from the stream. - * + * * @return the bytes read, converted from little endian to an integer. - * + * * @exception OtpErlangDecodeException * if the next byte cannot be read. */ public int read4LE() throws OtpErlangDecodeException { - final byte[] b = new byte[4]; - try { - super.read(b); - } catch (final IOException e) { - throw new OtpErlangDecodeException("Cannot read from input stream"); - } - return (b[3] << 24 & 0xff000000) + (b[2] << 16 & 0xff0000) - + (b[1] << 8 & 0xff00) + (b[0] & 0xff); + final byte[] b = new byte[4]; + try { + super.read(b); + } catch (final IOException e) { + throw new OtpErlangDecodeException("Cannot read from input stream"); + } + return (b[3] << 24 & 0xff000000) + (b[2] << 16 & 0xff0000) + + (b[1] << 8 & 0xff00) + (b[0] & 0xff); } /** * Read a little endian integer from the stream. - * + * * @param n * the number of bytes to read - * + * * @return the bytes read, converted from little endian to an integer. - * + * * @exception OtpErlangDecodeException * if the next byte cannot be read. */ public long readLE(final int n) throws OtpErlangDecodeException { - final byte[] b = new byte[n]; - try { - super.read(b); - } catch (final IOException e) { - throw new OtpErlangDecodeException("Cannot read from input stream"); - } - long v = 0; - int i = n; - while (i-- > 0) { - v = v << 8 | (long) b[i] & 0xff; - } - return v; + final byte[] b = new byte[n]; + try { + super.read(b); + } catch (final IOException e) { + throw new OtpErlangDecodeException("Cannot read from input stream"); + } + long v = 0; + int i = n; + while (i-- > 0) { + v = v << 8 | (long) b[i] & 0xff; + } + return v; } /** * Read a bigendian integer from the stream. - * + * * @param n * the number of bytes to read - * + * * @return the bytes read, converted from big endian to an integer. - * + * * @exception OtpErlangDecodeException * if the next byte cannot be read. */ public long readBE(final int n) throws OtpErlangDecodeException { - final byte[] b = new byte[n]; - try { - super.read(b); - } catch (final IOException e) { - throw new OtpErlangDecodeException("Cannot read from input stream"); - } - long v = 0; - for (int i = 0; i < n; i++) { - v = v << 8 | (long) b[i] & 0xff; - } - return v; + final byte[] b = new byte[n]; + try { + super.read(b); + } catch (final IOException e) { + throw new OtpErlangDecodeException("Cannot read from input stream"); + } + long v = 0; + for (int i = 0; i < n; i++) { + v = v << 8 | (long) b[i] & 0xff; + } + return v; } /** * Read an Erlang atom from the stream and interpret the value as a boolean. - * + * * @return true if the atom at the current position in the stream contains * the value 'true' (ignoring case), false otherwise. - * + * * @exception OtpErlangDecodeException * if the next term in the stream is not an atom. */ public boolean read_boolean() throws OtpErlangDecodeException { - return Boolean.valueOf(read_atom()).booleanValue(); + return Boolean.valueOf(read_atom()).booleanValue(); } /** * Read an Erlang atom from the stream. - * + * * @return a String containing the value of the atom. - * + * * @exception OtpErlangDecodeException * if the next term in the stream is not an atom. */ @SuppressWarnings("fallthrough") public String read_atom() throws OtpErlangDecodeException { - int tag; - int len = -1; - byte[] strbuf; - String atom; - - tag = read1skip_version(); - - switch (tag) { - - case OtpExternal.atomTag: - len = read2BE(); - strbuf = new byte[len]; - this.readN(strbuf); - try { - atom = new String(strbuf, "ISO-8859-1"); - } catch (final java.io.UnsupportedEncodingException e) { - throw new OtpErlangDecodeException( - "Failed to decode ISO-8859-1 atom"); - } - if (atom.length() > OtpExternal.maxAtomLength) { - /* - * Throwing an exception would be better I think, - * but truncation seems to be the way it has - * been done in other parts of OTP... - */ - atom = atom.substring(0, OtpExternal.maxAtomLength); - } - break; - - case OtpExternal.smallAtomUtf8Tag: - len = read1(); - // fall-through - case OtpExternal.atomUtf8Tag: - if (len < 0) { - len = read2BE(); - } - strbuf = new byte[len]; - this.readN(strbuf); - try { - atom = new String(strbuf, "UTF-8"); - } catch (final java.io.UnsupportedEncodingException e) { - throw new OtpErlangDecodeException( - "Failed to decode UTF-8 atom"); - } - if (atom.codePointCount(0, atom.length()) > OtpExternal.maxAtomLength) { - /* - * Throwing an exception would be better I think, - * but truncation seems to be the way it has - * been done in other parts of OTP... - */ - final int[] cps = OtpErlangString.stringToCodePoints(atom); - atom = new String(cps, 0, OtpExternal.maxAtomLength); - } - break; - - default: - throw new OtpErlangDecodeException( - "wrong tag encountered, expected " + OtpExternal.atomTag - + ", or " + OtpExternal.atomUtf8Tag + ", got " + tag); - } - - return atom; + int tag; + int len = -1; + byte[] strbuf; + String atom; + + tag = read1skip_version(); + + switch (tag) { + + case OtpExternal.atomTag: + len = read2BE(); + strbuf = new byte[len]; + this.readN(strbuf); + try { + atom = new String(strbuf, "ISO-8859-1"); + } catch (final java.io.UnsupportedEncodingException e) { + throw new OtpErlangDecodeException( + "Failed to decode ISO-8859-1 atom"); + } + if (atom.length() > OtpExternal.maxAtomLength) { + /* + * Throwing an exception would be better I think, but truncation + * seems to be the way it has been done in other parts of OTP... + */ + atom = atom.substring(0, OtpExternal.maxAtomLength); + } + break; + + case OtpExternal.smallAtomUtf8Tag: + len = read1(); + // fall-through + case OtpExternal.atomUtf8Tag: + if (len < 0) { + len = read2BE(); + } + strbuf = new byte[len]; + this.readN(strbuf); + try { + atom = new String(strbuf, "UTF-8"); + } catch (final java.io.UnsupportedEncodingException e) { + throw new OtpErlangDecodeException( + "Failed to decode UTF-8 atom"); + } + if (atom.codePointCount(0, atom.length()) > OtpExternal.maxAtomLength) { + /* + * Throwing an exception would be better I think, but truncation + * seems to be the way it has been done in other parts of OTP... + */ + final int[] cps = OtpErlangString.stringToCodePoints(atom); + atom = new String(cps, 0, OtpExternal.maxAtomLength); + } + break; + + default: + throw new OtpErlangDecodeException( + "wrong tag encountered, expected " + OtpExternal.atomTag + + ", or " + OtpExternal.atomUtf8Tag + ", got " + + tag); + } + + return atom; } /** * Read an Erlang binary from the stream. - * + * * @return a byte array containing the value of the binary. - * + * * @exception OtpErlangDecodeException * if the next term in the stream is not a binary. */ public byte[] read_binary() throws OtpErlangDecodeException { - int tag; - int len; - byte[] bin; + int tag; + int len; + byte[] bin; - tag = read1skip_version(); + tag = read1skip_version(); - if (tag != OtpExternal.binTag) { - throw new OtpErlangDecodeException( - "Wrong tag encountered, expected " + OtpExternal.binTag - + ", got " + tag); - } + if (tag != OtpExternal.binTag) { + throw new OtpErlangDecodeException( + "Wrong tag encountered, expected " + OtpExternal.binTag + + ", got " + tag); + } - len = read4BE(); + len = read4BE(); - bin = new byte[len]; - this.readN(bin); + bin = new byte[len]; + this.readN(bin); - return bin; + return bin; } /** * Read an Erlang bitstr from the stream. - * + * * @param pad_bits * an int array whose first element will be set to the number of * pad bits in the last byte. - * + * * @return a byte array containing the value of the bitstr. - * + * * @exception OtpErlangDecodeException * if the next term in the stream is not a bitstr. */ public byte[] read_bitstr(final int pad_bits[]) - throws OtpErlangDecodeException { - int tag; - int len; - byte[] bin; - - tag = read1skip_version(); - - if (tag != OtpExternal.bitBinTag) { - throw new OtpErlangDecodeException( - "Wrong tag encountered, expected " + OtpExternal.bitBinTag - + ", got " + tag); - } - - len = read4BE(); - bin = new byte[len]; - final int tail_bits = read1(); - if (tail_bits < 0 || 7 < tail_bits) { - throw new OtpErlangDecodeException( - "Wrong tail bit count in bitstr: " + tail_bits); - } - if (len == 0 && tail_bits != 0) { - throw new OtpErlangDecodeException( - "Length 0 on bitstr with tail bit count: " + tail_bits); - } - this.readN(bin); - - pad_bits[0] = 8 - tail_bits; - return bin; + throws OtpErlangDecodeException { + int tag; + int len; + byte[] bin; + + tag = read1skip_version(); + + if (tag != OtpExternal.bitBinTag) { + throw new OtpErlangDecodeException( + "Wrong tag encountered, expected " + OtpExternal.bitBinTag + + ", got " + tag); + } + + len = read4BE(); + bin = new byte[len]; + final int tail_bits = read1(); + if (tail_bits < 0 || 7 < tail_bits) { + throw new OtpErlangDecodeException( + "Wrong tail bit count in bitstr: " + tail_bits); + } + if (len == 0 && tail_bits != 0) { + throw new OtpErlangDecodeException( + "Length 0 on bitstr with tail bit count: " + tail_bits); + } + this.readN(bin); + + pad_bits[0] = 8 - tail_bits; + return bin; } /** * Read an Erlang float from the stream. - * + * * @return the float value. - * + * * @exception OtpErlangDecodeException * if the next term in the stream is not a float. */ public float read_float() throws OtpErlangDecodeException { - final double d = read_double(); - return (float) d; + final double d = read_double(); + return (float) d; } /** * Read an Erlang float from the stream. - * + * * @return the float value, as a double. - * + * * @exception OtpErlangDecodeException * if the next term in the stream is not a float. */ public double read_double() throws OtpErlangDecodeException { - int tag; - - // parse the stream - tag = read1skip_version(); - - switch (tag) { - case OtpExternal.newFloatTag: { - return Double.longBitsToDouble(readBE(8)); - } - case OtpExternal.floatTag: { - BigDecimal val; - int epos; - int exp; - final byte[] strbuf = new byte[31]; - String str; - - // get the string - this.readN(strbuf); - str = OtpErlangString.newString(strbuf); - - // find the exponent prefix 'e' in the string - epos = str.indexOf('e', 0); - - if (epos < 0) { - throw new OtpErlangDecodeException("Invalid float format: '" - + str + "'"); - } - - // remove the sign from the exponent, if positive - String estr = str.substring(epos + 1).trim(); - - if (estr.substring(0, 1).equals("+")) { - estr = estr.substring(1); - } - - // now put the mantissa and exponent together - exp = Integer.valueOf(estr).intValue(); - val = new BigDecimal(str.substring(0, epos)).movePointRight(exp); - - return val.doubleValue(); - } - default: - throw new OtpErlangDecodeException( - "Wrong tag encountered, expected " - + OtpExternal.newFloatTag + ", got " + tag); - } + int tag; + + // parse the stream + tag = read1skip_version(); + + switch (tag) { + case OtpExternal.newFloatTag: { + return Double.longBitsToDouble(readBE(8)); + } + case OtpExternal.floatTag: { + BigDecimal val; + int epos; + int exp; + final byte[] strbuf = new byte[31]; + String str; + + // get the string + this.readN(strbuf); + str = OtpErlangString.newString(strbuf); + + // find the exponent prefix 'e' in the string + epos = str.indexOf('e', 0); + + if (epos < 0) { + throw new OtpErlangDecodeException("Invalid float format: '" + + str + "'"); + } + + // remove the sign from the exponent, if positive + String estr = str.substring(epos + 1).trim(); + + if (estr.substring(0, 1).equals("+")) { + estr = estr.substring(1); + } + + // now put the mantissa and exponent together + exp = Integer.valueOf(estr).intValue(); + val = new BigDecimal(str.substring(0, epos)).movePointRight(exp); + + return val.doubleValue(); + } + default: + throw new OtpErlangDecodeException( + "Wrong tag encountered, expected " + + OtpExternal.newFloatTag + ", got " + tag); + } } /** * Read one byte from the stream. - * + * * @return the byte read. - * + * * @exception OtpErlangDecodeException * if the next byte cannot be read. */ public byte read_byte() throws OtpErlangDecodeException { - final long l = this.read_long(false); - final byte i = (byte) l; + final long l = this.read_long(false); + final byte i = (byte) l; - if (l != i) { - throw new OtpErlangDecodeException("Value does not fit in byte: " - + l); - } + if (l != i) { + throw new OtpErlangDecodeException("Value does not fit in byte: " + + l); + } - return i; + return i; } /** * Read a character from the stream. - * + * * @return the character value. - * + * * @exception OtpErlangDecodeException * if the next term in the stream is not an integer that can * be represented as a char. */ public char read_char() throws OtpErlangDecodeException { - final long l = this.read_long(true); - final char i = (char) l; + final long l = this.read_long(true); + final char i = (char) l; - if (l != (i & 0xffffL)) { - throw new OtpErlangDecodeException("Value does not fit in char: " - + l); - } + if (l != (i & 0xffffL)) { + throw new OtpErlangDecodeException("Value does not fit in char: " + + l); + } - return i; + return i; } /** * Read an unsigned integer from the stream. - * + * * @return the integer value. - * + * * @exception OtpErlangDecodeException * if the next term in the stream can not be represented as a * positive integer. */ public int read_uint() throws OtpErlangDecodeException { - final long l = this.read_long(true); - final int i = (int) l; + final long l = this.read_long(true); + final int i = (int) l; - if (l != (i & 0xFFFFffffL)) { - throw new OtpErlangDecodeException("Value does not fit in uint: " - + l); - } + if (l != (i & 0xFFFFffffL)) { + throw new OtpErlangDecodeException("Value does not fit in uint: " + + l); + } - return i; + return i; } /** * Read an integer from the stream. - * + * * @return the integer value. - * + * * @exception OtpErlangDecodeException * if the next term in the stream can not be represented as * an integer. */ public int read_int() throws OtpErlangDecodeException { - final long l = this.read_long(false); - final int i = (int) l; + final long l = this.read_long(false); + final int i = (int) l; - if (l != i) { - throw new OtpErlangDecodeException("Value does not fit in int: " - + l); - } + if (l != i) { + throw new OtpErlangDecodeException("Value does not fit in int: " + + l); + } - return i; + return i; } /** * Read an unsigned short from the stream. - * + * * @return the short value. - * + * * @exception OtpErlangDecodeException * if the next term in the stream can not be represented as a * positive short. */ public short read_ushort() throws OtpErlangDecodeException { - final long l = this.read_long(true); - final short i = (short) l; + final long l = this.read_long(true); + final short i = (short) l; - if (l != (i & 0xffffL)) { - throw new OtpErlangDecodeException("Value does not fit in ushort: " - + l); - } + if (l != (i & 0xffffL)) { + throw new OtpErlangDecodeException("Value does not fit in ushort: " + + l); + } - return i; + return i; } /** * Read a short from the stream. - * + * * @return the short value. - * + * * @exception OtpErlangDecodeException * if the next term in the stream can not be represented as a * short. */ public short read_short() throws OtpErlangDecodeException { - final long l = this.read_long(false); - final short i = (short) l; + final long l = this.read_long(false); + final short i = (short) l; - if (l != i) { - throw new OtpErlangDecodeException("Value does not fit in short: " - + l); - } + if (l != i) { + throw new OtpErlangDecodeException("Value does not fit in short: " + + l); + } - return i; + return i; } /** * Read an unsigned long from the stream. - * + * * @return the long value. - * + * * @exception OtpErlangDecodeException * if the next term in the stream can not be represented as a * positive long. */ public long read_ulong() throws OtpErlangDecodeException { - return this.read_long(true); + return this.read_long(true); } /** * Read a long from the stream. - * + * * @return the long value. - * + * * @exception OtpErlangDecodeException * if the next term in the stream can not be represented as a * long. */ public long read_long() throws OtpErlangDecodeException { - return this.read_long(false); + return this.read_long(false); } public long read_long(final boolean unsigned) - throws OtpErlangDecodeException { - final byte[] b = read_integer_byte_array(); - return OtpInputStream.byte_array_to_long(b, unsigned); + throws OtpErlangDecodeException { + final byte[] b = read_integer_byte_array(); + return OtpInputStream.byte_array_to_long(b, unsigned); } /** * Read an integer from the stream. - * + * * @return the value as a big endian 2's complement byte array. - * + * * @exception OtpErlangDecodeException * if the next term in the stream is not an integer. */ public byte[] read_integer_byte_array() throws OtpErlangDecodeException { - int tag; - byte[] nb; - - tag = read1skip_version(); - - switch (tag) { - case OtpExternal.smallIntTag: - nb = new byte[2]; - nb[0] = 0; - nb[1] = (byte) read1(); - break; - - case OtpExternal.intTag: - nb = new byte[4]; - if (this.readN(nb) != 4) { // Big endian - throw new OtpErlangDecodeException( - "Cannot read from intput stream"); - } - break; - - case OtpExternal.smallBigTag: - case OtpExternal.largeBigTag: - int arity; - int sign; - if (tag == OtpExternal.smallBigTag) { - arity = read1(); - sign = read1(); - } else { - arity = read4BE(); - sign = read1(); - if (arity + 1 < 0) { - throw new OtpErlangDecodeException( - "Value of largeBig does not fit in BigInteger, arity " - + arity + " sign " + sign); - } - } - nb = new byte[arity + 1]; - // Value is read as little endian. The big end is augumented - // with one zero byte to make the value 2's complement positive. - if (this.readN(nb, 0, arity) != arity) { - throw new OtpErlangDecodeException( - "Cannot read from intput stream"); - } - // Reverse the array to make it big endian. - for (int i = 0, j = nb.length; i < j--; i++) { - // Swap [i] with [j] - final byte b = nb[i]; - nb[i] = nb[j]; - nb[j] = b; - } - if (sign != 0) { - // 2's complement negate the big endian value in the array - int c = 1; // Carry - for (int j = nb.length; j-- > 0;) { - c = (~nb[j] & 0xFF) + c; - nb[j] = (byte) c; - c >>= 8; - } - } - break; - - default: - throw new OtpErlangDecodeException("Not valid integer tag: " + tag); - } - - return nb; + int tag; + byte[] nb; + + tag = read1skip_version(); + + switch (tag) { + case OtpExternal.smallIntTag: + nb = new byte[2]; + nb[0] = 0; + nb[1] = (byte) read1(); + break; + + case OtpExternal.intTag: + nb = new byte[4]; + if (this.readN(nb) != 4) { // Big endian + throw new OtpErlangDecodeException( + "Cannot read from intput stream"); + } + break; + + case OtpExternal.smallBigTag: + case OtpExternal.largeBigTag: + int arity; + int sign; + if (tag == OtpExternal.smallBigTag) { + arity = read1(); + sign = read1(); + } else { + arity = read4BE(); + sign = read1(); + if (arity + 1 < 0) { + throw new OtpErlangDecodeException( + "Value of largeBig does not fit in BigInteger, arity " + + arity + " sign " + sign); + } + } + nb = new byte[arity + 1]; + // Value is read as little endian. The big end is augumented + // with one zero byte to make the value 2's complement positive. + if (this.readN(nb, 0, arity) != arity) { + throw new OtpErlangDecodeException( + "Cannot read from intput stream"); + } + // Reverse the array to make it big endian. + for (int i = 0, j = nb.length; i < j--; i++) { + // Swap [i] with [j] + final byte b = nb[i]; + nb[i] = nb[j]; + nb[j] = b; + } + if (sign != 0) { + // 2's complement negate the big endian value in the array + int c = 1; // Carry + for (int j = nb.length; j-- > 0;) { + c = (~nb[j] & 0xFF) + c; + nb[j] = (byte) c; + c >>= 8; + } + } + break; + + default: + throw new OtpErlangDecodeException("Not valid integer tag: " + tag); + } + + return nb; } public static long byte_array_to_long(final byte[] b, final boolean unsigned) - throws OtpErlangDecodeException { - long v; - switch (b.length) { - case 0: - v = 0; - break; - case 2: - v = ((b[0] & 0xFF) << 8) + (b[1] & 0xFF); - v = (short) v; // Sign extend - if (v < 0 && unsigned) { - throw new OtpErlangDecodeException("Value not unsigned: " + v); - } - break; - case 4: - v = ((b[0] & 0xFF) << 24) + ((b[1] & 0xFF) << 16) - + ((b[2] & 0xFF) << 8) + (b[3] & 0xFF); - v = (int) v; // Sign extend - if (v < 0 && unsigned) { - throw new OtpErlangDecodeException("Value not unsigned: " + v); - } - break; - default: - int i = 0; - final byte c = b[i]; - // Skip non-essential leading bytes - if (unsigned) { - if (c < 0) { - throw new OtpErlangDecodeException("Value not unsigned: " - + Arrays.toString(b)); - } - while (b[i] == 0) { - i++; // Skip leading zero sign bytes - } - } else { - if (c == 0 || c == -1) { // Leading sign byte - i = 1; - // Skip all leading sign bytes - while (i < b.length && b[i] == c) { - i++; - } - if (i < b.length) { - // Check first non-sign byte to see if its sign - // matches the whole number's sign. If not one more - // byte is needed to represent the value. - if (((c ^ b[i]) & 0x80) != 0) { - i--; - } - } - } - } - if (b.length - i > 8) { - // More than 64 bits of value - throw new OtpErlangDecodeException( - "Value does not fit in long: " + Arrays.toString(b)); - } - // Convert the necessary bytes - for (v = c < 0 ? -1 : 0; i < b.length; i++) { - v = v << 8 | b[i] & 0xFF; - } - } - return v; + throws OtpErlangDecodeException { + long v; + switch (b.length) { + case 0: + v = 0; + break; + case 2: + v = ((b[0] & 0xFF) << 8) + (b[1] & 0xFF); + v = (short) v; // Sign extend + if (v < 0 && unsigned) { + throw new OtpErlangDecodeException("Value not unsigned: " + v); + } + break; + case 4: + v = ((b[0] & 0xFF) << 24) + ((b[1] & 0xFF) << 16) + + ((b[2] & 0xFF) << 8) + (b[3] & 0xFF); + v = (int) v; // Sign extend + if (v < 0 && unsigned) { + throw new OtpErlangDecodeException("Value not unsigned: " + v); + } + break; + default: + int i = 0; + final byte c = b[i]; + // Skip non-essential leading bytes + if (unsigned) { + if (c < 0) { + throw new OtpErlangDecodeException("Value not unsigned: " + + Arrays.toString(b)); + } + while (b[i] == 0) { + i++; // Skip leading zero sign bytes + } + } else { + if (c == 0 || c == -1) { // Leading sign byte + i = 1; + // Skip all leading sign bytes + while (i < b.length && b[i] == c) { + i++; + } + if (i < b.length) { + // Check first non-sign byte to see if its sign + // matches the whole number's sign. If not one more + // byte is needed to represent the value. + if (((c ^ b[i]) & 0x80) != 0) { + i--; + } + } + } + } + if (b.length - i > 8) { + // More than 64 bits of value + throw new OtpErlangDecodeException( + "Value does not fit in long: " + Arrays.toString(b)); + } + // Convert the necessary bytes + for (v = c < 0 ? -1 : 0; i < b.length; i++) { + v = v << 8 | b[i] & 0xFF; + } + } + return v; } /** * Read a list header from the stream. - * + * * @return the arity of the list. - * + * * @exception OtpErlangDecodeException * if the next term in the stream is not a list. */ public int read_list_head() throws OtpErlangDecodeException { - int arity = 0; - final int tag = read1skip_version(); + int arity = 0; + final int tag = read1skip_version(); - switch (tag) { - case OtpExternal.nilTag: - arity = 0; - break; + switch (tag) { + case OtpExternal.nilTag: + arity = 0; + break; - case OtpExternal.stringTag: - arity = read2BE(); - break; + case OtpExternal.stringTag: + arity = read2BE(); + break; - case OtpExternal.listTag: - arity = read4BE(); - break; + case OtpExternal.listTag: + arity = read4BE(); + break; - default: - throw new OtpErlangDecodeException("Not valid list tag: " + tag); - } + default: + throw new OtpErlangDecodeException("Not valid list tag: " + tag); + } - return arity; + return arity; } /** * Read a tuple header from the stream. - * + * * @return the arity of the tuple. - * + * * @exception OtpErlangDecodeException * if the next term in the stream is not a tuple. */ public int read_tuple_head() throws OtpErlangDecodeException { - int arity = 0; - final int tag = read1skip_version(); + int arity = 0; + final int tag = read1skip_version(); - // decode the tuple header and get arity - switch (tag) { - case OtpExternal.smallTupleTag: - arity = read1(); - break; + // decode the tuple header and get arity + switch (tag) { + case OtpExternal.smallTupleTag: + arity = read1(); + break; - case OtpExternal.largeTupleTag: - arity = read4BE(); - break; + case OtpExternal.largeTupleTag: + arity = read4BE(); + break; - default: - throw new OtpErlangDecodeException("Not valid tuple tag: " + tag); - } + default: + throw new OtpErlangDecodeException("Not valid tuple tag: " + tag); + } - return arity; + return arity; } /** * Read an empty list from the stream. - * + * * @return zero (the arity of the list). - * + * * @exception OtpErlangDecodeException * if the next term in the stream is not an empty list. */ public int read_nil() throws OtpErlangDecodeException { - int arity = 0; - final int tag = read1skip_version(); + int arity = 0; + final int tag = read1skip_version(); - switch (tag) { - case OtpExternal.nilTag: - arity = 0; - break; + switch (tag) { + case OtpExternal.nilTag: + arity = 0; + break; - default: - throw new OtpErlangDecodeException("Not valid nil tag: " + tag); - } + default: + throw new OtpErlangDecodeException("Not valid nil tag: " + tag); + } - return arity; + return arity; } /** * Read an Erlang PID from the stream. - * + * * @return the value of the PID. - * + * * @exception OtpErlangDecodeException * if the next term in the stream is not an Erlang PID. */ public OtpErlangPid read_pid() throws OtpErlangDecodeException { - String node; - int id; - int serial; - int creation; - int tag; - - tag = read1skip_version(); - - if (tag != OtpExternal.pidTag) { - throw new OtpErlangDecodeException( - "Wrong tag encountered, expected " + OtpExternal.pidTag - + ", got " + tag); - } - - node = read_atom(); - id = read4BE() & 0x7fff; // 15 bits - serial = read4BE() & 0x1fff; // 13 bits - creation = read1() & 0x03; // 2 bits - - return new OtpErlangPid(node, id, serial, creation); + String node; + int id; + int serial; + int creation; + int tag; + + tag = read1skip_version(); + + if (tag != OtpExternal.pidTag) { + throw new OtpErlangDecodeException( + "Wrong tag encountered, expected " + OtpExternal.pidTag + + ", got " + tag); + } + + node = read_atom(); + id = read4BE() & 0x7fff; // 15 bits + serial = read4BE() & 0x1fff; // 13 bits + creation = read1() & 0x03; // 2 bits + + return new OtpErlangPid(node, id, serial, creation); } /** * Read an Erlang port from the stream. - * + * * @return the value of the port. - * + * * @exception OtpErlangDecodeException * if the next term in the stream is not an Erlang port. */ public OtpErlangPort read_port() throws OtpErlangDecodeException { - String node; - int id; - int creation; - int tag; + String node; + int id; + int creation; + int tag; - tag = read1skip_version(); + tag = read1skip_version(); - if (tag != OtpExternal.portTag) { - throw new OtpErlangDecodeException( - "Wrong tag encountered, expected " + OtpExternal.portTag - + ", got " + tag); - } + if (tag != OtpExternal.portTag) { + throw new OtpErlangDecodeException( + "Wrong tag encountered, expected " + OtpExternal.portTag + + ", got " + tag); + } - node = read_atom(); - id = read4BE() & 0xfffffff; // 28 bits - creation = read1() & 0x03; // 2 bits + node = read_atom(); + id = read4BE() & 0xfffffff; // 28 bits + creation = read1() & 0x03; // 2 bits - return new OtpErlangPort(node, id, creation); + return new OtpErlangPort(node, id, creation); } /** * Read an Erlang reference from the stream. - * + * * @return the value of the reference - * + * * @exception OtpErlangDecodeException * if the next term in the stream is not an Erlang reference. */ public OtpErlangRef read_ref() throws OtpErlangDecodeException { - String node; - int id; - int creation; - int tag; - - tag = read1skip_version(); - - switch (tag) { - case OtpExternal.refTag: - node = read_atom(); - id = read4BE() & 0x3ffff; // 18 bits - creation = read1() & 0x03; // 2 bits - return new OtpErlangRef(node, id, creation); - - case OtpExternal.newRefTag: - final int arity = read2BE(); - node = read_atom(); - creation = read1() & 0x03; // 2 bits - - final int[] ids = new int[arity]; - for (int i = 0; i < arity; i++) { - ids[i] = read4BE(); - } - ids[0] &= 0x3ffff; // first id gets truncated to 18 bits - return new OtpErlangRef(node, ids, creation); - - default: - throw new OtpErlangDecodeException( - "Wrong tag encountered, expected ref, got " + tag); - } + String node; + int id; + int creation; + int tag; + + tag = read1skip_version(); + + switch (tag) { + case OtpExternal.refTag: + node = read_atom(); + id = read4BE() & 0x3ffff; // 18 bits + creation = read1() & 0x03; // 2 bits + return new OtpErlangRef(node, id, creation); + + case OtpExternal.newRefTag: + final int arity = read2BE(); + node = read_atom(); + creation = read1() & 0x03; // 2 bits + + final int[] ids = new int[arity]; + for (int i = 0; i < arity; i++) { + ids[i] = read4BE(); + } + ids[0] &= 0x3ffff; // first id gets truncated to 18 bits + return new OtpErlangRef(node, ids, creation); + + default: + throw new OtpErlangDecodeException( + "Wrong tag encountered, expected ref, got " + tag); + } } public OtpErlangFun read_fun() throws OtpErlangDecodeException { - final int tag = read1skip_version(); - if (tag == OtpExternal.funTag) { - final int nFreeVars = read4BE(); - final OtpErlangPid pid = read_pid(); - final String module = read_atom(); - final long index = read_long(); - final long uniq = read_long(); - final OtpErlangObject[] freeVars = new OtpErlangObject[nFreeVars]; - for (int i = 0; i < nFreeVars; ++i) { - freeVars[i] = read_any(); - } - return new OtpErlangFun(pid, module, index, uniq, freeVars); - } else if (tag == OtpExternal.newFunTag) { - read4BE(); - final int arity = read1(); - final byte[] md5 = new byte[16]; - readN(md5); - final int index = read4BE(); - final int nFreeVars = read4BE(); - final String module = read_atom(); - final long oldIndex = read_long(); - final long uniq = read_long(); - final OtpErlangPid pid = read_pid(); - final OtpErlangObject[] freeVars = new OtpErlangObject[nFreeVars]; - for (int i = 0; i < nFreeVars; ++i) { - freeVars[i] = read_any(); - } - return new OtpErlangFun(pid, module, arity, md5, index, oldIndex, - uniq, freeVars); - } else { - throw new OtpErlangDecodeException( - "Wrong tag encountered, expected fun, got " + tag); - } + final int tag = read1skip_version(); + if (tag == OtpExternal.funTag) { + final int nFreeVars = read4BE(); + final OtpErlangPid pid = read_pid(); + final String module = read_atom(); + final long index = read_long(); + final long uniq = read_long(); + final OtpErlangObject[] freeVars = new OtpErlangObject[nFreeVars]; + for (int i = 0; i < nFreeVars; ++i) { + freeVars[i] = read_any(); + } + return new OtpErlangFun(pid, module, index, uniq, freeVars); + } else if (tag == OtpExternal.newFunTag) { + read4BE(); + final int arity = read1(); + final byte[] md5 = new byte[16]; + readN(md5); + final int index = read4BE(); + final int nFreeVars = read4BE(); + final String module = read_atom(); + final long oldIndex = read_long(); + final long uniq = read_long(); + final OtpErlangPid pid = read_pid(); + final OtpErlangObject[] freeVars = new OtpErlangObject[nFreeVars]; + for (int i = 0; i < nFreeVars; ++i) { + freeVars[i] = read_any(); + } + return new OtpErlangFun(pid, module, arity, md5, index, oldIndex, + uniq, freeVars); + } else { + throw new OtpErlangDecodeException( + "Wrong tag encountered, expected fun, got " + tag); + } } public OtpErlangExternalFun read_external_fun() - throws OtpErlangDecodeException { - final int tag = read1skip_version(); - if (tag != OtpExternal.externalFunTag) { - throw new OtpErlangDecodeException( - "Wrong tag encountered, expected external fun, got " + tag); - } - final String module = read_atom(); - final String function = read_atom(); - final int arity = (int) read_long(); - return new OtpErlangExternalFun(module, function, arity); + throws OtpErlangDecodeException { + final int tag = read1skip_version(); + if (tag != OtpExternal.externalFunTag) { + throw new OtpErlangDecodeException( + "Wrong tag encountered, expected external fun, got " + tag); + } + final String module = read_atom(); + final String function = read_atom(); + final int arity = (int) read_long(); + return new OtpErlangExternalFun(module, function, arity); } /** * Read a string from the stream. - * + * * @return the value of the string. - * + * * @exception OtpErlangDecodeException * if the next term in the stream is not a string. */ public String read_string() throws OtpErlangDecodeException { - int tag; - int len; - byte[] strbuf; - int[] intbuf; - tag = read1skip_version(); - switch (tag) { - case OtpExternal.stringTag: - len = read2BE(); - strbuf = new byte[len]; - this.readN(strbuf); - return OtpErlangString.newString(strbuf); - case OtpExternal.nilTag: - return ""; - case OtpExternal.listTag: // List when unicode + - len = read4BE(); - intbuf = new int[len]; - for (int i = 0; i < len; i++) { - intbuf[i] = read_int(); - if (! OtpErlangString.isValidCodePoint(intbuf[i])) { - throw new OtpErlangDecodeException - ("Invalid CodePoint: " + intbuf[i]); - } - } - read_nil(); - return new String(intbuf, 0, intbuf.length); - default: - throw new OtpErlangDecodeException( - "Wrong tag encountered, expected " + OtpExternal.stringTag - + " or " + OtpExternal.listTag + ", got " + tag); - } + int tag; + int len; + byte[] strbuf; + int[] intbuf; + tag = read1skip_version(); + switch (tag) { + case OtpExternal.stringTag: + len = read2BE(); + strbuf = new byte[len]; + this.readN(strbuf); + return OtpErlangString.newString(strbuf); + case OtpExternal.nilTag: + return ""; + case OtpExternal.listTag: // List when unicode + + len = read4BE(); + intbuf = new int[len]; + for (int i = 0; i < len; i++) { + intbuf[i] = read_int(); + if (!OtpErlangString.isValidCodePoint(intbuf[i])) { + throw new OtpErlangDecodeException("Invalid CodePoint: " + + intbuf[i]); + } + } + read_nil(); + return new String(intbuf, 0, intbuf.length); + default: + throw new OtpErlangDecodeException( + "Wrong tag encountered, expected " + OtpExternal.stringTag + + " or " + OtpExternal.listTag + ", got " + tag); + } } /** * Read a compressed term from the stream - * + * * @return the resulting uncompressed term. - * + * * @exception OtpErlangDecodeException * if the next term in the stream is not a compressed term. */ public OtpErlangObject read_compressed() throws OtpErlangDecodeException { - final int tag = read1skip_version(); - - if (tag != OtpExternal.compressedTag) { - throw new OtpErlangDecodeException( - "Wrong tag encountered, expected " - + OtpExternal.compressedTag + ", got " + tag); - } - - final int size = read4BE(); - final byte[] abuf = new byte[size]; - final java.util.zip.InflaterInputStream is = - new java.util.zip.InflaterInputStream(this, new java.util.zip.Inflater(), size); - int curPos = 0; - try { - int curRead; - while(curPos < size && (curRead = is.read(abuf, curPos, size - curPos)) != -1) { - curPos += curRead; - } - if (curPos != size) { - throw new OtpErlangDecodeException("Decompression gave " - + curPos + " bytes, not " + size); - } - } catch (final IOException e) { - throw new OtpErlangDecodeException("Cannot read from input stream"); - } - - @SuppressWarnings("resource") - final OtpInputStream ois = new OtpInputStream(abuf, flags); - return ois.read_any(); + final int tag = read1skip_version(); + + if (tag != OtpExternal.compressedTag) { + throw new OtpErlangDecodeException( + "Wrong tag encountered, expected " + + OtpExternal.compressedTag + ", got " + tag); + } + + final int size = read4BE(); + final byte[] abuf = new byte[size]; + final java.util.zip.InflaterInputStream is = new java.util.zip.InflaterInputStream( + this, new java.util.zip.Inflater(), size); + int curPos = 0; + try { + int curRead; + while (curPos < size + && (curRead = is.read(abuf, curPos, size - curPos)) != -1) { + curPos += curRead; + } + if (curPos != size) { + throw new OtpErlangDecodeException("Decompression gave " + + curPos + " bytes, not " + size); + } + } catch (final IOException e) { + throw new OtpErlangDecodeException("Cannot read from input stream"); + } + + @SuppressWarnings("resource") + final OtpInputStream ois = new OtpInputStream(abuf, flags); + return ois.read_any(); } /** * Read an arbitrary Erlang term from the stream. - * + * * @return the Erlang term. - * + * * @exception OtpErlangDecodeException * if the stream does not contain a known Erlang type at the * next position. */ public OtpErlangObject read_any() throws OtpErlangDecodeException { - // calls one of the above functions, depending on o - final int tag = peek1skip_version(); + // calls one of the above functions, depending on o + final int tag = peek1skip_version(); - switch (tag) { - case OtpExternal.smallIntTag: - case OtpExternal.intTag: - case OtpExternal.smallBigTag: - case OtpExternal.largeBigTag: - return new OtpErlangLong(this); + switch (tag) { + case OtpExternal.smallIntTag: + case OtpExternal.intTag: + case OtpExternal.smallBigTag: + case OtpExternal.largeBigTag: + return new OtpErlangLong(this); - case OtpExternal.atomTag: - case OtpExternal.smallAtomUtf8Tag: - case OtpExternal.atomUtf8Tag: - return new OtpErlangAtom(this); + case OtpExternal.atomTag: + case OtpExternal.smallAtomUtf8Tag: + case OtpExternal.atomUtf8Tag: + return new OtpErlangAtom(this); - case OtpExternal.floatTag: - case OtpExternal.newFloatTag: - return new OtpErlangDouble(this); + case OtpExternal.floatTag: + case OtpExternal.newFloatTag: + return new OtpErlangDouble(this); - case OtpExternal.refTag: - case OtpExternal.newRefTag: - return new OtpErlangRef(this); + case OtpExternal.refTag: + case OtpExternal.newRefTag: + return new OtpErlangRef(this); case OtpExternal.mapTag: return new OtpErlangMap(this); - case OtpExternal.portTag: - return new OtpErlangPort(this); + case OtpExternal.portTag: + return new OtpErlangPort(this); - case OtpExternal.pidTag: - return new OtpErlangPid(this); + case OtpExternal.pidTag: + return new OtpErlangPid(this); - case OtpExternal.stringTag: - return new OtpErlangString(this); + case OtpExternal.stringTag: + return new OtpErlangString(this); - case OtpExternal.listTag: - case OtpExternal.nilTag: - if ((flags & DECODE_INT_LISTS_AS_STRINGS) != 0) { - final int savePos = getPos(); - try { - return new OtpErlangString(this); - } catch (final OtpErlangDecodeException e) { - } - setPos(savePos); - } - return new OtpErlangList(this); + case OtpExternal.listTag: + case OtpExternal.nilTag: + if ((flags & DECODE_INT_LISTS_AS_STRINGS) != 0) { + final int savePos = getPos(); + try { + return new OtpErlangString(this); + } catch (final OtpErlangDecodeException e) { + } + setPos(savePos); + } + return new OtpErlangList(this); - case OtpExternal.smallTupleTag: - case OtpExternal.largeTupleTag: - return new OtpErlangTuple(this); + case OtpExternal.smallTupleTag: + case OtpExternal.largeTupleTag: + return new OtpErlangTuple(this); - case OtpExternal.binTag: - return new OtpErlangBinary(this); + case OtpExternal.binTag: + return new OtpErlangBinary(this); - case OtpExternal.bitBinTag: - return new OtpErlangBitstr(this); + case OtpExternal.bitBinTag: + return new OtpErlangBitstr(this); - case OtpExternal.compressedTag: - return read_compressed(); + case OtpExternal.compressedTag: + return read_compressed(); - case OtpExternal.newFunTag: - case OtpExternal.funTag: - return new OtpErlangFun(this); + case OtpExternal.newFunTag: + case OtpExternal.funTag: + return new OtpErlangFun(this); - default: - throw new OtpErlangDecodeException("Uknown data type: " + tag); - } + default: + throw new OtpErlangDecodeException("Uknown data type: " + tag); + } } public int read_map_head() throws OtpErlangDecodeException { diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpLocalNode.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpLocalNode.java index fbd0eb4073..dd1d299297 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpLocalNode.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpLocalNode.java @@ -1,19 +1,19 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; @@ -29,89 +29,103 @@ public class OtpLocalNode extends AbstractNode { private int refId[]; protected int port; - protected java.net.Socket epmd; - - protected OtpLocalNode() { - super(); - init(); - } + protected OtpTransport epmd; /** * Create a node with the given name and the default cookie. */ protected OtpLocalNode(final String node) { - super(node); - init(); + super(node); + init(); + } + + /** + * Create a node with the given name, transport factory and the default + * cookie. + */ + protected OtpLocalNode(final String node, + final OtpTransportFactory transportFactory) { + super(node, transportFactory); + init(); } /** * Create a node with the given name and cookie. */ protected OtpLocalNode(final String node, final String cookie) { - super(node, cookie); - init(); + super(node, cookie); + init(); + } + + /** + * Create a node with the given name, cookie and transport factory. + */ + protected OtpLocalNode(final String node, final String cookie, + final OtpTransportFactory transportFactory) { + super(node, cookie, transportFactory); + init(); } private void init() { - serial = 0; - pidCount = 1; - portCount = 1; - refId = new int[3]; - refId[0] = 1; - refId[1] = 0; - refId[2] = 0; + serial = 0; + pidCount = 1; + portCount = 1; + refId = new int[3]; + refId[0] = 1; + refId[1] = 0; + refId[2] = 0; } /** * Get the port number used by this node. - * + * * @return the port number this server node is accepting connections on. */ public int port() { - return port; + return port; } /** * Set the Epmd socket after publishing this nodes listen port to Epmd. - * + * * @param s - * The socket connecting this node to Epmd. + * The socket connecting this node to Epmd. */ - protected void setEpmd(final java.net.Socket s) { - epmd = s; + protected void setEpmd(final OtpTransport s) { + epmd = s; } /** * Get the Epmd socket. - * + * * @return The socket connecting this node to Epmd. */ - protected java.net.Socket getEpmd() { - return epmd; + protected OtpTransport getEpmd() { + return epmd; } /** * Create an Erlang {@link OtpErlangPid pid}. Erlang pids are based upon * some node specific information; this method creates a pid using the * information in this node. Each call to this method produces a unique pid. - * + * * @return an Erlang pid. */ public synchronized OtpErlangPid createPid() { - final OtpErlangPid p = new OtpErlangPid(node, pidCount, serial, - creation); + final OtpErlangPid p = new OtpErlangPid(node, pidCount, serial, + creation); - pidCount++; - if (pidCount > 0x7fff) { - pidCount = 0; + pidCount++; + if (pidCount > 0x7fff) { + pidCount = 0; - serial++; - if (serial > 0x1fff) { /* 13 bits */ - serial = 0; - } - } + serial++; + if (serial > 0x1fff) { /* 13 bits */ + serial = 0; + } + } - return p; + return p; } /** @@ -120,18 +134,18 @@ public class OtpLocalNode extends AbstractNode { * information in this node. Each call to this method produces a unique * port. It may not be meaningful to create a port in a non-Erlang * environment, but this method is provided for completeness. - * + * * @return an Erlang port. */ public synchronized OtpErlangPort createPort() { - final OtpErlangPort p = new OtpErlangPort(node, portCount, creation); + final OtpErlangPort p = new OtpErlangPort(node, portCount, creation); - portCount++; - if (portCount > 0xfffffff) { /* 28 bits */ - portCount = 0; - } + portCount++; + if (portCount > 0xfffffff) { /* 28 bits */ + portCount = 0; + } - return p; + return p; } /** @@ -139,23 +153,23 @@ public class OtpLocalNode extends AbstractNode { * based upon some node specific information; this method creates a * reference using the information in this node. Each call to this method * produces a unique reference. - * + * * @return an Erlang reference. */ public synchronized OtpErlangRef createRef() { - final OtpErlangRef r = new OtpErlangRef(node, refId, creation); + final OtpErlangRef r = new OtpErlangRef(node, refId, creation); - // increment ref ids (3 ints: 18 + 32 + 32 bits) - refId[0]++; - if (refId[0] > 0x3ffff) { - refId[0] = 0; + // increment ref ids (3 ints: 18 + 32 + 32 bits) + refId[0]++; + if (refId[0] > 0x3ffff) { + refId[0] = 0; - refId[1]++; - if (refId[1] == 0) { - refId[2]++; - } - } + refId[1]++; + if (refId[1] == 0) { + refId[2]++; + } + } - return r; + return r; } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpMD5.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpMD5.java index a5a4d86602..41be523eb2 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpMD5.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpMD5.java @@ -1,19 +1,19 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; @@ -46,37 +46,37 @@ class OtpMD5 { */ private final long state[] = { 0x67452301L, 0xefcdab89L, 0x98badcfeL, - 0x10325476L }; + 0x10325476L }; private final long count[] = { 0L, 0L }; private final int buffer[]; public OtpMD5() { - buffer = new int[64]; - int i; - for (i = 0; i < 64; ++i) { - buffer[i] = 0; - } + buffer = new int[64]; + int i; + for (i = 0; i < 64; ++i) { + buffer[i] = 0; + } } private int[] to_bytes(final String s) { - final char tmp[] = s.toCharArray(); - final int ret[] = new int[tmp.length]; - int i; - - for (i = 0; i < tmp.length; ++i) { - ret[i] = tmp[i] & 0xFF; - } - return ret; + final char tmp[] = s.toCharArray(); + final int ret[] = new int[tmp.length]; + int i; + + for (i = 0; i < tmp.length; ++i) { + ret[i] = tmp[i] & 0xFF; + } + return ret; } private int[] clean_bytes(final int bytes[]) { - final int ret[] = new int[bytes.length]; - int i; + final int ret[] = new int[bytes.length]; + int i; - for (i = 0; i < bytes.length; ++i) { - ret[i] = bytes[i] & 0xFF; - } - return ret; + for (i = 0; i < bytes.length; ++i) { + ret[i] = bytes[i] & 0xFF; + } + return ret; } /* @@ -84,83 +84,83 @@ class OtpMD5 { */ private long shl(final long what, final int steps) { - return what << steps & 0xFFFFFFFFL; + return what << steps & 0xFFFFFFFFL; } private long shr(final long what, final int steps) { - return what >>> steps; + return what >>> steps; } private long plus(final long a, final long b) { - return a + b & 0xFFFFFFFFL; + return a + b & 0xFFFFFFFFL; } - private long not(long x) { - return ~x & 0xFFFFFFFFL; + private long not(final long x) { + return ~x & 0xFFFFFFFFL; } - private void to_buffer(int to_start, final int[] from, int from_start, - int num) { - int ix = num; - int to_ix = to_start; - int from_ix = from_start; - while (ix-- > 0) { - buffer[to_ix++] = from[from_ix++]; - } + private void to_buffer(final int to_start, final int[] from, + final int from_start, final int num) { + int ix = num; + int to_ix = to_start; + int from_ix = from_start; + while (ix-- > 0) { + buffer[to_ix++] = from[from_ix++]; + } } private void do_update(final int bytes[]) { - int index = (int) (count[0] >>> 3 & 0x3F); - final long inlen = bytes.length; - final long addcount = shl(inlen, 3); - final long partlen = 64 - index; - int i; + int index = (int) (count[0] >>> 3 & 0x3F); + final long inlen = bytes.length; + final long addcount = shl(inlen, 3); + final long partlen = 64 - index; + int i; - count[0] = plus(count[0], addcount); + count[0] = plus(count[0], addcount); - if (count[0] < addcount) { - ++count[1]; - } + if (count[0] < addcount) { + ++count[1]; + } - count[1] = plus(count[1], shr(inlen, 29)); + count[1] = plus(count[1], shr(inlen, 29)); - // dumpstate(); + // dumpstate(); - if (inlen >= partlen) { - to_buffer(index, bytes, 0, (int) partlen); - transform(buffer, 0); + if (inlen >= partlen) { + to_buffer(index, bytes, 0, (int) partlen); + transform(buffer, 0); - for (i = (int) partlen; i + 63 < inlen; i += 64) { - transform(bytes, i); - } + for (i = (int) partlen; i + 63 < inlen; i += 64) { + transform(bytes, i); + } - index = 0; - } else { - i = 0; - } + index = 0; + } else { + i = 0; + } - /* dumpstate(); */ + /* dumpstate(); */ - to_buffer(index, bytes, i, (int) inlen - i); + to_buffer(index, bytes, i, (int) inlen - i); - /* dumpstate(); */ + /* dumpstate(); */ } @SuppressWarnings("unused") private void dumpstate() { - System.out.println("state = {" + state[0] + ", " + state[1] + ", " - + state[2] + ", " + state[3] + "}"); - System.out.println("count = {" + count[0] + ", " + count[1] + "}"); - System.out.print("buffer = {"); - int i; - for (i = 0; i < 64; ++i) { - if (i > 0) { - System.out.print(", "); - } - System.out.print(buffer[i]); - } - System.out.println("}"); + System.out.println("state = {" + state[0] + ", " + state[1] + ", " + + state[2] + ", " + state[3] + "}"); + System.out.println("count = {" + count[0] + ", " + count[1] + "}"); + System.out.print("buffer = {"); + int i; + for (i = 0; i < 64; ++i) { + if (i > 0) { + System.out.print(", "); + } + System.out.print(buffer[i]); + } + System.out.println("}"); } /* @@ -168,191 +168,191 @@ class OtpMD5 { */ private long F(final long x, final long y, final long z) { - return x & y | not(x) & z; + return x & y | not(x) & z; } private long G(final long x, final long y, final long z) { - return x & z | y & not(z); + return x & z | y & not(z); } private long H(final long x, final long y, final long z) { - return x ^ y ^ z; + return x ^ y ^ z; } private long I(final long x, final long y, final long z) { - return y ^ (x | not(z)); + return y ^ (x | not(z)); } private long ROTATE_LEFT(final long x, final long n) { - return shl(x, (int) n) | shr(x, (int) (32 - n)); + return shl(x, (int) n) | shr(x, (int) (32 - n)); } - private long FF(long a, final long b, final long c, final long d, - final long x, final long s, final long ac) { - long tmp = plus(a, plus(plus(F(b, c, d), x), ac)); - tmp = ROTATE_LEFT(tmp, s); - return plus(tmp, b); + private long FF(final long a, final long b, final long c, final long d, + final long x, final long s, final long ac) { + long tmp = plus(a, plus(plus(F(b, c, d), x), ac)); + tmp = ROTATE_LEFT(tmp, s); + return plus(tmp, b); } - private long GG(long a, final long b, final long c, final long d, - final long x, final long s, final long ac) { - long tmp = plus(a, plus(plus(G(b, c, d), x), ac)); - tmp = ROTATE_LEFT(tmp, s); - return plus(tmp, b); + private long GG(final long a, final long b, final long c, final long d, + final long x, final long s, final long ac) { + long tmp = plus(a, plus(plus(G(b, c, d), x), ac)); + tmp = ROTATE_LEFT(tmp, s); + return plus(tmp, b); } - private long HH(long a, final long b, final long c, final long d, - final long x, final long s, final long ac) { - long tmp = plus(a, plus(plus(H(b, c, d), x), ac)); - tmp = ROTATE_LEFT(tmp, s); - return plus(tmp, b); + private long HH(final long a, final long b, final long c, final long d, + final long x, final long s, final long ac) { + long tmp = plus(a, plus(plus(H(b, c, d), x), ac)); + tmp = ROTATE_LEFT(tmp, s); + return plus(tmp, b); } - private long II(long a, final long b, final long c, final long d, - final long x, final long s, final long ac) { - long tmp = plus(a, plus(plus(I(b, c, d), x), ac)); - tmp = ROTATE_LEFT(tmp, s); - return plus(tmp, b); + private long II(final long a, final long b, final long c, final long d, + final long x, final long s, final long ac) { + long tmp = plus(a, plus(plus(I(b, c, d), x), ac)); + tmp = ROTATE_LEFT(tmp, s); + return plus(tmp, b); } private void decode(final long output[], final int input[], - final int in_from, final int len) { - int i, j; - - for (i = 0, j = 0; j < len; i++, j += 4) { - output[i] = input[j + in_from] | shl(input[j + in_from + 1], 8) - | shl(input[j + in_from + 2], 16) - | shl(input[j + in_from + 3], 24); - } + final int in_from, final int len) { + int i, j; + + for (i = 0, j = 0; j < len; i++, j += 4) { + output[i] = input[j + in_from] | shl(input[j + in_from + 1], 8) + | shl(input[j + in_from + 2], 16) + | shl(input[j + in_from + 3], 24); + } } private void transform(final int block[], final int from) { - long a = state[0]; - long b = state[1]; - long c = state[2]; - long d = state[3]; - final long x[] = new long[16]; - - decode(x, block, from, 64); - - a = FF(a, b, c, d, x[0], S11, 0xd76aa478L); /* 1 */ - d = FF(d, a, b, c, x[1], S12, 0xe8c7b756L); /* 2 */ - c = FF(c, d, a, b, x[2], S13, 0x242070dbL); /* 3 */ - b = FF(b, c, d, a, x[3], S14, 0xc1bdceeeL); /* 4 */ - a = FF(a, b, c, d, x[4], S11, 0xf57c0fafL); /* 5 */ - d = FF(d, a, b, c, x[5], S12, 0x4787c62aL); /* 6 */ - c = FF(c, d, a, b, x[6], S13, 0xa8304613L); /* 7 */ - b = FF(b, c, d, a, x[7], S14, 0xfd469501L); /* 8 */ - a = FF(a, b, c, d, x[8], S11, 0x698098d8L); /* 9 */ - d = FF(d, a, b, c, x[9], S12, 0x8b44f7afL); /* 10 */ - c = FF(c, d, a, b, x[10], S13, 0xffff5bb1L); /* 11 */ - b = FF(b, c, d, a, x[11], S14, 0x895cd7beL); /* 12 */ - a = FF(a, b, c, d, x[12], S11, 0x6b901122L); /* 13 */ - d = FF(d, a, b, c, x[13], S12, 0xfd987193L); /* 14 */ - c = FF(c, d, a, b, x[14], S13, 0xa679438eL); /* 15 */ - b = FF(b, c, d, a, x[15], S14, 0x49b40821L); /* 16 */ - - /* Round 2 */ - a = GG(a, b, c, d, x[1], S21, 0xf61e2562L); /* 17 */ - d = GG(d, a, b, c, x[6], S22, 0xc040b340L); /* 18 */ - c = GG(c, d, a, b, x[11], S23, 0x265e5a51L); /* 19 */ - b = GG(b, c, d, a, x[0], S24, 0xe9b6c7aaL); /* 20 */ - a = GG(a, b, c, d, x[5], S21, 0xd62f105dL); /* 21 */ - d = GG(d, a, b, c, x[10], S22, 0x2441453L); /* 22 */ - c = GG(c, d, a, b, x[15], S23, 0xd8a1e681L); /* 23 */ - b = GG(b, c, d, a, x[4], S24, 0xe7d3fbc8L); /* 24 */ - a = GG(a, b, c, d, x[9], S21, 0x21e1cde6L); /* 25 */ - d = GG(d, a, b, c, x[14], S22, 0xc33707d6L); /* 26 */ - c = GG(c, d, a, b, x[3], S23, 0xf4d50d87L); /* 27 */ - b = GG(b, c, d, a, x[8], S24, 0x455a14edL); /* 28 */ - a = GG(a, b, c, d, x[13], S21, 0xa9e3e905L); /* 29 */ - d = GG(d, a, b, c, x[2], S22, 0xfcefa3f8L); /* 30 */ - c = GG(c, d, a, b, x[7], S23, 0x676f02d9L); /* 31 */ - b = GG(b, c, d, a, x[12], S24, 0x8d2a4c8aL); /* 32 */ - - /* Round 3 */ - a = HH(a, b, c, d, x[5], S31, 0xfffa3942L); /* 33 */ - d = HH(d, a, b, c, x[8], S32, 0x8771f681L); /* 34 */ - c = HH(c, d, a, b, x[11], S33, 0x6d9d6122L); /* 35 */ - b = HH(b, c, d, a, x[14], S34, 0xfde5380cL); /* 36 */ - a = HH(a, b, c, d, x[1], S31, 0xa4beea44L); /* 37 */ - d = HH(d, a, b, c, x[4], S32, 0x4bdecfa9L); /* 38 */ - c = HH(c, d, a, b, x[7], S33, 0xf6bb4b60L); /* 39 */ - b = HH(b, c, d, a, x[10], S34, 0xbebfbc70L); /* 40 */ - a = HH(a, b, c, d, x[13], S31, 0x289b7ec6L); /* 41 */ - d = HH(d, a, b, c, x[0], S32, 0xeaa127faL); /* 42 */ - c = HH(c, d, a, b, x[3], S33, 0xd4ef3085L); /* 43 */ - b = HH(b, c, d, a, x[6], S34, 0x4881d05L); /* 44 */ - a = HH(a, b, c, d, x[9], S31, 0xd9d4d039L); /* 45 */ - d = HH(d, a, b, c, x[12], S32, 0xe6db99e5L); /* 46 */ - c = HH(c, d, a, b, x[15], S33, 0x1fa27cf8L); /* 47 */ - b = HH(b, c, d, a, x[2], S34, 0xc4ac5665L); /* 48 */ - - /* Round 4 */ - a = II(a, b, c, d, x[0], S41, 0xf4292244L); /* 49 */ - d = II(d, a, b, c, x[7], S42, 0x432aff97L); /* 50 */ - c = II(c, d, a, b, x[14], S43, 0xab9423a7L); /* 51 */ - b = II(b, c, d, a, x[5], S44, 0xfc93a039L); /* 52 */ - a = II(a, b, c, d, x[12], S41, 0x655b59c3L); /* 53 */ - d = II(d, a, b, c, x[3], S42, 0x8f0ccc92L); /* 54 */ - c = II(c, d, a, b, x[10], S43, 0xffeff47dL); /* 55 */ - b = II(b, c, d, a, x[1], S44, 0x85845dd1L); /* 56 */ - a = II(a, b, c, d, x[8], S41, 0x6fa87e4fL); /* 57 */ - d = II(d, a, b, c, x[15], S42, 0xfe2ce6e0L); /* 58 */ - c = II(c, d, a, b, x[6], S43, 0xa3014314L); /* 59 */ - b = II(b, c, d, a, x[13], S44, 0x4e0811a1L); /* 60 */ - a = II(a, b, c, d, x[4], S41, 0xf7537e82L); /* 61 */ - d = II(d, a, b, c, x[11], S42, 0xbd3af235L); /* 62 */ - c = II(c, d, a, b, x[2], S43, 0x2ad7d2bbL); /* 63 */ - b = II(b, c, d, a, x[9], S44, 0xeb86d391L); /* 64 */ - - state[0] = plus(state[0], a); - state[1] = plus(state[1], b); - state[2] = plus(state[2], c); - state[3] = plus(state[3], d); + long a = state[0]; + long b = state[1]; + long c = state[2]; + long d = state[3]; + final long x[] = new long[16]; + + decode(x, block, from, 64); + + a = FF(a, b, c, d, x[0], S11, 0xd76aa478L); /* 1 */ + d = FF(d, a, b, c, x[1], S12, 0xe8c7b756L); /* 2 */ + c = FF(c, d, a, b, x[2], S13, 0x242070dbL); /* 3 */ + b = FF(b, c, d, a, x[3], S14, 0xc1bdceeeL); /* 4 */ + a = FF(a, b, c, d, x[4], S11, 0xf57c0fafL); /* 5 */ + d = FF(d, a, b, c, x[5], S12, 0x4787c62aL); /* 6 */ + c = FF(c, d, a, b, x[6], S13, 0xa8304613L); /* 7 */ + b = FF(b, c, d, a, x[7], S14, 0xfd469501L); /* 8 */ + a = FF(a, b, c, d, x[8], S11, 0x698098d8L); /* 9 */ + d = FF(d, a, b, c, x[9], S12, 0x8b44f7afL); /* 10 */ + c = FF(c, d, a, b, x[10], S13, 0xffff5bb1L); /* 11 */ + b = FF(b, c, d, a, x[11], S14, 0x895cd7beL); /* 12 */ + a = FF(a, b, c, d, x[12], S11, 0x6b901122L); /* 13 */ + d = FF(d, a, b, c, x[13], S12, 0xfd987193L); /* 14 */ + c = FF(c, d, a, b, x[14], S13, 0xa679438eL); /* 15 */ + b = FF(b, c, d, a, x[15], S14, 0x49b40821L); /* 16 */ + + /* Round 2 */ + a = GG(a, b, c, d, x[1], S21, 0xf61e2562L); /* 17 */ + d = GG(d, a, b, c, x[6], S22, 0xc040b340L); /* 18 */ + c = GG(c, d, a, b, x[11], S23, 0x265e5a51L); /* 19 */ + b = GG(b, c, d, a, x[0], S24, 0xe9b6c7aaL); /* 20 */ + a = GG(a, b, c, d, x[5], S21, 0xd62f105dL); /* 21 */ + d = GG(d, a, b, c, x[10], S22, 0x2441453L); /* 22 */ + c = GG(c, d, a, b, x[15], S23, 0xd8a1e681L); /* 23 */ + b = GG(b, c, d, a, x[4], S24, 0xe7d3fbc8L); /* 24 */ + a = GG(a, b, c, d, x[9], S21, 0x21e1cde6L); /* 25 */ + d = GG(d, a, b, c, x[14], S22, 0xc33707d6L); /* 26 */ + c = GG(c, d, a, b, x[3], S23, 0xf4d50d87L); /* 27 */ + b = GG(b, c, d, a, x[8], S24, 0x455a14edL); /* 28 */ + a = GG(a, b, c, d, x[13], S21, 0xa9e3e905L); /* 29 */ + d = GG(d, a, b, c, x[2], S22, 0xfcefa3f8L); /* 30 */ + c = GG(c, d, a, b, x[7], S23, 0x676f02d9L); /* 31 */ + b = GG(b, c, d, a, x[12], S24, 0x8d2a4c8aL); /* 32 */ + + /* Round 3 */ + a = HH(a, b, c, d, x[5], S31, 0xfffa3942L); /* 33 */ + d = HH(d, a, b, c, x[8], S32, 0x8771f681L); /* 34 */ + c = HH(c, d, a, b, x[11], S33, 0x6d9d6122L); /* 35 */ + b = HH(b, c, d, a, x[14], S34, 0xfde5380cL); /* 36 */ + a = HH(a, b, c, d, x[1], S31, 0xa4beea44L); /* 37 */ + d = HH(d, a, b, c, x[4], S32, 0x4bdecfa9L); /* 38 */ + c = HH(c, d, a, b, x[7], S33, 0xf6bb4b60L); /* 39 */ + b = HH(b, c, d, a, x[10], S34, 0xbebfbc70L); /* 40 */ + a = HH(a, b, c, d, x[13], S31, 0x289b7ec6L); /* 41 */ + d = HH(d, a, b, c, x[0], S32, 0xeaa127faL); /* 42 */ + c = HH(c, d, a, b, x[3], S33, 0xd4ef3085L); /* 43 */ + b = HH(b, c, d, a, x[6], S34, 0x4881d05L); /* 44 */ + a = HH(a, b, c, d, x[9], S31, 0xd9d4d039L); /* 45 */ + d = HH(d, a, b, c, x[12], S32, 0xe6db99e5L); /* 46 */ + c = HH(c, d, a, b, x[15], S33, 0x1fa27cf8L); /* 47 */ + b = HH(b, c, d, a, x[2], S34, 0xc4ac5665L); /* 48 */ + + /* Round 4 */ + a = II(a, b, c, d, x[0], S41, 0xf4292244L); /* 49 */ + d = II(d, a, b, c, x[7], S42, 0x432aff97L); /* 50 */ + c = II(c, d, a, b, x[14], S43, 0xab9423a7L); /* 51 */ + b = II(b, c, d, a, x[5], S44, 0xfc93a039L); /* 52 */ + a = II(a, b, c, d, x[12], S41, 0x655b59c3L); /* 53 */ + d = II(d, a, b, c, x[3], S42, 0x8f0ccc92L); /* 54 */ + c = II(c, d, a, b, x[10], S43, 0xffeff47dL); /* 55 */ + b = II(b, c, d, a, x[1], S44, 0x85845dd1L); /* 56 */ + a = II(a, b, c, d, x[8], S41, 0x6fa87e4fL); /* 57 */ + d = II(d, a, b, c, x[15], S42, 0xfe2ce6e0L); /* 58 */ + c = II(c, d, a, b, x[6], S43, 0xa3014314L); /* 59 */ + b = II(b, c, d, a, x[13], S44, 0x4e0811a1L); /* 60 */ + a = II(a, b, c, d, x[4], S41, 0xf7537e82L); /* 61 */ + d = II(d, a, b, c, x[11], S42, 0xbd3af235L); /* 62 */ + c = II(c, d, a, b, x[2], S43, 0x2ad7d2bbL); /* 63 */ + b = II(b, c, d, a, x[9], S44, 0xeb86d391L); /* 64 */ + + state[0] = plus(state[0], a); + state[1] = plus(state[1], b); + state[2] = plus(state[2], c); + state[3] = plus(state[3], d); } public void update(final int bytes[]) { - do_update(clean_bytes(bytes)); + do_update(clean_bytes(bytes)); } public void update(final String s) { - do_update(to_bytes(s)); + do_update(to_bytes(s)); } private int[] encode(final long[] input, final int len) { - final int output[] = new int[len]; - int i, j; - for (i = 0, j = 0; j < len; i++, j += 4) { - output[j] = (int) (input[i] & 0xff); - output[j + 1] = (int) (input[i] >>> 8 & 0xff); - output[j + 2] = (int) (input[i] >>> 16 & 0xff); - output[j + 3] = (int) (input[i] >>> 24 & 0xff); - } - return output; + final int output[] = new int[len]; + int i, j; + for (i = 0, j = 0; j < len; i++, j += 4) { + output[j] = (int) (input[i] & 0xff); + output[j + 1] = (int) (input[i] >>> 8 & 0xff); + output[j + 2] = (int) (input[i] >>> 16 & 0xff); + output[j + 3] = (int) (input[i] >>> 24 & 0xff); + } + return output; } public int[] final_bytes() { - final int bits[] = encode(count, 8); - int index, padlen; - int padding[], i; - int[] digest; + final int bits[] = encode(count, 8); + int index, padlen; + int padding[], i; + int[] digest; - index = (int) (count[0] >>> 3 & 0x3f); - padlen = index < 56 ? 56 - index : 120 - index; - /* padlen > 0 */ - padding = new int[padlen]; - padding[0] = 0x80; - for (i = 1; i < padlen; ++i) { - padding[i] = 0; - } + index = (int) (count[0] >>> 3 & 0x3f); + padlen = index < 56 ? 56 - index : 120 - index; + /* padlen > 0 */ + padding = new int[padlen]; + padding[0] = 0x80; + for (i = 1; i < padlen; ++i) { + padding[i] = 0; + } - do_update(padding); + do_update(padding); - do_update(bits); + do_update(bits); - digest = encode(state, 16); + digest = encode(state, 16); - return digest; + return digest; } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpMbox.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpMbox.java index fc592c222c..872dba6dab 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpMbox.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpMbox.java @@ -1,19 +1,19 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2012. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; @@ -23,7 +23,7 @@ package com.ericsson.otp.erlang; * Provides a simple mechanism for exchanging messages with Erlang processes or * other instances of this class. * </p> - * + * * <p> * Each mailbox is associated with a unique {@link OtpErlangPid pid} that * contains information necessary for delivery of messages. When sending @@ -33,7 +33,7 @@ package com.ericsson.otp.erlang; * message contents. The sender can determine his own pid by calling * {@link #self() self()}. * </p> - * + * * <p> * Mailboxes can be named, either at creation or later. Messages can be sent to * named mailboxes and named Erlang processes without knowing the @@ -41,7 +41,7 @@ package com.ericsson.otp.erlang; * order to set up initial communication between parts of an application. Each * mailbox can have at most one name. * </p> - * + * * <p> * Since this class was intended for communication with Erlang, all of the send * methods take {@link OtpErlangObject OtpErlangObject} arguments. However this @@ -49,14 +49,14 @@ package com.ericsson.otp.erlang; * implement one of java.io.Serializable or java.io.Externalizable) by * encapsulating the object in a {@link OtpErlangBinary OtpErlangBinary}. * </p> - * + * * <p> * Messages to remote nodes are externalized for transmission, and as a result * the recipient receives a <b>copy</b> of the original Java object. To ensure * consistent behaviour when messages are sent between local mailboxes, such * messages are cloned before delivery. * </p> - * + * * <p> * Additionally, mailboxes can be linked in much the same way as Erlang * processes. If a link is active when a mailbox is {@link #close closed}, any @@ -68,14 +68,14 @@ package com.ericsson.otp.erlang; * close mailboxes if you are using links instead of relying on finalization to * notify other parties in a timely manner. * </p> - * + * * <p> * When retrieving messages from a mailbox that has received an exit signal, an * {@link OtpErlangExit OtpErlangExit} exception will be raised. Note that the * exception is queued in the mailbox along with other messages, and will not be * raised until it reaches the head of the queue and is about to be retrieved. * </p> - * + * */ public class OtpMbox { OtpNode home; @@ -87,17 +87,17 @@ public class OtpMbox { // package constructor: called by OtpNode:createMbox(name) // to create a named mbox OtpMbox(final OtpNode home, final OtpErlangPid self, final String name) { - this.self = self; - this.home = home; - this.name = name; - queue = new GenericQueue(); - links = new Links(10); + this.self = self; + this.home = home; + this.name = name; + queue = new GenericQueue(); + links = new Links(10); } // package constructor: called by OtpNode:createMbox() // to create an anonymous OtpMbox(final OtpNode home, final OtpErlangPid self) { - this(home, self, null); + this(home, self, null); } /** @@ -105,18 +105,18 @@ public class OtpMbox { * Get the identifying {@link OtpErlangPid pid} associated with this * mailbox. * </p> - * + * * <p> * The {@link OtpErlangPid pid} associated with this mailbox uniquely * identifies the mailbox and can be used to address the mailbox. You can * send the {@link OtpErlangPid pid} to a remote communicating part so that * he can know where to send his response. * </p> - * + * * @return the self pid for this mailbox. */ public OtpErlangPid self() { - return self; + return self; } /** @@ -127,305 +127,305 @@ public class OtpMbox { * name; if the mailbox already had a name, calling this method will * supercede that name. * </p> - * + * * @param aname - * the name to register for the mailbox. Specify null to - * unregister the existing name from this mailbox. - * + * the name to register for the mailbox. Specify null to + * unregister the existing name from this mailbox. + * * @return true if the name was available, or false otherwise. */ public synchronized boolean registerName(final String aname) { - return home.registerName(aname, this); + return home.registerName(aname, this); } /** * Get the registered name of this mailbox. - * + * * @return the registered name of this mailbox, or null if the mailbox had * no registered name. */ public String getName() { - return name; + return name; } /** * Block until a message arrives for this mailbox. - * + * * @return an {@link OtpErlangObject OtpErlangObject} representing the body * of the next message waiting in this mailbox. - * + * * @exception OtpErlangDecodeException - * if the message can not be decoded. - * + * if the message can not be decoded. + * * @exception OtpErlangExit - * if a linked {@link OtpErlangPid pid} has exited or has - * sent an exit signal to this mailbox. + * if a linked {@link OtpErlangPid pid} has exited or has + * sent an exit signal to this mailbox. */ public OtpErlangObject receive() throws OtpErlangExit, - OtpErlangDecodeException { - try { - return receiveMsg().getMsg(); - } catch (final OtpErlangExit e) { - throw e; - } catch (final OtpErlangDecodeException f) { - throw f; - } + OtpErlangDecodeException { + try { + return receiveMsg().getMsg(); + } catch (final OtpErlangExit e) { + throw e; + } catch (final OtpErlangDecodeException f) { + throw f; + } } /** * Wait for a message to arrive for this mailbox. - * + * * @param timeout - * the time, in milliseconds, to wait for a message before - * returning null. - * + * the time, in milliseconds, to wait for a message before + * returning null. + * * @return an {@link OtpErlangObject OtpErlangObject} representing the body * of the next message waiting in this mailbox. - * + * * @exception OtpErlangDecodeException - * if the message can not be decoded. - * + * if the message can not be decoded. + * * @exception OtpErlangExit - * if a linked {@link OtpErlangPid pid} has exited or has - * sent an exit signal to this mailbox. + * if a linked {@link OtpErlangPid pid} has exited or has + * sent an exit signal to this mailbox. */ public OtpErlangObject receive(final long timeout) throws OtpErlangExit, - OtpErlangDecodeException { - try { - final OtpMsg m = receiveMsg(timeout); - if (m != null) { - return m.getMsg(); - } - } catch (final OtpErlangExit e) { - throw e; - } catch (final OtpErlangDecodeException f) { - throw f; - } catch (final InterruptedException g) { - } - return null; + OtpErlangDecodeException { + try { + final OtpMsg m = receiveMsg(timeout); + if (m != null) { + return m.getMsg(); + } + } catch (final OtpErlangExit e) { + throw e; + } catch (final OtpErlangDecodeException f) { + throw f; + } catch (final InterruptedException g) { + } + return null; } /** * Block until a message arrives for this mailbox. - * + * * @return a byte array representing the still-encoded body of the next * message waiting in this mailbox. - * + * * @exception OtpErlangExit - * if a linked {@link OtpErlangPid pid} has exited or has - * sent an exit signal to this mailbox. - * + * if a linked {@link OtpErlangPid pid} has exited or has + * sent an exit signal to this mailbox. + * */ public OtpInputStream receiveBuf() throws OtpErlangExit { - return receiveMsg().getMsgBuf(); + return receiveMsg().getMsgBuf(); } /** * Wait for a message to arrive for this mailbox. - * + * * @param timeout - * the time, in milliseconds, to wait for a message before - * returning null. - * + * the time, in milliseconds, to wait for a message before + * returning null. + * * @return a byte array representing the still-encoded body of the next * message waiting in this mailbox. - * + * * @exception OtpErlangExit - * if a linked {@link OtpErlangPid pid} has exited or has - * sent an exit signal to this mailbox. - * + * if a linked {@link OtpErlangPid pid} has exited or has + * sent an exit signal to this mailbox. + * * @exception InterruptedException - * if no message if the method times out before a message - * becomes available. + * if no message if the method times out before a message + * becomes available. */ public OtpInputStream receiveBuf(final long timeout) - throws InterruptedException, OtpErlangExit { - final OtpMsg m = receiveMsg(timeout); - if (m != null) { - return m.getMsgBuf(); - } + throws InterruptedException, OtpErlangExit { + final OtpMsg m = receiveMsg(timeout); + if (m != null) { + return m.getMsgBuf(); + } - return null; + return null; } /** * Block until a message arrives for this mailbox. - * + * * @return an {@link OtpMsg OtpMsg} containing the header information as * well as the body of the next message waiting in this mailbox. - * + * * @exception OtpErlangExit - * if a linked {@link OtpErlangPid pid} has exited or has - * sent an exit signal to this mailbox. - * + * if a linked {@link OtpErlangPid pid} has exited or has + * sent an exit signal to this mailbox. + * */ public OtpMsg receiveMsg() throws OtpErlangExit { - final OtpMsg m = (OtpMsg) queue.get(); - - switch (m.type()) { - case OtpMsg.exitTag: - case OtpMsg.exit2Tag: - try { - final OtpErlangObject o = m.getMsg(); - throw new OtpErlangExit(o, m.getSenderPid()); - } catch (final OtpErlangDecodeException e) { - throw new OtpErlangExit("unknown", m.getSenderPid()); - } - - default: - return m; - } + final OtpMsg m = (OtpMsg) queue.get(); + + switch (m.type()) { + case OtpMsg.exitTag: + case OtpMsg.exit2Tag: + try { + final OtpErlangObject o = m.getMsg(); + throw new OtpErlangExit(o, m.getSenderPid()); + } catch (final OtpErlangDecodeException e) { + throw new OtpErlangExit("unknown", m.getSenderPid()); + } + + default: + return m; + } } /** * Wait for a message to arrive for this mailbox. - * + * * @param timeout - * the time, in milliseconds, to wait for a message. - * + * the time, in milliseconds, to wait for a message. + * * @return an {@link OtpMsg OtpMsg} containing the header information as * well as the body of the next message waiting in this mailbox. - * + * * @exception OtpErlangExit - * if a linked {@link OtpErlangPid pid} has exited or has - * sent an exit signal to this mailbox. - * + * if a linked {@link OtpErlangPid pid} has exited or has + * sent an exit signal to this mailbox. + * * @exception InterruptedException - * if no message if the method times out before a message - * becomes available. + * if no message if the method times out before a message + * becomes available. */ public OtpMsg receiveMsg(final long timeout) throws InterruptedException, - OtpErlangExit { - final OtpMsg m = (OtpMsg) queue.get(timeout); - - if (m == null) { - return null; - } - - switch (m.type()) { - case OtpMsg.exitTag: - case OtpMsg.exit2Tag: - try { - final OtpErlangObject o = m.getMsg(); - throw new OtpErlangExit(o, m.getSenderPid()); - } catch (final OtpErlangDecodeException e) { - throw new OtpErlangExit("unknown", m.getSenderPid()); - } - - default: - return m; - } + OtpErlangExit { + final OtpMsg m = (OtpMsg) queue.get(timeout); + + if (m == null) { + return null; + } + + switch (m.type()) { + case OtpMsg.exitTag: + case OtpMsg.exit2Tag: + try { + final OtpErlangObject o = m.getMsg(); + throw new OtpErlangExit(o, m.getSenderPid()); + } catch (final OtpErlangDecodeException e) { + throw new OtpErlangExit("unknown", m.getSenderPid()); + } + + default: + return m; + } } /** * Send a message to a remote {@link OtpErlangPid pid}, representing either * another {@link OtpMbox mailbox} or an Erlang process. - * + * * @param to - * the {@link OtpErlangPid pid} identifying the intended - * recipient of the message. - * + * the {@link OtpErlangPid pid} identifying the intended + * recipient of the message. + * * @param msg - * the body of the message to send. - * + * the body of the message to send. + * */ public void send(final OtpErlangPid to, final OtpErlangObject msg) { - try { - final String node = to.node(); - if (node.equals(home.node())) { - home.deliver(new OtpMsg(to, (OtpErlangObject) msg.clone())); - } else { - final OtpCookedConnection conn = home.getConnection(node); - if (conn == null) { - return; - } - conn.send(self, to, msg); - } - } catch (final Exception e) { - } + try { + final String node = to.node(); + if (node.equals(home.node())) { + home.deliver(new OtpMsg(to, (OtpErlangObject) msg.clone())); + } else { + final OtpCookedConnection conn = home.getConnection(node); + if (conn == null) { + return; + } + conn.send(self, to, msg); + } + } catch (final Exception e) { + } } /** * Send a message to a named mailbox created from the same node as this * mailbox. - * + * * @param aname - * the registered name of recipient mailbox. - * + * the registered name of recipient mailbox. + * * @param msg - * the body of the message to send. - * + * the body of the message to send. + * */ public void send(final String aname, final OtpErlangObject msg) { - home.deliver(new OtpMsg(self, aname, (OtpErlangObject) msg.clone())); + home.deliver(new OtpMsg(self, aname, (OtpErlangObject) msg.clone())); } /** * Send a message to a named mailbox created from another node. - * + * * @param aname - * the registered name of recipient mailbox. - * + * the registered name of recipient mailbox. + * * @param node - * the name of the remote node where the recipient mailbox is - * registered. - * + * the name of the remote node where the recipient mailbox is + * registered. + * * @param msg - * the body of the message to send. - * + * the body of the message to send. + * */ public void send(final String aname, final String node, - final OtpErlangObject msg) { - try { - final String currentNode = home.node(); - if (node.equals(currentNode)) { - send(aname, msg); - } else if (node.indexOf('@', 0) < 0 - && node.equals(currentNode.substring(0, currentNode - .indexOf('@', 0)))) { - send(aname, msg); - } else { - // other node - final OtpCookedConnection conn = home.getConnection(node); - if (conn == null) { - return; - } - conn.send(self, aname, msg); - } - } catch (final Exception e) { - } + final OtpErlangObject msg) { + try { + final String currentNode = home.node(); + if (node.equals(currentNode)) { + send(aname, msg); + } else if (node.indexOf('@', 0) < 0 + && node.equals(currentNode.substring(0, + currentNode.indexOf('@', 0)))) { + send(aname, msg); + } else { + // other node + final OtpCookedConnection conn = home.getConnection(node); + if (conn == null) { + return; + } + conn.send(self, aname, msg); + } + } catch (final Exception e) { + } } /** * Close this mailbox with the given reason. - * + * * <p> * After this operation, the mailbox will no longer be able to receive * messages. Any delivered but as yet unretrieved messages can still be * retrieved however. * </p> - * + * * <p> * If there are links from this mailbox to other {@link OtpErlangPid pids}, * they will be broken when this method is called and exit signals will be * sent. * </p> - * + * * @param reason - * an Erlang term describing the reason for the exit. + * an Erlang term describing the reason for the exit. */ public void exit(final OtpErlangObject reason) { - home.closeMbox(this, reason); + home.closeMbox(this, reason); } /** * Equivalent to <code>exit(new OtpErlangAtom(reason))</code>. - * + * * @see #exit(OtpErlangObject) */ public void exit(final String reason) { - exit(new OtpErlangAtom(reason)); + exit(new OtpErlangAtom(reason)); } /** @@ -434,17 +434,17 @@ public class OtpMbox { * does not cause any links to be broken, except indirectly if the remote * {@link OtpErlangPid pid} exits as a result of this exit signal. * </p> - * + * * @param to - * the {@link OtpErlangPid pid} to which the exit signal - * should be sent. - * + * the {@link OtpErlangPid pid} to which the exit signal should + * be sent. + * * @param reason - * an Erlang term indicating the reason for the exit. + * an Erlang term indicating the reason for the exit. */ // it's called exit, but it sends exit2 public void exit(final OtpErlangPid to, final OtpErlangObject reason) { - exit(2, to, reason); + exit(2, to, reason); } /** @@ -452,38 +452,38 @@ public class OtpMbox { * Equivalent to <code>exit(to, new * OtpErlangAtom(reason))</code>. * </p> - * + * * @see #exit(OtpErlangPid, OtpErlangObject) */ public void exit(final OtpErlangPid to, final String reason) { - exit(to, new OtpErlangAtom(reason)); + exit(to, new OtpErlangAtom(reason)); } // this function used internally when "process" dies // since Erlang discerns between exit and exit/2. private void exit(final int arity, final OtpErlangPid to, - final OtpErlangObject reason) { - try { - final String node = to.node(); - if (node.equals(home.node())) { - home.deliver(new OtpMsg(OtpMsg.exitTag, self, to, reason)); - } else { - final OtpCookedConnection conn = home.getConnection(node); - if (conn == null) { - return; - } - switch (arity) { - case 1: - conn.exit(self, to, reason); - break; - - case 2: - conn.exit2(self, to, reason); - break; - } - } - } catch (final Exception e) { - } + final OtpErlangObject reason) { + try { + final String node = to.node(); + if (node.equals(home.node())) { + home.deliver(new OtpMsg(OtpMsg.exitTag, self, to, reason)); + } else { + final OtpCookedConnection conn = home.getConnection(node); + if (conn == null) { + return; + } + switch (arity) { + case 1: + conn.exit(self, to, reason); + break; + + case 2: + conn.exit2(self, to, reason); + break; + } + } + } catch (final Exception e) { + } } /** @@ -492,7 +492,7 @@ public class OtpMbox { * this method multiple times will not result in more than one link being * created. * </p> - * + * * <p> * If the remote process subsequently exits or the mailbox is closed, a * subsequent attempt to retrieve a message through this mailbox will cause @@ -500,42 +500,42 @@ public class OtpMbox { * if the sending mailbox is closed, the linked mailbox or process will * receive an exit signal. * </p> - * + * * <p> * If the remote process cannot be reached in order to set the link, the * exception is raised immediately. * </p> - * + * * @param to - * the {@link OtpErlangPid pid} representing the object to - * link to. - * + * the {@link OtpErlangPid pid} representing the object to link + * to. + * * @exception OtpErlangExit - * if the {@link OtpErlangPid pid} referred to does not - * exist or could not be reached. - * + * if the {@link OtpErlangPid pid} referred to does not exist + * or could not be reached. + * */ public void link(final OtpErlangPid to) throws OtpErlangExit { - try { - final String node = to.node(); - if (node.equals(home.node())) { - if (!home.deliver(new OtpMsg(OtpMsg.linkTag, self, to))) { - throw new OtpErlangExit("noproc", to); - } - } else { - final OtpCookedConnection conn = home.getConnection(node); - if (conn != null) { - conn.link(self, to); - } else { - throw new OtpErlangExit("noproc", to); - } - } - } catch (final OtpErlangExit e) { - throw e; - } catch (final Exception e) { - } - - links.addLink(self, to); + try { + final String node = to.node(); + if (node.equals(home.node())) { + if (!home.deliver(new OtpMsg(OtpMsg.linkTag, self, to))) { + throw new OtpErlangExit("noproc", to); + } + } else { + final OtpCookedConnection conn = home.getConnection(node); + if (conn != null) { + conn.link(self, to); + } else { + throw new OtpErlangExit("noproc", to); + } + } + } catch (final OtpErlangExit e) { + throw e; + } catch (final Exception e) { + } + + links.addLink(self, to); } /** @@ -545,58 +545,58 @@ public class OtpMbox { * this method once will remove all links between this mailbox and the * remote {@link OtpErlangPid pid}. * </p> - * + * * @param to - * the {@link OtpErlangPid pid} representing the object to - * unlink from. - * + * the {@link OtpErlangPid pid} representing the object to unlink + * from. + * */ public void unlink(final OtpErlangPid to) { - links.removeLink(self, to); - - try { - final String node = to.node(); - if (node.equals(home.node())) { - home.deliver(new OtpMsg(OtpMsg.unlinkTag, self, to)); - } else { - final OtpCookedConnection conn = home.getConnection(node); - if (conn != null) { - conn.unlink(self, to); - } - } - } catch (final Exception e) { - } + links.removeLink(self, to); + + try { + final String node = to.node(); + if (node.equals(home.node())) { + home.deliver(new OtpMsg(OtpMsg.unlinkTag, self, to)); + } else { + final OtpCookedConnection conn = home.getConnection(node); + if (conn != null) { + conn.unlink(self, to); + } + } + } catch (final Exception e) { + } } /** * <p> * Create a connection to a remote node. * </p> - * + * * <p> * Strictly speaking, this method is not necessary simply to set up a * connection, since connections are created automatically first time a * message is sent to a {@link OtpErlangPid pid} on the remote node. * </p> - * + * * <p> * This method makes it possible to wait for a node to come up, however, or * check that a node is still alive. * </p> - * + * * <p> * This method calls a method with the same name in {@link OtpNode#ping * Otpnode} but is provided here for convenience. * </p> - * + * * @param node - * the name of the node to ping. - * + * the name of the node to ping. + * * @param timeout - * the time, in milliseconds, before reporting failure. + * the time, in milliseconds, before reporting failure. */ public boolean ping(final String node, final long timeout) { - return home.ping(node, timeout); + return home.ping(node, timeout); } /** @@ -604,78 +604,78 @@ public class OtpMbox { * Get a list of all known registered names on the same {@link OtpNode node} * as this mailbox. * </p> - * + * * <p> * This method calls a method with the same name in {@link OtpNode#getNames * Otpnode} but is provided here for convenience. * </p> - * + * * @return an array of Strings containing all registered names on this * {@link OtpNode node}. */ public String[] getNames() { - return home.getNames(); + return home.getNames(); } /** * Determine the {@link OtpErlangPid pid} corresponding to a registered name * on this {@link OtpNode node}. - * + * * <p> * This method calls a method with the same name in {@link OtpNode#whereis * Otpnode} but is provided here for convenience. * </p> - * + * * @return the {@link OtpErlangPid pid} corresponding to the registered * name, or null if the name is not known on this node. */ public OtpErlangPid whereis(final String aname) { - return home.whereis(aname); + return home.whereis(aname); } /** * Close this mailbox. - * + * * <p> * After this operation, the mailbox will no longer be able to receive * messages. Any delivered but as yet unretrieved messages can still be * retrieved however. * </p> - * + * * <p> * If there are links from this mailbox to other {@link OtpErlangPid pids}, * they will be broken when this method is called and exit signals with * reason 'normal' will be sent. * </p> - * + * * <p> * This is equivalent to {@link #exit(String) exit("normal")}. * </p> */ public void close() { - home.closeMbox(this); + home.closeMbox(this); } @Override protected void finalize() { - close(); - queue.flush(); + close(); + queue.flush(); } /** * Determine if two mailboxes are equal. - * + * * @return true if both Objects are mailboxes with the same identifying * {@link OtpErlangPid pids}. */ @Override public boolean equals(final Object o) { - if (!(o instanceof OtpMbox)) { - return false; - } + if (!(o instanceof OtpMbox)) { + return false; + } - final OtpMbox m = (OtpMbox) o; - return m.self.equals(self); + final OtpMbox m = (OtpMbox) o; + return m.self.equals(self); } @Override @@ -685,43 +685,43 @@ public class OtpMbox { /* * called by OtpNode to deliver message to this mailbox. - * + * * About exit and exit2: both cause exception to be raised upon receive(). * However exit (not 2) causes any link to be removed as well, while exit2 * leaves any links intact. */ void deliver(final OtpMsg m) { - switch (m.type()) { - case OtpMsg.linkTag: - links.addLink(self, m.getSenderPid()); - break; - - case OtpMsg.unlinkTag: - links.removeLink(self, m.getSenderPid()); - break; - - case OtpMsg.exitTag: - links.removeLink(self, m.getSenderPid()); - queue.put(m); - break; - - case OtpMsg.exit2Tag: - default: - queue.put(m); - break; - } + switch (m.type()) { + case OtpMsg.linkTag: + links.addLink(self, m.getSenderPid()); + break; + + case OtpMsg.unlinkTag: + links.removeLink(self, m.getSenderPid()); + break; + + case OtpMsg.exitTag: + links.removeLink(self, m.getSenderPid()); + queue.put(m); + break; + + case OtpMsg.exit2Tag: + default: + queue.put(m); + break; + } } // used to break all known links to this mbox void breakLinks(final OtpErlangObject reason) { - final Link[] l = links.clearLinks(); + final Link[] l = links.clearLinks(); - if (l != null) { - final int len = l.length; + if (l != null) { + final int len = l.length; - for (int i = 0; i < len; i++) { - exit(1, l[i].remote(), reason); - } - } + for (int i = 0; i < len; i++) { + exit(1, l[i].remote(), reason); + } + } } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpMsg.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpMsg.java index 7c5bc69361..fb750d8afe 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpMsg.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpMsg.java @@ -1,19 +1,19 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2010. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; @@ -22,23 +22,25 @@ package com.ericsson.otp.erlang; * <p> * Provides a carrier for Erlang messages. * </p> - * + * * <p> * Instances of this class are created to package header and payload information * in received Erlang messages so that the recipient can obtain both parts with * a single call to {@link OtpMbox#receiveMsg receiveMsg()}. * </p> - * + * * <p> - * The header information that is available is as follows: <ul> - * <li> a tag indicating the type of message - * <li> the intended recipient of the message, either as a - * {@link OtpErlangPid pid} or as a String, but never both. - * <li> (sometimes) the sender of the message. Due to some eccentric + * The header information that is available is as follows: + * <ul> + * <li>a tag indicating the type of message + * <li>the intended recipient of the message, either as a {@link OtpErlangPid + * pid} or as a String, but never both. + * <li>(sometimes) the sender of the message. Due to some eccentric * characteristics of the Erlang distribution protocol, not all messages have * information about the sending process. In particular, only messages whose tag - * is {@link OtpMsg#regSendTag regSendTag} contain sender information. </ul> - * + * is {@link OtpMsg#regSendTag regSendTag} contain sender information. + * </ul> + * * <p> * Message are sent using the Erlang external format (see separate * documentation). When a message is received and delivered to the recipient @@ -68,87 +70,87 @@ public class OtpMsg { // send has receiver pid but no sender information OtpMsg(final OtpErlangPid to, final OtpInputStream paybuf) { - tag = sendTag; - from = null; - this.to = to; - toName = null; - this.paybuf = paybuf; - payload = null; + tag = sendTag; + from = null; + this.to = to; + toName = null; + this.paybuf = paybuf; + payload = null; } // send has receiver pid but no sender information OtpMsg(final OtpErlangPid to, final OtpErlangObject payload) { - tag = sendTag; - from = null; - this.to = to; - toName = null; - paybuf = null; - this.payload = payload; + tag = sendTag; + from = null; + this.to = to; + toName = null; + paybuf = null; + this.payload = payload; } // send_reg has sender pid and receiver name OtpMsg(final OtpErlangPid from, final String toName, - final OtpInputStream paybuf) { - tag = regSendTag; - this.from = from; - this.toName = toName; - to = null; - this.paybuf = paybuf; - payload = null; + final OtpInputStream paybuf) { + tag = regSendTag; + this.from = from; + this.toName = toName; + to = null; + this.paybuf = paybuf; + payload = null; } // send_reg has sender pid and receiver name OtpMsg(final OtpErlangPid from, final String toName, - final OtpErlangObject payload) { - tag = regSendTag; - this.from = from; - this.toName = toName; - to = null; - paybuf = null; - this.payload = payload; + final OtpErlangObject payload) { + tag = regSendTag; + this.from = from; + this.toName = toName; + to = null; + paybuf = null; + this.payload = payload; } // exit (etc) has from, to, reason OtpMsg(final int tag, final OtpErlangPid from, final OtpErlangPid to, - final OtpErlangObject reason) { - this.tag = tag; - this.from = from; - this.to = to; - paybuf = null; - payload = reason; + final OtpErlangObject reason) { + this.tag = tag; + this.from = from; + this.to = to; + paybuf = null; + payload = reason; } // special case when reason is an atom (i.e. most of the time) OtpMsg(final int tag, final OtpErlangPid from, final OtpErlangPid to, - final String reason) { - this.tag = tag; - this.from = from; - this.to = to; - paybuf = null; - payload = new OtpErlangAtom(reason); + final String reason) { + this.tag = tag; + this.from = from; + this.to = to; + paybuf = null; + payload = new OtpErlangAtom(reason); } // other message types (link, unlink) OtpMsg(final int tag, final OtpErlangPid from, final OtpErlangPid to) { - // convert TT-tags to equiv non-TT versions - int atag = tag; - if (tag > 10) { - atag -= 10; - } + // convert TT-tags to equiv non-TT versions + int atag = tag; + if (tag > 10) { + atag -= 10; + } - this.tag = atag; - this.from = from; - this.to = to; + this.tag = atag; + this.from = from; + this.to = to; } /** * Get the payload from this message without deserializing it. - * + * * @return the serialized Erlang term contained in this message. - * + * */ OtpInputStream getMsgBuf() { - return paybuf; + return paybuf; } /** @@ -157,36 +159,37 @@ public class OtpMsg { * type of message. Valid values are the ``tag'' constants defined in this * class. * </p> - * + * * <p> * The tab identifies not only the type of message but also the content of * the OtpMsg object, since different messages have different components, as * follows: * </p> - * + * * <ul> - * <li> sendTag identifies a "normal" message. The recipient is a - * {@link OtpErlangPid Pid} and it is available through {@link - * #getRecipientPid getRecipientPid()}. Sender information is not available. - * The message body can be retrieved with {@link #getMsg getMsg()}. </li> - * - * <li> regSendTag also identifies a "normal" message. The recipient here is + * <li>sendTag identifies a "normal" message. The recipient is a + * {@link OtpErlangPid Pid} and it is available through + * {@link #getRecipientPid getRecipientPid()}. Sender information is not + * available. The message body can be retrieved with {@link #getMsg + * getMsg()}.</li> + * + * <li>regSendTag also identifies a "normal" message. The recipient here is * a String and it is available through {@link #getRecipientName * getRecipientName()}. Sender information is available through * #getSenderPid getSenderPid()}. The message body can be retrieved with - * {@link #getMsg getMsg()}. </li> - * - * <li> linkTag identifies a link request. The Pid of the sender is - * available, as well as the Pid to which the link should be made. </li> - * - * <li> exitTag and exit2Tag messages are sent as a result of broken links. + * {@link #getMsg getMsg()}.</li> + * + * <li>linkTag identifies a link request. The Pid of the sender is + * available, as well as the Pid to which the link should be made.</li> + * + * <li>exitTag and exit2Tag messages are sent as a result of broken links. * Both sender and recipient Pids and are available through the * corresponding methods, and the "reason" is available through - * {@link #getMsg getMsg()}. </li> + * {@link #getMsg getMsg()}.</li> * </ul> */ public int type() { - return tag; + return tag; } /** @@ -194,42 +197,42 @@ public class OtpMsg { * Deserialize and return a new copy of the message contained in this * OtpMsg. * </p> - * + * * <p> * The first time this method is called the actual payload is deserialized * and the Erlang term is created. Calling this method subsequent times will * not cuase the message to be deserialized additional times, instead the * same Erlang term object will be returned. * </p> - * + * * @return an Erlang term. - * + * * @exception OtpErlangDecodeException - * if the byte stream could not be deserialized. - * + * if the byte stream could not be deserialized. + * */ public OtpErlangObject getMsg() throws OtpErlangDecodeException { - if (payload == null) { - payload = paybuf.read_any(); - } - return payload; + if (payload == null) { + payload = paybuf.read_any(); + } + return payload; } /** * <p> * Get the name of the recipient for this message. * </p> - * + * * <p> * Messages are sent to Pids or names. If this message was sent to a name * then the name is returned by this method. * </p> - * + * * @return the name of the recipient, or null if the recipient was in fact a * Pid. */ public String getRecipientName() { - return toName; + return toName; } /** @@ -237,18 +240,18 @@ public class OtpMsg { * Get the Pid of the recipient for this message, if it is a sendTag * message. * </p> - * + * * <p> * Messages are sent to Pids or names. If this message was sent to a Pid * then the Pid is returned by this method. The recipient Pid is also * available for link, unlink and exit messages. * </p> - * + * * @return the Pid of the recipient, or null if the recipient was in fact a * name. */ public OtpErlangPid getRecipientPid() { - return to; + return to; } /** @@ -256,36 +259,36 @@ public class OtpMsg { * Get the name of the recipient for this message, if it is a regSendTag * message. * </p> - * + * * <p> * Messages are sent to Pids or names. If this message was sent to a name * then the name is returned by this method. * </p> - * + * * @return the Pid of the recipient, or null if the recipient was in fact a * name. */ public Object getRecipient() { - if (toName != null) { - return toName; - } - return to; + if (toName != null) { + return toName; + } + return to; } /** * <p> * Get the Pid of the sender of this message. * </p> - * + * * <p> * For messages sent to names, the Pid of the sender is included with the * message. The sender Pid is also available for link, unlink and exit * messages. It is not available for sendTag messages sent to Pids. * </p> - * + * * @return the Pid of the sender, or null if it was not available. */ public OtpErlangPid getSenderPid() { - return from; + return from; } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpNode.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpNode.java index 68addb9f2c..7512d34c21 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpNode.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpNode.java @@ -1,27 +1,25 @@ -/* +/* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2012. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; import java.io.IOException; import java.lang.ref.WeakReference; -import java.net.ServerSocket; -import java.net.Socket; import java.util.Collection; import java.util.Enumeration; import java.util.Hashtable; @@ -36,21 +34,21 @@ import java.util.Iterator; * communication mechanism is automatic and hidden from the application * programmer. * </p> - * + * * <p> * Once an instance of this class has been created, obtain one or more mailboxes * in order to send or receive messages. The first message sent to a given node * will cause a connection to be set up to that node. Any messages received will * be delivered to the appropriate mailboxes. * </p> - * + * * <p> * To shut down the node, call {@link #close close()}. This will prevent the * node from accepting additional connections and it will cause all existing * connections to be closed. Any unread messages in existing mailboxes can still * be read, however no new messages will be delivered to the mailboxes. * </p> - * + * * <p> * Note that the use of this class requires that Epmd (Erlang Port Mapper * Daemon) is running on each cooperating host. This class does not start Epmd @@ -83,74 +81,156 @@ public class OtpNode extends OtpLocalNode { * directory. The home directory is obtained from the System property * "user.home". * </p> - * + * * <p> * If the file does not exist, an empty string is used. This method makes no * attempt to create the file. * </p> - * + * * @param node * the name of this node. - * + * * @exception IOException * if communication could not be initialized. - * + * */ public OtpNode(final String node) throws IOException { - this(node, defaultCookie, 0); + super(node); + + init(0); + } + + /** + * <p> + * Create a node using the default cookie. The default cookie is found by + * reading the first line of the .erlang.cookie file in the user's home + * directory. The home directory is obtained from the System property + * "user.home". + * </p> + * + * <p> + * If the file does not exist, an empty string is used. This method makes no + * attempt to create the file. + * </p> + * + * @param node + * the name of this node. + * + * @param transportFactory + * the transport factory to use when creating connections. + * + * @exception IOException + * if communication could not be initialized. + * + */ + public OtpNode(final String node, + final OtpTransportFactory transportFactory) throws IOException { + super(node, transportFactory); + + init(0); } /** * Create a node. - * + * * @param node * the name of this node. - * + * * @param cookie * the authorization cookie that will be used by this node when * it communicates with other nodes. - * + * * @exception IOException * if communication could not be initialized. - * + * */ public OtpNode(final String node, final String cookie) throws IOException { - this(node, cookie, 0); + this(node, cookie, 0); + } + + /** + * Create a node. + * + * @param node + * the name of this node. + * + * @param cookie + * the authorization cookie that will be used by this node when + * it communicates with other nodes. + * + * @param transportFactory + * the transport factory to use when creating connections. + * + * @exception IOException + * if communication could not be initialized. + * + */ + public OtpNode(final String node, final String cookie, + final OtpTransportFactory transportFactory) throws IOException { + this(node, cookie, 0, transportFactory); } /** * Create a node. - * + * * @param node * the name of this node. - * + * * @param cookie * the authorization cookie that will be used by this node when * it communicates with other nodes. - * + * * @param port * the port number you wish to use for incoming connections. * Specifying 0 lets the system choose an available port. - * + * * @exception IOException * if communication could not be initialized. - * + * */ public OtpNode(final String node, final String cookie, final int port) - throws IOException { - super(node, cookie); + throws IOException { + super(node, cookie); + + init(port); + } + + /** + * Create a node. + * + * @param node + * the name of this node. + * + * @param cookie + * the authorization cookie that will be used by this node when + * it communicates with other nodes. + * + * @param port + * the port number you wish to use for incoming connections. + * Specifying 0 lets the system choose an available port. + * + * @param transportFactory + * the transport factory to use when creating connections. + * + * @exception IOException + * if communication could not be initialized. + * + */ + public OtpNode(final String node, final String cookie, final int port, + final OtpTransportFactory transportFactory) throws IOException { + super(node, cookie, transportFactory); - init(port); + init(port); } private synchronized void init(final int aport) throws IOException { - if (!initDone) { - connections = new Hashtable<String, OtpCookedConnection>(17, - (float) 0.95); - mboxes = new Mailboxes(); - acceptor = new Acceptor(aport); - initDone = true; - } + if (!initDone) { + connections = new Hashtable<String, OtpCookedConnection>(17, + (float) 0.95); + mboxes = new Mailboxes(); + acceptor = new Acceptor(aport); + initDone = true; + } } /** @@ -158,24 +238,24 @@ public class OtpNode extends OtpLocalNode { * and close all existing connections. */ public synchronized void close() { - acceptor.quit(); - OtpCookedConnection conn; - final Collection<OtpCookedConnection> coll = connections.values(); - final Iterator<OtpCookedConnection> it = coll.iterator(); - - mboxes.clear(); - - while (it.hasNext()) { - conn = it.next(); - it.remove(); - conn.close(); - } - initDone = false; + acceptor.quit(); + OtpCookedConnection conn; + final Collection<OtpCookedConnection> coll = connections.values(); + final Iterator<OtpCookedConnection> it = coll.iterator(); + + mboxes.clear(); + + while (it.hasNext()) { + conn = it.next(); + it.remove(); + conn.close(); + } + initDone = false; } @Override protected void finalize() { - close(); + close(); } /** @@ -183,65 +263,65 @@ public class OtpNode extends OtpLocalNode { * receive messages with other, similar mailboxes and with Erlang processes. * Messages can be sent to this mailbox by using its associated * {@link OtpMbox#self() pid}. - * + * * @return a mailbox. */ public OtpMbox createMbox() { - return mboxes.create(); + return mboxes.create(); } /** * Close the specified mailbox with reason 'normal'. - * + * * @param mbox * the mailbox to close. - * + * * <p> * After this operation, the mailbox will no longer be able to * receive messages. Any delivered but as yet unretrieved * messages can still be retrieved however. * </p> - * + * * <p> * If there are links from the mailbox to other * {@link OtpErlangPid pids}, they will be broken when this * method is called and exit signals with reason 'normal' will be * sent. * </p> - * + * */ public void closeMbox(final OtpMbox mbox) { - closeMbox(mbox, new OtpErlangAtom("normal")); + closeMbox(mbox, new OtpErlangAtom("normal")); } /** * Close the specified mailbox with the given reason. - * + * * @param mbox * the mailbox to close. * @param reason * an Erlang term describing the reason for the termination. - * + * * <p> * After this operation, the mailbox will no longer be able to * receive messages. Any delivered but as yet unretrieved * messages can still be retrieved however. * </p> - * + * * <p> * If there are links from the mailbox to other * {@link OtpErlangPid pids}, they will be broken when this * method is called and exit signals with the given reason will * be sent. * </p> - * + * */ public void closeMbox(final OtpMbox mbox, final OtpErlangObject reason) { - if (mbox != null) { - mboxes.remove(mbox); - mbox.name = null; - mbox.breakLinks(reason); - } + if (mbox != null) { + mboxes.remove(mbox); + mbox.name = null; + mbox.breakLinks(reason); + } } /** @@ -249,16 +329,16 @@ public class OtpNode extends OtpLocalNode { * with other, similar mailboxes and with Erlang processes. Messages can be * sent to this mailbox by using its registered name or the associated * {@link OtpMbox#self() pid}. - * + * * @param name * a name to register for this mailbox. The name must be unique * within this OtpNode. - * + * * @return a mailbox, or null if the name was already in use. - * + * */ public OtpMbox createMbox(final String name) { - return mboxes.create(name); + return mboxes.create(name); } /** @@ -269,58 +349,58 @@ public class OtpNode extends OtpLocalNode { * name; if the mailbox already had a name, calling this method will * supercede that name. * </p> - * + * * @param name * the name to register for the mailbox. Specify null to * unregister the existing name from this mailbox. - * + * * @param mbox * the mailbox to associate with the name. - * + * * @return true if the name was available, or false otherwise. */ public boolean registerName(final String name, final OtpMbox mbox) { - return mboxes.register(name, mbox); + return mboxes.register(name, mbox); } /** * Get a list of all known registered names on this node. - * + * * @return an array of Strings, containins all known registered names on * this node. */ public String[] getNames() { - return mboxes.names(); + return mboxes.names(); } /** * Determine the {@link OtpErlangPid pid} corresponding to a registered name * on this node. - * + * * @return the {@link OtpErlangPid pid} corresponding to the registered * name, or null if the name is not known on this node. */ public OtpErlangPid whereis(final String name) { - final OtpMbox m = mboxes.get(name); - if (m != null) { - return m.self(); - } - return null; + final OtpMbox m = mboxes.get(name); + if (m != null) { + return m.self(); + } + return null; } /** * Register interest in certain system events. The {@link OtpNodeStatus * OtpNodeStatus} handler object contains callback methods, that will be * called when certain events occur. - * + * * @param ahandler * the callback object to register. To clear the handler, specify * null as the handler to use. - * + * */ public synchronized void registerStatusHandler(final OtpNodeStatus ahandler) { - this.handler = ahandler; + handler = ahandler; } /** @@ -329,7 +409,7 @@ public class OtpNode extends OtpLocalNode { * setting up a connection to the remote node (if possible). Only a single * outgoing message is sent; the timeout is how long to wait for a response. * </p> - * + * * <p> * Only a single attempt is made to connect to the remote node, so for * example it is not possible to specify an extremely long timeout and @@ -337,74 +417,73 @@ public class OtpNode extends OtpLocalNode { * wait for a remote node to be started, the following construction may be * useful: * </p> - * + * * <pre> * // ping every 2 seconds until positive response * while (!me.ping(him, 2000)) * ; * </pre> - * + * * @param anode * the name of the node to ping. - * + * * @param timeout * the time, in milliseconds, to wait for response before * returning false. - * + * * @return true if the node was alive and the correct ping response was * returned. false if the correct response was not returned on time. */ /* * internal info about the message formats... - * + * * the request: -> REG_SEND {6,#Pid<[email protected]>,'',net_kernel} * {'$gen_call',{#Pid<[email protected]>,#Ref<[email protected]>},{is_auth,bingo@aule}} - * + * * the reply: <- SEND {2,'',#Pid<[email protected]>} {#Ref<[email protected]>,yes} */ public boolean ping(final String anode, final long timeout) { - if (anode.equals(this.node)) { - return true; - } else if (anode.indexOf('@', 0) < 0 - && anode.equals(this.node - .substring(0, this.node.indexOf('@', 0)))) { - return true; - } - - // other node - OtpMbox mbox = null; - try { - mbox = createMbox(); - mbox.send("net_kernel", anode, getPingTuple(mbox)); - final OtpErlangObject reply = mbox.receive(timeout); - - final OtpErlangTuple t = (OtpErlangTuple) reply; - final OtpErlangAtom a = (OtpErlangAtom) t.elementAt(1); - return "yes".equals(a.atomValue()); - } catch (final Exception e) { - } finally { - closeMbox(mbox); - } - return false; + if (anode.equals(node)) { + return true; + } else if (anode.indexOf('@', 0) < 0 + && anode.equals(node.substring(0, node.indexOf('@', 0)))) { + return true; + } + + // other node + OtpMbox mbox = null; + try { + mbox = createMbox(); + mbox.send("net_kernel", anode, getPingTuple(mbox)); + final OtpErlangObject reply = mbox.receive(timeout); + + final OtpErlangTuple t = (OtpErlangTuple) reply; + final OtpErlangAtom a = (OtpErlangAtom) t.elementAt(1); + return "yes".equals(a.atomValue()); + } catch (final Exception e) { + } finally { + closeMbox(mbox); + } + return false; } /* create the outgoing ping message */ private OtpErlangTuple getPingTuple(final OtpMbox mbox) { - final OtpErlangObject[] ping = new OtpErlangObject[3]; - final OtpErlangObject[] pid = new OtpErlangObject[2]; - final OtpErlangObject[] anode = new OtpErlangObject[2]; + final OtpErlangObject[] ping = new OtpErlangObject[3]; + final OtpErlangObject[] pid = new OtpErlangObject[2]; + final OtpErlangObject[] anode = new OtpErlangObject[2]; - pid[0] = mbox.self(); - pid[1] = createRef(); + pid[0] = mbox.self(); + pid[1] = createRef(); - anode[0] = new OtpErlangAtom("is_auth"); - anode[1] = new OtpErlangAtom(node()); + anode[0] = new OtpErlangAtom("is_auth"); + anode[1] = new OtpErlangAtom(node()); - ping[0] = new OtpErlangAtom("$gen_call"); - ping[1] = new OtpErlangTuple(pid); - ping[2] = new OtpErlangTuple(anode); + ping[0] = new OtpErlangAtom("$gen_call"); + ping[1] = new OtpErlangTuple(pid); + ping[2] = new OtpErlangTuple(anode); - return new OtpErlangTuple(ping); + return new OtpErlangTuple(ping); } /* @@ -412,27 +491,27 @@ public class OtpNode extends OtpLocalNode { * pings. */ private boolean netKernel(final OtpMsg m) { - OtpMbox mbox = null; - try { - final OtpErlangTuple t = (OtpErlangTuple) m.getMsg(); - final OtpErlangTuple req = (OtpErlangTuple) t.elementAt(1); // actual - // request - - final OtpErlangPid pid = (OtpErlangPid) req.elementAt(0); // originating - // pid - - final OtpErlangObject[] pong = new OtpErlangObject[2]; - pong[0] = req.elementAt(1); // his #Ref - pong[1] = new OtpErlangAtom("yes"); - - mbox = createMbox(); - mbox.send(pid, new OtpErlangTuple(pong)); - return true; - } catch (final Exception e) { - } finally { - closeMbox(mbox); - } - return false; + OtpMbox mbox = null; + try { + final OtpErlangTuple t = (OtpErlangTuple) m.getMsg(); + final OtpErlangTuple req = (OtpErlangTuple) t.elementAt(1); // actual + // request + + final OtpErlangPid pid = (OtpErlangPid) req.elementAt(0); // originating + // pid + + final OtpErlangObject[] pong = new OtpErlangObject[2]; + pong[0] = req.elementAt(1); // his #Ref + pong[1] = new OtpErlangAtom("yes"); + + mbox = createMbox(); + mbox.send(pid, new OtpErlangTuple(pong)); + return true; + } catch (final Exception e) { + } finally { + closeMbox(mbox); + } + return false; } /* @@ -440,31 +519,31 @@ public class OtpNode extends OtpLocalNode { * delivered successfully, or false otherwise. */ boolean deliver(final OtpMsg m) { - OtpMbox mbox = null; - - try { - final int t = m.type(); - - if (t == OtpMsg.regSendTag) { - final String name = m.getRecipientName(); - /* special case for netKernel requests */ - if (name.equals("net_kernel")) { - return netKernel(m); - } - mbox = mboxes.get(name); - } else { - mbox = mboxes.get(m.getRecipientPid()); - } - - if (mbox == null) { - return false; - } - mbox.deliver(m); - } catch (final Exception e) { - return false; - } - - return true; + OtpMbox mbox = null; + + try { + final int t = m.type(); + + if (t == OtpMsg.regSendTag) { + final String name = m.getRecipientName(); + /* special case for netKernel requests */ + if (name.equals("net_kernel")) { + return netKernel(m); + } + mbox = mboxes.get(name); + } else { + mbox = mboxes.get(m.getRecipientPid()); + } + + if (mbox == null) { + return false; + } + mbox.deliver(m); + } catch (final Exception e) { + return false; + } + + return true; } /* @@ -472,86 +551,86 @@ public class OtpNode extends OtpLocalNode { * specified by the application */ void deliverError(final OtpCookedConnection conn, final Exception e) { - removeConnection(conn); - remoteStatus(conn.name, false, e); + removeConnection(conn); + remoteStatus(conn.name, false, e); } /* * find or create a connection to the given node */ OtpCookedConnection getConnection(final String anode) { - OtpPeer peer = null; - OtpCookedConnection conn = null; - - synchronized (connections) { - // first just try looking up the name as-is - conn = connections.get(anode); - - if (conn == null) { - // in case node had no '@' add localhost info and try again - peer = new OtpPeer(anode); - conn = connections.get(peer.node()); - - if (conn == null) { - try { - conn = new OtpCookedConnection(this, peer); - conn.setFlags(connFlags); - addConnection(conn); - } catch (final Exception e) { - /* false = outgoing */ - connAttempt(peer.node(), false, e); - } - } - } - return conn; - } + OtpPeer peer = null; + OtpCookedConnection conn = null; + + synchronized (connections) { + // first just try looking up the name as-is + conn = connections.get(anode); + + if (conn == null) { + // in case node had no '@' add localhost info and try again + peer = new OtpPeer(anode); + conn = connections.get(peer.node()); + + if (conn == null) { + try { + conn = new OtpCookedConnection(this, peer); + conn.setFlags(connFlags); + addConnection(conn); + } catch (final Exception e) { + /* false = outgoing */ + connAttempt(peer.node(), false, e); + } + } + } + return conn; + } } void addConnection(final OtpCookedConnection conn) { - if (conn != null && conn.name != null) { - connections.put(conn.name, conn); - remoteStatus(conn.name, true, null); - } + if (conn != null && conn.name != null) { + connections.put(conn.name, conn); + remoteStatus(conn.name, true, null); + } } private void removeConnection(final OtpCookedConnection conn) { - if (conn != null && conn.name != null) { - connections.remove(conn.name); - } + if (conn != null && conn.name != null) { + connections.remove(conn.name); + } } /* use these wrappers to call handler functions */ - private synchronized void remoteStatus(final String anode, final boolean up, - final Object info) { - if (handler == null) { - return; - } - try { - handler.remoteStatus(anode, up, info); - } catch (final Exception e) { - } + private synchronized void remoteStatus(final String anode, + final boolean up, final Object info) { + if (handler == null) { + return; + } + try { + handler.remoteStatus(anode, up, info); + } catch (final Exception e) { + } } synchronized void localStatus(final String anode, final boolean up, - final Object info) { - if (handler == null) { - return; - } - try { - handler.localStatus(anode, up, info); - } catch (final Exception e) { - } + final Object info) { + if (handler == null) { + return; + } + try { + handler.localStatus(anode, up, info); + } catch (final Exception e) { + } } synchronized void connAttempt(final String anode, final boolean incoming, - final Object info) { - if (handler == null) { - return; - } - try { - handler.connAttempt(anode, incoming, info); - } catch (final Exception e) { - } + final Object info) { + if (handler == null) { + return; + } + try { + handler.connAttempt(anode, incoming, info); + } catch (final Exception e) { + } } /* @@ -559,248 +638,248 @@ public class OtpNode extends OtpLocalNode { * references */ public class Mailboxes { - // mbox pids here - private Hashtable<OtpErlangPid, WeakReference<OtpMbox>> byPid = null; - // mbox names here - private Hashtable<String, WeakReference<OtpMbox>> byName = null; - - public Mailboxes() { - byPid = new Hashtable<OtpErlangPid, WeakReference<OtpMbox>>(17, - (float) 0.95); - byName = new Hashtable<String, WeakReference<OtpMbox>>(17, - (float) 0.95); - } - - public OtpMbox create(final String name) { - OtpMbox m = null; - - synchronized (byName) { - if (get(name) != null) { - return null; - } - final OtpErlangPid pid = createPid(); - m = new OtpMbox(OtpNode.this, pid, name); - byPid.put(pid, new WeakReference<OtpMbox>(m)); - byName.put(name, new WeakReference<OtpMbox>(m)); - } - return m; - } - - public OtpMbox create() { - final OtpErlangPid pid = createPid(); - final OtpMbox m = new OtpMbox(OtpNode.this, pid); - byPid.put(pid, new WeakReference<OtpMbox>(m)); - return m; - } - - public void clear() { - byPid.clear(); - byName.clear(); - } - - public String[] names() { - String allnames[] = null; - - synchronized (byName) { - final int n = byName.size(); - final Enumeration<String> keys = byName.keys(); - allnames = new String[n]; - - int i = 0; - while (keys.hasMoreElements()) { - allnames[i++] = keys.nextElement(); - } - } - return allnames; - } - - public boolean register(final String name, final OtpMbox mbox) { - if (name == null) { - if (mbox.name != null) { - byName.remove(mbox.name); - mbox.name = null; - } - } else { - synchronized (byName) { - if (get(name) != null) { - return false; - } - byName.put(name, new WeakReference<OtpMbox>(mbox)); - mbox.name = name; - } - } - return true; - } - - /* - * look up a mailbox based on its name. If the mailbox has gone out of - * scope we also remove the reference from the hashtable so we don't - * find it again. - */ - public OtpMbox get(final String name) { - final WeakReference<OtpMbox> wr = byName.get(name); - - if (wr != null) { - final OtpMbox m = wr.get(); - - if (m != null) { - return m; - } - byName.remove(name); - } - return null; - } - - /* - * look up a mailbox based on its pid. If the mailbox has gone out of - * scope we also remove the reference from the hashtable so we don't - * find it again. - */ - public OtpMbox get(final OtpErlangPid pid) { - final WeakReference<OtpMbox> wr = byPid.get(pid); - - if (wr != null) { - final OtpMbox m = wr.get(); - - if (m != null) { - return m; - } - byPid.remove(pid); - } - return null; - } - - public void remove(final OtpMbox mbox) { - byPid.remove(mbox.self); - if (mbox.name != null) { - byName.remove(mbox.name); - } - } + // mbox pids here + private Hashtable<OtpErlangPid, WeakReference<OtpMbox>> byPid = null; + // mbox names here + private Hashtable<String, WeakReference<OtpMbox>> byName = null; + + public Mailboxes() { + byPid = new Hashtable<OtpErlangPid, WeakReference<OtpMbox>>(17, + (float) 0.95); + byName = new Hashtable<String, WeakReference<OtpMbox>>(17, + (float) 0.95); + } + + public OtpMbox create(final String name) { + OtpMbox m = null; + + synchronized (byName) { + if (get(name) != null) { + return null; + } + final OtpErlangPid pid = createPid(); + m = new OtpMbox(OtpNode.this, pid, name); + byPid.put(pid, new WeakReference<OtpMbox>(m)); + byName.put(name, new WeakReference<OtpMbox>(m)); + } + return m; + } + + public OtpMbox create() { + final OtpErlangPid pid = createPid(); + final OtpMbox m = new OtpMbox(OtpNode.this, pid); + byPid.put(pid, new WeakReference<OtpMbox>(m)); + return m; + } + + public void clear() { + byPid.clear(); + byName.clear(); + } + + public String[] names() { + String allnames[] = null; + + synchronized (byName) { + final int n = byName.size(); + final Enumeration<String> keys = byName.keys(); + allnames = new String[n]; + + int i = 0; + while (keys.hasMoreElements()) { + allnames[i++] = keys.nextElement(); + } + } + return allnames; + } + + public boolean register(final String name, final OtpMbox mbox) { + if (name == null) { + if (mbox.name != null) { + byName.remove(mbox.name); + mbox.name = null; + } + } else { + synchronized (byName) { + if (get(name) != null) { + return false; + } + byName.put(name, new WeakReference<OtpMbox>(mbox)); + mbox.name = name; + } + } + return true; + } + + /* + * look up a mailbox based on its name. If the mailbox has gone out of + * scope we also remove the reference from the hashtable so we don't + * find it again. + */ + public OtpMbox get(final String name) { + final WeakReference<OtpMbox> wr = byName.get(name); + + if (wr != null) { + final OtpMbox m = wr.get(); + + if (m != null) { + return m; + } + byName.remove(name); + } + return null; + } + + /* + * look up a mailbox based on its pid. If the mailbox has gone out of + * scope we also remove the reference from the hashtable so we don't + * find it again. + */ + public OtpMbox get(final OtpErlangPid pid) { + final WeakReference<OtpMbox> wr = byPid.get(pid); + + if (wr != null) { + final OtpMbox m = wr.get(); + + if (m != null) { + return m; + } + byPid.remove(pid); + } + return null; + } + + public void remove(final OtpMbox mbox) { + byPid.remove(mbox.self); + if (mbox.name != null) { + byName.remove(mbox.name); + } + } } /* * this thread simply listens for incoming connections */ public class Acceptor extends Thread { - private final ServerSocket sock; - private final int acceptorPort; - private volatile boolean done = false; - - Acceptor(final int port) throws IOException { - sock = new ServerSocket(port); - this.acceptorPort = sock.getLocalPort(); - OtpNode.this.port = this.acceptorPort; - - setDaemon(true); - setName("acceptor"); - publishPort(); - start(); - } - - private boolean publishPort() throws IOException { - if (getEpmd() != null) { - return false; // already published - } - OtpEpmd.publishPort(OtpNode.this); - return true; - } - - private void unPublishPort() { - // unregister with epmd - OtpEpmd.unPublishPort(OtpNode.this); - - // close the local descriptor (if we have one) - closeSock(epmd); - epmd = null; - } - - public void quit() { - unPublishPort(); - done = true; - closeSock(sock); - localStatus(node, false, null); - } - - private void closeSock(final ServerSocket s) { - try { - if (s != null) { - s.close(); - } - } catch (final Exception e) { - } - } - - private void closeSock(final Socket s) { - try { - if (s != null) { - s.close(); - } - } catch (final Exception e) { - } - } - - public int port() { - return acceptorPort; - } - - @Override - public void run() { - Socket newsock = null; - OtpCookedConnection conn = null; - - localStatus(node, true, null); - - accept_loop: while (!done) { - conn = null; - - try { - newsock = sock.accept(); - } catch (final Exception e) { - // Problem in java1.2.2: accept throws SocketException - // when socket is closed. This will happen when - // acceptor.quit() - // is called. acceptor.quit() will call localStatus(...), so - // we have to check if that's where we come from. - if (!done) { - localStatus(node, false, e); - } - break accept_loop; - } - - try { - synchronized (connections) { - conn = new OtpCookedConnection(OtpNode.this, newsock); - conn.setFlags(connFlags); - addConnection(conn); - } - } catch (final OtpAuthException e) { - if (conn != null && conn.name != null) { - connAttempt(conn.name, true, e); - } else { - connAttempt("unknown", true, e); - } - closeSock(newsock); - } catch (final IOException e) { - if (conn != null && conn.name != null) { - connAttempt(conn.name, true, e); - } else { - connAttempt("unknown", true, e); - } - closeSock(newsock); - } catch (final Exception e) { - closeSock(newsock); - closeSock(sock); - localStatus(node, false, e); - break accept_loop; - } - } // while - - // if we have exited loop we must do this too - unPublishPort(); - } + private final OtpServerTransport sock; + private final int acceptorPort; + private volatile boolean done = false; + + Acceptor(final int port) throws IOException { + sock = createServerTransport(port); + acceptorPort = sock.getLocalPort(); + OtpNode.this.port = acceptorPort; + + setDaemon(true); + setName("acceptor"); + publishPort(); + start(); + } + + private boolean publishPort() throws IOException { + if (getEpmd() != null) { + return false; // already published + } + OtpEpmd.publishPort(OtpNode.this); + return true; + } + + private void unPublishPort() { + // unregister with epmd + OtpEpmd.unPublishPort(OtpNode.this); + + // close the local descriptor (if we have one) + closeSock(epmd); + epmd = null; + } + + public void quit() { + unPublishPort(); + done = true; + closeSock(sock); + localStatus(node, false, null); + } + + private void closeSock(final OtpServerTransport s) { + try { + if (s != null) { + s.close(); + } + } catch (final Exception e) { + } + } + + private void closeSock(final OtpTransport s) { + try { + if (s != null) { + s.close(); + } + } catch (final Exception e) { + } + } + + public int port() { + return acceptorPort; + } + + @Override + public void run() { + OtpTransport newsock = null; + OtpCookedConnection conn = null; + + localStatus(node, true, null); + + accept_loop: while (!done) { + conn = null; + + try { + newsock = sock.accept(); + } catch (final Exception e) { + // Problem in java1.2.2: accept throws SocketException + // when socket is closed. This will happen when + // acceptor.quit() + // is called. acceptor.quit() will call localStatus(...), so + // we have to check if that's where we come from. + if (!done) { + localStatus(node, false, e); + } + break accept_loop; + } + + try { + synchronized (connections) { + conn = new OtpCookedConnection(OtpNode.this, newsock); + conn.setFlags(connFlags); + addConnection(conn); + } + } catch (final OtpAuthException e) { + if (conn != null && conn.name != null) { + connAttempt(conn.name, true, e); + } else { + connAttempt("unknown", true, e); + } + closeSock(newsock); + } catch (final IOException e) { + if (conn != null && conn.name != null) { + connAttempt(conn.name, true, e); + } else { + connAttempt("unknown", true, e); + } + closeSock(newsock); + } catch (final Exception e) { + closeSock(newsock); + closeSock(sock); + localStatus(node, false, e); + break accept_loop; + } + } // while + + // if we have exited loop we must do this too + unPublishPort(); + } } public void setFlags(final int flags) { - this.connFlags = flags; + connFlags = flags; } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpNodeStatus.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpNodeStatus.java index aee1f8b67a..889f1d1b1f 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpNodeStatus.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpNodeStatus.java @@ -1,19 +1,19 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; @@ -25,13 +25,13 @@ package com.ericsson.otp.erlang; * with your {@link OtpNode OtpNode} when you wish to be notified about such * status changes and other similar events. * </p> - * + * * <p> * This class provides default handers that ignore all events. Applications are * expected to extend this class in order to act on events that are deemed * interesting. * </p> - * + * * <p> * <b> Note that this class is likely to change in the near future </b> * </p> @@ -42,59 +42,57 @@ public class OtpNodeStatus { /** * Notify about remote node status changes. - * + * * @param node - * the node whose status change is being indicated by this - * call. - * + * the node whose status change is being indicated by this call. + * * @param up - * true if the node has come up, false if it has gone down. - * + * true if the node has come up, false if it has gone down. + * * @param info - * additional info that may be available, for example an - * exception that was raised causing the event in question - * (may be null). - * + * additional info that may be available, for example an + * exception that was raised causing the event in question (may + * be null). + * */ public void remoteStatus(final String node, final boolean up, - final Object info) { + final Object info) { } /** * Notify about local node exceptions. - * + * * @param node - * the node whose status change is being indicated by this - * call. - * + * the node whose status change is being indicated by this call. + * * @param up - * true if the node has come up, false if it has gone down. - * + * true if the node has come up, false if it has gone down. + * * @param info - * additional info that may be available, for example an - * exception that was raised causing the event in question - * (may be null). + * additional info that may be available, for example an + * exception that was raised causing the event in question (may + * be null). */ public void localStatus(final String node, final boolean up, - final Object info) { + final Object info) { } /** * Notify about failed connection attempts. - * + * * @param node - * The name of the remote node - * + * The name of the remote node + * * @param incoming - * The direction of the connection attempt, i.e. true for - * incoming, false for outgoing. - * + * The direction of the connection attempt, i.e. true for + * incoming, false for outgoing. + * * @param info - * additional info that may be available, for example an - * exception that was raised causing the event in question - * (may be null). + * additional info that may be available, for example an + * exception that was raised causing the event in question (may + * be null). */ public void connAttempt(final String node, final boolean incoming, - final Object info) { + final Object info) { } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpOutputStream.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpOutputStream.java index ef60a9f38a..2ec583ff5c 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpOutputStream.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpOutputStream.java @@ -1,19 +1,19 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2013. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; @@ -21,6 +21,7 @@ package com.ericsson.otp.erlang; // import java.io.OutputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.math.BigDecimal; import java.math.BigInteger; @@ -30,17 +31,20 @@ import java.util.zip.Deflater; /** * Provides a stream for encoding Erlang terms to external format, for * transmission or storage. - * + * * <p> * Note that this class is not synchronized, if you need synchronization you * must provide it yourself. - * + * */ public class OtpOutputStream extends ByteArrayOutputStream { /** The default initial size of the stream. * */ public static final int defaultInitialSize = 2048; - /** The default increment used when growing the stream (increment at least this much). * */ + /** + * The default increment used when growing the stream (increment at least + * this much). * + */ public static final int defaultIncrement = 2048; // static formats, used to encode floats and doubles @@ -57,66 +61,66 @@ public class OtpOutputStream extends ByteArrayOutputStream { * Create a stream with the default initial size (2048 bytes). */ public OtpOutputStream() { - this(defaultInitialSize); + this(defaultInitialSize); } /** * Create a stream with the specified initial size. */ public OtpOutputStream(final int size) { - super(size); + super(size); } /** * Create a stream containing the encoded version of the given Erlang term. */ public OtpOutputStream(final OtpErlangObject o) { - this(); - write_any(o); + this(); + write_any(o); } // package scope /* * Get the contents of the output stream as an input stream instead. This is * used internally in {@link OtpCconnection} for tracing outgoing packages. - * + * * @param offset where in the output stream to read data from when creating * the input stream. The offset is necessary because header contents start 5 * bytes into the header buffer, whereas payload contents start at the * beginning - * + * * @return an input stream containing the same raw data. */ OtpInputStream getOtpInputStream(final int offset) { - return new OtpInputStream(super.buf, offset, super.count - offset, 0); + return new OtpInputStream(super.buf, offset, super.count - offset, 0); } /** * Get the current position in the stream. - * + * * @return the current position in the stream. */ public int getPos() { - return super.count; + return super.count; } /** * Trims the capacity of this <tt>OtpOutputStream</tt> instance to be the - * buffer's current size. An application can use this operation to minimize + * buffer's current size. An application can use this operation to minimize * the storage of an <tt>OtpOutputStream</tt> instance. */ public void trimToSize() { - resize(super.count); + resize(super.count); } - private void resize(int size) { - if (size < super.buf.length) { - final byte[] tmp = new byte[size]; - System.arraycopy(super.buf, 0, tmp, 0, size); - super.buf = tmp; - } else if (size > super.buf.length) { - ensureCapacity(size); - } + private void resize(final int size) { + if (size < super.buf.length) { + final byte[] tmp = new byte[size]; + System.arraycopy(super.buf, 0, tmp, 0, size); + super.buf = tmp; + } else if (size > super.buf.length) { + ensureCapacity(size); + } } /** @@ -124,228 +128,247 @@ public class OtpOutputStream extends ByteArrayOutputStream { * necessary, to ensure that it can hold at least the number of elements * specified by the minimum capacity argument. * - * @param minCapacity the desired minimum capacity + * @param minCapacity + * the desired minimum capacity */ - public void ensureCapacity(int minCapacity) { - if (minCapacity > fixedSize) { - throw new IllegalArgumentException("Trying to increase fixed-size buffer"); - } - int oldCapacity = super.buf.length; - if (minCapacity > oldCapacity) { - int newCapacity = (oldCapacity * 3)/2 + 1; - if (newCapacity < oldCapacity + defaultIncrement) - newCapacity = oldCapacity + defaultIncrement; - if (newCapacity < minCapacity) - newCapacity = minCapacity; - newCapacity = Math.min(fixedSize, newCapacity); - // minCapacity is usually close to size, so this is a win: - final byte[] tmp = new byte[newCapacity]; - System.arraycopy(super.buf, 0, tmp, 0, super.count); - super.buf = tmp; - } + public void ensureCapacity(final int minCapacity) { + if (minCapacity > fixedSize) { + throw new IllegalArgumentException( + "Trying to increase fixed-size buffer"); + } + final int oldCapacity = super.buf.length; + if (minCapacity > oldCapacity) { + int newCapacity = oldCapacity * 3 / 2 + 1; + if (newCapacity < oldCapacity + defaultIncrement) { + newCapacity = oldCapacity + defaultIncrement; + } + if (newCapacity < minCapacity) { + newCapacity = minCapacity; + } + newCapacity = Math.min(fixedSize, newCapacity); + // minCapacity is usually close to size, so this is a win: + final byte[] tmp = new byte[newCapacity]; + System.arraycopy(super.buf, 0, tmp, 0, super.count); + super.buf = tmp; + } } /** * Write one byte to the stream. - * + * * @param b * the byte to write. - * + * */ public void write(final byte b) { - ensureCapacity(super.count + 1); - super.buf[super.count++] = b; + ensureCapacity(super.count + 1); + super.buf[super.count++] = b; } - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see java.io.ByteArrayOutputStream#write(byte[]) */ @Override public void write(final byte[] abuf) { - // don't assume that super.write(byte[]) calls write(buf, 0, buf.length) - write(abuf, 0, abuf.length); + // don't assume that super.write(byte[]) calls write(buf, 0, buf.length) + write(abuf, 0, abuf.length); } - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see java.io.ByteArrayOutputStream#write(int) */ @Override - public synchronized void write(int b) { - ensureCapacity(super.count + 1); - super.buf[super.count] = (byte) b; - count += 1; + public synchronized void write(final int b) { + ensureCapacity(super.count + 1); + super.buf[super.count] = (byte) b; + count += 1; } - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see java.io.ByteArrayOutputStream#write(byte[], int, int) */ @Override - public synchronized void write(byte[] b, int off, int len) { - if ((off < 0) || (off > b.length) || (len < 0) - || ((off + len) - b.length > 0)) { - throw new IndexOutOfBoundsException(); - } - ensureCapacity(super.count + len); - System.arraycopy(b, off, super.buf, super.count, len); - super.count += len; + public synchronized void write(final byte[] b, final int off, final int len) { + if (off < 0 || off > b.length || len < 0 || off + len - b.length > 0) { + throw new IndexOutOfBoundsException(); + } + ensureCapacity(super.count + len); + System.arraycopy(b, off, super.buf, super.count, len); + super.count += len; + } + + @Override + public synchronized void writeTo(OutputStream out) throws IOException { + super.writeTo(out); + } + + public synchronized void writeToAndFlush(OutputStream out) throws IOException { + super.writeTo(out); + out.flush(); } /** * Write the low byte of a value to the stream. - * + * * @param n * the value to use. - * + * */ public void write1(final long n) { - write((byte) (n & 0xff)); + write((byte) (n & 0xff)); } /** * Write an array of bytes to the stream. - * + * * @param bytes * the array of bytes to write. - * + * */ public void writeN(final byte[] bytes) { - write(bytes); + write(bytes); } /** * Get the current capacity of the stream. As bytes are added the capacity * of the stream is increased automatically, however this method returns the * current size. - * + * * @return the size of the internal buffer used by the stream. */ public int length() { - return super.buf.length; + return super.buf.length; } /** * Get the number of bytes in the stream. - * + * * @return the number of bytes in the stream. - * + * * @deprecated As of Jinterface 1.4, replaced by super.size(). * @see #size() */ @Deprecated public int count() { - return count; + return count; } /** * Write the low two bytes of a value to the stream in big endian order. - * + * * @param n * the value to use. */ public void write2BE(final long n) { - write((byte) ((n & 0xff00) >> 8)); - write((byte) (n & 0xff)); + write((byte) ((n & 0xff00) >> 8)); + write((byte) (n & 0xff)); } /** * Write the low four bytes of a value to the stream in big endian order. - * + * * @param n * the value to use. */ public void write4BE(final long n) { - write((byte) ((n & 0xff000000) >> 24)); - write((byte) ((n & 0xff0000) >> 16)); - write((byte) ((n & 0xff00) >> 8)); - write((byte) (n & 0xff)); + write((byte) ((n & 0xff000000) >> 24)); + write((byte) ((n & 0xff0000) >> 16)); + write((byte) ((n & 0xff00) >> 8)); + write((byte) (n & 0xff)); } /** * Write the low eight (all) bytes of a value to the stream in big endian * order. - * + * * @param n * the value to use. */ public void write8BE(final long n) { - write((byte) (n >> 56 & 0xff)); - write((byte) (n >> 48 & 0xff)); - write((byte) (n >> 40 & 0xff)); - write((byte) (n >> 32 & 0xff)); - write((byte) (n >> 24 & 0xff)); - write((byte) (n >> 16 & 0xff)); - write((byte) (n >> 8 & 0xff)); - write((byte) (n & 0xff)); + write((byte) (n >> 56 & 0xff)); + write((byte) (n >> 48 & 0xff)); + write((byte) (n >> 40 & 0xff)); + write((byte) (n >> 32 & 0xff)); + write((byte) (n >> 24 & 0xff)); + write((byte) (n >> 16 & 0xff)); + write((byte) (n >> 8 & 0xff)); + write((byte) (n & 0xff)); } /** * Write any number of bytes in little endian format. - * + * * @param n * the value to use. * @param b * the number of bytes to write from the little end. */ public void writeLE(final long n, final int b) { - long v = n; - for (int i = 0; i < b; i++) { - write((byte) (v & 0xff)); - v >>= 8; - } + long v = n; + for (int i = 0; i < b; i++) { + write((byte) (v & 0xff)); + v >>= 8; + } } /** * Write the low two bytes of a value to the stream in little endian order. - * + * * @param n * the value to use. */ public void write2LE(final long n) { - write((byte) (n & 0xff)); - write((byte) ((n & 0xff00) >> 8)); + write((byte) (n & 0xff)); + write((byte) ((n & 0xff00) >> 8)); } /** * Write the low four bytes of a value to the stream in little endian order. - * + * * @param n * the value to use. */ public void write4LE(final long n) { - write((byte) (n & 0xff)); - write((byte) ((n & 0xff00) >> 8)); - write((byte) ((n & 0xff0000) >> 16)); - write((byte) ((n & 0xff000000) >> 24)); + write((byte) (n & 0xff)); + write((byte) ((n & 0xff00) >> 8)); + write((byte) ((n & 0xff0000) >> 16)); + write((byte) ((n & 0xff000000) >> 24)); } /** * Write the low eight bytes of a value to the stream in little endian * order. - * + * * @param n * the value to use. */ public void write8LE(final long n) { - write((byte) (n & 0xff)); - write((byte) (n >> 8 & 0xff)); - write((byte) (n >> 16 & 0xff)); - write((byte) (n >> 24 & 0xff)); - write((byte) (n >> 32 & 0xff)); - write((byte) (n >> 40 & 0xff)); - write((byte) (n >> 48 & 0xff)); - write((byte) (n >> 56 & 0xff)); + write((byte) (n & 0xff)); + write((byte) (n >> 8 & 0xff)); + write((byte) (n >> 16 & 0xff)); + write((byte) (n >> 24 & 0xff)); + write((byte) (n >> 32 & 0xff)); + write((byte) (n >> 40 & 0xff)); + write((byte) (n >> 48 & 0xff)); + write((byte) (n >> 56 & 0xff)); } /** * Write the low four bytes of a value to the stream in bif endian order, at * the specified position. If the position specified is beyond the end of * the stream, this method will have no effect. - * + * * Normally this method should be used in conjunction with {@link #size() * size()}, when is is necessary to insert data into the stream before it is * known what the actual value should be. For example: - * + * * <pre> * int pos = s.size(); * s.write4BE(0); // make space for length data, @@ -354,501 +377,495 @@ public class OtpOutputStream extends ByteArrayOutputStream { * // later... when we know the length value * s.poke4BE(pos, length); * </pre> - * - * + * + * * @param offset * the position in the stream. * @param n * the value to use. */ public void poke4BE(final int offset, final long n) { - if (offset < super.count) { - buf[offset + 0] = (byte) ((n & 0xff000000) >> 24); - buf[offset + 1] = (byte) ((n & 0xff0000) >> 16); - buf[offset + 2] = (byte) ((n & 0xff00) >> 8); - buf[offset + 3] = (byte) (n & 0xff); - } + if (offset < super.count) { + buf[offset + 0] = (byte) ((n & 0xff000000) >> 24); + buf[offset + 1] = (byte) ((n & 0xff0000) >> 16); + buf[offset + 2] = (byte) ((n & 0xff00) >> 8); + buf[offset + 3] = (byte) (n & 0xff); + } } /** * Write a string to the stream as an Erlang atom. - * + * * @param atom * the string to write. */ public void write_atom(final String atom) { - String enc_atom; - byte[] bytes; - boolean isLatin1 = true; - - if (atom.codePointCount(0, atom.length()) <= OtpExternal.maxAtomLength) { - enc_atom = atom; - } - else { - /* - * Throwing an exception would be better I think, - * but truncation seems to be the way it has - * been done in other parts of OTP... - */ - enc_atom = new String(OtpErlangString.stringToCodePoints(atom), - 0, OtpExternal.maxAtomLength); - } - - for (int offset = 0; offset < enc_atom.length();) { - final int cp = enc_atom.codePointAt(offset); - if ((cp & ~0xFF) != 0) { - isLatin1 = false; - break; - } - offset += Character.charCount(cp); - } - try { - if (isLatin1) { - bytes = enc_atom.getBytes("ISO-8859-1"); - write1(OtpExternal.atomTag); - write2BE(bytes.length); - } - else { - bytes = enc_atom.getBytes("UTF-8"); - final int length = bytes.length; - if (length < 256) { - write1(OtpExternal.smallAtomUtf8Tag); - write1(length); - } - else { - write1(OtpExternal.atomUtf8Tag); - write2BE(length); - } - } - writeN(bytes); - } catch (final java.io.UnsupportedEncodingException e) { - /* - * Sigh, why didn't the API designer add an - * OtpErlangEncodeException to these encoding - * functions?!? Instead of changing the API we - * write an invalid atom and let it fail for - * whoever trying to decode this... Sigh, - * again... - */ - write1(OtpExternal.smallAtomUtf8Tag); - write1(2); - write2BE(0xffff); /* Invalid UTF-8 */ - } + String enc_atom; + byte[] bytes; + boolean isLatin1 = true; + + if (atom.codePointCount(0, atom.length()) <= OtpExternal.maxAtomLength) { + enc_atom = atom; + } else { + /* + * Throwing an exception would be better I think, but truncation + * seems to be the way it has been done in other parts of OTP... + */ + enc_atom = new String(OtpErlangString.stringToCodePoints(atom), 0, + OtpExternal.maxAtomLength); + } + + for (int offset = 0; offset < enc_atom.length();) { + final int cp = enc_atom.codePointAt(offset); + if ((cp & ~0xFF) != 0) { + isLatin1 = false; + break; + } + offset += Character.charCount(cp); + } + try { + if (isLatin1) { + bytes = enc_atom.getBytes("ISO-8859-1"); + write1(OtpExternal.atomTag); + write2BE(bytes.length); + } else { + bytes = enc_atom.getBytes("UTF-8"); + final int length = bytes.length; + if (length < 256) { + write1(OtpExternal.smallAtomUtf8Tag); + write1(length); + } else { + write1(OtpExternal.atomUtf8Tag); + write2BE(length); + } + } + writeN(bytes); + } catch (final java.io.UnsupportedEncodingException e) { + /* + * Sigh, why didn't the API designer add an OtpErlangEncodeException + * to these encoding functions?!? Instead of changing the API we + * write an invalid atom and let it fail for whoever trying to + * decode this... Sigh, again... + */ + write1(OtpExternal.smallAtomUtf8Tag); + write1(2); + write2BE(0xffff); /* Invalid UTF-8 */ + } } /** * Write an array of bytes to the stream as an Erlang binary. - * + * * @param bin * the array of bytes to write. */ public void write_binary(final byte[] bin) { - write1(OtpExternal.binTag); - write4BE(bin.length); - writeN(bin); + write1(OtpExternal.binTag); + write4BE(bin.length); + writeN(bin); } /** * Write an array of bytes to the stream as an Erlang bitstr. - * + * * @param bin * the array of bytes to write. * @param pad_bits * the number of zero pad bits at the low end of the last byte */ public void write_bitstr(final byte[] bin, final int pad_bits) { - if (pad_bits == 0) { - write_binary(bin); - return; - } - write1(OtpExternal.bitBinTag); - write4BE(bin.length); - write1(8 - pad_bits); - writeN(bin); + if (pad_bits == 0) { + write_binary(bin); + return; + } + write1(OtpExternal.bitBinTag); + write4BE(bin.length); + write1(8 - pad_bits); + writeN(bin); } /** * Write a boolean value to the stream as the Erlang atom 'true' or 'false'. - * + * * @param b * the boolean value to write. */ public void write_boolean(final boolean b) { - write_atom(String.valueOf(b)); + write_atom(String.valueOf(b)); } /** * Write a single byte to the stream as an Erlang integer. The byte is * really an IDL 'octet', that is, unsigned. - * + * * @param b * the byte to use. */ public void write_byte(final byte b) { - this.write_long(b & 0xffL, true); + this.write_long(b & 0xffL, true); } /** * Write a character to the stream as an Erlang integer. The character may * be a 16 bit character, kind of IDL 'wchar'. It is up to the Erlang side * to take care of souch, if they should be used. - * + * * @param c * the character to use. */ public void write_char(final char c) { - this.write_long(c & 0xffffL, true); + this.write_long(c & 0xffffL, true); } /** * Write a double value to the stream. - * + * * @param d * the double to use. */ public void write_double(final double d) { - write1(OtpExternal.newFloatTag); - write8BE(Double.doubleToLongBits(d)); + write1(OtpExternal.newFloatTag); + write8BE(Double.doubleToLongBits(d)); } /** * Write a float value to the stream. - * + * * @param f * the float to use. */ public void write_float(final float f) { - write_double(f); + write_double(f); } public void write_big_integer(final BigInteger v) { - if (v.bitLength() < 64) { - this.write_long(v.longValue(), true); - return; - } - final int signum = v.signum(); - BigInteger val = v; - if (signum < 0) { - val = val.negate(); - } - final byte[] magnitude = val.toByteArray(); - final int n = magnitude.length; - // Reverse the array to make it little endian. - for (int i = 0, j = n; i < j--; i++) { - // Swap [i] with [j] - final byte b = magnitude[i]; - magnitude[i] = magnitude[j]; - magnitude[j] = b; - } - if ((n & 0xFF) == n) { - write1(OtpExternal.smallBigTag); - write1(n); // length - } else { - write1(OtpExternal.largeBigTag); - write4BE(n); // length - } - write1(signum < 0 ? 1 : 0); // sign - // Write the array - writeN(magnitude); + if (v.bitLength() < 64) { + this.write_long(v.longValue(), true); + return; + } + final int signum = v.signum(); + BigInteger val = v; + if (signum < 0) { + val = val.negate(); + } + final byte[] magnitude = val.toByteArray(); + final int n = magnitude.length; + // Reverse the array to make it little endian. + for (int i = 0, j = n; i < j--; i++) { + // Swap [i] with [j] + final byte b = magnitude[i]; + magnitude[i] = magnitude[j]; + magnitude[j] = b; + } + if ((n & 0xFF) == n) { + write1(OtpExternal.smallBigTag); + write1(n); // length + } else { + write1(OtpExternal.largeBigTag); + write4BE(n); // length + } + write1(signum < 0 ? 1 : 0); // sign + // Write the array + writeN(magnitude); } void write_long(final long v, final boolean unsigned) { - /* - * If v<0 and unsigned==true the value - * java.lang.Long.MAX_VALUE-java.lang.Long.MIN_VALUE+1+v is written, i.e - * v is regarded as unsigned two's complement. - */ - if ((v & 0xffL) == v) { - // will fit in one byte - write1(OtpExternal.smallIntTag); - write1(v); - } else { - // note that v != 0L - if (v < 0 && unsigned || v < OtpExternal.erlMin - || v > OtpExternal.erlMax) { - // some kind of bignum - final long abs = unsigned ? v : v < 0 ? -v : v; - final int sign = unsigned ? 0 : v < 0 ? 1 : 0; - int n; - long mask; - for (mask = 0xFFFFffffL, n = 4; (abs & mask) != abs; n++, mask = mask << 8 | 0xffL) { - // count nonzero bytes - } - write1(OtpExternal.smallBigTag); - write1(n); // length - write1(sign); // sign - writeLE(abs, n); // value. obs! little endian - } else { - write1(OtpExternal.intTag); - write4BE(v); - } - } + /* + * If v<0 and unsigned==true the value + * java.lang.Long.MAX_VALUE-java.lang.Long.MIN_VALUE+1+v is written, i.e + * v is regarded as unsigned two's complement. + */ + if ((v & 0xffL) == v) { + // will fit in one byte + write1(OtpExternal.smallIntTag); + write1(v); + } else { + // note that v != 0L + if (v < 0 && unsigned || v < OtpExternal.erlMin + || v > OtpExternal.erlMax) { + // some kind of bignum + final long abs = unsigned ? v : v < 0 ? -v : v; + final int sign = unsigned ? 0 : v < 0 ? 1 : 0; + int n; + long mask; + for (mask = 0xFFFFffffL, n = 4; (abs & mask) != abs; n++, mask = mask << 8 | 0xffL) { + // count nonzero bytes + } + write1(OtpExternal.smallBigTag); + write1(n); // length + write1(sign); // sign + writeLE(abs, n); // value. obs! little endian + } else { + write1(OtpExternal.intTag); + write4BE(v); + } + } } /** * Write a long to the stream. - * + * * @param l * the long to use. */ public void write_long(final long l) { - this.write_long(l, false); + this.write_long(l, false); } /** * Write a positive long to the stream. The long is interpreted as a two's * complement unsigned long even if it is negative. - * + * * @param ul * the long to use. */ public void write_ulong(final long ul) { - this.write_long(ul, true); + this.write_long(ul, true); } /** * Write an integer to the stream. - * + * * @param i * the integer to use. */ public void write_int(final int i) { - this.write_long(i, false); + this.write_long(i, false); } /** * Write a positive integer to the stream. The integer is interpreted as a * two's complement unsigned integer even if it is negative. - * + * * @param ui * the integer to use. */ public void write_uint(final int ui) { - this.write_long(ui & 0xFFFFffffL, true); + this.write_long(ui & 0xFFFFffffL, true); } /** * Write a short to the stream. - * + * * @param s * the short to use. */ public void write_short(final short s) { - this.write_long(s, false); + this.write_long(s, false); } /** * Write a positive short to the stream. The short is interpreted as a two's * complement unsigned short even if it is negative. - * + * * @param us * the short to use. */ public void write_ushort(final short us) { - this.write_long(us & 0xffffL, true); + this.write_long(us & 0xffffL, true); } /** * Write an Erlang list header to the stream. After calling this method, you * must write 'arity' elements to the stream followed by nil, or it will not * be possible to decode it later. - * + * * @param arity * the number of elements in the list. */ public void write_list_head(final int arity) { - if (arity == 0) { - write_nil(); - } else { - write1(OtpExternal.listTag); - write4BE(arity); - } + if (arity == 0) { + write_nil(); + } else { + write1(OtpExternal.listTag); + write4BE(arity); + } } /** * Write an empty Erlang list to the stream. */ public void write_nil() { - write1(OtpExternal.nilTag); + write1(OtpExternal.nilTag); } /** * Write an Erlang tuple header to the stream. After calling this method, * you must write 'arity' elements to the stream or it will not be possible * to decode it later. - * + * * @param arity * the number of elements in the tuple. */ public void write_tuple_head(final int arity) { - if (arity < 0xff) { - write1(OtpExternal.smallTupleTag); - write1(arity); - } else { - write1(OtpExternal.largeTupleTag); - write4BE(arity); - } + if (arity < 0xff) { + write1(OtpExternal.smallTupleTag); + write1(arity); + } else { + write1(OtpExternal.largeTupleTag); + write4BE(arity); + } } /** * Write an Erlang PID to the stream. - * + * * @param node * the nodename. - * + * * @param id * an arbitrary number. Only the low order 15 bits will be used. - * + * * @param serial * another arbitrary number. Only the low order 13 bits will be * used. - * + * * @param creation * yet another arbitrary number. Only the low order 2 bits will * be used. - * + * */ public void write_pid(final String node, final int id, final int serial, - final int creation) { - write1(OtpExternal.pidTag); - write_atom(node); - write4BE(id & 0x7fff); // 15 bits - write4BE(serial & 0x1fff); // 13 bits - write1(creation & 0x3); // 2 bits + final int creation) { + write1(OtpExternal.pidTag); + write_atom(node); + write4BE(id & 0x7fff); // 15 bits + write4BE(serial & 0x1fff); // 13 bits + write1(creation & 0x3); // 2 bits } /** * Write an Erlang port to the stream. - * + * * @param node * the nodename. - * + * * @param id * an arbitrary number. Only the low order 28 bits will be used. - * + * * @param creation * another arbitrary number. Only the low order 2 bits will be * used. - * + * */ public void write_port(final String node, final int id, final int creation) { - write1(OtpExternal.portTag); - write_atom(node); - write4BE(id & 0xfffffff); // 28 bits - write1(creation & 0x3); // 2 bits + write1(OtpExternal.portTag); + write_atom(node); + write4BE(id & 0xfffffff); // 28 bits + write1(creation & 0x3); // 2 bits } /** * Write an old style Erlang ref to the stream. - * + * * @param node * the nodename. - * + * * @param id * an arbitrary number. Only the low order 18 bits will be used. - * + * * @param creation * another arbitrary number. Only the low order 2 bits will be * used. - * + * */ public void write_ref(final String node, final int id, final int creation) { - write1(OtpExternal.refTag); - write_atom(node); - write4BE(id & 0x3ffff); // 18 bits - write1(creation & 0x3); // 2 bits + write1(OtpExternal.refTag); + write_atom(node); + write4BE(id & 0x3ffff); // 18 bits + write1(creation & 0x3); // 2 bits } /** * Write a new style (R6 and later) Erlang ref to the stream. - * + * * @param node * the nodename. - * + * * @param ids * an array of arbitrary numbers. Only the low order 18 bits of * the first number will be used. If the array contains only one * number, an old style ref will be written instead. At most * three numbers will be read from the array. - * + * * @param creation * another arbitrary number. Only the low order 2 bits will be * used. - * + * */ public void write_ref(final String node, final int[] ids, final int creation) { - int arity = ids.length; - if (arity > 3) { - arity = 3; // max 3 words in ref - } + int arity = ids.length; + if (arity > 3) { + arity = 3; // max 3 words in ref + } - if (arity == 1) { - // use old method - this.write_ref(node, ids[0], creation); - } else { - // r6 ref - write1(OtpExternal.newRefTag); + if (arity == 1) { + // use old method + this.write_ref(node, ids[0], creation); + } else { + // r6 ref + write1(OtpExternal.newRefTag); - // how many id values - write2BE(arity); + // how many id values + write2BE(arity); - write_atom(node); + write_atom(node); - // note: creation BEFORE id in r6 ref - write1(creation & 0x3); // 2 bits + // note: creation BEFORE id in r6 ref + write1(creation & 0x3); // 2 bits - // first int gets truncated to 18 bits - write4BE(ids[0] & 0x3ffff); + // first int gets truncated to 18 bits + write4BE(ids[0] & 0x3ffff); - // remaining ones are left as is - for (int i = 1; i < arity; i++) { - write4BE(ids[i]); - } - } + // remaining ones are left as is + for (int i = 1; i < arity; i++) { + write4BE(ids[i]); + } + } } /** * Write a string to the stream. - * + * * @param s * the string to write. */ public void write_string(final String s) { - final int len = s.length(); - - switch (len) { - case 0: - write_nil(); - break; - default: - if (len <= 65535 && is8bitString(s)) { // 8-bit string - try { - final byte[] bytebuf = s.getBytes("ISO-8859-1"); - write1(OtpExternal.stringTag); - write2BE(len); - writeN(bytebuf); - } catch (final UnsupportedEncodingException e) { - write_nil(); // it should never ever get here... - } - } else { // unicode or longer, must code as list - final int[] codePoints = OtpErlangString.stringToCodePoints(s); - write_list_head(codePoints.length); - for (final int codePoint : codePoints) { - write_int(codePoint); - } - write_nil(); - } - } + final int len = s.length(); + + switch (len) { + case 0: + write_nil(); + break; + default: + if (len <= 65535 && is8bitString(s)) { // 8-bit string + try { + final byte[] bytebuf = s.getBytes("ISO-8859-1"); + write1(OtpExternal.stringTag); + write2BE(len); + writeN(bytebuf); + } catch (final UnsupportedEncodingException e) { + write_nil(); // it should never ever get here... + } + } else { // unicode or longer, must code as list + final int[] codePoints = OtpErlangString.stringToCodePoints(s); + write_list_head(codePoints.length); + for (final int codePoint : codePoints) { + write_int(codePoint); + } + write_nil(); + } + } } private boolean is8bitString(final String s) { - for (int i = 0; i < s.length(); ++i) { - final char c = s.charAt(i); - if (c < 0 || c > 255) { - return false; - } - } - return true; + for (int i = 0; i < s.length(); ++i) { + final char c = s.charAt(i); + if (c < 0 || c > 255) { + return false; + } + } + return true; } /** @@ -858,7 +875,7 @@ public class OtpOutputStream extends ByteArrayOutputStream { * the Erlang term to write. */ public void write_compressed(final OtpErlangObject o) { - write_compressed(o, Deflater.DEFAULT_COMPRESSION); + write_compressed(o, Deflater.DEFAULT_COMPRESSION); } /** @@ -869,119 +886,119 @@ public class OtpOutputStream extends ByteArrayOutputStream { * @param level * the compression level (<tt>0..9</tt>) */ - public void write_compressed(final OtpErlangObject o, int level) { - @SuppressWarnings("resource") - final OtpOutputStream oos = new OtpOutputStream(o); - /* - * similar to erts_term_to_binary() in external.c: - * We don't want to compress if compression actually increases the size. - * Since compression uses 5 extra bytes (COMPRESSED tag + size), don't - * compress if the original term is smaller. - */ - if (oos.size() < 5) { - // fast path for small terms - try { - oos.writeTo(this); - // if the term is written as a compressed term, the output - // stream is closed, so we do this here, too - this.close(); - } catch (IOException e) { - throw new java.lang.IllegalArgumentException( - "Intermediate stream failed for Erlang object " + o); - } - } else { - int startCount = super.count; - // we need destCount bytes for an uncompressed term - // -> if compression uses more, use the uncompressed term! - int destCount = startCount + oos.size(); - this.fixedSize = destCount; - Deflater def = new Deflater(level); - final java.util.zip.DeflaterOutputStream dos = new java.util.zip.DeflaterOutputStream( - this, def); - try { - write1(OtpExternal.compressedTag); - write4BE(oos.size()); - oos.writeTo(dos); - dos.close(); // note: closes this, too! - } catch (final IllegalArgumentException e) { - // discard further un-compressed data - // -> if not called, there may be memory leaks! - def.end(); - // could not make the value smaller than originally - // -> reset to starting count, write uncompressed - super.count = startCount; - try { - oos.writeTo(this); - // if the term is written as a compressed term, the output - // stream is closed, so we do this here, too - this.close(); - } catch (IOException e2) { - throw new java.lang.IllegalArgumentException( - "Intermediate stream failed for Erlang object " + o); - } - } catch (final IOException e) { - throw new java.lang.IllegalArgumentException( - "Intermediate stream failed for Erlang object " + o); - } finally { - this.fixedSize = Integer.MAX_VALUE; - try { - dos.close(); - } catch (IOException e) { - // ignore + public void write_compressed(final OtpErlangObject o, final int level) { + @SuppressWarnings("resource") + final OtpOutputStream oos = new OtpOutputStream(o); + /* + * similar to erts_term_to_binary() in external.c: We don't want to + * compress if compression actually increases the size. Since + * compression uses 5 extra bytes (COMPRESSED tag + size), don't + * compress if the original term is smaller. + */ + if (oos.size() < 5) { + // fast path for small terms + try { + oos.writeToAndFlush(this); + // if the term is written as a compressed term, the output + // stream is closed, so we do this here, too + close(); + } catch (final IOException e) { + throw new java.lang.IllegalArgumentException( + "Intermediate stream failed for Erlang object " + o); + } + } else { + final int startCount = super.count; + // we need destCount bytes for an uncompressed term + // -> if compression uses more, use the uncompressed term! + final int destCount = startCount + oos.size(); + fixedSize = destCount; + final Deflater def = new Deflater(level); + final java.util.zip.DeflaterOutputStream dos = new java.util.zip.DeflaterOutputStream( + this, def); + try { + write1(OtpExternal.compressedTag); + write4BE(oos.size()); + oos.writeTo(dos); + dos.close(); // note: closes this, too! + } catch (final IllegalArgumentException e) { + // discard further un-compressed data + // -> if not called, there may be memory leaks! + def.end(); + // could not make the value smaller than originally + // -> reset to starting count, write uncompressed + super.count = startCount; + try { + oos.writeTo(this); + // if the term is written as a compressed term, the output + // stream is closed, so we do this here, too + close(); + } catch (final IOException e2) { + throw new java.lang.IllegalArgumentException( + "Intermediate stream failed for Erlang object " + o); + } + } catch (final IOException e) { + throw new java.lang.IllegalArgumentException( + "Intermediate stream failed for Erlang object " + o); + } finally { + fixedSize = Integer.MAX_VALUE; + try { + dos.close(); + } catch (final IOException e) { + // ignore + } + } } - } - } } /** * Write an arbitrary Erlang term to the stream. - * + * * @param o * the Erlang term to write. */ public void write_any(final OtpErlangObject o) { - // calls one of the above functions, depending on o - o.encode(this); + // calls one of the above functions, depending on o + o.encode(this); } public void write_fun(final OtpErlangPid pid, final String module, - final long old_index, final int arity, final byte[] md5, - final long index, final long uniq, final OtpErlangObject[] freeVars) { - if (arity == -1) { - write1(OtpExternal.funTag); - write4BE(freeVars.length); - pid.encode(this); - write_atom(module); - write_long(index); - write_long(uniq); - for (final OtpErlangObject fv : freeVars) { - fv.encode(this); - } - } else { - write1(OtpExternal.newFunTag); - final int saveSizePos = getPos(); - write4BE(0); // this is where we patch in the size - write1(arity); - writeN(md5); - write4BE(index); - write4BE(freeVars.length); - write_atom(module); - write_long(old_index); - write_long(uniq); - pid.encode(this); - for (final OtpErlangObject fv : freeVars) { - fv.encode(this); - } - poke4BE(saveSizePos, getPos() - saveSizePos); - } + final long old_index, final int arity, final byte[] md5, + final long index, final long uniq, final OtpErlangObject[] freeVars) { + if (arity == -1) { + write1(OtpExternal.funTag); + write4BE(freeVars.length); + pid.encode(this); + write_atom(module); + write_long(index); + write_long(uniq); + for (final OtpErlangObject fv : freeVars) { + fv.encode(this); + } + } else { + write1(OtpExternal.newFunTag); + final int saveSizePos = getPos(); + write4BE(0); // this is where we patch in the size + write1(arity); + writeN(md5); + write4BE(index); + write4BE(freeVars.length); + write_atom(module); + write_long(old_index); + write_long(uniq); + pid.encode(this); + for (final OtpErlangObject fv : freeVars) { + fv.encode(this); + } + poke4BE(saveSizePos, getPos() - saveSizePos); + } } public void write_external_fun(final String module, final String function, - final int arity) { - write1(OtpExternal.externalFunTag); - write_atom(module); - write_atom(function); - write_long(arity); + final int arity) { + write1(OtpExternal.externalFunTag); + write_atom(module); + write_atom(function); + write_long(arity); } public void write_map_head(final int arity) { diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpPeer.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpPeer.java index df5ce61820..cb09b40f47 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpPeer.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpPeer.java @@ -1,19 +1,19 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; @@ -28,59 +28,72 @@ import java.net.UnknownHostException; */ public class OtpPeer extends AbstractNode { int distChoose = 0; /* - * this is set by OtpConnection and is the highest - * common protocol version we both support - */ + * this is set by OtpConnection and is the highest + * common protocol version we both support + */ - OtpPeer() { - super(); + OtpPeer(final OtpTransportFactory transportFactory) { + super(transportFactory); } /** * Create a peer node. - * + * * @param node - * the name of the node. + * the name of the node. */ public OtpPeer(final String node) { - super(node); + super(node); + } + + /** + * Create a peer node with custom transport factory. + * + * @param node + * the name of the node. + * @param transportFactory + * custom transport factory + */ + public OtpPeer(final String node, final OtpTransportFactory + transportFactory) { + super(node, transportFactory); } /** * Create a connection to a remote node. - * + * * @param self - * the local node from which you wish to connect. - * + * the local node from which you wish to connect. + * * @return a connection to the remote node. - * + * * @exception java.net.UnknownHostException - * if the remote host could not be found. - * + * if the remote host could not be found. + * * @exception java.io.IOException - * if it was not possible to connect to the remote node. - * + * if it was not possible to connect to the remote node. + * * @exception OtpAuthException - * if the connection was refused by the remote node. - * + * if the connection was refused by the remote node. + * * @deprecated Use the corresponding method in {@link OtpSelf} instead. */ @Deprecated public OtpConnection connect(final OtpSelf self) throws IOException, - UnknownHostException, OtpAuthException { - return new OtpConnection(self, this); + UnknownHostException, OtpAuthException { + return new OtpConnection(self, this); } // package /* * Get the port number used by the remote node. - * + * * @return the port number used by the remote node, or 0 if the node was not * registered with the port mapper. - * + * * @exception java.io.IOException if the port mapper could not be contacted. */ int port() throws IOException { - return OtpEpmd.lookupPort(this); + return OtpEpmd.lookupPort(this); } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpSelf.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpSelf.java index 8e78cda894..74afbbcca6 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpSelf.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpSelf.java @@ -1,54 +1,52 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; import java.io.IOException; -import java.net.ServerSocket; -import java.net.Socket; import java.net.UnknownHostException; /** * Represents an OTP node. It is used to connect to remote nodes or accept * incoming connections from remote nodes. - * + * * <p> * When the Java node will be connecting to a remote Erlang, Java or C node, it * must first identify itself as a node by creating an instance of this class, * after which it may connect to the remote node. - * + * * <p> * When you create an instance of this class, it will bind a socket to a port so * that incoming connections can be accepted. However the port number will not * be made available to other nodes wishing to connect until you explicitely * register with the port mapper daemon by calling {@link #publishPort()}. * </p> - * + * * <pre> * OtpSelf self = new OtpSelf("client", "authcookie"); // identify self * OtpPeer other = new OtpPeer("server"); // identify peer - * + * * OtpConnection conn = self.connect(other); // connect to peer * </pre> - * + * */ public class OtpSelf extends OtpLocalNode { - private final ServerSocket sock; + private final OtpServerTransport sock; private final OtpErlangPid pid; /** @@ -58,47 +56,154 @@ public class OtpSelf extends OtpLocalNode { * directory. The home directory is obtained from the System property * "user.home". * </p> - * + * * <p> * If the file does not exist, an empty string is used. This method makes no * attempt to create the file. * </p> - * + * * @param node - * the name of this node. - * + * the name of this node. + * + * @exception IOException + * in case of server transport failure + * */ public OtpSelf(final String node) throws IOException { - this(node, defaultCookie, 0); + this(node, defaultCookie, 0); + } + + /** + * <p> + * Create a self node using the default cookie and custom transport factory. + * The default cookie is found by reading the first line of the + * .erlang.cookie file in the user's home directory. The home directory is + * obtained from the System property "user.home". + * </p> + * + * <p> + * If the file does not exist, an empty string is used. This method makes no + * attempt to create the file. + * </p> + * + * @param node + * the name of this node. + * + * @param transportFactory + * the transport factory to use when creating connections. + * + * @exception IOException + * in case of server transport failure + * + */ + public OtpSelf(final String node, + final OtpTransportFactory transportFactory) throws IOException { + this(node, defaultCookie, 0, transportFactory); } /** * Create a self node. - * + * * @param node - * the name of this node. - * + * the name of this node. + * * @param cookie - * the authorization cookie that will be used by this node - * when it communicates with other nodes. + * the authorization cookie that will be used by this node when + * it communicates with other nodes. + * + * @exception IOException + * in case of server transport failure */ public OtpSelf(final String node, final String cookie) throws IOException { - this(node, cookie, 0); + this(node, cookie, 0); + } + + /** + * Create a self node. + * + * @param node + * the name of this node. + * + * @param cookie + * the authorization cookie that will be used by this node when + * it communicates with other nodes. + * + * @param transportFactory + * the transport factory to use when creating connections. + * + * @exception IOException + * in case of server transport failure + */ + public OtpSelf(final String node, final String cookie, + final OtpTransportFactory transportFactory) throws IOException { + this(node, cookie, 0, transportFactory); } + /** + * Create a self node. + * + * @param node + * the name of this node. + * + * @param cookie + * the authorization cookie that will be used by this node when + * it communicates with other nodes. + * + * @param port + * the port number you wish to use for incoming connections. + * Specifying 0 lets the system choose an available port. + * + * @exception IOException + * in case of server transport failure + */ public OtpSelf(final String node, final String cookie, final int port) - throws IOException { - super(node, cookie); + throws IOException { + super(node, cookie); - sock = new ServerSocket(port); + sock = createServerTransport(port); - if (port != 0) { - this.port = port; - } else { - this.port = sock.getLocalPort(); - } + if (port != 0) { + this.port = port; + } else { + this.port = sock.getLocalPort(); + } - pid = createPid(); + pid = createPid(); + } + + /** + * Create a self node. + * + * @param node + * the name of this node. + * + * @param cookie + * the authorization cookie that will be used by this node when + * it communicates with other nodes. + * + * @param port + * the port number you wish to use for incoming connections. + * Specifying 0 lets the system choose an available port. + * + * @param transportFactory + * the transport factory to use when creating connections. + * + * @exception IOException + * in case of server transport failure + */ + public OtpSelf(final String node, final String cookie, final int port, + final OtpTransportFactory transportFactory) throws IOException { + super(node, cookie, transportFactory); + + sock = createServerTransport(port); + + if (port != 0) { + this.port = port; + } else { + this.port = sock.getLocalPort(); + } + + pid = createPid(); } /** @@ -106,12 +211,12 @@ public class OtpSelf extends OtpLocalNode { * messages sent by this node. Anonymous messages are those sent via send * methods in {@link OtpConnection OtpConnection} that do not specify a * sender. - * + * * @return the Erlang PID that will be used as the sender id in all * anonymous messages sent by this node. */ public OtpErlangPid pid() { - return pid; + return pid; } /** @@ -119,31 +224,31 @@ public class OtpSelf extends OtpLocalNode { * connect to this one. This method establishes a connection to the Erlang * port mapper (Epmd) and registers the server node's name and port so that * remote nodes are able to connect. - * + * * <p> * This method will fail if an Epmd process is not running on the localhost. * See the Erlang documentation for information about starting Epmd. - * + * * <p> * Note that once this method has been called, the node is expected to be * available to accept incoming connections. For that reason you should make * sure that you call {@link #accept()} shortly after calling * {@link #publishPort()}. When you no longer intend to accept connections * you should call {@link #unPublishPort()}. - * + * * @return true if the operation was successful, false if the node was * already registered. - * + * * @exception java.io.IOException - * if the port mapper could not be contacted. + * if the port mapper could not be contacted. */ public boolean publishPort() throws IOException { - if (getEpmd() != null) { - return false; // already published - } + if (getEpmd() != null) { + return false; // already published + } - OtpEpmd.publishPort(this); - return getEpmd() != null; + OtpEpmd.publishPort(this); + return getEpmd() != null; } /** @@ -151,71 +256,71 @@ public class OtpSelf extends OtpLocalNode { * mapper, thus preventing any new connections from remote nodes. */ public void unPublishPort() { - // unregister with epmd - OtpEpmd.unPublishPort(this); - - // close the local descriptor (if we have one) - try { - if (super.epmd != null) { - super.epmd.close(); - } - } catch (final IOException e) {/* ignore close errors */ - } - super.epmd = null; + // unregister with epmd + OtpEpmd.unPublishPort(this); + + // close the local descriptor (if we have one) + try { + if (super.epmd != null) { + super.epmd.close(); + } + } catch (final IOException e) {/* ignore close errors */ + } + super.epmd = null; } /** * Accept an incoming connection from a remote node. A call to this method * will block until an incoming connection is at least attempted. - * + * * @return a connection to a remote node. - * + * * @exception java.io.IOException - * if a remote node attempted to connect but no common - * protocol was found. - * + * if a remote node attempted to connect but no common + * protocol was found. + * * @exception OtpAuthException - * if a remote node attempted to connect, but was not - * authorized to connect. + * if a remote node attempted to connect, but was not + * authorized to connect. */ public OtpConnection accept() throws IOException, OtpAuthException { - Socket newsock = null; - - while (true) { - try { - newsock = sock.accept(); - return new OtpConnection(this, newsock); - } catch (final IOException e) { - try { - if (newsock != null) { - newsock.close(); - } - } catch (final IOException f) {/* ignore close errors */ - } - throw e; - } - } + OtpTransport newsock = null; + + while (true) { + try { + newsock = sock.accept(); + return new OtpConnection(this, newsock); + } catch (final IOException e) { + try { + if (newsock != null) { + newsock.close(); + } + } catch (final IOException f) {/* ignore close errors */ + } + throw e; + } + } } /** * Open a connection to a remote node. - * + * * @param other - * the remote node to which you wish to connect. - * + * the remote node to which you wish to connect. + * * @return a connection to the remote node. - * + * * @exception java.net.UnknownHostException - * if the remote host could not be found. - * + * if the remote host could not be found. + * * @exception java.io.IOException - * if it was not possible to connect to the remote node. - * + * if it was not possible to connect to the remote node. + * * @exception OtpAuthException - * if the connection was refused by the remote node. + * if the connection was refused by the remote node. */ public OtpConnection connect(final OtpPeer other) throws IOException, - UnknownHostException, OtpAuthException { - return new OtpConnection(this, other); + UnknownHostException, OtpAuthException { + return new OtpConnection(this, other); } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpServer.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpServer.java index 0de399ac61..9a7d8bdd60 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpServer.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpServer.java @@ -1,19 +1,19 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2000-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; @@ -23,88 +23,89 @@ import java.io.IOException; /** * Represents a local OTP client or server node. It is used when you want other * nodes to be able to establish connections to this one. - * + * * When you create an instance of this class, it will bind a socket to a port so * that incoming connections can be accepted. However the port number will not * be made available to other nodes wishing to connect until you explicitely * register with the port mapper daemon by calling {@link #publishPort()}. - * + * * <p> * When the Java node will be connecting to a remote Erlang, Java or C node, it * must first identify itself as a node by creating an instance of this class, * after which it may connect to the remote node. - * + * * <p> * Setting up a connection may be done as follows: - * - * + * + * * <pre> * OtpServer self = new OtpServer("server", "cookie"); // identify self * self.publishPort(); // make port information available - * + * * OtpConnection conn = self.accept(); // get incoming connection * </pre> - * + * * @see OtpSelf - * - * @deprecated the functionality of this class has been moved to {@link OtpSelf}. + * + * @deprecated the functionality of this class has been moved to {@link OtpSelf} + * . */ @Deprecated public class OtpServer extends OtpSelf { /** * Create an {@link OtpServer} from an existing {@link OtpSelf}. - * + * * @param self - * an existing self node. - * + * an existing self node. + * * @exception java.io.IOException - * if a ServerSocket could not be created. - * + * if a ServerSocket could not be created. + * */ public OtpServer(final OtpSelf self) throws IOException { - super(self.node(), self.cookie()); + super(self.node(), self.cookie()); } /** * Create an OtpServer, using a vacant port chosen by the operating system. * To determine what port was chosen, call the object's {@link #port()} * method. - * + * * @param node - * the name of the node. - * + * the name of the node. + * * @param cookie - * the authorization cookie that will be used by this node - * when accepts connections from remote nodes. - * + * the authorization cookie that will be used by this node when + * accepts connections from remote nodes. + * * @exception java.io.IOException - * if a ServerSocket could not be created. - * + * if a ServerSocket could not be created. + * */ public OtpServer(final String node, final String cookie) throws IOException { - super(node, cookie); + super(node, cookie); } /** * Create an OtpServer, using the specified port number. - * + * * @param node - * a name for this node, as above. - * + * a name for this node, as above. + * * @param cookie - * the authorization cookie that will be used by this node - * when accepts connections from remote nodes. - * + * the authorization cookie that will be used by this node when + * accepts connections from remote nodes. + * * @param port - * the port number to bind the socket to. - * + * the port number to bind the socket to. + * * @exception java.io.IOException - * if a ServerSocket could not be created or if the - * chosen port number was not available. - * + * if a ServerSocket could not be created or if the chosen + * port number was not available. + * */ public OtpServer(final String node, final String cookie, final int port) - throws IOException { - super(node, cookie, port); + throws IOException { + super(node, cookie, port); } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpServerSocketTransport.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpServerSocketTransport.java new file mode 100644 index 0000000000..0e25b6bfb7 --- /dev/null +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpServerSocketTransport.java @@ -0,0 +1,68 @@ +/* + * %CopyrightBegin% + * + * Copyright Ericsson AB 2015. All Rights Reserved. + * + * The contents of this file are subject to the Erlang Public License, + * Version 1.1, (the "License"); you may not use this file except in + * compliance with the License. You should have received a copy of the + * Erlang Public License along with this software. If not, it can be + * retrieved online at http://www.erlang.org/. + * + * Software distributed under the License is distributed on an "AS IS" + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See + * the License for the specific language governing rights and limitations + * under the License. + * + * %CopyrightEnd% + */ + +package com.ericsson.otp.erlang; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; + +/** + * Default socket-based server transport + * + * @author Dmitriy Kargapolov + */ +public class OtpServerSocketTransport implements OtpServerTransport { + + /** + * Underlying server socket + */ + private final ServerSocket socket; + + /** + * @see ServerSocket#ServerSocket(int) + */ + public OtpServerSocketTransport(final int port) throws IOException { + socket = new ServerSocket(port); + } + + /** + * @see ServerSocket#getLocalPort() + */ + public int getLocalPort() { + return socket.getLocalPort(); + } + + /** + * @see ServerSocket#accept() + */ + public OtpTransport accept() throws IOException { + final Socket sock = socket.accept(); + sock.setTcpNoDelay(true); + return new OtpSocketTransport(sock); + } + + /** + * @see ServerSocket#close() + */ + public void close() throws IOException { + socket.close(); + } + +} diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpServerTransport.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpServerTransport.java new file mode 100644 index 0000000000..4d31380bee --- /dev/null +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpServerTransport.java @@ -0,0 +1,46 @@ +/* + * %CopyrightBegin% + * + * Copyright Ericsson AB 2015. All Rights Reserved. + * + * The contents of this file are subject to the Erlang Public License, + * Version 1.1, (the "License"); you may not use this file except in + * compliance with the License. You should have received a copy of the + * Erlang Public License along with this software. If not, it can be + * retrieved online at http://www.erlang.org/. + * + * Software distributed under the License is distributed on an "AS IS" + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See + * the License for the specific language governing rights and limitations + * under the License. + * + * %CopyrightEnd% + */ + +package com.ericsson.otp.erlang; + +import java.io.IOException; +import java.net.ServerSocket; + +/** + * Server-side connection-oriented transport interface. + * + * @author Dmitriy Kargapolov + */ +public interface OtpServerTransport { + + /** + * @see ServerSocket#getLocalPort() + */ + int getLocalPort(); + + /** + * @see ServerSocket#accept() + */ + OtpTransport accept() throws IOException; + + /** + * @see ServerSocket#close() + */ + void close() throws IOException; +} diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpSocketTransport.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpSocketTransport.java new file mode 100644 index 0000000000..f690ab59ed --- /dev/null +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpSocketTransport.java @@ -0,0 +1,89 @@ +/* + * %CopyrightBegin% + * + * Copyright Ericsson AB 2015. All Rights Reserved. + * + * The contents of this file are subject to the Erlang Public License, + * Version 1.1, (the "License"); you may not use this file except in + * compliance with the License. You should have received a copy of the + * Erlang Public License along with this software. If not, it can be + * retrieved online at http://www.erlang.org/. + * + * Software distributed under the License is distributed on an "AS IS" + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See + * the License for the specific language governing rights and limitations + * under the License. + * + * %CopyrightEnd% + */ + +package com.ericsson.otp.erlang; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.InetAddress; +import java.net.Socket; +import java.net.UnknownHostException; + +/** + * Default socket-based client transport + * + * @author Dmitriy Kargapolov + */ +public class OtpSocketTransport implements OtpTransport { + + /** + * Underlying socket + */ + private final Socket socket; + + /** + * @see Socket#Socket(String, int) + */ + public OtpSocketTransport(final String addr, final int port) + throws UnknownHostException, IOException { + socket = new Socket(addr, port); + socket.setTcpNoDelay(true); + } + + /** + * @see Socket#Socket(InetAddress, int) + */ + public OtpSocketTransport(final InetAddress addr, final int port) + throws UnknownHostException, IOException { + socket = new Socket(addr, port); + socket.setTcpNoDelay(true); + } + + /** + * Socket wrapping constructor + * + * @param s + * socket to wrap + */ + public OtpSocketTransport(final Socket s) { + socket = s; + } + + /** + * @see Socket#getInputStream() + */ + public InputStream getInputStream() throws IOException { + return socket.getInputStream(); + } + + /** + * @see Socket#getOutputStream() + */ + public OutputStream getOutputStream() throws IOException { + return socket.getOutputStream(); + } + + /** + * @see Socket#close() + */ + public void close() throws IOException { + socket.close(); + } +} diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpSocketTransportFactory.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpSocketTransportFactory.java new file mode 100644 index 0000000000..f6b5bfc86d --- /dev/null +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpSocketTransportFactory.java @@ -0,0 +1,56 @@ +/* + * %CopyrightBegin% + * + * Copyright Ericsson AB 2015. All Rights Reserved. + * + * The contents of this file are subject to the Erlang Public License, + * Version 1.1, (the "License"); you may not use this file except in + * compliance with the License. You should have received a copy of the + * Erlang Public License along with this software. If not, it can be + * retrieved online at http://www.erlang.org/. + * + * Software distributed under the License is distributed on an "AS IS" + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See + * the License for the specific language governing rights and limitations + * under the License. + * + * %CopyrightEnd% + */ + +package com.ericsson.otp.erlang; + +import java.io.IOException; +import java.net.InetAddress; + +/** + * Default socket-based transport factory + * + * @author Dmitriy Kargapolov + */ +public class OtpSocketTransportFactory implements OtpTransportFactory { + + /** + * @see OtpTransportFactory#createTransport(String, int) + */ + public OtpTransport createTransport(final String addr, final int port) + throws IOException { + return new OtpSocketTransport(addr, port); + } + + /** + * @see OtpTransportFactory#createTransport(InetAddress, int) + */ + public OtpTransport createTransport(final InetAddress addr, final int port) + throws IOException { + return new OtpSocketTransport(addr, port); + } + + /** + * @see OtpTransportFactory#createServerTransport(int) + */ + public OtpServerTransport createServerTransport(final int port) + throws IOException { + return new OtpServerSocketTransport(port); + } + +} diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpSystem.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpSystem.java index 969da39d70..8eb1f86764 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpSystem.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpSystem.java @@ -1,19 +1,19 @@ /* * %CopyrightBegin% - * + * * Copyright Ericsson AB 2004-2009. All Rights Reserved. - * + * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in * compliance with the License. You should have received a copy of the * Erlang Public License along with this software. If not, it can be * retrieved online at http://www.erlang.org/. - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. - * + * * %CopyrightEnd% */ package com.ericsson.otp.erlang; @@ -24,27 +24,27 @@ final class OtpSystem { static { - final String rel = System.getProperty("OtpCompatRel", "0"); - - try { - - switch (Integer.parseInt(rel)) { - case 1: - case 2: - case 3: - case 4: - case 5: - case 6: - case 7: - case 8: - case 9: - case 0: - default: - break; - } - } catch (final NumberFormatException e) { - /* Ignore ... */ - } + final String rel = System.getProperty("OtpCompatRel", "0"); + + try { + + switch (Integer.parseInt(rel)) { + case 1: + case 2: + case 3: + case 4: + case 5: + case 6: + case 7: + case 8: + case 9: + case 0: + default: + break; + } + } catch (final NumberFormatException e) { + /* Ignore ... */ + } } diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpTransport.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpTransport.java new file mode 100644 index 0000000000..51c62d9ef0 --- /dev/null +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpTransport.java @@ -0,0 +1,49 @@ +/* + * %CopyrightBegin% + * + * Copyright Ericsson AB 2015. All Rights Reserved. + * + * The contents of this file are subject to the Erlang Public License, + * Version 1.1, (the "License"); you may not use this file except in + * compliance with the License. You should have received a copy of the + * Erlang Public License along with this software. If not, it can be + * retrieved online at http://www.erlang.org/. + * + * Software distributed under the License is distributed on an "AS IS" + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See + * the License for the specific language governing rights and limitations + * under the License. + * + * %CopyrightEnd% + */ + +package com.ericsson.otp.erlang; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.Socket; + +/** + * Client-side connection-oriented transport interface. + * + * @author Dmitriy Kargapolov + */ +public interface OtpTransport { + + /** + * @see Socket#getInputStream() + */ + public abstract InputStream getInputStream() throws IOException; + + /** + * @see Socket#getOutputStream() + */ + public abstract OutputStream getOutputStream() throws IOException; + + /** + * @see Socket#close() + */ + public abstract void close() throws IOException; + +} diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpTransportFactory.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpTransportFactory.java new file mode 100644 index 0000000000..bd404daea5 --- /dev/null +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpTransportFactory.java @@ -0,0 +1,124 @@ +/* + * %CopyrightBegin% + * + * Copyright Ericsson AB 2015. All Rights Reserved. + * + * The contents of this file are subject to the Erlang Public License, + * Version 1.1, (the "License"); you may not use this file except in + * compliance with the License. You should have received a copy of the + * Erlang Public License along with this software. If not, it can be + * retrieved online at http://www.erlang.org/. + * + * Software distributed under the License is distributed on an "AS IS" + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See + * the License for the specific language governing rights and limitations + * under the License. + * + * %CopyrightEnd% + */ + +package com.ericsson.otp.erlang; + +import java.io.IOException; +import java.net.InetAddress; + +/** + * Factory class used to create client- and server-side transport instances. One + * static instance of class implementing this interface is created when program + * loaded. Default implementation used is {@link OtpSocketTransportFactory}. + * JInterface user can specify custom transport factory implementing this + * interface in the following ways: + * <dl> + * <dt>defining static class as internal to class holding main() method</dt> + * <dd>In the systems, where main class can be retrieved with + * <code>System.getProperty("sun.java.command")</code>, user can define static + * class <b>OtpErlangSystemTuner</b> internal to the main class, providing at + * least one static method with the name <b>getOtpTransportFactory</b>, with no + * parameters, returning object of class implementing + * <b>OtpTransportFactory</b>, for example: + * + * <pre> + * + * public class MyMainClass { + * + * public static class OtpErlangSystemTuner { + * ... + * public static OtpTransportFactory getOtpTransportFactory() { + * return new MyTransportFactory(); + * } + * } + * + * public static class MyTransportFactory implements OtpTransportFactory { + * ... + * } + * + * public static void main(String[] args) { + * ... + * } + * } + * + * + * </pre> + * + * </dd> + * + * <dt>specifying factory class in the system properties</dt> + * <dd>User-defined transport factory class may be specified via system property + * <b>OtpTransportFactory</b>, for example: + * + * <pre> + * + * package com.my.company; + * + * public static class MyTransportFactory implements OtpTransportFactory { + * ... + * } + * </pre> + * + * In such case program may be run with + * -DOtpTransportFactory=com.my.company.MyTransportFactory, or other way of + * setting system property <i>before execution of static initializers</i> may be + * used.</dd> + * </dl> + * + * @author Dmitriy Kargapolov + */ +public interface OtpTransportFactory { + + /** + * Create instance of {@link OtpTransport} + * + * @param addr + * host name or IP address string + * @param port + * port number + * @return new socket object + * @throws IOException + */ + public abstract OtpTransport createTransport(String addr, int port) + throws IOException; + + /** + * Create instance of {@link OtpTransport} + * + * @param addr + * peer address + * @param port + * port number + * @return new socket object + * @throws IOException + */ + public abstract OtpTransport createTransport(InetAddress addr, int port) + throws IOException; + + /** + * Create instance of {@link OtpServerTransport} + * + * @param port + * port number to listen on + * @return new socket object + * @throws IOException + */ + public OtpServerTransport createServerTransport(int port) + throws IOException; +} diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/java_files b/lib/jinterface/java_src/com/ericsson/otp/erlang/java_files index 62fa7f990e..a0f19bc1aa 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/java_files +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/java_files @@ -53,7 +53,13 @@ COMM = \ OtpOutputStream \ OtpPeer \ OtpSelf \ - OtpServer + OtpServer \ + OtpServerSocketTransport \ + OtpServerTransport \ + OtpSocketTransport \ + OtpSocketTransportFactory \ + OtpTransport \ + OtpTransportFactory ERL = \ OtpErlangAtom \ diff --git a/lib/jinterface/priv/.gitignore b/lib/jinterface/priv/.gitignore deleted file mode 100644 index e69de29bb2..0000000000 --- a/lib/jinterface/priv/.gitignore +++ /dev/null diff --git a/lib/jinterface/test/.classpath b/lib/jinterface/test/.classpath new file mode 100644 index 0000000000..2e4a3e6776 --- /dev/null +++ b/lib/jinterface/test/.classpath @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" output="jinterface_SUITE_data" path="jinterface_SUITE_data"/> + <classpathentry kind="src" output="nc_SUITE_data" path="nc_SUITE_data"/> + <classpathentry combineaccessrules="false" kind="src" path="/jinterface"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> + <classpathentry kind="output" path="bin"/> +</classpath> diff --git a/lib/jinterface/test/.project b/lib/jinterface/test/.project new file mode 100644 index 0000000000..4144c6ebea --- /dev/null +++ b/lib/jinterface/test/.project @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>jinterface_tests</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> diff --git a/lib/jinterface/test/.settings/org.eclipse.jdt.core.prefs b/lib/jinterface/test/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..af0f20f97a --- /dev/null +++ b/lib/jinterface/test/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,7 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 +org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.5 diff --git a/lib/jinterface/test/jinterface_SUITE.erl b/lib/jinterface/test/jinterface_SUITE.erl index 00abc97ff5..73bab98559 100644 --- a/lib/jinterface/test/jinterface_SUITE.erl +++ b/lib/jinterface/test/jinterface_SUITE.erl @@ -22,7 +22,8 @@ init_per_suite/1, end_per_suite/1, init_per_testcase/2, end_per_testcase/2]). --export([nodename/1, register_and_whereis/1, get_names/1, boolean_atom/1, +-export([transport_factory/1, + nodename/1, register_and_whereis/1, get_names/1, boolean_atom/1, node_ping/1, mbox_ping/1, java_erlang_send_receive/1, java_internal_send_receive_same_node/1, @@ -39,7 +40,8 @@ status_handler_localStatus/1, status_handler_remoteStatus/1, status_handler_connAttempt/1, maps/1, - fun_equals/1 + fun_equals/1, + core_match_bind/1 ]). -include_lib("common_test/include/ct.hrl"). @@ -103,12 +105,14 @@ end_per_group(_GroupName, Config) -> fundamental() -> [ + transport_factory, % TransportFactoryTest.java nodename, % Nodename.java register_and_whereis, % RegisterAndWhereis.java get_names, % GetNames.java boolean_atom, % BooleanAtom.java maps, % Maps.java - fun_equals % FunEquals.java + fun_equals, % FunEquals.java + core_match_bind % CoreMatchBind.java ]. ping() -> @@ -201,6 +205,16 @@ end_per_testcase(_Case,Config) -> %%%----------------------------------------------------------------- %%% TEST CASES %%%----------------------------------------------------------------- +transport_factory(doc) -> + ["TransportFactoryTest.java: Test custom OTP Transport Factory"]; +transport_factory(suite) -> + []; +transport_factory(Config) when is_list(Config) -> + ok = jitu:java(?config(java, Config), + ?config(data_dir, Config), + "TransportFactoryTest"). + +%%%----------------------------------------------------------------- nodename(doc) -> ["Nodename.java: " "Test OtpNode.node(), OtpNode.alive() and OtpNode.host()"]; @@ -705,6 +719,18 @@ fun_equals(Config) when is_list(Config) -> []). %%%----------------------------------------------------------------- +core_match_bind(doc) -> + ["CoreMatchBind.java: " + "Test OtpErlangObject.match() and bind()"]; +core_match_bind(suite) -> + []; +core_match_bind(Config) when is_list(Config) -> + ok = jitu:java(?config(java, Config), + ?config(data_dir, Config), + "CoreMatchBind", + []). + +%%%----------------------------------------------------------------- %%% INTERNAL FUNCTIONS %%%----------------------------------------------------------------- send_receive(TestCaseTag,Fun,Config) -> diff --git a/lib/jinterface/test/jinterface_SUITE_data/.gitignore b/lib/jinterface/test/jinterface_SUITE_data/.gitignore new file mode 100644 index 0000000000..6b468b62a9 --- /dev/null +++ b/lib/jinterface/test/jinterface_SUITE_data/.gitignore @@ -0,0 +1 @@ +*.class diff --git a/lib/jinterface/test/jinterface_SUITE_data/CoreMatchBind.java b/lib/jinterface/test/jinterface_SUITE_data/CoreMatchBind.java new file mode 100644 index 0000000000..a78a63093e --- /dev/null +++ b/lib/jinterface/test/jinterface_SUITE_data/CoreMatchBind.java @@ -0,0 +1,584 @@ +/* + * %CopyrightBegin% + * + * Copyright Ericsson AB 2000-2015. All Rights Reserved. + * + * The contents of this file are subject to the Erlang Public License, + * Version 1.1, (the "License"); you may not use this file except in + * compliance with the License. You should have received a copy of the + * Erlang Public License along with this software. If not, it can be + * retrieved online at http://www.erlang.org/. + * + * Software distributed under the License is distributed on an "AS IS" + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See + * the License for the specific language governing rights and limitations + * under the License. + * + * %CopyrightEnd% + */ + +import com.ericsson.otp.erlang.OtpErlangException; +import com.ericsson.otp.erlang.OtpErlangInt; +import com.ericsson.otp.erlang.OtpErlangList; +import com.ericsson.otp.erlang.OtpErlangMap; +import com.ericsson.otp.erlang.OtpErlangObject; +import com.ericsson.otp.erlang.OtpErlangTuple; +import com.ericsson.otp.erlang.OtpOutputStream; + +public class CoreMatchBind { + + @SuppressWarnings("serial") + private static class DumbObject extends OtpErlangObject { + + @Override + public String toString() { + return this.getClass().getSimpleName(); + } + + @Override + public void encode(final OtpOutputStream buf) { + fail("unexpected encode() call"); + } + + @Override + public boolean equals(final Object o) { + fail("unexpected equals() call"); + return false; + } + + } + + @SuppressWarnings("serial") + private static class BoundObject extends OtpErlangObject { + + @Override + public String toString() { + return this.getClass().getSimpleName(); + } + + @Override + public void encode(final OtpOutputStream buf) { + fail("unexpected encode() call"); + } + + @Override + public boolean equals(final Object o) { + fail("unexpected equals() call"); + return false; + } + + } + + @SuppressWarnings("serial") + private static class TestObject extends OtpErlangObject { + + private final Binder binder; + private DumbObject dumb; + private boolean flag; + private BoundObject obj; + + public TestObject(final boolean flag, final Binder binder, + final DumbObject dumb) { + this.flag = flag; + this.binder = binder; + this.dumb = dumb; + } + + public TestObject(final Binder binder, final BoundObject obj) { + this.binder = binder; + this.obj = obj; + } + + public DumbObject getDumb() { + return dumb; + } + + @Override + public String toString() { + return flag ? "T" : "F"; + } + + @Override + public void encode(final OtpOutputStream buf) { + fail("unexpected encode() call"); + } + + @Override + public boolean equals(final Object o) { + if (obj == null) { + fail("unexpected equals() call"); + } + return o == obj; + } + + @Override + public <T> boolean match(final OtpErlangObject term, final T binds) { + if (binds != binder) { + fail("invalid binder"); + } + if (term != dumb) { + fail("invalid object"); + } + return flag; + } + + @Override + public <T> OtpErlangObject bind(final T binds) + throws OtpErlangException { + if (binds != binder) { + fail("invalid binder"); + } + return obj; + } + + } + + /* + * "always matched" object + */ + @SuppressWarnings("serial") + private static class Any extends OtpErlangObject { + + @Override + public String toString() { + return "any"; + } + + @Override + public void encode(final OtpOutputStream buf) { + fail("unexpected encode() call"); + } + + @Override + public boolean equals(final Object o) { + fail("unexpected equals() call"); + return false; + } + + @Override + public <T> boolean match(final OtpErlangObject term, final T binds) { + return true; + } + } + + private static class Binder { + // make object pair for match() testing + TestObject makeTest(final boolean flag) { + return new TestObject(flag, this, new DumbObject()); + } + + // make object pair for bind() testing + TestObject makeTest() { + return new TestObject(this, new BoundObject()); + } + } + + private static void isNotNull(final Object o) throws Exception { + if (o == null) { + throw new Exception("not null expected"); + } + } + + private static void fail(final String string) { + System.err.println(string); + new Throwable().printStackTrace(System.err); + System.exit(1); + } + + private static void isT(final boolean b) throws Exception { + if (!b) { + throw new Exception("true expected"); + } + } + + private static void isF(final boolean b) throws Exception { + if (b) { + throw new Exception("false expected"); + } + } + + private static void equals(final OtpErlangObject a, final OtpErlangObject b) + throws Exception { + if (!a.equals(b)) { + throw new Exception(a + " != " + b); + } + } + + /* + * scalar match test - match particular test object (producing given result) + * against particular dumb object passing particular bindings object; ensure + * all participants are used as expected in match behavior, check result. + */ + private static void scalar_match_test() throws Exception { + final Binder bind = new Binder(); + + final TestObject t = bind.makeTest(true); + isT(t.match(t.getDumb(), bind)); + + final TestObject f = bind.makeTest(false); + isF(f.match(f.getDumb(), bind)); + } + + /* + * scalar bind test - ensure right object generated based on bindings + */ + private static void scalar_bind_test() throws Exception { + final Binder bind = new Binder(); + final TestObject t = bind.makeTest(); + final OtpErlangObject o = t.bind(bind); + isNotNull(o); + equals(t, o); + } + + /* + * used by tuple_arity_match_test() + */ + private static OtpErlangObject mkTuplePattern(final int n) { + final Any a[] = new Any[n]; + for (int i = 0; i < n; i++) { + a[i] = new Any(); + } + return new OtpErlangTuple(a); + } + + /* + * used by tuple_arity_match_test() + */ + private static OtpErlangObject mkTupleObject(final int n) { + final DumbObject a[] = new DumbObject[n]; + for (int i = 0; i < n; i++) { + a[i] = new DumbObject(); + } + return new OtpErlangTuple(a); + } + + /* + * ensure only tuples of the same arity can match + */ + private static void tuple_arity_match_test(final int m, final int n) + throws Exception { + final Binder bind = new Binder(); + for (int i = m; i < n; i++) { + for (int j = m; j < n; j++) { + final OtpErlangObject p = mkTuplePattern(i); + final OtpErlangObject o = mkTupleObject(j); + if (i == j) { + isT(p.match(o, bind)); + } else { + isF(p.match(o, bind)); + } + } + } + } + + /* + * tuple match test - ensure elements of tuple are matched to corresponding + * elements of tested object and result is logical "and" over all elements. + */ + private static void tuple_match_test(final int n) throws Exception { + final Binder bind = new Binder(); + final int max = 1 << n; + final TestObject a[] = new TestObject[n]; + final DumbObject d[] = new DumbObject[n]; + for (int k = 0; k < max; k++) { + for (int m = 1, i = 0; m < max; m = m << 1, i++) { + d[i] = new DumbObject(); + a[i] = new TestObject((k & m) != 0, bind, d[i]); + } + final OtpErlangObject tpl = new OtpErlangTuple(a); + final OtpErlangObject obj = new OtpErlangTuple(d); + if (k + 1 < max) { + isF(tpl.match(obj, bind)); + } else { + isT(tpl.match(obj, bind)); + } + } + } + + /* + * tuple bind test - ensure result is a tuple where each element is a result + * of binding of corresponding pattern element using provided bindings. + */ + private static void tuple_bind_test(final int n) throws Exception { + final Binder bind = new Binder(); + final TestObject a[] = new TestObject[n]; + final OtpErlangObject b[] = new OtpErlangObject[n]; + for (int i = 0; i < n; i++) { + a[i] = bind.makeTest(); + b[i] = a[i].obj; + } + final OtpErlangObject t = new OtpErlangTuple(a); + final OtpErlangObject o = t.bind(bind); + isNotNull(o); + equals(t, o); + } + + private static OtpErlangObject mkListPattern(final int n, final boolean tail) + throws OtpErlangException { + final Any a[] = new Any[n]; + for (int i = 0; i < n; i++) { + a[i] = new Any(); + } + return tail ? new OtpErlangList(a, new Any()) : new OtpErlangList(a); + } + + private static OtpErlangObject mkListObject(final int n, final boolean tail) + throws OtpErlangException { + final DumbObject a[] = new DumbObject[n]; + for (int i = 0; i < n; i++) { + a[i] = new DumbObject(); + } + return tail ? new OtpErlangList(a, new DumbObject()) + : new OtpErlangList(a); + } + + /* + * ensure only lists of the same arity and same tail presence can match + */ + private static void list_arity_match_test(final int m, final int n) + throws Exception { + final Binder bind = new Binder(); + for (int i = m; i < n; i++) { + for (int j = m; j < n; j++) { + for (int k = 0; k < 2; k++) { + if (i == 0 && k == 1) { + continue; + } + for (int l = 0; l < 2; l++) { + if (j == 0 && l == 1) { + continue; + } + final OtpErlangObject p = mkListPattern(i, k == 1); + final OtpErlangObject o = mkListObject(j, l == 1); + if (i == j && k == l || k == 1 && i <= j) { + isT(p.match(o, bind)); + } else { + isF(p.match(o, bind)); + } + } + } + } + } + } + + /* + * lists match test - ensure elements of lists are matched to corresponding + * elements of tested object and result is logical "and" over all elements, + * count tails as well + */ + private static void list_match_test(final int n) throws Exception { + final Binder bind = new Binder(); + final int max = 1 << n; + final TestObject a[] = new TestObject[n]; + final DumbObject d[] = new DumbObject[n]; + final DumbObject e[] = new DumbObject[n + 1]; + for (int k = 0; k < max; k++) { + for (int m = 1, i = 0; m < max; m = m << 1, i++) { + d[i] = new DumbObject(); + e[i] = d[i]; + a[i] = new TestObject((k & m) != 0, bind, d[i]); + } + for (int i = n; i < n + 1; i++) { + e[i] = new DumbObject(); + } + final OtpErlangObject lst = new OtpErlangList(a); + final OtpErlangObject obj = new OtpErlangList(d); + final OtpErlangObject ext = new OtpErlangList(e); + final OtpErlangObject eTl = new OtpErlangList(e, new DumbObject()); + + if (n > 0) { + final DumbObject dTail = new DumbObject(); + final TestObject tTail = new TestObject(true, bind, dTail); + final TestObject fTail = new TestObject(false, bind, dTail); + final OtpErlangObject fTailLst = new OtpErlangList(a, fTail); + final OtpErlangObject tTailLst = new OtpErlangList(a, tTail); + final OtpErlangObject tailObj = new OtpErlangList(d, dTail); + + // match lists with non-matching tails is always false + isF(fTailLst.match(tailObj, bind)); + + // match list with no tail to list with tail is always false + isF(lst.match(tailObj, bind)); + + // matching lists with matching tails + if (k + 1 < max) { + isF(tTailLst.match(tailObj, bind)); + } else { + isT(tTailLst.match(tailObj, bind)); + } + + // matching shorter pattern with last tail to longer list + // with or with no extra tail; matching list pattern + // with last tail to same length list with no tail. + final Any aTail = new Any(); + final OtpErlangObject shortLst = new OtpErlangList(a, aTail); + if (k + 1 < max) { + isF(shortLst.match(obj, bind)); // same arity + isF(shortLst.match(ext, bind)); // pattern arity is less + isF(shortLst.match(eTl, bind)); // + } else { + isT(shortLst.match(obj, bind)); // same arity + isT(shortLst.match(ext, bind)); // pattern arity is less + isT(shortLst.match(eTl, bind)); // + } + } + + // matching lists with no tails + if (k + 1 < max) { + isF(lst.match(obj, bind)); + } else { + isT(lst.match(obj, bind)); + } + + // extra-length object, no tail in "pattern" + isF(lst.match(ext, bind)); + } + } + + /* + * list bind test - ensure result is a list where each element is a result + * of binding of corresponding pattern element using provided bindings. + */ + private static void list_bind_test(final int n) throws Exception { + final Binder bind = new Binder(); + final TestObject a[] = new TestObject[n]; + final OtpErlangObject b[] = new OtpErlangObject[n]; + for (int i = 0; i < n; i++) { + a[i] = bind.makeTest(); + b[i] = a[i].obj; + } + OtpErlangObject t = new OtpErlangList(a); + OtpErlangObject o = t.bind(bind); + isNotNull(o); + equals(t, o); + if (n > 0) { + // improper list case + t = new OtpErlangList(a, bind.makeTest()); + o = t.bind(bind); + isNotNull(o); + equals(t, o); + } + } + + /* + * map match test - object may have more keys than pattern + */ + private static void map_match_test(final int m, final int n) + throws Exception { + final Binder bind = new Binder(); + + // pattern side - m elements + final OtpErlangObject k1[] = new OtpErlangObject[m]; + final TestObject a[] = new TestObject[m]; + + // object side - n elements + final OtpErlangObject k2[] = new OtpErlangObject[n]; + final DumbObject d[] = new DumbObject[n]; + + final int max = Math.max(m, n); + final int mskHi = 1 << max; + final int full = (1 << m) - 1; + for (int k = 0; k < mskHi; k++) { + for (int msk = 1, i = 0; msk < mskHi; msk = msk << 1, i++) { + if (i < n) { + k2[i] = new OtpErlangInt(i); + d[i] = new DumbObject(); + } + if (i < m) { + k1[i] = new OtpErlangInt(i); + a[i] = new TestObject((k & msk) != 0, bind, i < n ? d[i] + : new DumbObject()); + } + } + final OtpErlangObject map = new OtpErlangMap(k1, a); // m items + final OtpErlangObject obj = new OtpErlangMap(k2, d); // n items + if ((k & full) == full && m <= n) { + isT(map.match(obj, bind)); + } else { + isF(map.match(obj, bind)); + } + } + } + + /* + * map bind test - ensure result is a map where each element is a result of + * binding of corresponding pattern element using provided bindings. + */ + private static void map_bind_test(final int n) throws Exception { + final Binder bind = new Binder(); + final TestObject a[] = new TestObject[n]; + final OtpErlangObject b[] = new OtpErlangObject[n]; + final OtpErlangObject k[] = new OtpErlangObject[n]; + for (int i = 0; i < n; i++) { + a[i] = bind.makeTest(); + b[i] = a[i].obj; + k[i] = new OtpErlangInt(i); + } + final OtpErlangObject t = new OtpErlangMap(k, a); + final OtpErlangObject o = t.bind(bind); + isNotNull(o); + equals(t, o); + } + + public static void main(final String[] args) { + try { + scalar_match_test(); + System.out.println("scalar_match_test() passed"); + + scalar_bind_test(); + System.out.println("scalar_bind_test() passed"); + + for (int m = 0; m < 16; m++) { + for (int n = 0; n < 16; n++) { + tuple_arity_match_test(m, n); + } + } + System.out.println("tuple_arity_match_test() passed"); + + for (int n = 0; n < 16; n++) { + tuple_match_test(n); + } + System.out.println("tuple_match_test() passed"); + + for (int n = 0; n < 16; n++) { + tuple_bind_test(n); + } + System.out.println("tuple_bind_test() passed"); + + for (int m = 0; m < 16; m++) { + for (int n = 0; n < 16; n++) { + list_arity_match_test(m, n); + } + } + System.out.println("list_arity_match_test() passed"); + + for (int n = 0; n < 16; n++) { + list_match_test(n); + } + System.out.println("list_match_test() passed"); + + for (int n = 0; n < 16; n++) { + list_bind_test(n); + } + System.out.println("list_bind_test() passed"); + + for (int m = 0; m < 12; m++) { + for (int n = 0; n < 12; n++) { + map_match_test(m, n); + } + } + System.out.println("map_match_test() passed"); + + for (int n = 0; n < 16; n++) { + map_bind_test(n); + } + System.out.println("map_bind_test() passed"); + + } catch (final Exception e) { + e.printStackTrace(); + System.exit(1); + } + + System.out.println("ok"); + } +} diff --git a/lib/jinterface/test/jinterface_SUITE_data/Makefile.src b/lib/jinterface/test/jinterface_SUITE_data/Makefile.src index cd68f1ead5..a4a69000c6 100644 --- a/lib/jinterface/test/jinterface_SUITE_data/Makefile.src +++ b/lib/jinterface/test/jinterface_SUITE_data/Makefile.src @@ -38,6 +38,7 @@ JINTERFACE_CLASSPATH = @jinterface_classpath@ CLASSPATH = .@PS@$(JINTERFACE_CLASSPATH)@PS@ JAVA_FILES = \ + TransportFactoryTest.java \ Nodename.java \ RegisterAndWhereis.java \ GetNames.java \ @@ -48,7 +49,8 @@ JAVA_FILES = \ MboxLinkUnlink.java \ NodeStatusHandler.java \ Maps.java \ - FunEquals.java + FunEquals.java \ + CoreMatchBind.java CLASS_FILES = $(JAVA_FILES:.java=.class) diff --git a/lib/jinterface/test/jinterface_SUITE_data/TransportFactoryTest.java b/lib/jinterface/test/jinterface_SUITE_data/TransportFactoryTest.java new file mode 100644 index 0000000000..367e28a512 --- /dev/null +++ b/lib/jinterface/test/jinterface_SUITE_data/TransportFactoryTest.java @@ -0,0 +1,90 @@ +/* + * %CopyrightBegin% + * + * Copyright Ericsson AB 2015. All Rights Reserved. + * + * The contents of this file are subject to the Erlang Public License, + * Version 1.1, (the "License"); you may not use this file except in + * compliance with the License. You should have received a copy of the + * Erlang Public License along with this software. If not, it can be + * retrieved online at http://www.erlang.org/. + * + * Software distributed under the License is distributed on an "AS IS" + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See + * the License for the specific language governing rights and limitations + * under the License. + * + * %CopyrightEnd% + */ + +import java.io.IOException; +import java.net.InetAddress; + +import com.ericsson.otp.erlang.OtpSelf; +import com.ericsson.otp.erlang.OtpServerTransport; +import com.ericsson.otp.erlang.OtpSocketTransportFactory; +import com.ericsson.otp.erlang.OtpTransport; +import com.ericsson.otp.erlang.OtpTransportFactory; + +/** + * @author Dmitriy Kargapolov + */ +public class TransportFactoryTest { + + /** + * example of custom transport factory wrapping default one + */ + public static class TransportFactory implements OtpTransportFactory { + + OtpSocketTransportFactory tf = new OtpSocketTransportFactory(); + + public OtpTransport createTransport(final String addr, final int port) + throws IOException { + clientOk = true; + System.out.println("creating transport to " + addr + ", " + port); + return tf.createTransport(addr, port); + } + + public OtpTransport createTransport(final InetAddress addr, + final int port) throws IOException { + clientOk = true; + System.out.println("creating transport to " + addr + ", " + port); + return tf.createTransport(addr, port); + } + + public OtpServerTransport createServerTransport(final int port) + throws IOException { + serverOk = true; + System.out.println("creating server transport to " + port); + return tf.createServerTransport(port); + } + + } + + static boolean serverOk = false; + static boolean clientOk = false; + + public static void main(final String[] args) throws IOException { + + // check server transport + final OtpSelf self = new OtpSelf("local", new TransportFactory()); + if (!serverOk) { + fail("custom server transport was not created"); + } + System.out.println("accepting connections on " + self.port()); + + // check client transport + try { + self.publishPort(); + } catch (final Exception e) { + } + if (!clientOk) { + fail("custom client transport was not created"); + } + } + + private static void fail(final String string) { + System.err.println(string); + System.exit(1); + } +} diff --git a/lib/jinterface/test/jitu.erl b/lib/jinterface/test/jitu.erl index 46b8cb3ac2..8097237af6 100644 --- a/lib/jinterface/test/jitu.erl +++ b/lib/jinterface/test/jitu.erl @@ -117,10 +117,7 @@ classpath(Dir) -> end, es(Dir++PS++ filename:join([code:lib_dir(jinterface),"priv","OtpErlang.jar"])++PS++ - case os:getenv("CLASSPATH") of - false -> ""; - Classpath -> Classpath - end, + os:getenv("CLASSPATH", ""), Quote, EscSpace). diff --git a/lib/jinterface/test/nc_SUITE_data/.gitignore b/lib/jinterface/test/nc_SUITE_data/.gitignore new file mode 100644 index 0000000000..6b468b62a9 --- /dev/null +++ b/lib/jinterface/test/nc_SUITE_data/.gitignore @@ -0,0 +1 @@ +*.class |