aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/ConsoleClient
diff options
context:
space:
mode:
authorMelanie2009-08-17 08:45:20 +0100
committerMelanie2009-08-17 08:45:20 +0100
commitcef16bec6dabc90fdccf82f755d24683d834208b (patch)
tree3265dbc64721f17eef82b06c7e16a7b4c6e0f657 /OpenSim/ConsoleClient
parentMerge branch 'master' of ssh://melanie@opensimulator.org/var/git/opensim (diff)
downloadopensim-SC_OLD-cef16bec6dabc90fdccf82f755d24683d834208b.zip
opensim-SC_OLD-cef16bec6dabc90fdccf82f755d24683d834208b.tar.gz
opensim-SC_OLD-cef16bec6dabc90fdccf82f755d24683d834208b.tar.bz2
opensim-SC_OLD-cef16bec6dabc90fdccf82f755d24683d834208b.tar.xz
Add the OpenSim.ConsoleClient app.
Usage: OpenSim.ConsoleClient -h <host> -p <port> -u <user> -P <pass> host defaults to localhost, port defaults to 8003.
Diffstat (limited to 'OpenSim/ConsoleClient')
-rw-r--r--OpenSim/ConsoleClient/ConsoleClient.cs185
-rw-r--r--OpenSim/ConsoleClient/Requester.cs86
2 files changed, 271 insertions, 0 deletions
diff --git a/OpenSim/ConsoleClient/ConsoleClient.cs b/OpenSim/ConsoleClient/ConsoleClient.cs
new file mode 100644
index 0000000..0d4f3cf
--- /dev/null
+++ b/OpenSim/ConsoleClient/ConsoleClient.cs
@@ -0,0 +1,185 @@
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 */
27
28using Nini.Config;
29using log4net;
30using System.Reflection;
31using System;
32using System.Xml;
33using System.Collections.Generic;
34using OpenSim.Server.Base;
35using OpenSim.Framework.Console;
36using OpenMetaverse;
37
38namespace OpenSim.ConsoleClient
39{
40 public class OpenSimConsoleClient
41 {
42 private static readonly ILog m_log =
43 LogManager.GetLogger(
44 MethodBase.GetCurrentMethod().DeclaringType);
45
46 protected static ServicesServerBase m_Server = null;
47 private static string m_Host;
48 private static int m_Port;
49 private static string m_User;
50 private static string m_Pass;
51 private static UUID m_SessionID;
52
53 static int Main(string[] args)
54 {
55 m_Server = new ServicesServerBase("Client", args);
56
57 IConfig serverConfig = m_Server.Config.Configs["Startup"];
58 if (serverConfig == null)
59 {
60 System.Console.WriteLine("Startup config section missing in .ini file");
61 throw new Exception("Configuration error");
62 }
63
64 ArgvConfigSource argvConfig = new ArgvConfigSource(args);
65
66 argvConfig.AddSwitch("Startup", "host", "h");
67 argvConfig.AddSwitch("Startup", "port", "p");
68 argvConfig.AddSwitch("Startup", "user", "u");
69 argvConfig.AddSwitch("Startup", "pass", "P");
70
71 m_Server.Config.Merge(argvConfig);
72
73 m_User = serverConfig.GetString("user", "Test");
74 m_Host = serverConfig.GetString("host", "localhost");
75 m_Port = serverConfig.GetInt("port", 8003);
76 m_Pass = serverConfig.GetString("pass", "secret");
77
78 Requester.MakeRequest("http://"+m_Host+":"+m_Port.ToString()+"/StartSession/", String.Format("USER={0}&PASS={1}", m_User, m_Pass), LoginReply);
79
80 int res = m_Server.Run();
81
82 Environment.Exit(res);
83
84 return 0;
85 }
86
87 private static void SendCommand(string module, string[] cmd)
88 {
89 string sendCmd = String.Join(" ", cmd);
90
91 Requester.MakeRequest("http://"+m_Host+":"+m_Port.ToString()+"/SessionCommand/", String.Format("ID={0}&COMMAND={1}", m_SessionID, sendCmd), CommandReply);
92 }
93
94 public static void LoginReply(string requestUrl, string requestData, string replyData)
95 {
96 XmlDocument doc = new XmlDocument();
97
98 doc.LoadXml(replyData);
99
100 XmlNodeList rootL = doc.GetElementsByTagName("ConsoleSession");
101 if (rootL.Count != 1)
102 {
103 MainConsole.Instance.Output("Connection data info was not valid");
104 Environment.Exit(1);
105 }
106 XmlElement rootNode = (XmlElement)rootL[0];
107
108 if (rootNode == null)
109 {
110 MainConsole.Instance.Output("Connection data info was not valid");
111 Environment.Exit(1);
112 }
113
114 XmlNodeList helpNodeL = rootNode.GetElementsByTagName("HelpTree");
115 if (helpNodeL.Count != 1)
116 {
117 MainConsole.Instance.Output("Connection data info was not valid");
118 Environment.Exit(1);
119 }
120
121 XmlElement helpNode = (XmlElement)helpNodeL[0];
122 if (helpNode == null)
123 {
124 MainConsole.Instance.Output("Connection data info was not valid");
125 Environment.Exit(1);
126 }
127
128 XmlNodeList sessionL = rootNode.GetElementsByTagName("SessionID");
129 if (sessionL.Count != 1)
130 {
131 MainConsole.Instance.Output("Connection data info was not valid");
132 Environment.Exit(1);
133 }
134
135 XmlElement sessionNode = (XmlElement)sessionL[0];
136 if (sessionNode == null)
137 {
138 MainConsole.Instance.Output("Connection data info was not valid");
139 Environment.Exit(1);
140 }
141
142 if (!UUID.TryParse(sessionNode.InnerText, out m_SessionID))
143 {
144 MainConsole.Instance.Output("Connection data info was not valid");
145 Environment.Exit(1);
146 }
147
148 MainConsole.Instance.Commands.FromXml(helpNode, SendCommand);
149
150 Requester.MakeRequest("http://"+m_Host+":"+m_Port.ToString()+"/ReadResponses/"+m_SessionID.ToString()+"/", String.Empty, ReadResponses);
151 }
152
153 public static void ReadResponses(string requestUrl, string requestData, string replyData)
154 {
155 XmlDocument doc = new XmlDocument();
156
157 doc.LoadXml(replyData);
158
159 XmlNodeList rootNodeL = doc.GetElementsByTagName("ConsoleSession");
160 if (rootNodeL.Count != 1 || rootNodeL[0] == null)
161 {
162 Requester.MakeRequest(requestUrl, requestData, ReadResponses);
163 return;
164 }
165
166 foreach (XmlNode part in rootNodeL[0].ChildNodes)
167 {
168 if (part.Name != "Line")
169 continue;
170
171 string[] parts = part.InnerText.Split(new char[] {':'}, 3);
172 if (parts.Length != 3)
173 continue;
174
175 MainConsole.Instance.Output(parts[2], parts[1]);
176 }
177
178 Requester.MakeRequest(requestUrl, requestData, ReadResponses);
179 }
180
181 public static void CommandReply(string requestUrl, string requestData, string replyData)
182 {
183 }
184 }
185}
diff --git a/OpenSim/ConsoleClient/Requester.cs b/OpenSim/ConsoleClient/Requester.cs
new file mode 100644
index 0000000..af7860d
--- /dev/null
+++ b/OpenSim/ConsoleClient/Requester.cs
@@ -0,0 +1,86 @@
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 */
27
28using System;
29using System.IO;
30using System.Net;
31using System.Reflection;
32using System.Text;
33using System.Xml;
34using System.Xml.Serialization;
35using log4net;
36
37namespace OpenSim.ConsoleClient
38{
39 public delegate void ReplyDelegate(string requestUrl, string requestData, string replyData);
40
41 public class Requester
42 {
43 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
44
45 public static void MakeRequest(string requestUrl, string data,
46 ReplyDelegate action)
47 {
48 WebRequest request = WebRequest.Create(requestUrl);
49 WebResponse response = null;
50
51 request.Method = "POST";
52
53 request.ContentType = "application/x-www-form-urlencoded";
54
55 byte[] buffer = new System.Text.ASCIIEncoding().GetBytes(data);
56 int length = (int) buffer.Length;
57 request.ContentLength = length;
58
59 request.BeginGetRequestStream(delegate(IAsyncResult res)
60 {
61 Stream requestStream = request.EndGetRequestStream(res);
62
63 requestStream.Write(buffer, 0, length);
64
65 request.BeginGetResponse(delegate(IAsyncResult ar)
66 {
67 string reply = String.Empty;
68
69 response = request.EndGetResponse(ar);
70
71 try
72 {
73 StreamReader r = new StreamReader(response.GetResponseStream());
74 reply = r.ReadToEnd();
75
76 }
77 catch (System.InvalidOperationException)
78 {
79 }
80
81 action(requestUrl, data, reply);
82 }, null);
83 }, null);
84 }
85 }
86}