Esempio n. 1
0
    private void update()
    {
        Dictionary <string, DRSocket> nodeDict = nodeManager.NodeDict;

        Debug.Log("here: " + nodeDict.Count);
        // deal with attention request
        foreach (KeyValuePair <string, DRSocket> nodePair in nodeDict)
        {
            DRSocket node = nodePair.Value;
            if (node.isConnected() && node.IsAttentionRequired)
            {
                // if node is publisher
                if (node.IsPublisher)
                {
                    node.TopicPort = ++port;
                    node.sendMyInfo();
                }
                // if node is subscriber, find publisher and notice the publisher ip & port.
                else
                {
                    string targetName = node.TargetNodeName;
                    Debug.Log("++++++" + targetName);
                    Debug.Log("++++++" + nodeDict.ContainsKey(targetName));
                    if (nodeDict.ContainsKey(targetName))
                    {
                        Debug.Log("++++++" + nodeDict[targetName].isConnected());
                    }
                    if (nodeDict.ContainsKey(targetName) && nodeDict[targetName].isConnected())
                    {
                        DRSocket temp = nodeDict[targetName];
                        node.TargetPort = temp.TopicPort;
                        node.TargetIP   = temp.MyIp;
                        Debug.Log(temp.TopicPort);
                        var targetNode = nodeDict[targetName];
                        node.sendMyInfo();
                        Debug.Log(node.ClientName + " is requesting " + node.TargetNodeName + " : " + node.TargetIP + " : " + node.TargetPort);
                    }
                    else
                    {
                        node.TargetIP   = "0.0.0.0";
                        node.TargetPort = -1;
                        node.sendMyInfo();
                    }
                }
                node.IsAttentionRequired = false;
            }
        }
    }
Esempio n. 2
0
    public byte[] update()
    {
        if (client.IsAttentionRequired) // when server response msg NEWLY arrived
        {
            if (sub == null)
            {
                if (client.TargetPort < 1)
                {
                    Debug.Log("no publisher found");
                    client.sendMyInfo();
                    return(null);
                }
                sub = new DRSocket(client.ClientName);
                Debug.Log("connectig to : " + client.TargetPort);
                sub.setMyInfo(false, client.MyIp, client.TargetNodeName);
                sub.connectToServer(client.TargetIP, client.TargetPort);
                sub.sendMyInfo();
                client.IsAttentionRequired = false;
            }

            if (sub.isConnected())
            {
                okToSubscribe = true;
            }
            return(null);
        }
        else if (okToSubscribe) // server reponse msg is already porcessed and connected to publisher
        {
            Debug.Log("here 1");
            if (sub.IsNewlyReceived)
            {
                Debug.Log("here 2");
                sub.IsNewlyReceived = false;
                byte[] bMsg = sub.getRecentData();
                return(bMsg);
            }
            else
            {
                return(null);
            }
        }
        else // when server response msg not arrived
        {
            if (sub == null)
            {
                return(null);
            }
            if (!sub.isConnected())
            {
                if (!sub.isConnected())
                {
                    okToSubscribe = false;
                }
                client.TargetPort          = -1;
                client.IsAttentionRequired = true;
                sub = null;
            }
            return(null);
        }
    }
Esempio n. 3
0
 private void connMethod()
 {
     while (!isConnected)
     {
         client.sendMyInfo();
         isConnected = client.IsSocketReady;
         Debug.Log("is DRMonitor Running?");
         knockServerCounter++;
         if (knockServerCounter > 10)
         {
             client.connectToServer(serverIP, serverPort);
             knockServerCounter = 0;
         }
         Thread.Sleep((int)1000.0 / 30);
     }
 }
Esempio n. 4
0
 private void constructorHelper(string name, string serverIP, int serverPort)
 {
     isConnected        = false;
     okToPublish        = false;
     updateRate         = 0;
     client             = new DRSocket(name);
     client.IsPublisher = true;
     client.connectToServer(serverIP, serverPort);
     isConnected = client.IsSocketReady;
     client.sendMyInfo();
     while (!client.IsAttentionRequired)
     {
         if (serverCheckCount > 60 * 5)
         {
             Debug.Log("is DRMonitor there?");
             throw new Exception();
         }
         Debug.Log(client.IsAttentionRequired);
         serverCheckCount++;
         Thread.Sleep(20);
     }
     okToPublish = true;
     Debug.Log("port to pub: " + client.TopicPort);
     openTopicRegistrationDesk();
 }
Esempio n. 5
0
    public DRSubscriber(string name, string serverIP, int serverPort, string targetPubName)
    {
        this.serverIP   = serverIP;
        this.serverPort = serverPort;

        okToSubscribe = false;
        isConnected   = false;
        client        = new DRSocket(name);
        client.connectToServer(serverIP, serverPort);
        isConnected = client.IsSocketReady;
        client.setMyInfo(false, "127.0.0.1", targetPubName);
        client.sendMyInfo();
        Thread connThread = new Thread(new ThreadStart(connMethod));

        connThread.Start();
    }
Esempio n. 6
0
 private void connMethod()
 {
     while (!client.IsAttentionRequired)
     {
         client.sendMyInfo();
         Debug.Log("is DRMonitor there?");
         isConnected = client.IsSocketReady;
         Thread.Sleep(500);
         knockServerCounter++;
         if (knockServerCounter > 10)
         {
             client.connectToServer(serverIP, serverPort);
             knockServerCounter = 0;
         }
     }
     okToPublish = true;
     Debug.Log("port to pub: " + client.TopicPort);
     openTopicRegistrationDesk();
     Debug.Log("after regi desk");
 }
Esempio n. 7
0
 public DRSubscriber(string name, string serverIP, int serverPort, string targetPubName)
 {
     okToSubscribe = false;
     isConnected   = false;
     client        = new DRSocket(name);
     client.connectToServer(serverIP, serverPort);
     isConnected = client.IsSocketReady;
     client.setMyInfo(false, "127.0.0.1", targetPubName);
     client.sendMyInfo();
     while (!client.IsAttentionRequired)
     {
         if (serverCheckCount > 60 * 5)
         {
             Debug.Log("is DRMonitor there?");
             QuitGame();
         }
         Debug.Log(client.IsAttentionRequired);
         serverCheckCount++;
         Thread.Sleep(100);
     }
 }
Esempio n. 8
0
    public byte[] update()
    {
        if (client.IsAttentionRequired)
        {
            if (sub == null)
            {
                if (client.TargetPort == -1)
                {
                    Debug.Log("no publisher found");
                    QuitGame();
                    return(null);
                }
                sub = new DRSocket(client.ClientName);
                Debug.Log("connectig to : " + client.TargetPort);
                sub.setMyInfo(false, client.MyIp, client.TargetNodeName);
                sub.connectToServer(client.MyIp, client.TargetPort);
                sub.sendMyInfo();
            }
            client.IsAttentionRequired = false;
            if (sub.isConnected())
            {
                okToSubscribe = true;
            }
        }

        if (!okToSubscribe)
        {
            return(null);
        }
        if (sub.IsNewlyReceived && okToSubscribe)
        {
            sub.IsNewlyReceived = false;
            byte[] bMsg = sub.getRecentData();
            return(bMsg);
        }
        else
        {
            return(null);
        }
    }