1 // Copyright 2006 Alp Toker <alp@atoker.com>
2 // This software is made available under the MIT License
3 // See COPYING for details
4 
5 using System;
6 using DBus;
7 using DBus.Transports;
8 using org.freedesktop.DBus;
9 
10 using System.IO;
11 using System.Net;
12 using System.Net.Sockets;
13 
14 using System.Threading;
15 
16 public class TestServerTcp
17 {
Main(string[] args)18 	public static void Main (string[] args)
19 	{
20 		bool isServer;
21 
22 		int port;
23 		string hostname = "127.0.0.1";
24 		//IPAddress ipaddr = IPAddress.Parse ("127.0.0.1");
25 
26 		if (args.Length == 2 && args[0] == "server") {
27 			isServer = true;
28 			port = Int32.Parse (args[1]);
29 		} else if (args.Length == 3 && args[0] == "client") {
30 			isServer = false;
31 			hostname = args[1];
32 			port = Int32.Parse (args[2]);
33 		} else {
34 			Console.Error.WriteLine ("Usage: test-server-tcp [server PORT|client HOSTNAME PORT]");
35 			return;
36 		}
37 
38 		Connection conn;
39 
40 		ObjectPath myOpath = new ObjectPath ("/org/ndesk/test");
41 		string myNameReq = "org.ndesk.test";
42 
43 		if (!isServer) {
44 			SocketTransport transport = new SocketTransport ();
45 			transport.Open (hostname, port);
46 			conn = new Connection (transport);
47 
48 			DemoObject demo = conn.GetObject<DemoObject> (myNameReq, myOpath);
49 			demo.GiveNoReply ();
50 			//float ret = demo.Hello ("hi from test client", 21);
51 			float ret = 200;
52 			while (ret > 5) {
53 				ret = demo.Hello ("hi from test client", (int)ret);
54 				Console.WriteLine ("Returned float: " + ret);
55 				System.Threading.Thread.Sleep (1000);
56 			}
57 		} else {
58 			TcpListener server = new TcpListener (IPAddress.Any, port);
59 
60 			server.Start ();
61 
62 			while (true) {
63 				Console.WriteLine ("Waiting for client on " + port);
64 				TcpClient client = server.AcceptTcpClient ();
65 				Console.WriteLine ("Client accepted");
66 
67 				//TODO: use the right abstraction here, probably using the Server class
68 				SocketTransport transport = new SocketTransport ();
69 				transport.Stream = client.GetStream ();
70 				conn = new Connection (transport);
71 
72 				//conn.SocketHandle = (long)clientSocket.Handle;
73 
74 				//ConnectionHandler.Handle (conn);
75 
76 				//in reality a thread per connection is of course too expensive
77 				ConnectionHandler hnd = new ConnectionHandler (conn);
78 				new Thread (new ThreadStart (hnd.Handle)).Start ();
79 
80 				Console.WriteLine ();
81 			}
82 		}
83 	}
84 }
85 
86 public class ConnectionHandler
87 {
88 	protected Connection conn;
89 
ConnectionHandler(Connection conn)90 	public ConnectionHandler (Connection conn)
91 	{
92 		this.conn = conn;
93 	}
94 
Handle()95 	public void Handle ()
96 	{
97 		ConnectionHandler.Handle (conn);
98 	}
99 
Handle(Connection conn)100 	public static void Handle (Connection conn)
101 	{
102 		string myNameReq = "org.ndesk.test";
103 		ObjectPath myOpath = new ObjectPath ("/org/ndesk/test");
104 
105 		DemoObject demo = new DemoObject ();
106 		conn.Register (myNameReq, myOpath, demo);
107 
108 		//TODO: handle lost connections etc. properly instead of stupido try/catch
109 		try {
110 		while (true)
111 			conn.Iterate ();
112 		} catch (Exception e) {
113 			//Console.Error.WriteLine (e);
114 		}
115 
116 		conn.Unregister (myNameReq, myOpath);
117 	}
118 }
119 
120 [Interface ("org.ndesk.test")]
121 public class DemoObject : MarshalByRefObject
122 {
Hello(string arg0, int arg1)123 	public float Hello (string arg0, int arg1)
124 	{
125 		Console.WriteLine ("Got a Hello(" + arg0 + ", " + arg1 +")");
126 
127 		return (float)arg1/2;
128 	}
129 
GiveNoReply()130 	public void GiveNoReply ()
131 	{
132 		Console.WriteLine ("Asked to give no reply");
133 	}
134 }
135