Exemplo n.º 1
0
	//TODO: complete this test daemon/server example, and a client
	//TODO: maybe generalise it and integrate it into the core
	public static void Main (string[] args)
	{
		bool isServer;

		if (args.Length == 1 && args[0] == "server")
			isServer = true;
		else if (args.Length == 1 && args[0] == "client")
			isServer = false;
		else {
			Console.Error.WriteLine ("Usage: test-server [server|client]");
			return;
		}

		//string addr = "unix:abstract=/tmp/dbus-ABCDEFGHIJ";
		string addr = "unix:path=/tmp/dbus-ABCDEFGHIJ";

		Connection conn;

		ObjectPath myOpath = new ObjectPath ("/org/ndesk/test");
		string myNameReq = "org.ndesk.test";

		if (!isServer) {
			conn = new Connection (Transport.Create (AddressEntry.Parse (addr)));
			DemoObject demo = conn.GetObject<DemoObject> (myNameReq, myOpath);
			demo.GiveNoReply ();
			//float ret = demo.Hello ("hi from test client", 21);
			float ret = 200;
			while (ret > 5) {
				ret = demo.Hello ("hi from test client", (int)ret);
				Console.WriteLine ("Returned float: " + ret);
				System.Threading.Thread.Sleep (1000);
			}
		} else {
			string path;
			bool abstr;

			AddressEntry entry = AddressEntry.Parse (addr);
			path = entry.Properties["path"];

			UnixSocket server = new UnixSocket ();


			byte[] p = Encoding.Default.GetBytes (path);

			byte[] sa = new byte[2 + p.Length + 1];

			//we use BitConverter to stay endian-safe
			byte[] afData = BitConverter.GetBytes (UnixSocket.AF_UNIX);
			sa[0] = afData[0];
			sa[1] = afData[1];

			for (int i = 0 ; i != p.Length ; i++)
				sa[2 + i] = p[i];
			sa[2 + p.Length] = 0; //null suffix for domain socket addresses, see unix(7)


			server.Bind (sa);
			//server.Listen (1);
			server.Listen (5);

			while (true) {
				Console.WriteLine ("Waiting for client on " + addr);
				UnixSocket client = server.Accept ();
				Console.WriteLine ("Client accepted");
				//client.Blocking = true;

				//PeerCred pc = new PeerCred (client);
				//Console.WriteLine ("PeerCred: pid={0}, uid={1}, gid={2}", pc.ProcessID, pc.UserID, pc.GroupID);

				UnixNativeTransport transport = new UnixNativeTransport ();
				transport.Stream = new UnixStream (client.Handle);
				conn = new Connection (transport);

				//ConnectionHandler.Handle (conn);

				//in reality a thread per connection is of course too expensive
				ConnectionHandler hnd = new ConnectionHandler (conn);
				new Thread (new ThreadStart (hnd.Handle)).Start ();

				Console.WriteLine ();
			}
		}
	}
Exemplo n.º 2
0
	public static void Main (string[] args)
	{
		bool isServer;

		int port;
		string hostname = "127.0.0.1";
		//IPAddress ipaddr = IPAddress.Parse ("127.0.0.1");

		if (args.Length == 2 && args[0] == "server") {
			isServer = true;
			port = Int32.Parse (args[1]);
		} else if (args.Length == 3 && args[0] == "client") {
			isServer = false;
			hostname = args[1];
			port = Int32.Parse (args[2]);
		} else {
			Console.Error.WriteLine ("Usage: test-server-tcp [server PORT|client HOSTNAME PORT]");
			return;
		}

		Connection conn;

		ObjectPath myOpath = new ObjectPath ("/org/ndesk/test");
		string myNameReq = "org.ndesk.test";

		if (!isServer) {
			SocketTransport transport = new SocketTransport ();
			transport.Open (hostname, port);
			conn = new Connection (transport);

			DemoObject demo = conn.GetObject<DemoObject> (myNameReq, myOpath);
			demo.GiveNoReply ();
			//float ret = demo.Hello ("hi from test client", 21);
			float ret = 200;
			while (ret > 5) {
				ret = demo.Hello ("hi from test client", (int)ret);
				Console.WriteLine ("Returned float: " + ret);
				System.Threading.Thread.Sleep (1000);
			}
		} else {
			TcpListener server = new TcpListener (IPAddress.Any, port);

			server.Start ();

			while (true) {
				Console.WriteLine ("Waiting for client on " + port);
				TcpClient client = server.AcceptTcpClient ();
				Console.WriteLine ("Client accepted");

				//TODO: use the right abstraction here, probably using the Server class
				SocketTransport transport = new SocketTransport ();
				transport.Stream = client.GetStream ();
				conn = new Connection (transport);

				//conn.SocketHandle = (long)clientSocket.Handle;

				//ConnectionHandler.Handle (conn);

				//in reality a thread per connection is of course too expensive
				ConnectionHandler hnd = new ConnectionHandler (conn);
				new Thread (new ThreadStart (hnd.Handle)).Start ();

				Console.WriteLine ();
			}
		}
	}
Exemplo n.º 3
0
        /*
        private void OnNewChannel (ObjectPath object_path, string channel_type, HandleType handle_type,
                                   uint handle, bool suppress_handler)
        {
            Console.WriteLine (MSG_PREFIX + "OnNewChannel: op {0}, type {1}, handle {2}",
                               object_path, channel_type, handle);

            if (channel_type.Equals (CHANNEL_TYPE_DBUSTUBE)) {
                itube = bus.GetObject<IDBusTube> (CONNMANAGER_GABBLE_IFACE, object_path);
                //itube.NewTube += OnNewTube;
                itube.TubeChannelStateChanged += OnTubeChannelStateChanged;

                // get tube initiator handle
                // should be = to self_handle on client
                // should be != to self_handle on server
                Properties p = bus.GetObject<Properties> (CONNMANAGER_GABBLE_IFACE, object_path);
                tube_initiator = (uint) p.Get (CHANNEL_IFACE, "InitiatorHandle");

                if (tube_initiator == self_handle) {
                    Console.WriteLine (MSG_PREFIX + "Offering DTube");
                    addr = itube.OfferDBusTube (new Dictionary<string, object>());
                    Console.WriteLine (MSG_PREFIX + "Tube from {0} offered", addr);
                }
                else {
                    addr = itube.AcceptDBusTube ();
                    Console.WriteLine (MSG_PREFIX + "Tube from {0} accepted", addr);
                }

            }
        }
        */
        /*
        private void OnNewTube (uint id, uint initiator, TubeType type, string service,
                                IDictionary<string,object> parameters, TubeState state)
        {
            Console.WriteLine (MSG_PREFIX + "OnNewTube: id {0), initiator {1}, service {2}",
                               id, initiator, service);
            switch (state) {
                case TubeState.LocalPending:
                    if (type == TubeType.DBus && initiator != self_handle && service.Equals (DTUBETEST_IFACE)) {
                        Console.WriteLine (MSG_PREFIX + "Accepting DTube");
                        itube.AcceptDBusTube (id);
                    }
                    break;
            }

        }
        */
        private void OnTubeChannelStateChanged(TubeChannelState state)
        {
            Console.WriteLine (MSG_PREFIX + "OnTubeStateChanged: state {0}",
                               state);

            switch (state) {
                // this state is never reached, so accepting OnNewChannel
                // leaving here just in case, however
                case TubeChannelState.LocalPending:
                    addr = itube.Accept (SocketAccessControl.Localhost);
                    Console.WriteLine (MSG_PREFIX + "Tube from {0} accepted", addr);
                    break;

                // tube ready. set up connection and export/get object
                case TubeChannelState.Open:

                    dbus_conn = Connection.Open (addr);
                    BusG.Init (dbus_conn);

                    if (tube_initiator != self_handle)
                        RegisterDBusObject ();
                    else {
                        obj = dbus_conn.GetObject<IDTubeTest> (DTUBETEST_IFACE, new ObjectPath (DTUBETEST_PATH));
                        obj.TestSignal += OnTestSignal;
                        obj.Hello ();
                        IDictionary<string, object>[] dic = obj.HelloDictionary ();
                        Console.WriteLine (MSG_PREFIX + "Got response on tube. Dictionary array has {0} items.", dic.Length);
                        TestStruct[] ts = obj.HelloStruct ();
                        Console.WriteLine (MSG_PREFIX + "Got response on tube. Struct array has {0} items", ts.Length);
                        Properties p = dbus_conn.GetObject<Properties> (DTUBETEST_IFACE, new ObjectPath (DTUBETEST_PATH));
                        string property = (string) p.Get (DTUBETEST_IFACE, "TestProperty");
                        Console.WriteLine (MSG_PREFIX + "Got response on tube. Property {0}", property);
                        itube.Close ();
                    }
                    break;

                case TubeChannelState.NotOffered:
                    break;

            }
        }
Exemplo n.º 4
0
	//TODO: complete this test daemon/server example, and a client
	//TODO: maybe generalise it and integrate it into the core
	public static void Main (string[] args)
	{
		bool isServer;

		if (args.Length == 1 && args[0] == "server")
			isServer = true;
		else if (args.Length == 1 && args[0] == "client")
			isServer = false;
		else {
			Console.Error.WriteLine ("Usage: test-server [server|client]");
			return;
		}

		string addr = "unix:abstract=/tmp/dbus-ABCDEFGHIJ";

		Connection conn;

		ObjectPath myOpath = new ObjectPath ("/org/ndesk/test");
		string myNameReq = "org.ndesk.test";

		if (!isServer) {
			conn = new Connection (addr);
			DemoObject demo = conn.GetObject<DemoObject> (myNameReq, myOpath);
			demo.GiveNoReply ();
			//float ret = demo.Hello ("hi from test client", 21);
			float ret = 200;
			while (ret > 5) {
				ret = demo.Hello ("hi from test client", (int)ret);
				Console.WriteLine ("Returned float: " + ret);
				System.Threading.Thread.Sleep (1000);
			}
		} else {
			string path;
			bool abstr;

			Address.Parse (addr, out path, out abstr);

			AbstractUnixEndPoint ep = new AbstractUnixEndPoint (path);
			Socket server = new Socket (AddressFamily.Unix, SocketType.Stream, 0);

			server.Bind (ep);
			//server.Listen (1);
			server.Listen (5);

			while (true) {
				Console.WriteLine ("Waiting for client on " + addr);
				Socket client = server.Accept ();
				Console.WriteLine ("Client accepted");
				client.Blocking = true;

				PeerCred pc = new PeerCred (client);
				Console.WriteLine ("PeerCred: pid={0}, uid={1}, gid={2}", pc.ProcessID, pc.UserID, pc.GroupID);

				conn = new Connection (null);
				conn.ns = new NetworkStream (client);
				conn.SocketHandle = (long)client.Handle;

				//ConnectionHandler.Handle (conn);

				//in reality a thread per connection is of course too expensive
				ConnectionHandler hnd = new ConnectionHandler (conn);
				new Thread (new ThreadStart (hnd.Handle)).Start ();

				Console.WriteLine ();
			}
		}
	}