Exemple #1
0
        /// <summary>
        /// Calls the appropriate GetInterfaceInfo method depending on whether we are dealing with an implicit or explicit service type and
        /// returns the a dictionary of Inteface and Event info exposed by either service type
        /// </summary>
        /// <param name="grainType"></param>
        /// <returns></returns>
        internal static GrainInterfaceInfo GetInterfaceInfo(Type grainType)
        {
            var result = new GrainInterfaceInfo();
            Dictionary <int, Type> interfaces = GrainInterfaceData.GetRemoteInterfaces(grainType);

            if (interfaces.Keys.Count == 0)
            {
                // Should never happen!
                Debug.Fail("Could not find any service interfaces for type=" + grainType.Name);
            }

            IEqualityComparer <InterfaceInfo> ifaceComparer = new InterfaceInfoComparer();

            foreach (var interfaceId in interfaces.Keys)
            {
                Type interfaceType = interfaces[interfaceId];
                var  interfaceInfo = new InterfaceInfo(interfaceType);

                if (!result.Interfaces.Values.Contains(interfaceInfo, ifaceComparer))
                {
                    result.Interfaces.Add(GrainInterfaceData.GetGrainInterfaceId(interfaceType), interfaceInfo);
                }
            }

            return(result);
        }
Exemple #2
0
        protected override string GetOrleansGetMethodNameImpl(Type grainType, GrainInterfaceInfo grainInterfaceInfo)
        {
            if (grainInterfaceInfo.Interfaces.Keys.Count == 0)
            {
                // No public method is implemented in this grain type for orleans messages
                var nullInvokeMethod = @"
                Throw New NotImplementedException()
                ";

                return(nullInvokeMethod);
            }

            var interfaces = new Dictionary <int, InterfaceInfo>(grainInterfaceInfo.Interfaces); // Copy, as we may alter the original collection in the loop below

            var interfaceSwitchBody = String.Empty;

            foreach (var kv in interfaces)
            {
                var           methodSwitchBody = String.Empty;
                int           interfaceId      = kv.Key;
                InterfaceInfo interfaceInfo    = kv.Value;

                foreach (int methodId in interfaceInfo.Methods.Keys)
                {
                    var methodInfo = interfaceInfo.Methods[methodId];

                    //add return type assembly and namespace in
                    GetGenericTypeName(methodInfo.ReturnType);

                    var invokeGrainMethod = string.Format("Return \"{0}\"", methodInfo.Name);
                    methodSwitchBody += string.Format(
                        @"Case {0}
                            {1}
                    "
                        , methodId, invokeGrainMethod);
                }

                interfaceSwitchBody += String.Format(@"
                Case {0}  ' {1}
                    Select methodId
                        {2}

                        Case Else 
                            Throw New NotImplementedException(""interfaceId=""+interfaceId+"",methodId=""+methodId)
                    End Select
",
                                                     interfaceId, interfaceInfo.InterfaceType.Name, methodSwitchBody);
            } // End for each interface

            return(string.Format(@"
            Select interfaceId
                {0}
                Case Else
                    Throw New System.InvalidCastException(""interfaceId=""+interfaceId)
            End Select",
                                 interfaceSwitchBody));
        }
        protected override string GetInvokerImpl(
            GrainInterfaceData si,
            CodeTypeDeclaration invokerClass,
            Type grainType,
            GrainInterfaceInfo grainInterfaceInfo,
            bool isClient)
        {
            //No public method is implemented in this grain type for orleans messages
            if (grainInterfaceInfo.Interfaces.Count == 0)
            {
                return(string.Format(@"
                var t = new System.Threading.Tasks.TaskCompletionSource<object>();
                t.SetException(new NotImplementedException(""No grain interfaces for type {0}""));
                return t.Task;
                ", TypeUtils.GetFullName(grainType, Language.CSharp)));
            }

            var builder = new StringBuilder();

            builder.AppendFormat(@"
            try
            {{");

            var interfaceSwitchBody = String.Empty;

            foreach (int interfaceId in grainInterfaceInfo.Interfaces.Keys)
            {
                InterfaceInfo interfaceInfo = grainInterfaceInfo.Interfaces[interfaceId];
                interfaceSwitchBody += GetMethodDispatchSwitchForInterface(interfaceId, interfaceInfo);
            }

            builder.AppendFormat(
                @"                    if (grain == null) throw new System.ArgumentNullException(""grain"");
                switch (interfaceId)
                {{
                    {0}
                    default:
                        {1};
                }}", interfaceSwitchBody, "throw new System.InvalidCastException(\"interfaceId=\"+interfaceId)");

            builder.AppendFormat(@"
            }}
            catch(Exception ex)
            {{
                var t = new System.Threading.Tasks.TaskCompletionSource<object>();
                t.SetException(ex);
                return t.Task;
            }}");

            return(builder.ToString());
        }
        protected override string GetOrleansGetMethodNameImpl(Type grainType, GrainInterfaceInfo grainInterfaceInfo)
        {
            if (grainInterfaceInfo.Interfaces.Keys.Count == 0)
            {
                // No public method is implemented in this grain type for orleans messages
                return @"
                throw new NotImplementedException();
                ";
            }

            var interfaces = new Dictionary<int, InterfaceInfo>(grainInterfaceInfo.Interfaces); // Copy, as we may alter the original collection in the loop below
            var interfaceSwitchBody = String.Empty;

            foreach (var kv in interfaces)
            {
                var methodSwitchBody = String.Empty;
                int interfaceId = kv.Key;
                InterfaceInfo interfaceInfo = kv.Value;

                foreach (int methodId in interfaceInfo.Methods.Keys)
                {
                    MethodInfo methodInfo = interfaceInfo.Methods[methodId];

                    //add return type assembly and namespace in
                    GetGenericTypeName(methodInfo.ReturnType);

                    var invokeGrainMethod = string.Format("return \"{0}\"", methodInfo.Name);
                    methodSwitchBody += string.Format(
                    @"case {0}:
                            {1};
                    ", methodId, invokeGrainMethod);
                }

                interfaceSwitchBody += String.Format(@"
                case {0}:  // {1}
                    switch (methodId)
                    {{
                        {2}
                        default: 
                            throw new NotImplementedException(""interfaceId=""+interfaceId+"",methodId=""+methodId);
                    }}", interfaceId, interfaceInfo.InterfaceType.Name, methodSwitchBody);
            } // End for each interface

            return string.Format(@"
            switch (interfaceId)
            {{
                {0}

                default:
                    throw new System.InvalidCastException(""interfaceId=""+interfaceId);
            }}", interfaceSwitchBody);
        }
        protected override string GetInvokerImpl(
            GrainInterfaceData si, 
            CodeTypeDeclaration invokerClass, 
            Type grainType, 
            GrainInterfaceInfo grainInterfaceInfo, 
            bool isClient)
        {
            //No public method is implemented in this grain type for orleans messages
            if (grainInterfaceInfo.Interfaces.Count == 0)
            {
                return string.Format(@"
                var t = new System.Threading.Tasks.TaskCompletionSource<object>();
                t.SetException(new NotImplementedException(""No grain interfaces for type {0}""));
                return t.Task;
                ", TypeUtils.GetFullName(grainType, Language.CSharp));
            }

            var builder = new StringBuilder();
            builder.AppendFormat(@"
            try
            {{");

            var interfaceSwitchBody = String.Empty;
            foreach (int interfaceId in grainInterfaceInfo.Interfaces.Keys)
            {
                InterfaceInfo interfaceInfo = grainInterfaceInfo.Interfaces[interfaceId];
                interfaceSwitchBody += GetMethodDispatchSwitchForInterface(interfaceId, interfaceInfo);
            }

            builder.AppendFormat(
                @"                    if (grain == null) throw new System.ArgumentNullException(""grain"");
                switch (interfaceId)
                {{
                    {0}
                    default:
                        {1};
                }}", interfaceSwitchBody, "throw new System.InvalidCastException(\"interfaceId=\"+interfaceId)");

            builder.AppendFormat(@"
            }}
            catch(Exception ex)
            {{
                var t = new System.Threading.Tasks.TaskCompletionSource<object>();
                t.SetException(ex);
                return t.Task;
            }}");

            return builder.ToString();
        }
        private CodeTypeDeclaration GetInvokerClass(GrainInterfaceData si, bool isClient)
        {
            Type grainType = si.Type;
            CodeTypeParameterCollection genericTypeParams = si.GenericTypeParams;

            var invokerClass = new CodeTypeDeclaration(si.InvokerClassBaseName);

            if (genericTypeParams != null)
            {
                invokerClass.TypeParameters.AddRange(genericTypeParams);
            }

            invokerClass.IsClass = true;
            MarkAsGeneratedCode(invokerClass);
            invokerClass.BaseTypes.Add(si.IsExtension
                ? new CodeTypeReference(typeof(IGrainExtensionMethodInvoker), CodeTypeReferenceOptions.GlobalReference)
                : new CodeTypeReference(typeof(IGrainMethodInvoker), CodeTypeReferenceOptions.GlobalReference));

            GrainInterfaceInfo grainInterfaceInfo = GetInterfaceInfo(grainType);
            var interfaceId = grainInterfaceInfo.Interfaces.Keys.First();

            invokerClass.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(MethodInvokerAttribute), CodeTypeReferenceOptions.GlobalReference),
                                                                           new CodeAttributeArgument(new CodePrimitiveExpression(grainType.Namespace + "." + TypeUtils.GetParameterizedTemplateName(grainType))),
                                                                           new CodeAttributeArgument(new CodePrimitiveExpression(interfaceId))));

            var interfaceIdProperty = new CodeMemberProperty
            {
                Name       = "InterfaceId",
                Attributes = MemberAttributes.Public | MemberAttributes.Final,
                Type       = new CodeTypeReference(typeof(int))
            };

            interfaceIdProperty.GetStatements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(interfaceId)));
            interfaceIdProperty.PrivateImplementationType = new CodeTypeReference(typeof(IGrainMethodInvoker), CodeTypeReferenceOptions.GlobalReference);
            invokerClass.Members.Add(interfaceIdProperty);

            //Add invoke method for Orleans message
            var orleansInvoker = new CodeMemberMethod
            {
                Attributes = MemberAttributes.Public | MemberAttributes.Final,
                Name       = "Invoke",
                ReturnType = new CodeTypeReference(typeof(Task <object>), CodeTypeReferenceOptions.GlobalReference)
            };

            orleansInvoker.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(IAddressable), CodeTypeReferenceOptions.GlobalReference), "grain"));
            orleansInvoker.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "interfaceId"));
            orleansInvoker.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "methodId"));
            orleansInvoker.Parameters.Add(new CodeParameterDeclarationExpression(typeof(object[]), "arguments"));
            orleansInvoker.PrivateImplementationType = new CodeTypeReference(typeof(IGrainMethodInvoker), CodeTypeReferenceOptions.GlobalReference);

            var orleansInvokerImpl = new CodeSnippetStatement(GetInvokerImpl(si, invokerClass, grainType, grainInterfaceInfo, isClient));

            orleansInvoker.Statements.Add(orleansInvokerImpl);
            invokerClass.Members.Add(orleansInvoker);

            //Add TryInvoke method for Orleans message, if the type is an extension interface
            if (si.IsExtension)
            {
                var orleansTryInvoker = new CodeMemberMethod
                {
                    Attributes = MemberAttributes.Public | MemberAttributes.Final,
                    Name       = "Invoke",
                    ReturnType = new CodeTypeReference(typeof(Task <object>), CodeTypeReferenceOptions.GlobalReference)
                };
                orleansTryInvoker.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(IGrainExtension), CodeTypeReferenceOptions.GlobalReference), "grain"));
                orleansTryInvoker.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "interfaceId"));
                orleansTryInvoker.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "methodId"));
                orleansTryInvoker.Parameters.Add(new CodeParameterDeclarationExpression(typeof(object[]), "arguments"));
                orleansTryInvoker.PrivateImplementationType = new CodeTypeReference(typeof(IGrainExtensionMethodInvoker), CodeTypeReferenceOptions.GlobalReference);

                var orleansTryInvokerImp = new CodeSnippetStatement(GetInvokerImpl(si, invokerClass, grainType, grainInterfaceInfo, isClient));
                orleansTryInvoker.Statements.Add(orleansTryInvokerImp);
                invokerClass.Members.Add(orleansTryInvoker);
            }

            //Add GetMethodName() method
            var getMethodName = new CodeMemberMethod
            {
                Attributes = MemberAttributes.Public | MemberAttributes.Final | MemberAttributes.Static,
                Name       = "GetMethodName",
                ReturnType = new CodeTypeReference(typeof(string))
            };

            getMethodName.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "interfaceId"));
            getMethodName.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "methodId"));

            var orleansGetMethodNameImpl = new CodeSnippetStatement(GetOrleansGetMethodNameImpl(grainType, grainInterfaceInfo));

            getMethodName.Statements.Add(orleansGetMethodNameImpl);
            invokerClass.Members.Add(getMethodName);
            return(invokerClass);
        }
Exemple #7
0
 protected override string GetInvokerImpl(GrainInterfaceData si, CodeTypeDeclaration invokerClass, Type grainType, GrainInterfaceInfo grainInterfaceInfo, bool isClient)
 {
     throw new NotImplementedException("GetInvokerImpl");
 }
Exemple #8
0
 protected override string GetOrleansGetMethodNameImpl(Type grainType, GrainInterfaceInfo grainInterfaceInfo)
 {
     throw new NotImplementedException("GetOrleansGetMethodNameImpl");
 }
Exemple #9
0
        protected override string GetInvokerImpl(GrainInterfaceData si, CodeTypeDeclaration invokerClass, Type grainType, GrainInterfaceInfo grainInterfaceInfo, bool isClient)
        {
            //No public method is implemented in this grain type for orleans messages
            if (grainInterfaceInfo.Interfaces.Count == 0)
            {
                return(string.Format(@"
            Dim t = New System.Threading.Tasks.TaskCompletionSource(Of Object)()
            t.SetException(New NotImplementedException(""No grain interfaces for type {0}""))
            Return t.Task
                ", TypeUtils.GetFullName(grainType)));
            }

            var builder = new StringBuilder();

            builder.Append(@"            Try
            ");

            var interfaceSwitchBody = String.Empty;

            foreach (int interfaceId in grainInterfaceInfo.Interfaces.Keys)
            {
                InterfaceInfo interfaceInfo = grainInterfaceInfo.Interfaces[interfaceId];
                interfaceSwitchBody += GetMethodDispatchSwitchForInterface(interfaceId, interfaceInfo);
            }

            builder.AppendFormat(
                @"If grain Is Nothing Then : Throw New System.ArgumentNullException(""grain"") : End If
                Select Case interfaceId
                    {0}
                    Case Else
                        {1}
                End Select", interfaceSwitchBody, "Throw New System.InvalidCastException(\"interfaceId=\"+interfaceId)");

            builder.Append(@"
            Catch ex As Exception
                Dim t = New System.Threading.Tasks.TaskCompletionSource(Of Object)()
                t.SetException(ex)
                Return t.Task
            End Try");

            return(builder.ToString());
        }
 protected override string GetInvokerImpl(GrainInterfaceData si, CodeTypeDeclaration invokerClass, Type grainType, GrainInterfaceInfo grainInterfaceInfo, bool isClient)
 {
     throw new NotImplementedException("GetInvokerImpl");
 }
 protected override string GetOrleansGetMethodNameImpl(Type grainType, GrainInterfaceInfo grainInterfaceInfo)
 {
     throw new NotImplementedException("GetOrleansGetMethodNameImpl");
 }
Exemple #12
0
 protected virtual string GetOrleansGetMethodNameImpl(Type grainType, GrainInterfaceInfo grainInterfaceInfo)
 {
     throw new NotImplementedException("InvokerGeneratorBasics.GetOrleansGetMethodNameImpl");
 }
Exemple #13
0
 protected virtual string GetInvokerImpl(GrainInterfaceData si, CodeTypeDeclaration invokerClass, Type grainType, GrainInterfaceInfo grainInterfaceInfo, bool isClient)
 {
     throw new NotImplementedException("InvokerGeneratorBasics.GetInvokerImpl");
 }
Exemple #14
0
        protected override string GetInvokerImpl(GrainInterfaceData si, CodeTypeDeclaration invokerClass, Type grainType, GrainInterfaceInfo grainInterfaceInfo, bool isClient)
        {
            //No public method is implemented in this grain type for orleans messages
            if (grainInterfaceInfo.Interfaces.Count == 0)
            {
                return string.Format(@"
            Dim t = New System.Threading.Tasks.TaskCompletionSource(Of Object)()
            t.SetException(New NotImplementedException(""No grain interfaces for type {0}""))
            Return t.Task
                ", TypeUtils.GetFullName(grainType, Language.VisualBasic));
            }

            var builder = new StringBuilder();
            builder.Append(@"            Try
            ");

            var interfaceSwitchBody = String.Empty;
            foreach (int interfaceId in grainInterfaceInfo.Interfaces.Keys)
            {
                InterfaceInfo interfaceInfo = grainInterfaceInfo.Interfaces[interfaceId];
                interfaceSwitchBody += GetMethodDispatchSwitchForInterface(interfaceId, interfaceInfo);
            }

            builder.AppendFormat(
                @"If grain Is Nothing Then : Throw New System.ArgumentNullException(""grain"") : End If
                Select Case interfaceId
                    {0}
                    Case Else
                        {1}
                End Select", interfaceSwitchBody, "Throw New System.InvalidCastException(\"interfaceId=\"+interfaceId)");

            builder.Append(@"
            Catch ex As Exception
                Dim t = New System.Threading.Tasks.TaskCompletionSource(Of Object)()
                t.SetException(ex)
                Return t.Task
            End Try");

            return builder.ToString();
        }