public NewPayLoad ExecuteActionOnRemoteGridPlugin(NewPayLoad payload)
        {
            bool closeConn = false;

            if (hubClient == null)
            {
                hubClient = new GingerSocketClient2();
                hubClient.Connect(RemoteGridIP, RemoteGridPort);
                // For action without session
                NewPayLoad fpl = new NewPayLoad(SocketMessages.FindNode, "MathService", "ccc");    // !!!!!!!!!!!!!!!!
                                                                                                   // NewPayLoad fpl = new NewPayLoad(SocketMessages.FindNode, "SeleniumChromeService", "ccc");    // !!!!!!!!!!!!!!!!         DUP REMOVE !!!!
                NewPayLoad rc = hubClient.SendRequestPayLoad(fpl);

                sessionID = rc.GetGuid();
                closeConn = true;
            }
            else
            {
            }


            //  TODO: reserve if session


            NewPayLoad fpl3 = new NewPayLoad(SocketMessages.SendToNode, sessionID, payload);
            // fpl3.ClosePackage();
            NewPayLoad rc4 = hubClient.SendRequestPayLoad(fpl3); // Send to Ginger Grid which will send to Ginger Node to run the action

            if (closeConn)
            {
                hubClient.CloseConnection();   // Not for session
            }

            return(rc4);
        }
Beispiel #2
0
        public NewPayLoad ExecuteActionOnRemoteGridPlugin(NewPayLoad payload)
        {
            // Improve for speed keep connection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! find correct service/node, session !!!!!!!!!!!!!!
            if (mHubClient == null)
            {
                mHubClient = new GingerSocketClient2();
                mHubClient.Connect(RemoteGridIP, RemoteGridPort);
            }


            NewPayLoad fpl = new NewPayLoad(SocketMessages.FindNode, "MathService", "ccc");
            NewPayLoad rc  = mHubClient.SendRequestPayLoad(fpl);

            Guid sessionID = rc.GetGuid();

            //  TODO: reserve if session

            // NewPayLoad actionPayload = CreateActionPayload(actPlugin);

            NewPayLoad fpl3 = new NewPayLoad(SocketMessages.SendToNode, sessionID, payload);
            // fpl3.ClosePackage();
            NewPayLoad rc4 = mHubClient.SendRequestPayLoad(fpl3); // Send to Ginger Grid which will send to Ginger Node to run the action

            mHubClient.CloseConnection();

            return(rc4);
            // rc4.DumpToConsole();
        }
Beispiel #3
0
        private void GingerSocketServerMessageHandler(GingerSocketInfo gingerSocketInfo)
        {
            NewPayLoad p = gingerSocketInfo.DataAsPayload;

            switch (p.Name)
            {
            case "List":      //TODO: use const
            {
                NewPayLoad RC = new NewPayLoad("RC");
                RC.AddValue("OK");

                //TODO: return the list of GingerNodeInfo

                RC.ClosePackage();
                gingerSocketInfo.Response = RC;
                break;
            }

            case SocketMessages.Register:
            {
                string NodeName      = p.GetValueString();
                string NodeServiceID = p.GetValueString();
                string NodeOS        = p.GetValueString();
                string NodeHost      = p.GetValueString();
                string NodeIP        = p.GetValueString();

                NewPayLoad RC = new NewPayLoad("SessionID", gingerSocketInfo.SessionID);
                gingerSocketInfo.Response = RC;

                // add the info of the new node to the grid list
                mGingerNodeInfo.Add(new GingerNodeInfo()
                    {
                        Name = NodeName, ServiceId = NodeServiceID, OS = NodeOS, Host = NodeHost, IP = NodeIP, SessionID = gingerSocketInfo.SessionID, Status = GingerNodeInfo.eStatus.Ready
                    });
                break;
            }

            case SocketMessages.Unregister:
            {
                Guid           SessionID = p.GetGuid();
                GingerNodeInfo GNI       = (from x in mGingerNodeInfo where x.SessionID == SessionID select x).FirstOrDefault();
                if (GNI == null)
                {
                    gingerSocketInfo.Response = new NewPayLoad("Error", "Ginger node info not found for session id " + SessionID.ToString());
                }

                mGingerNodeInfo.Remove(GNI);

                NewPayLoad RC = new NewPayLoad("OK");
                RC.ClosePackage();
                gingerSocketInfo.Response = RC;
                break;
            }

            default:
                throw new Exception("GingerSocketServerMessageHandler: Unknown Message type: " + p.Name);
            }
        }
Beispiel #4
0
        private NewPayLoad Reserve(NewPayLoad pl)
        {
            Guid sessionID = pl.GetGuid();

            if (sessionID.Equals(mSessionID))
            {
                return(new NewPayLoad("Connected", "OK"));
            }
            return(NewPayLoad.Error("Bad Session ID: " + sessionID));
        }
Beispiel #5
0
        public void StartGingerNode(string Name, string HubIP, int HubPort)
        {
            Console.WriteLine("Starting Ginger Node");
            Console.WriteLine("ServiceID: " + mServiceID);

            string Domain      = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
            string IP          = SocketHelper.GetLocalHostIP();
            string MachineName = System.Environment.MachineName;
            string OSVersion   = System.Environment.OSVersion.ToString();

            //TODO: first register at the hub
            mHubClient = new GingerSocketClient2();
            mHubClient.MessageHandler = HubClientMessageHandler;

            Console.WriteLine("Connecting to Ginger Grid Hub: " + HubIP + ":" + HubPort);
            mHubClient.Connect(HubIP, HubPort);
            if (!mHubClient.IsConnected)
            {
                return;
            }
            Console.WriteLine("Connected!");
            Console.WriteLine("Registering Ginger node");
            //Register the service in GG
            NewPayLoad PLRegister = new NewPayLoad(SocketMessages.Register);

            PLRegister.AddValue(Name);
            PLRegister.AddValue(mServiceID);
            PLRegister.AddValue(OSVersion);
            PLRegister.AddValue(MachineName);
            PLRegister.AddValue(IP);
            PLRegister.ClosePackage();
            NewPayLoad RC = mHubClient.SendRequestPayLoad(PLRegister);

            if (RC.Name == "SessionID")
            {
                mSessionID = RC.GetGuid();
                Console.WriteLine("Ginger Node started SessionID - " + mSessionID);
                if (GingerNodeMessage != null)
                {
                    GingerNodeMessage.Invoke(this, eGingerNodeEventType.Started);
                }
            }
            else
            {
                throw new Exception("Unable to find Ginger Grid at: " + HubIP + ":" + HubPort);
            }
        }
Beispiel #6
0
        public void Connect(string IP, int port)
        {
            try
            {
                //Local host
                IPAddress  ipAddress = IPAddress.Parse(IP);
                IPEndPoint remoteEP  = new IPEndPoint(ipAddress, port);
                Socket     socket    = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                // Connect to Ginger Server async
                socket.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), socket);
                mConnectDone.WaitOne();

                //start waiting for incoming data async - non blocking

                // Create the state object.
                mGingerSocketInfo                = new GingerSocketInfo();
                mGingerSocketInfo.Socket         = socket;
                mGingerSocketInfo.MessageHandler = MessageHandler;
                mGingerSocketInfo.Receive();

                // Now Ginger client is ready for incoming data

                mConnected = true;
                // if there is code here it will run - no wait

                //TODO: handshake: version, security, encryption
                NewPayLoad HandShake1 = new NewPayLoad("GetSession", "v1");
                HandShake1.PaylodType = NewPayLoad.ePaylodType.SocketRequest;
                NewPayLoad RC = SendSocketPayLoad(HandShake1);
                if (RC.Name == "SessionID")
                {
                    mGingerSocketInfo.SessionID = RC.GetGuid();
                }
                else
                {
                    throw new Exception("Error in connecting to GingerSocketServer invalid Session");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.Message);
                throw e;
            }
        }
        /// <summary>
        /// Find Node in Remote Grid and return GingerNodeProxy
        /// </summary>
        /// <param name="ServiceID"></param>
        /// <param name="remoteServiceGrids"></param>
        /// <returns></returns>
        public static GingerNodeProxy FindRemoteNode(string ServiceID, ObservableList <RemoteServiceGrid> remoteServiceGrids)
        {
            foreach (RemoteServiceGrid remoteServiceGrid in remoteServiceGrids)
            {
                GingerSocketClient2 hubClient = new GingerSocketClient2();
                hubClient.Connect(remoteServiceGrid.Host, remoteServiceGrid.HostPort);

                NewPayLoad findNodePayload = new NewPayLoad(SocketMessages.FindNode, ServiceID, "ccc");    // !!!!!!!!!!!!!!!!   ccc
                NewPayLoad RC = hubClient.SendRequestPayLoad(findNodePayload);
                if (RC.Name == "NodeInfo")
                {
                    Guid            sessionID       = RC.GetGuid();
                    GingerNodeProxy gingerNodeProxy = new GingerNodeProxy(hubClient, sessionID);
                    return(gingerNodeProxy);
                }
            }
            return(null);
        }
        public void GuidTest()
        {
            //Arrange
            Guid guid = System.Guid.NewGuid();


            NewPayLoad pl = new NewPayLoad("SimpleGuid");

            pl.AddValue(guid);
            pl.ClosePackage();

            // Act
            byte[] b = pl.GetPackage();

            NewPayLoad pl2 = new NewPayLoad(b);
            Guid       g1  = pl2.GetGuid();

            //Assert
            Assert.AreEqual(guid.ToString(), g1.ToString());
            Assert.AreEqual(pl.Name, pl2.Name);
        }
Beispiel #9
0
        /// <summary>
        /// Connect to Services Grid
        /// Retry mechanism will retry every 5 seconds up to total 30 seconds
        /// </summary>
        /// <param name="IP"></param>
        /// <param name="port"></param>
        public void Connect(string IP, int port)
        {
            try
            {
                //Local host
                IPAddress  ipAddress = IPAddress.Parse(IP);
                IPEndPoint remoteIP  = new IPEndPoint(ipAddress, port);
                Socket     socket    = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                Console.WriteLine("Connecting to: " + remoteIP + ":" + port);
                // Connect to Ginger Server async, retry max 30 seconds
                Stopwatch stopwatch  = Stopwatch.StartNew();
                int       retrycount = 0;
                while (!socket.Connected && stopwatch.ElapsedMilliseconds < 30000)
                {
                    socket.BeginConnect(remoteIP, new AsyncCallback(ConnectCallback), socket);
                    mConnectDone.WaitOne();
                    if (!socket.Connected)
                    {
                        retrycount++;
                        Console.WriteLine("Connect retry #" + retrycount);
                        Thread.Sleep(5000);
                    }
                }

                if (socket.Connected)
                {
                    // Now Ginger client is ready for incoming data
                    mConnected = true;
                }
                else
                {
                    Console.WriteLine("Failed to connect, exiting");
                    mConnected = false;
                    return;
                }

                //start waiting for incoming data async - non blocking

                // Create the state object.
                mGingerSocketInfo                = new GingerSocketInfo();
                mGingerSocketInfo.Socket         = socket;
                mGingerSocketInfo.MessageHandler = MessageHandler;
                mGingerSocketInfo.Receive();   // not blocking


                // if there is code here it will run - no wait

                //TODO: handshake: version, security, encryption
                NewPayLoad HandShake1 = new NewPayLoad("GetSession", "v1");
                HandShake1.PaylodType = NewPayLoad.ePaylodType.SocketRequest;
                NewPayLoad RC = SendSocketPayLoad(HandShake1);
                if (RC.Name == "SessionID")
                {
                    mGingerSocketInfo.SessionID = RC.GetGuid();
                }
                else
                {
                    throw new Exception("Error in connecting to GingerSocketServer invalid Session");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.Message);
                throw e;
            }
        }
Beispiel #10
0
        private void GingerSocketServerMessageHandler(GingerSocketInfo gingerSocketInfo)
        {
            NewPayLoad p = gingerSocketInfo.DataAsPayload;

            switch (p.Name)
            {
            case "List":      //TODO: use const
            {
                NewPayLoad RC = new NewPayLoad("RC");
                RC.AddValue("OK");

                //TODO: return the list of GingerNodeInfo

                RC.ClosePackage();
                gingerSocketInfo.Response = RC;
                break;
            }

            case SocketMessages.Register:
            {
                string NodeName      = p.GetValueString();
                string NodeServiceID = p.GetValueString();
                string NodeOS        = p.GetValueString();
                string NodeHost      = p.GetValueString();
                string NodeIP        = p.GetValueString();

                NewPayLoad RC = new NewPayLoad("SessionID", gingerSocketInfo.SessionID);
                gingerSocketInfo.Response = RC;

                // add the info of the new node to the grid list
                mGingerNodeInfo.Add(new GingerNodeInfo()
                    {
                        Name = NodeName, ServiceId = NodeServiceID, OS = NodeOS, Host = NodeHost, IP = NodeIP, SessionID = gingerSocketInfo.SessionID, Status = GingerNodeInfo.eStatus.Ready
                    });
                break;
            }

            case SocketMessages.Unregister:
            {
                Guid           SessionID = p.GetGuid();
                GingerNodeInfo GNI       = (from x in mGingerNodeInfo where x.SessionID == SessionID select x).FirstOrDefault();
                if (GNI == null)
                {
                    gingerSocketInfo.Response = new NewPayLoad("Error", "Ginger node info not found for session id " + SessionID.ToString());
                }

                mGingerNodeInfo.Remove(GNI);

                NewPayLoad RC = new NewPayLoad("OK");
                RC.ClosePackage();
                gingerSocketInfo.Response = RC;
                break;
            }


            // Combine find and send to one - send session id or how to find
            // Change to reserve node
            case SocketMessages.FindNode:      // Find node which match criteria, used for remote grid
                string ServiceID = p.GetValueString();

                // !!! find first or use better algorithm
                GingerNodeInfo gingerNodeInfo1 = (from x in NodeList where x.ServiceId == ServiceID select x).FirstOrDefault();

                // Reserve
                // TODO: lock !!!!!!!!!!!!!!!!!!!!
                gingerNodeInfo1.Status = GingerNodeInfo.eStatus.Reserved;     // TODO: release !!!!!!!!!!!!!!!!
                NewPayLoad RC2 = new NewPayLoad("NodeInfo", gingerNodeInfo1.SessionID);
                gingerSocketInfo.Response = RC2;
                break;

            case SocketMessages.SendToNode:      // Send action to Node, used when Grid is remote
                Guid           SessionID2               = p.GetGuid();
                GingerNodeInfo gingerNodeInfo           = (from x in NodeList where x.SessionID == SessionID2 select x).SingleOrDefault();
                NewPayLoad     actionPayload            = p.ReadPayload();
                NewPayLoad     remoteNodeActionResponce = SendRequestPayLoad(gingerNodeInfo.SessionID, actionPayload);
                remoteNodeActionResponce.Truncate();
                gingerSocketInfo.Response = remoteNodeActionResponce;
                gingerNodeInfo.Status     = GingerNodeInfo.eStatus.Ready;  //TODO: in case of session to do not release
                break;

            default:
                throw new Exception("GingerSocketServerMessageHandler: Unknown Message type: " + p.Name);
            }
        }
Beispiel #11
0
        public void StartGingerNode(string Name, string HubIP, int HubPort)
        {
            Console.WriteLine("Starting Ginger Node");
            Console.WriteLine("ServiceID: " + mServiceID);

            string Domain      = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
            string IP          = SocketHelper.GetLocalHostIP();
            string MachineName = System.Environment.MachineName;
            string OSVersion   = System.Environment.OSVersion.ToString();

            //TODO: first register at the hub
            mHubClient = new GingerSocketClient2();
            mHubClient.MessageHandler = HubClientMessageHandler;

            // We have retry mechanism
            bool IsConnected = false;
            int  count       = 0;

            while (!IsConnected)
            {
                try
                {
                    Console.WriteLine("Connecting to Ginger Grid Hub: " + HubIP + ":" + HubPort);
                    mHubClient.Connect(HubIP, HubPort);
                    IsConnected = true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Connection failed: " + ex.Message);
                    Console.WriteLine("Will retry in 5 seconds");
                    Thread.Sleep(5000);
                    count++;
                    if (count > 50) //TODO: Change it to stop watch wait for 60 seconds, or based on config
                    {
                        Console.WriteLine("All connection attempts failed, exiting");
                        return;
                    }
                }
            }

            //Register the service in GG
            NewPayLoad PLRegister = new NewPayLoad(SocketMessages.Register);

            PLRegister.AddValue(Name);
            PLRegister.AddValue(mServiceID);
            PLRegister.AddValue(OSVersion);   // TODO: translate to normal name?
            PLRegister.AddValue(MachineName); // TODO: if local host write local host
            PLRegister.AddValue(IP);

            PLRegister.ClosePackage();
            NewPayLoad RC = mHubClient.SendRequestPayLoad(PLRegister);

            //TODO: we can disconnect from hub, make Register/Unregister a function

            if (RC.Name == "SessionID")
            {
                mSessionID = RC.GetGuid();
                Console.WriteLine("Ginger Node started SessionID - " + mSessionID);
                if (GingerNodeMessage != null)
                {
                    GingerNodeMessage.Invoke(this, eGingerNodeEventType.Started);
                }
            }
            else
            {
                throw new Exception("Unable to find Ginger Grid at: " + HubIP + ":" + HubPort);
            }
        }