GAMSRemoteClass.cs
1using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.IO;
6 using GAMS;
7 
8 
9 namespace GAMSRemoteObject
10 {
24  public class GAMSRemoteClass : MarshalByRefObject
25  {
26 
27  public GAMSRemoteClass()
28  {
29  Console.WriteLine("Constructor");
30  }
31 
33  {
34  Console.WriteLine("Destructor");
35  }
36 
37  public int RunServer(string ModelText, byte[] GDXInFile, byte[] ParameterFile, ref byte[] GDXOutFile, ref string LogOutput, ref string Message)
38  {
39  try
40  {
41  bool fDebug = false;
42 
43  if (ModelText.Length == 0)
44  {
45  Message = "No model text received";
46  return 1;
47  }
48 
49  GAMSWorkspace ws = new GAMSWorkspace();
50  GAMSOptions opt = null;
51  if (ParameterFile.Length > 0)
52  {
53  string ParameterFileName = Path.Combine(ws.WorkingDirectory, ws.ScratchFilePrefix + Path.GetRandomFileName());
54  if (fDebug)
55  Console.WriteLine("Writing Parameter file:" + ParameterFileName);
56  File.WriteAllBytes(ParameterFileName, ParameterFile);
57  opt = ws.AddOptions(optFile: ParameterFileName);
58  if (!fDebug)
59  File.Delete(ParameterFileName);
60  }
61 
62  string GDXInFileName = Path.Combine(ws.WorkingDirectory, ws.ScratchFilePrefix + Path.GetRandomFileName());
63  string GDXOutFileName = Path.Combine(ws.WorkingDirectory, ws.ScratchFilePrefix + Path.GetRandomFileName());
64 
65  if (GDXInFile.Length > 0)
66  {
67  if (fDebug)
68  Console.WriteLine("Writing GDX File:" + GDXInFileName);
69  File.WriteAllBytes(GDXInFileName, GDXInFile);
70  opt.Defines["GDXInFile"] = GDXInFileName;
71  }
72  opt.Defines["GDXOutFile"] = GDXOutFileName;
73 
74  if (fDebug)
75  Console.WriteLine("Starting job...");
76 
77  StringWriter log = new StringWriter();
78  GAMSJob job = ws.AddJobFromString(ModelText);
79  job.Run(opt, log, false);
80  LogOutput = log.ToString();
81 
82  if (fDebug)
83  Console.WriteLine("Reading GDXOut File: " + GDXOutFileName);
84  GDXOutFile = File.ReadAllBytes(GDXOutFileName);
85 
86  if (!fDebug)
87  {
88  File.Delete(GDXInFileName);
89  File.Delete(GDXOutFileName);
90  }
91  Message = "Successful Run";
92  return 0;
93 
94  }
95  catch (Exception ex)
96  {
97  Console.WriteLine("Error:\n{0}", ex.Message);
98  Message = ex.Message;
99  return 1;
100  }
101  }
102 
103 
104  }
105 }
Dictionary< string, string > Defines
This example demonstrates how to implement a simple GAMS Server. The example has two parts: GAMSServe...
GAMSOptions AddOptions(GAMSOptions optFrom=null)
GAMSJob AddJobFromString(string gamsSource, GAMSCheckpoint checkpoint=null, string jobName=null)
void Run(GAMSOptions gamsOptions=null, GAMSCheckpoint checkpoint=null, TextWriter output=null, Boolean createOutDB=true)