Exemplo n.º 1
0
        /// <summary>
        /// Publishes a single Nodelookup
        /// </summary>
        private ServiceResult DoPublish(NodeLookup lookup)
        {
            try
            {
                // find the right session using our lookup
                Session matchingSession = null;
                foreach (Session session in Program.m_sessions)
                {
                    char[] trimChars = { '/', ' ' };
                    if (session.Endpoint.EndpointUrl.TrimEnd(trimChars).StartsWith(lookup.EndPointURL.ToString().TrimEnd(trimChars), StringComparison.OrdinalIgnoreCase))
                    {
                        lookup.EndPointURL = new Uri(session.Endpoint.EndpointUrl);
                        matchingSession    = session;
                        break;
                    }
                }

                if (matchingSession == null)
                {
                    Program.Trace("PublishNodeMethod: No matching session found for " + lookup.EndPointURL.ToString());
                    return(ServiceResult.Create(StatusCodes.BadSessionIdInvalid, "Session for published node not found!"));
                }
                Program.Trace("PublishNodeMethod: Session found.");


                // check if the node has already been published
                foreach (MonitoredItem item in matchingSession.DefaultSubscription.MonitoredItems)
                {
                    if (item.StartNodeId == lookup.NodeID)
                    {
                        Program.Trace("PublishNodeMethod: Node ID has already been published " + lookup.NodeID.ToString());
                        return(ServiceResult.Create(StatusCodes.BadNodeIdExists, "Node has already been published!"));
                    }
                }

                // subscribe to the node
                Program.CreateMonitoredItem(lookup);
                Program.Trace("PublishNodeMethod: Monitored item created.");

                // update our data
                Program.m_nodesLookups.Add(lookup);
                if (!Program.m_endpointUrls.Contains(lookup.EndPointURL))
                {
                    Program.m_endpointUrls.Add(lookup.EndPointURL);
                }

                //serialize Program.m_nodesLookups to disk
                string publishedNodesFilePath = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "publishednodes.json";
                if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("_GW_PNFP")))
                {
                    publishedNodesFilePath = Environment.GetEnvironmentVariable("_GW_PNFP");
                }
                File.WriteAllText(publishedNodesFilePath, JsonConvert.SerializeObject(Program.m_nodesLookups));

                Program.Trace("PublishNodeMethod: Successful publish: " + lookup.ToString());
                return(ServiceResult.Good);
            }
            catch (Exception ex)
            {
                Program.Trace("PublishNodeMethod: Exception: " + ex.ToString());
                return(ServiceResult.Create(ex, StatusCodes.BadUnexpectedError, "Unexpected error publishing node: " + ex.Message));
            }
        }