PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/verify/src/Checked/Libraries/NetStack2/NetStack2.cs

#
C# | 149 lines | 101 code | 21 blank | 27 comment | 3 complexity | d727f000e3852bca8fc4175ff28910b3 MD5 | raw file
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Microsoft Research Singularity
  4. //
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. //
  7. // File: NetStack.sg
  8. //
  9. // Note:
  10. //
  11. #if false
  12. using System;
  13. using System.Runtime.InteropServices;
  14. using System.Runtime.CompilerServices;
  15. using System.Threading;
  16. using Microsoft.SingSharp;
  17. using Microsoft.Singularity;
  18. using Microsoft.Singularity.Channels;
  19. using Microsoft.Singularity.Directory;
  20. using Microsoft.Singularity.Security;
  21. using Microsoft.Singularity.Io;
  22. using Microsoft.Singularity.Io.Net;
  23. using Microsoft.Singularity.Configuration;
  24. using Microsoft.Singularity.ServiceManager;
  25. using Microsoft.Singularity.NetStack;
  26. using Microsoft.Singularity.NetStack2.Channels.Nic;
  27. //[assembly: ApplicationPublisherAttribute("singularity.microsoft.com")]
  28. //[assembly: AssertPrivilegeAttribute("$register-privilege.localhost")]
  29. namespace Microsoft.Singularity.NetStack2
  30. {
  31. using NetStack2.Channels.Private;
  32. using NetStack2.Channels.Nic;
  33. //[Category("Service")]
  34. internal class ServiceParameters
  35. {
  36. //[Endpoint(Affinity=1)]
  37. public readonly /*TRef<NicDeviceContract.Imp>*/ContractRef NicEndpointRef;
  38. //[EndpointArray(Affinity=1)]
  39. public readonly /*TRef<NicDeviceContract.Imp>*/ContractRef[] nicRefs;
  40. }
  41. /// <summary>
  42. /// The NetStack Application class is a single instance service running
  43. /// on a host. When it runs it attempts to register as /service/netstack;
  44. /// success indicates no other instances and it then initializes
  45. /// the various NetStack subsystems.
  46. /// </summary>
  47. class NetStackApplication
  48. {
  49. NetStackApplication(ServiceParameters parameters)
  50. {
  51. routingManager = new RoutingExpManager();
  52. ipManager = new IPContract();
  53. this.nicRefs = parameters.nicRefs;
  54. }
  55. readonly RoutingExpManager routingManager;
  56. readonly IPContract ipManager;
  57. readonly /*TRef*/NicDeviceContract/*.Imp*/[] nicRefs;
  58. bool Start()
  59. {
  60. try {
  61. ipManager.Start();
  62. routingManager.Start();
  63. for (int i = 0; i < nicRefs.Length; i++) {
  64. NicDeviceContract/*.Imp*/ imp = (nicRefs[i]).Acquire();
  65. Nic.CreateAndRegister(imp, String.Format("/dev/nic{0}", i));
  66. }
  67. return true;
  68. }
  69. catch (Exception ex) {
  70. Dbg("An exception occurred during service startup: " + ex.Message);
  71. return false;
  72. }
  73. }
  74. void Stop()
  75. {
  76. // Stop the service provider threads.
  77. Dbg("Stopping service provider threads");
  78. routingManager.Stop();
  79. ipManager.Stop();
  80. // StaticConfiguration.Stop() walks the list of installed modules and calls
  81. // the StopModule() method of each. We do this after stopping the service
  82. // provider threads.
  83. Dbg("Stopping protocol modules");
  84. }
  85. internal static int AppMain(ServiceParameters parameters)
  86. {
  87. NicDeviceContract/*.Imp*/ nicImp = parameters.NicEndpointRef.Acquire();
  88. nicImp.RecvSuccess();
  89. //delete nicImp;
  90. DebugStub.WriteLine("Closed nic contract via reflection\n");
  91. ARP arp = new ARP();
  92. IP.Initialize(arp);
  93. Ethernet.Initialize(arp);
  94. NetStackApplication app = new NetStackApplication(parameters);
  95. try {
  96. return app.Run();
  97. }
  98. finally {
  99. //delete app;
  100. Dbg("NetStack is terminating.");
  101. }
  102. }
  103. int Run()
  104. {
  105. if (Start()) {
  106. svcontrol.SendStartSucceeded();
  107. ServiceMain();
  108. Stop();
  109. return 0;
  110. }
  111. else {
  112. Dbg("Sending StartFailed to Service Manager.");
  113. svcontrol.SendStartFailed(ServiceError.Unknown);
  114. Stop();
  115. return -1;
  116. }
  117. }
  118. static void Dbg(string line)
  119. {
  120. DebugStub.WriteLine(line);
  121. }
  122. static void Dbg(string format, params object[] args)
  123. {
  124. Dbg(String.Format(format, args));
  125. }
  126. }
  127. }
  128. #endif