Beispiel #1
0
        private ProtoPadServer(View window, int? overrideListeningPort = null, string overrideBroadcastedAppName = null)
        {
            _window = window;
            _contextActivity = window.Context as Activity;

            _httpServer = new SimpleHttpServer(responseBytes =>
            {
                var response = "{}";
                var remoteCommandDoneEvent = new AutoResetEvent(false);
                _contextActivity.RunOnUiThread(() => Response(responseBytes, remoteCommandDoneEvent, ref response));
                remoteCommandDoneEvent.WaitOne();
                return response;
            });

            IPAddress broadCastAddress;
            using (var wifi = _contextActivity.GetSystemService(Android.Content.Context.WifiService) as WifiManager)
            {
                _mcLock = wifi.CreateMulticastLock("ProtoPadLock");
                _mcLock.Acquire();
                broadCastAddress = GetBroadcastAddress(wifi);
            }

            BroadcastedAppName = overrideBroadcastedAppName ?? String.Format("ProtoPad Service on ANDROID Device {0}", Android.OS.Build.Model);
            ListeningPort = overrideListeningPort ?? 8080;
            LocalIPAddress = Helpers.GetCurrentIPAddress();

            _udpServer = new UdpDiscoveryServer(BroadcastedAppName, String.Format("http://{0}:{1}/", LocalIPAddress, ListeningPort), broadCastAddress);
        }
Beispiel #2
0
        private ProtoPadServer(Activity activity, int? overrideListeningPort = null, string overrideBroadcastedAppName = null)
        {
            _contextActivity = activity;

            BroadcastedAppName = overrideBroadcastedAppName ?? String.Format("ProtoPad Service on ANDROID Device {0}", Android.OS.Build.Model);
            ListeningPort = overrideListeningPort ?? 8080;
            LocalIPAddress = Helpers.GetCurrentIPAddress();

            var mainMonodroidAssembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.GetName().Name.ToLower() == "mono.android");

            var requestHandlers = new Dictionary<string, Func<byte[], string>>
                {
                    {"GetMainXamarinAssembly", data => mainMonodroidAssembly.FullName},
                    {"WhoAreYou", data => "Android"},
                    {"ExecuteAssembly", data =>
                        {
                            var response = "{}";
                            var remoteCommandDoneEvent = new AutoResetEvent(false);
                            _contextActivity.RunOnUiThread(() => Response(data, remoteCommandDoneEvent, ref response));
                            remoteCommandDoneEvent.WaitOne();
                            return response;
                        }
                    }
                };

            _httpServer = new SimpleHttpServer(ListeningPort, requestHandlers);

            IPAddress broadCastAddress;
            using (var wifi = _contextActivity.GetSystemService(Android.Content.Context.WifiService) as WifiManager)
            {
                try
                {
                    _mcLock = wifi.CreateMulticastLock("ProtoPadLock");
                    _mcLock.Acquire();

                }
                catch (Java.Lang.SecurityException e)
                {
                    Debug.WriteLine("Could not optain Multicast lock: {0}. Did you enable CHANGE_WIFI_MULTICAST_STATE permission in your app manifest?", e.Message);
                }

                broadCastAddress = GetBroadcastAddress(wifi);
            }

            var inEmulator = Build.Brand.Equals("generic", StringComparison.InvariantCultureIgnoreCase);
            _udpServer = new UdpDiscoveryServer(BroadcastedAppName, String.Format("http://{0}:{1}/", inEmulator ? "localhost" : LocalIPAddress.ToString(), inEmulator ? "?" : ListeningPort.ToString()), broadCastAddress);
        }
Beispiel #3
0
        private ProtoPadServer(UIWindow window)
        {
            _window = window;

            _httpServer = new SimpleHttpServer(responseBytes =>
            {
                var response = "{}";
                var remoteCommandDoneEvent = new AutoResetEvent(false);
                _window.InvokeOnMainThread(() => Response(responseBytes, remoteCommandDoneEvent, ref response));
                remoteCommandDoneEvent.WaitOne();
                return response;
            });

            _udpServer = new UdpDiscoveryServer(
                String.Format("ProtoPad Service on Device {0}", UIDevice.CurrentDevice.Name),
                String.Format("http://{0}:8080/", Helpers.GetCurrentIPAddress()));
        }
Beispiel #4
0
        private ProtoPadServer(UIApplicationDelegate appDelegate, UIWindow window, int? overrideListeningPort = null, string overrideBroadcastedAppName = null)
        {
            _appDelegate = appDelegate;
            _window = window;

            BroadcastedAppName = overrideBroadcastedAppName ?? String.Format("ProtoPad Service on iOS Device {0}", UIDevice.CurrentDevice.Name);
            ListeningPort = overrideListeningPort ?? 8080;
            LocalIPAddress = Helpers.GetCurrentIPAddress();

            var mainMonotouchAssembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.GetName().Name.ToLower() == "monotouch");

            var requestHandlers = new Dictionary<string, Func<byte[], string>>
                {
                    {"GetMainXamarinAssembly", requestData => mainMonotouchAssembly.FullName},
                    {"WhoAreYou", requestData => "iOS"},
                    {"GetPixateCssFiles", requestData => JsonEncode(_pixateCssPaths.ToArray())},
                    {"ExecuteAssembly", requestData =>
                        {
                            var response = "{}";
                            var remoteCommandDoneEvent = new AutoResetEvent(false);
                            _appDelegate.InvokeOnMainThread(() => ExecuteAssemblyAndCreateResponse(requestData, remoteCommandDoneEvent, ref response));
                            remoteCommandDoneEvent.WaitOne();
                            return response;
                        }
                    },
                    {"UpdatePixateCSS", requestData =>
                        {
                            var response = "{}";
                            var remoteCommandDoneEvent = new AutoResetEvent(false);
                            var filePathDataLength = requestData[1] + (requestData[0] << 8);
                            var filePathData = new byte[filePathDataLength];
                            Array.Copy(requestData, 2, filePathData, 0, filePathDataLength);
                            var filePath = Encoding.UTF8.GetString(filePathData);
                            var cssFileDataLength = requestData.Length - (2 + filePathDataLength);
                            var cssFileData = new byte[cssFileDataLength];
                            Array.Copy(requestData, 2 + filePathDataLength, cssFileData, 0, cssFileDataLength);
                            _appDelegate.InvokeOnMainThread(() => UpdatePixateCssFile(filePath, cssFileData, remoteCommandDoneEvent, ref response));
                            remoteCommandDoneEvent.WaitOne();
                            return response;
                        }
                    },
                    {"GetFileContents", requestData =>
                        {
                            var response = "";
                            var filePath = Encoding.UTF8.GetString(requestData);
                            var remoteCommandDoneEvent = new AutoResetEvent(false);
                            _appDelegate.InvokeOnMainThread(() => GetFileContents(filePath, remoteCommandDoneEvent, ref response));
                            remoteCommandDoneEvent.WaitOne();
                            return response;
                        }
                    }
                };

            _httpServer = new SimpleHttpServer(ListeningPort, requestHandlers);

            _udpServer = new UdpDiscoveryServer(BroadcastedAppName, String.Format("http://{0}:{1}/", LocalIPAddress, ListeningPort));
        }