PageRenderTime 47ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/base/Applications/TypeTest/TypeTest.cs

#
C# | 213 lines | 171 code | 34 blank | 8 comment | 15 complexity | da4296764978c8e67fb4c8dbff4b1127 MD5 | raw file
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Microsoft Research Singularity
  4. //
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. //
  7. // Note: Simple Singularity test program.
  8. //
  9. using Microsoft.Singularity.V1.Services;
  10. using System;
  11. using System.Runtime.CompilerServices;
  12. using System.Threading;
  13. using Microsoft.SingSharp;
  14. using System.Reflection;
  15. using Microsoft.Singularity.Io;
  16. using Microsoft.Singularity.Directory;
  17. using Microsoft.Singularity.Channels;
  18. using Microsoft.Singularity.Channels;
  19. using Microsoft.Contracts;
  20. using Microsoft.SingSharp.Reflection;
  21. using Microsoft.Singularity.Applications;
  22. using Microsoft.Singularity.Io;
  23. using Microsoft.Singularity.Configuration;
  24. [assembly: Transform(typeof(ApplicationResourceTransform))]
  25. namespace Microsoft.Singularity.Applications
  26. {
  27. [ConsoleCategory(DefaultAction=true)]
  28. internal class Parameters {
  29. [InputEndpoint("data")]
  30. public readonly TRef<UnicodePipeContract.Exp:READY> Stdin;
  31. [OutputEndpoint("data")]
  32. public readonly TRef<UnicodePipeContract.Imp:READY> Stdout;
  33. [BoolParameter( "help", Default=false, HelpMessage="Display Extended help message.")]
  34. internal bool doHelp;
  35. [StringParameter( "path", Position=0, Mandatory=true)]
  36. internal string nsPath;
  37. [LongParameter( "contract", Position=1, Mandatory=true)]
  38. internal long contractNum;
  39. reflective internal Parameters();
  40. internal int AppMain() {
  41. return TypeTest.AppMain(this);
  42. }
  43. }
  44. contract DummyContract : ServiceContract {
  45. }
  46. rep struct TestStruct {
  47. int x;
  48. int y;
  49. public TestStruct(int a, int b) { x = a; y = b; }
  50. }
  51. public class NS : ITracked {
  52. DirectoryServiceContract.Imp:Ready! ns;
  53. public NS() {
  54. this.ns = DirectoryService.NewClientEndpoint();
  55. base();
  56. }
  57. public ServiceContract.Exp Bind(string! path, [Claims] ServiceContract.Exp! ep)
  58. {
  59. char[] in ExHeap p = Bitter.FromString2(path);
  60. expose (this) {
  61. ns.SendBind(p, ep);
  62. switch receive {
  63. case ns.AckBind():
  64. return null;
  65. case ns.NakBind(reject, error):
  66. return reject;
  67. case ns.ChannelClosed():
  68. throw new Exception("ns channel closed");
  69. }
  70. }
  71. }
  72. public void Dispose() {
  73. delete this.ns;
  74. }
  75. void ITracked.Expose() {}
  76. void ITracked.UnExpose() {}
  77. void ITracked.Acquire() {}
  78. void ITracked.Release() {}
  79. }
  80. public class TypeTest
  81. {
  82. internal static int AppMain(Parameters! config)
  83. {
  84. if (config.doHelp) {
  85. Console.WriteLine("typetest <nspath> contract-num");
  86. Console.WriteLine(" where contract-nums are:");
  87. Console.WriteLine(" 0 SoundDeviceContract");
  88. Console.WriteLine(" 1 DiskDeviceContract");
  89. Console.WriteLine(" 2 DummyContract");
  90. Console.WriteLine(" 3 Downcast Imp contract");
  91. return 1;
  92. }
  93. int contractNum = (int) config.contractNum;
  94. NS ns = new NS();
  95. string path = (!)config.nsPath;
  96. Console.WriteLine("Trying to lookup {0} with contract {1}", path, contractNum);
  97. ServiceContract.Exp reject;
  98. switch (contractNum) {
  99. case 0:
  100. {
  101. Console.WriteLine("Allocating SoundDevice Channel");
  102. SoundDeviceContract.Exp! exp;
  103. SoundDeviceContract.Imp! imp;
  104. SoundDeviceContract.NewChannel(out imp, out exp);
  105. reject = ns.Bind(path, exp);
  106. delete imp;
  107. break;
  108. }
  109. case 1:
  110. {
  111. Console.WriteLine("Allocating DiskDevice Channel");
  112. DiskDeviceContract.Exp! exp;
  113. DiskDeviceContract.Imp! imp;
  114. DiskDeviceContract.NewChannel(out imp, out exp);
  115. reject = ns.Bind(path, exp);
  116. delete imp;
  117. break;
  118. }
  119. case 2:
  120. {
  121. Console.WriteLine("Allocating Channel");
  122. DummyContract.Exp! exp;
  123. DummyContract.Imp! imp;
  124. DummyContract.NewChannel(out imp, out exp);
  125. reject = ns.Bind(path, exp);
  126. delete imp;
  127. break;
  128. }
  129. case 3:
  130. {
  131. Console.WriteLine("Doing Imp cast test");
  132. DummyContract.Exp! exp;
  133. DummyContract.Imp! imp;
  134. DummyContract.NewChannel(out imp, out exp);
  135. Endpoint! upcast = imp;
  136. DummyContract.Imp imp2 = upcast as DummyContract.Imp;
  137. if (imp2 == null) {
  138. Console.WriteLine("Downcast to Imp failed");
  139. }
  140. else {
  141. Console.WriteLine("Downcast to Imp succeeded");
  142. }
  143. DiskDeviceContract.Imp imp3 = upcast as DiskDeviceContract.Imp;
  144. if (imp3 == null) {
  145. Console.WriteLine("Downcast to bad Imp failed (GOOD)");
  146. }
  147. else {
  148. Console.WriteLine("Downcast to bad Imp succeeded (BAD)");
  149. }
  150. DummyContract.Exp exp4 = upcast as DummyContract.Exp;
  151. if (exp4 == null) {
  152. Console.WriteLine("Downcast to Exp failed (GOOD)");
  153. }
  154. else {
  155. Console.WriteLine("Downcast to Exp succeeded (BAD)");
  156. }
  157. delete exp;
  158. delete imp;
  159. reject = null;
  160. break;
  161. }
  162. default:
  163. Console.WriteLine("Unsupported contract {0}", contractNum);
  164. reject = null;
  165. break;
  166. }
  167. if (reject == null) {
  168. Console.WriteLine("Bind succeeded!");
  169. }
  170. else {
  171. Console.WriteLine("Bind failed!");
  172. delete reject;
  173. }
  174. ns.Dispose();
  175. Console.WriteLine("TypeTest exiting.");
  176. return 0;
  177. }
  178. }
  179. }