/// <summary> /// Génère le code pour la fonction de traitement des messages. /// </summary> /// <param name="instruction"></param> /// <returns></returns> string GenerateProcessMessageMacro(Model.Language.Macros.ProcessMessageMacro instruction) { StringBuilder builder = new StringBuilder(); builder.AppendLine("string processRequest(string request, int " + Clank.Core.Model.Language.SemanticConstants.ClientID + ");"); return(builder.ToString()); }
/// <summary> /// Génère le code pour la fonction de traitement des messages. /// </summary> /// <param name="instruction"></param> /// <returns></returns> string GenerateProcessMessageMacro(Model.Language.Macros.ProcessMessageMacro instruction) { StringBuilder builder = new StringBuilder(); return("#TODO !!!"); builder.AppendLine("public string ProcessRequest(string request, int " + Clank.Core.Model.Language.SemanticConstants.ClientID + ")\r\n{"); builder.AppendLine("\tNewtonsoft.Json.Linq.JArray o = (Newtonsoft.Json.Linq.JArray)Newtonsoft.Json.JsonConvert.DeserializeObject(request);"); builder.AppendLine("\tint functionId = o.Value<int>(0);"); builder.AppendLine("\tswitch(functionId)\r\n\t{"); int id = 0; List <FunctionDeclaration> decls = new List <FunctionDeclaration>(); decls.AddRange(instruction.Access.Declarations); decls.AddRange(instruction.Write.Declarations); // Switch dans lequel on traite les différents messages. foreach (FunctionDeclaration func in decls) { StringBuilder argList = new StringBuilder(); builder.AppendLine("\t\tcase " + id + ":"); int argId = 0; foreach (FunctionArgument arg in func.Func.Arguments) { // Variable name and type. string varName = "arg" + id + "_" + argId; string typeName = GenerateTypeInstanceName(arg.ArgType); builder.Append("\t\t\t" + typeName + " " + varName + " = "); // Object if (arg.ArgType.BaseType.JType == JSONType.Object || arg.ArgType.BaseType.JType == JSONType.Array) { builder.Append("(" + typeName + ")o[1][" + argId + "].ToObject(typeof(" + typeName + "));\r\n"); } else // Value { builder.Append("o[1].Value<" + typeName + ">(" + argId + ");\r\n"); } argList.Append(varName + ", "); argId++; } argList.Append(Model.Language.SemanticConstants.ClientID); // Function call and return builder.Append("\t\t\treturn Newtonsoft.Json.JsonConvert.SerializeObject(new List<object>() { " + func.Func.Name + "(" + argList + ")" + " });\r\n"); id++; } builder.Append("\t}\r\n"); builder.Append("\treturn \"\";\r\n}\r\n"); return(builder.ToString()); }
/// <summary> /// Génère le code pour la fonction de traitement des messages. /// </summary> /// <param name="instruction"></param> /// <returns></returns> string GenerateProcessMessageMacro(Model.Language.Macros.ProcessMessageMacro instruction) { /* * StringBuilder builder = new StringBuilder(); * builder.AppendLine("string State::processRequest(string request, int " + Clank.Core.Model.Language.SemanticConstants.ClientID + ")\r\n{"); * builder.AppendLine("\tReader reader = Reader();"); * builder.AppendLine("\tFastWriter writer = FastWriter();"); * builder.AppendLine("\tValue response = Value(arrayValue);"); * builder.AppendLine("\tValue query = Value(arrayValue);"); * builder.AppendLine("\treader.parse(request, query);"); * builder.AppendLine("\tValue functionArgs = *(query.begin()+1);"); * // Récupère l'id * builder.AppendLine("\tint functionId = (*query.begin()).asInt();"); * builder.AppendLine("\tswitch functionId\r\n\t{\r\n"); * int id = 0; * List<FunctionDeclaration> decls = new List<FunctionDeclaration>(); * decls.AddRange(instruction.Access.Declarations); * decls.AddRange(instruction.Write.Declarations); * * // Switch dans lequel on traite les différents messages. * foreach(FunctionDeclaration func in decls) * { * StringBuilder argList = new StringBuilder(); * argList.Append("("); * builder.AppendLine("\t\tcase " + id + ":"); * int argId = 0; * foreach(FunctionArgument arg in func.Func.Arguments) * { * // Variable name and type. * string varName = "arg" + id + "_" + argId; * string typeName = GenerateTypeInstanceName(arg.ArgType); * Variable v = new Variable() * { * Name = "(functionArgs.begin() + " + argId + ")", * Type = arg.ArgType * }; * * // Petit commentaire. * builder.AppendLine("\t\t\t// Argument " + argId + "\r\n"); * * // On désérialise la variable. * builder.AppendLine(Tools.StringUtils.Indent( * GenerateDeserializerInstruction(v, varName), 3 * )); * * argList.Append(varName + ", "); * argId++; * } * argList.Append(Model.Language.SemanticConstants.ClientID + ")"); * * // Function call and return * * // Sérialise la réponse dans un array JSON. * builder.AppendLine(Tools.StringUtils.Indent(GenerateSerializerInstruction( * new Variable() * { * Name = func.Func.Name + argList, * Type = func.Func.ReturnType * }, * "response_" + id * ), 3)); * * * builder.AppendLine("\t\t\treturn writer.write(root0);"); * * id++; * } * * builder.Append("\t}\r\n"); * builder.Append("\treturn \"\";\r\n}\r\n"); * * return builder.ToString();*/ return(""); }
/// <summary> /// Génère le code pour la fonction de traitement des messages. /// </summary> /// <param name="instruction"></param> /// <returns></returns> string GenerateProcessMessageMacro(Model.Language.Macros.ProcessMessageMacro instruction) { StringBuilder builder = new StringBuilder(); builder.AppendLine("public String processRequest(String request, int " + Clank.Core.Model.Language.SemanticConstants.ClientID + ") throws IOException\r\n{"); builder.AppendLine("\tJSONArray o = (JSONArray)JsonReader.jsonToJava(request);"); builder.AppendLine("\tint functionId = o.getInt(0);"); builder.AppendLine("\tJSONArray jsonArgs = o.getJSONArray(1);"); builder.AppendLine("\tObject[] answer = new Object[1];"); builder.AppendLine("\tswitch(functionId)\r\n\t{"); int id = 0; List <FunctionDeclaration> decls = new List <FunctionDeclaration>(); decls.AddRange(instruction.Access.Declarations); decls.AddRange(instruction.Write.Declarations); // Switch dans lequel on traite les différents messages. foreach (FunctionDeclaration func in decls) { StringBuilder argList = new StringBuilder(); builder.AppendLine("\t\tcase " + id + ":"); int argId = 0; // Arguments à passer à la fonction. foreach (FunctionArgument arg in func.Func.Arguments) { // Variable name and type. string varName = "arg" + id + "_" + argId; string typeName = GenerateTypeInstanceName(arg.ArgType); // Désérialise l'argument. builder.AppendLine(Tools.StringUtils.Indent( GenerateDeserializerInstruction(new Variable() { Type = arg.ArgType }, varName, "jsonArgs", argId.ToString()) , 3)); argList.Append(varName); argList.Append(", "); argId++; } argList.Append(Model.Language.SemanticConstants.ClientID); argList.AppendLine(");"); builder.Append("\t\t\tanswer[0] = " + func.Func.Name + "("); builder.AppendLine(argList.ToString()); // Function call and return builder.AppendLine("\t\t\treturn JsonWriter.objectToJson(answer);"); id++; } builder.Append("\t}\r\n"); builder.Append("\treturn \"\";\r\n}\r\n"); return(builder.ToString()); }