Skip to content
This repository has been archived by the owner on Mar 15, 2021. It is now read-only.

Commit

Permalink
Merge pull request #200 from pmasrani/revert-changes
Browse files Browse the repository at this point in the history
Revert "Merge pull request #191 from winniex1/get_property"
  • Loading branch information
piyushmasrani authored Dec 19, 2017
2 parents 44b2acc + c55c57f commit 84f546e
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 131 deletions.
1 change: 0 additions & 1 deletion config/liota.conf
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ collect_thread_pool_size = 30
[PKG_CFG]
pkg_path = /usr/lib/liota/packages
pkg_msg_pipe = /usr/lib/liota/packages/liotad/package_messenger.fifo
pkg_rsp_pipe = /usr/lib/liota/packages/liotad/package_response.fifo
pkg_list = /usr/lib/liota/packages/liotad/packages_auto.txt

[DISC_CFG]
Expand Down
4 changes: 2 additions & 2 deletions liota/core/device_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from threading import Thread, Lock
from Queue import Queue

from liota.lib.utilities.utility import LiotaConfigPath, validate_named_pipe
from liota.lib.utilities.utility import LiotaConfigPath, DiscUtilities
from liota.disc_listeners.named_pipe import NamedPipeListener
from liota.disc_listeners.socket_svr import SocketListener
from liota.disc_listeners.mqtt import MqttListener
Expand Down Expand Up @@ -195,7 +195,7 @@ def _get_config_from_file(self):
assert(isinstance(self.cmd_messenger_pipe, basestring))

def _executable_check(self):
if validate_named_pipe(self.cmd_messenger_pipe) == False:
if DiscUtilities().validate_named_pipe(self.cmd_messenger_pipe) == False:
return None
# Will not initialize device discovery if DISCOVERY_LISTENER list is empty
if len(self.endpoint_list) is 0:
Expand Down
6 changes: 4 additions & 2 deletions liota/core/discovery_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
from Queue import Queue
from time import sleep

from liota.lib.utilities.utility import LiotaConfigPath, validate_named_pipe, read_user_config
from liota.lib.utilities.utility import LiotaConfigPath
from liota.lib.utilities.utility import DiscUtilities
from liota.lib.utilities.utility import read_user_config
from liota.dev_sims.named_pipe import NamedPipeSimulator
from liota.dev_sims.socket_clnt import SocketSimulator
from liota.dev_sims.mqtt import MqttSimulator
Expand Down Expand Up @@ -216,7 +218,7 @@ def _get_config_from_file(self):
assert(isinstance(self.cmd_messenger_pipe, basestring))

def _executable_check(self):
if validate_named_pipe(self.cmd_messenger_pipe) == False:
if DiscUtilities().validate_named_pipe(self.cmd_messenger_pipe) == False:
return None
# Will not initialize device simulator if simulator list is empty
if len(self.endpoint_list) is 0:
Expand Down
80 changes: 29 additions & 51 deletions liota/core/package_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
from Queue import Queue
from time import sleep
from abc import ABCMeta, abstractmethod
from liota.lib.utilities.utility import read_liota_config, sha1sum, validate_named_pipe

from liota.lib.utilities.utility import read_liota_config, sha1sum

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -72,7 +73,6 @@
package_lock = None
package_path = None
package_messenger_pipe = None
package_response_pipe = None

# Parse Liota configuration file
package_path = os.path.abspath(
Expand All @@ -81,12 +81,8 @@
package_messenger_pipe = os.path.abspath(
read_liota_config('PKG_CFG', 'pkg_msg_pipe')
)
package_response_pipe = os.path.abspath(
read_liota_config('PKG_CFG', 'pkg_rsp_pipe')
)
assert(isinstance(package_path, basestring))
assert(isinstance(package_messenger_pipe, basestring))
assert(isinstance(package_response_pipe, basestring))

package_startup_list_path = None
package_startup_list = []
Expand Down Expand Up @@ -350,7 +346,6 @@ def __init__(self, name=None):
self._packages_loaded = {} # key: package name, value: PackageRecord obj
self._resource_registry = ResourceRegistry()
self._resource_registry.register("package_conf", package_path)
self._rsp_pipe_file = package_response_pipe
self.flag_alive = True
self.start()

Expand Down Expand Up @@ -482,17 +477,10 @@ def run(self):

# Switch on message content (command), determine what to do
command = msg[0]
if command in ["unload", "delete", "list", "stat", "unload_all",\
"terminate"]:
# currently, write 'Success' to the named pipe once
# successfully receives these commands
self._write_cmd_exec_status('Success\n')

# currently, write 'Success' to the named pipe after verification
# of the checksum for load/reload/update related commands
if command in ["load", "reload", "update"]:
#-----------------------------------------------------------
# Use these commands to handle package management tasks

with package_lock:
offset = 0
autoload_flag = False
Expand Down Expand Up @@ -588,29 +576,9 @@ def run(self):
self.flag_alive = False
break
else:
# currently, write 'Unsupported' to the named pipe once
# successfully receives these commands
self._write_cmd_exec_status('Unsupported\n')
log.warning("Unsupported command is dropped")
log.info("Thread exits: %s" % str(self.name))

def _write_cmd_exec_status(self, msg):
"""
Write command status back to response named pipe.
Currently, for commands of ["unload", "delete", "list", "stat",
"unload_all", "terminate"], 'Success' means that PackageThread
has successfully received these commands;
for commands of ["load", "reload", "update"], 'Success' means
that verification of the package checksum succeeds, while
'Failure' means that verification fails; 'Unsupported' means
that receives some unsupported commands.
"""
try:
with open(self._rsp_pipe_file, "w+") as fp:
fp.write(msg)
except:
log.exception("open file:{0} failed".format(self._rsp_pipe_file))

#-----------------------------------------------------------------------
# This method is called to check if specified package exists

Expand Down Expand Up @@ -760,13 +728,11 @@ def _package_load(self, file_name, checksum=None, autoload_flag=False,
# Check if specified package is already loaded
if file_name in self._packages_loaded:
log.warning("Package already loaded: %s" % file_name)
self._write_cmd_exec_status('Success\n')
return None

path_file_ext, file_ext, checksum_list = self._package_chk_exists(
file_name, ext_forced)
if path_file_ext is None:
self._write_cmd_exec_status('Failure\n')
return None

try:
Expand All @@ -779,16 +745,12 @@ def _package_load(self, file_name, checksum=None, autoload_flag=False,
break
if (verify_flag == False):
log.error("Package %s integrity verification failed" % path_file_ext)
self._write_cmd_exec_status('Failure\n')
return None
else:
sha1 = sha1sum(path_file_ext);
except IOError:
log.error("Could not open file: %s" % path_file_ext)
self._write_cmd_exec_status('Failure\n')
return None

self._write_cmd_exec_status('Success\n')
log.info("Loaded package file: %s (%s)"
% (path_file_ext, sha1.hexdigest()))

Expand Down Expand Up @@ -1282,7 +1244,6 @@ def _package_delete(self, file_name):
def _terminate_all(self):
global package_messenger_thread
global package_messenger_pipe
global package_response_pipe

log.info("Shutting down package messenger...")
if package_messenger_thread.isAlive():
Expand Down Expand Up @@ -1394,15 +1355,32 @@ def initialize():

# Validate package messenger pipe
global package_messenger_pipe
if validate_named_pipe(package_messenger_pipe) == False:
log.error("The validation for package messenger pipe failed")
return

# Validate package response pipe
global package_response_pipe
if validate_named_pipe(package_response_pipe) == False:
log.error("The validation for package response pipe failed")
return
assert(isinstance(package_messenger_pipe, basestring))
if os.path.exists(package_messenger_pipe):
if stat.S_ISFIFO(os.stat(package_messenger_pipe).st_mode):
pass
else:
log.error("Pipe path exists, but it is not a pipe")
package_messenger_pipe = None
return
else:
package_messenger_pipe_dir = os.path.dirname(package_messenger_pipe)
if not os.path.isdir(package_messenger_pipe_dir):
try:
os.makedirs(package_messenger_pipe_dir)
log.info("Created directory: " + package_messenger_pipe_dir)
except OSError:
package_messenger_pipe = None
log.error("Could not create directory for messenger pipe")
return
try:
os.mkfifo(package_messenger_pipe, 0600)
log.info("Created pipe: " + package_messenger_pipe)
except OSError:
package_messenger_pipe = None
log.error("Could not create messenger pipe")
return
assert(stat.S_ISFIFO(os.stat(package_messenger_pipe).st_mode))

# Will not initialize package manager if package path is mis-configured
if package_path is None:
Expand Down
8 changes: 6 additions & 2 deletions liota/dev_sims/named_pipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@
# THE POSSIBILITY OF SUCH DAMAGE. #
# ----------------------------------------------------------------------------#
import os
import sys
import time
import json
import stat
import fcntl
import inspect
import logging
from Queue import Queue

from liota.lib.utilities.utility import validate_named_pipe
from liota.lib.utilities.utility import DiscUtilities
from liota.dev_sims.device_simulator import DeviceSimulator

log = logging.getLogger(__name__)
Expand All @@ -48,7 +52,7 @@ class NamedPipeSimulator(DeviceSimulator):

def __init__(self, pipe_file, name=None, simulator=None):
super(NamedPipeSimulator, self).__init__(name=name)
if validate_named_pipe(pipe_file) == False:
if DiscUtilities().validate_named_pipe(pipe_file) == False:
return None
self._pipe_file = pipe_file
self.simulator = simulator # backpoint to simulator obj
Expand Down
4 changes: 2 additions & 2 deletions liota/disc_listeners/named_pipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from Queue import Queue
from threading import Thread

from liota.lib.utilities.utility import validate_named_pipe
from liota.lib.utilities.utility import DiscUtilities
from liota.disc_listeners.discovery_listener import DiscoveryListener

log = logging.getLogger(__name__)
Expand All @@ -52,7 +52,7 @@ class NamedPipeListener(DiscoveryListener):

def __init__(self, pipe_file, name=None, discovery=None):
super(NamedPipeListener, self).__init__(name=name)
if validate_named_pipe(pipe_file) == False:
if DiscUtilities().validate_named_pipe(pipe_file) == False:
return None
self._pipe_file = pipe_file
self.msg_queue = Queue()
Expand Down
57 changes: 33 additions & 24 deletions liota/lib/utilities/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,33 +314,42 @@ def read_user_config(config_file_path):
return user_config


def validate_named_pipe(pipe_file):
class DiscUtilities:
"""
Check whether a named pipe exists: if not, create it and set permission.
:param pipe_file: path of a named pipe file
:return: True or False (named pipe finally exists or not)
DiscUtilities is a wrapper of utility functions
"""
assert (isinstance(pipe_file, basestring))
if os.path.exists(pipe_file):
if stat.S_ISFIFO(os.stat(pipe_file).st_mode):
pass

def __init__(self):
pass

def validate_named_pipe(self, pipe_file):
"""
Check whether a named pipe exists: if not, create it and set permission.
:param pipe_file: path of a named pipe file
:return: True or False (named pipe finally exists or not)
"""

assert (isinstance(pipe_file, basestring))
if os.path.exists(pipe_file):
if stat.S_ISFIFO(os.stat(pipe_file).st_mode):
pass
else:
log.error("Pipe path exists, but it is not a pipe")
return False
else:
log.error("Pipe path exists, but it is not a pipe")
return False
else:
pipe_dir = os.path.dirname(pipe_file)
if not os.path.isdir(pipe_dir):
pipe_dir = os.path.dirname(pipe_file)
if not os.path.isdir(pipe_dir):
try:
os.makedirs(pipe_dir)
log.info("Created directory: " + pipe_dir)
except OSError:
log.error("Could not create directory for messenger pipe")
return False
try:
os.makedirs(pipe_dir)
log.info("Created directory: " + pipe_dir)
os.mkfifo(pipe_file, 0600)
log.info("Created pipe: " + pipe_file)
except OSError:
log.error("Could not create directory for pipe")
log.error("Could not create messenger pipe")
return False
try:
os.mkfifo(pipe_file, 0600)
log.info("Created pipe: " + pipe_file)
except OSError:
log.error("Could not create pipe")
return False
assert (stat.S_ISFIFO(os.stat(pipe_file).st_mode))
return True
assert (stat.S_ISFIFO(os.stat(pipe_file).st_mode))
return True
10 changes: 0 additions & 10 deletions packages/liotad/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,6 @@ PackageMessengerThread listens on a named pipe whose location is defined in `lio

Different techniques can be supported in PackageMessengerThread in the future.

###Command Status Return

Once liotapkg.sh passes commands to PackageThread through PackageMessengerThread,
it will read from response namedPipe to get command status.
For commands of ["unload", "delete", "list", "stat", "unload_all", "terminate"],
'Success' means that PackageThread has successfully received these commands;
for commands of ["load", "reload", "update"], 'Success' means that verification
of the package checksum succeeds, while 'Failure' means that verification fails;
'Unsupported' means that receives some unsupported commands.

##LiotaPackage

There is a LiotaPackage class defined in `package_manager.py` which looks like
Expand Down
Loading

0 comments on commit 84f546e

Please sign in to comment.