PageRenderTime 6178ms CodeModel.GetById 56ms RepoModel.GetById 0ms app.codeStats 0ms

/source/samples/ObviousCode.Interlace.BitTunnel/Knock/KnockServer/Main.cs

http://interlace.googlecode.com/
C# | 404 lines | 323 code | 81 blank | 0 comment | 50 complexity | c8e4e8a112b578f0d56cdaef9c0d6b5f MD5 | raw file
  1. using System;
  2. using ObviousCode.Interlace.BitTunnel;
  3. using ObviousCode.Interlace.BitTunnelServer;
  4. using ObviousCode.Interlace.BitTunnelLibrary;
  5. using System.IO;
  6. using ObviousCode.Interlace.BitTunnel.Connectivity;
  7. using ObviousCode.Interlace.BitTunnelLibrary.File;
  8. using System.Net;
  9. using KnockServer;
  10. using System.Collections.Generic;
  11. using KnockServer.BasicCommands;
  12. using System.Linq;
  13. using KnockServer.ServerActivities;
  14. using TelexplorerServer.Mounting;
  15. using KnockServer.Mounting;
  16. namespace TelexplorerServer
  17. {
  18. class MainClass
  19. {
  20. static string _cursor = "> ";
  21. static string _currentPath;
  22. static ServerInstance _server;
  23. static ClientInstance _localClient;
  24. static bool _forceQuit = false;
  25. static List<ICommand> _commands;
  26. IPAddress _address;
  27. int _port;
  28. public static void Main (string[] args)
  29. {
  30. AppSettings settings = new AppSettings();
  31. settings.ClientConnectionTimeout = 10000;
  32. if (!LoadSettings(settings, args))
  33. {
  34. Console.WriteLine("Bad or no parameters given");
  35. Console.WriteLine("Usage: Knock -a[ipaddress] -p[port]");
  36. return;
  37. }
  38. _localClient = new ClientInstance(settings);
  39. _localClient.LostConnection += new EventHandler<ObviousCode.Interlace.BitTunnelLibrary.Events.ExceptionEventArgs>(_localClient_LostConnection);
  40. _localClient.ConnectionMade += new EventHandler(_localClient_ConnectionMade);
  41. _localClient.ConnectionTerminated += new EventHandler(_localClient_ConnectionTerminated);
  42. Console.Write ("Starting Server ... ");
  43. _server = new ServerInstance(settings);
  44. _server.ConnectionMade += new EventHandler(_server_ConnectionMade);
  45. _server.ConnectionTerminated += new EventHandler(_server_ConnectionTerminated);
  46. _server.LostConnection += new EventHandler<ObviousCode.Interlace.BitTunnelLibrary.Events.ExceptionEventArgs>(_server_LostConnection);
  47. _server.Connect();
  48. Console.WriteLine();
  49. Console.WriteLine ("Server started");
  50. Console.WriteLine();
  51. LoadCommands();
  52. MountedFileCache.Cache.Client = _localClient;
  53. MountedFileCache.Cache.Server = _server;
  54. MountedFileCache.Cache.PromptRequested += new EventHandler(Cache_PromptRequested);
  55. try
  56. {
  57. Console.Write ("Preparing local client ... ");
  58. if (!_localClient.Connect())
  59. {
  60. Console.WriteLine("Unable to connect ...");
  61. Console.WriteLine("Stopping ...");
  62. Console.WriteLine("Bye");
  63. Console.ReadLine();
  64. return;
  65. }
  66. }
  67. catch (Exception e)
  68. {
  69. Console.WriteLine ("Unable to prepare locale client: {0}", e.Message);
  70. Console.ReadLine();
  71. Console.WriteLine("Bye");
  72. return;
  73. }
  74. _currentPath = "/";
  75. Console.WriteLine();
  76. Console.WriteLine("Welcome. Type \"quit\" to exit");
  77. Console.WriteLine();
  78. string line = "";
  79. Console.Write(Cursor);
  80. while(true)
  81. {
  82. if (_forceQuit) break;
  83. line = Console.ReadLine();
  84. if (line == string.Empty)
  85. {
  86. Console.Write(Cursor);
  87. continue;
  88. }
  89. if (line == "quit")
  90. {
  91. ConsoleKeyInfo response;
  92. do
  93. {
  94. Console.WriteLine();
  95. Console.Write("Quit? (Y/N)");
  96. response = Console.ReadKey();
  97. }
  98. while (response.Key != ConsoleKey.Y && response.Key != ConsoleKey.N);
  99. if (response.Key == ConsoleKey.Y)
  100. {
  101. break;
  102. }
  103. else
  104. {
  105. Console.WriteLine();
  106. Console.Write(Cursor);
  107. continue;
  108. }
  109. }
  110. try
  111. {
  112. ParseCommand(line);
  113. }
  114. catch (Exception e)
  115. {
  116. Console.WriteLine ("System Error: {0}", e.Message);
  117. }
  118. Console.Write(Cursor);
  119. }
  120. _server.Dispose();
  121. _localClient.Dispose();
  122. Console.WriteLine("Bye");
  123. }
  124. private static bool LoadSettings(AppSettings settings, string[] args)
  125. {
  126. Arguments a = new Arguments(args);
  127. string ip;
  128. string portString;
  129. try
  130. {
  131. ip = a.Get("-a");
  132. portString = a.Get("-p");
  133. }
  134. catch (System.Exception ex)
  135. {
  136. return false;
  137. }
  138. IPAddress address;
  139. int port;
  140. if (ip == null || !IPAddress.TryParse(ip, out address))
  141. {
  142. return false;
  143. }
  144. if (portString == null || !Int32.TryParse(portString, out port))
  145. {
  146. return false;
  147. }
  148. settings.Port = port;
  149. settings.ServerAddress = address;
  150. settings.ServerIsRemote = false;
  151. return true;
  152. }
  153. static void Cache_PromptRequested(object sender, EventArgs e)
  154. {
  155. Console.Write(Cursor);
  156. }
  157. private static void LoadCommands()
  158. {
  159. _commands = new List<ICommand>();
  160. _commands.Add(new PingServer());
  161. _commands.Add(new ServerLists());
  162. _commands.Add(new DirectoryMounter());
  163. _commands.Add(new FileMounter());
  164. _commands.Add(new ClearConsole());
  165. foreach(ICommand command in _commands)
  166. {
  167. command.PromptRequested += new EventHandler(command_PromptRequested);
  168. }
  169. }
  170. static void command_PromptRequested(object sender, EventArgs e)
  171. {
  172. Console.WriteLine(Cursor);
  173. }
  174. static void _server_LostConnection(object sender, ObviousCode.Interlace.BitTunnelLibrary.Events.ExceptionEventArgs e)
  175. {
  176. throw new NotImplementedException();
  177. }
  178. static void _server_ConnectionTerminated(object sender, EventArgs e)
  179. {
  180. Console.WriteLine("Server Disconnected ... ");
  181. }
  182. static void _server_ConnectionMade(object sender, EventArgs e)
  183. {
  184. Console.WriteLine("Server Connected");
  185. }
  186. static void _localClient_ConnectionTerminated(object sender, EventArgs e)
  187. {
  188. Console.WriteLine("Client Connection Terminated");
  189. _forceQuit = true;
  190. }
  191. static void _localClient_ConnectionMade(object sender, EventArgs e)
  192. {
  193. Console.WriteLine("Client Connection Made.");
  194. }
  195. static void _localClient_LostConnection(object sender, ObviousCode.Interlace.BitTunnelLibrary.Events.ExceptionEventArgs e)
  196. {
  197. Console.WriteLine("Client Lost Connection: {0}", e.ThrownException.Message);
  198. _forceQuit = true;
  199. }
  200. static void ParseCommand (string line)
  201. {
  202. CommandContext context = new CommandContext();
  203. context.Command = line ;
  204. context.CurrentPath = CurrentPath;
  205. context.LocalClient = _localClient;
  206. context.Server = _server;
  207. List<ICommand> valid = _commands.Where(c => c.HandlesCommand(context)).ToList();
  208. if (valid.Count == 1)
  209. {
  210. valid[0].HandleCommand(context);
  211. return;
  212. }
  213. else if (valid.Count > 1)
  214. {
  215. valid[valid.Max(c => c.Priority)].HandleCommand(context);
  216. return;
  217. }
  218. if (line.Trim().StartsWith("dir") || line.Trim().StartsWith("ls"))
  219. {
  220. ListCurrentDirectory(line.Substring(
  221. line.Trim().StartsWith("dir")?3:2
  222. ));
  223. }
  224. else if (line.Trim().StartsWith("cd "))
  225. {
  226. SwapPath(line.Trim().Substring(3));
  227. }
  228. else if (line.Trim()=="fsi")
  229. {
  230. ListFileSystemInfo();
  231. }
  232. else if (line.Trim()=="path" || line.Trim() =="pwd")
  233. {
  234. EchoPath();
  235. }
  236. else
  237. {
  238. Console.WriteLine("Bad command {0}", line);
  239. }
  240. }
  241. static void EchoPath ()
  242. {
  243. Console.WriteLine ("Current Path: {0}", CurrentPath);
  244. }
  245. static void ListFileSystemInfo ()
  246. {
  247. DirectoryInfo dir = new DirectoryInfo("/");
  248. foreach (FileSystemInfo fsi in dir.GetFileSystemInfos())
  249. {
  250. Console.WriteLine (fsi.FullName);
  251. }
  252. }
  253. static void SwapPath (string path)
  254. {
  255. string subPath = Path.Combine(_currentPath, path);
  256. if (path == "..")
  257. {
  258. DirectoryInfo parent = Directory.GetParent(_currentPath);
  259. _currentPath = parent == null ? _currentPath : parent.FullName;
  260. }
  261. else if (Directory.Exists(subPath))
  262. {
  263. _currentPath = subPath;
  264. }
  265. else if (Directory.Exists(path))
  266. {
  267. _currentPath = path;
  268. }
  269. else
  270. {
  271. Console.WriteLine ("Bad path \"{0}\"", path);
  272. return;
  273. }
  274. }
  275. static void ListCurrentDirectory(string command)
  276. {
  277. command = command.Trim();
  278. if (!string.IsNullOrEmpty(command) && command != "-d" && command != "-f")
  279. {
  280. Console.WriteLine ("Bad parameter \"{0}\"", command);
  281. return;
  282. }
  283. DirectoryInfo directory = new DirectoryInfo(CurrentPath);
  284. if (string.IsNullOrEmpty(command) || command == "-d")
  285. {
  286. ListDirectories (directory);
  287. }
  288. if (string.IsNullOrEmpty(command) || command == "-f")
  289. {
  290. ListFiles (directory);
  291. }
  292. }
  293. static void ListFiles (DirectoryInfo directory)
  294. {
  295. foreach (FileInfo file in directory.GetFiles())
  296. {
  297. Console.WriteLine (file.Name);
  298. }
  299. }
  300. static void ListDirectories (DirectoryInfo directory)
  301. {
  302. foreach(DirectoryInfo subpath in directory.GetDirectories())
  303. {
  304. Console.WriteLine (subpath.Name);
  305. }
  306. }
  307. static string CurrentPath
  308. {
  309. get
  310. {
  311. DirectoryInfo dir = new DirectoryInfo(_currentPath);
  312. return dir.FullName;
  313. }
  314. }
  315. static string Cursor
  316. {
  317. get
  318. {
  319. return string.Format("{0}{1}", CurrentPath, _cursor);
  320. }
  321. }
  322. }
  323. }