aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Tools/OpenSim.GridLaunch/GUI/Network/TCPD.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Tools/OpenSim.GridLaunch/GUI/Network/TCPD.cs231
1 files changed, 0 insertions, 231 deletions
diff --git a/OpenSim/Tools/OpenSim.GridLaunch/GUI/Network/TCPD.cs b/OpenSim/Tools/OpenSim.GridLaunch/GUI/Network/TCPD.cs
deleted file mode 100644
index b48cfea..0000000
--- a/OpenSim/Tools/OpenSim.GridLaunch/GUI/Network/TCPD.cs
+++ /dev/null
@@ -1,231 +0,0 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSimulator Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27using System;
28using System.Collections;
29using System.Collections.Generic;
30using System.Net;
31using System.Net.Sockets;
32using System.Reflection;
33using System.Text;
34using System.Threading;
35using log4net;
36
37namespace OpenSim.GridLaunch.GUI.Network
38{
39 public class TCPD : IGUI, IDisposable
40 {
41 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
42 private List<Client> Clients = new List<Client>();
43
44 private readonly int defaultPort = 7998;
45 private TcpListener tcpListener;
46 private Thread listenThread;
47 private Thread clientThread;
48
49
50 private List<string> Apps = new List<string>();
51 internal string currentApp = "";
52 private bool quitTyped = false;
53
54 public TCPD()
55 {
56 Program.AppCreated += Program_AppCreated;
57 Program.AppRemoved += Program_AppRemoved;
58 Program.AppConsoleOutput += Program_AppConsoleOutput;
59 Program.Command.CommandLine += Command_CommandLine;
60
61 }
62
63 ~TCPD()
64 {
65 Dispose();
66 }
67 private bool isDisposed = false;
68 ///<summary>
69 ///Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
70 ///</summary>
71 ///<filterpriority>2</filterpriority>
72 public void Dispose()
73 {
74 if (isDisposed)
75 return;
76 isDisposed = true;
77 tcpd_Stop();
78 }
79
80 public void StartGUI()
81 {
82 // We are starting
83 tcpd_Start();
84 }
85
86 public void StopGUI()
87 {
88 // We are stopping
89 tcpd_Stop();
90 }
91
92
93 #region GridLaunch Events
94 private void Command_CommandLine(string application, string command, string arguments)
95 {
96 // If command is a number then someone might be trying to change console: /1, /2, etc.
97 int currentAppNum = 0;
98 if (int.TryParse(command, out currentAppNum))
99 if (currentAppNum <= Apps.Count)
100 {
101 currentApp = Apps[currentAppNum - 1];
102 TCPWriteToAll("Changed console to app: " + currentApp + Environment.NewLine);
103 }
104 else
105 TCPWriteToAll("Unable to change to app number: " + currentAppNum + Environment.NewLine);
106
107 // Has user typed quit?
108 if (command.ToLower() == "quit")
109 quitTyped = true;
110
111 // Has user typed /list?
112 if (command.ToLower() == "list")
113 {
114 TCPWriteToAll("/0 Log console");
115 for (int i = 1; i <= Apps.Count; i++)
116 {
117 TCPWriteToAll(string.Format("/{0} {1}", i, Apps[i - 1]));
118 }
119 }
120
121 }
122
123 void Program_AppCreated(string App)
124 {
125 TCPWriteToAll("Started: " + App);
126 if (!Apps.Contains(App))
127 Apps.Add(App);
128 }
129
130 void Program_AppRemoved(string App)
131 {
132 TCPWriteToAll("Stopped: " + App);
133 if (Apps.Contains(App))
134 Apps.Remove(App);
135 }
136
137 private void Program_AppConsoleOutput(string App, string Text)
138 {
139 TCPWriteToAll(App, Text);
140 }
141
142 #endregion
143
144 private void tcpd_Start()
145 {
146 listenThread = new Thread(new ThreadStart(ListenForClients));
147 listenThread.Name = "TCPDThread";
148 listenThread.IsBackground = true;
149 listenThread.Start();
150
151 while (!quitTyped)
152 {
153 Thread.Sleep(500);
154 }
155
156 //clientThread = new Thread(new ThreadStart(ProcessClients));
157 //clientThread.Name = "TCPClientThread";
158 //clientThread.IsBackground = true;
159 ////clientThread.Start();
160
161 }
162 private void tcpd_Stop()
163 {
164 StopThread(listenThread);
165 StopThread(clientThread);
166 }
167 private void ListenForClients()
168 {
169 int Port = 0;
170 int.TryParse(Program.Settings["TCPPort"], out Port);
171 if (Port < 1)
172 Port = defaultPort;
173
174 m_log.Info("Starting TCP Server on port " + Port);
175 this.tcpListener = new TcpListener(IPAddress.Any, Port);
176
177 this.tcpListener.Start();
178
179 while (true)
180 {
181 // Blocks until a client has connected to the server
182 TcpClient tcpClient = this.tcpListener.AcceptTcpClient();
183 Client client = new Client(this, tcpClient);
184
185 lock (Clients)
186 {
187 Clients.Add(client);
188 }
189 System.Threading.Thread.Sleep(500);
190 }
191 }
192
193 private static void StopThread(Thread t)
194 {
195 if (t != null)
196 {
197 m_log.Debug("Stopping thread " + t.Name);
198 try
199 {
200 if (t.IsAlive)
201 t.Abort();
202 t.Join(2000);
203 t = null;
204 }
205 catch (Exception ex)
206 {
207 m_log.Error("Exception stopping thread: " + ex.ToString());
208 }
209 }
210 }
211
212 private void TCPWriteToAll(string app, string text)
213 {
214 TCPWriteToAll(text);
215 }
216 private void TCPWriteToAll(string text)
217 {
218 foreach (Client c in new ArrayList(Clients))
219 {
220 try
221 {
222 c.Write(text);
223 } catch (Exception ex)
224 {
225 m_log.Error("Exception writing to TCP: " + ex.ToString());
226 }
227 }
228 }
229
230 }
231}