# Copyright 1999-2009 University of Chicago
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
# http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Object definition for processing Java WS Core (version 3) usage packets.
"""

import struct
from globus.usage.javawscorev1packet import JavaWSCoreV1Packet

class JavaWSCoreV3Packet(JavaWSCoreV1Packet):
    """
    Java WS Core Packet (Version 3) packets are generated by GT 4.2.x
    This packet adds JVM information, uptime, version inforatmion, and thread
    use statistics to the Version 2 packet, and drops the optional_int.
    """

    __START_EVENT = 1
    __STOP_EVENT = 2
    __UPDATE_EVENT = 3
    __JVM_INFO_LEN = 64

    def __init__(self, address, packet):
        JavaWSCoreV1Packet.__init__(self, address, packet)
        [
            self.version_major,
            self.version_minor,
            self.version_micro,
            self.uptime,
            self.port_number,
            self.thread_pool_size,
            self.thread_count,
            self.threads_high_watermark,
            self.threads_max,
            self.service_requests_count
        ] = self.unpack("hhhiihhhhi")

        if self.event_type == JavaWSCoreV3Packet.__START_EVENT:
            self.jvm_info = self.unpack_string(self.__JVM_INFO_LEN)
        else:
            self.jvm_info = None

        [service_len] = self.unpack("h")
        self.services = self.unpack_string(service_len)

    insert_statement = '''
            INSERT INTO java_ws_core_packets(
                component_code,
                version_code,
                send_time,
                ip_address,
                container_id,
                container_type,
                event_type,
                service_list,
                optional_val,
                version_major,
                version_minor,
                version_micro,
                port_number,
                thread_pool_size,
                thread_count,
                max_threads,
                threads_high_water_mark,
                service_request_count,
                jvm_info)
            VALUES (%s, %s, %s, %s, %s, %s, %s, %s, 
                    %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)'''

    def values(self, dbclass):
        """
        Return a values tuple which matches the parameters in the
        class's insert_statement.

        Arguments:
        self -- A JavaWSCoreV3Packet object

        Returns:
        Tuple containing
            (component_code, version_code, send_time, ip_address, container_id,
             container_type, event_type, service_list, optional_val,
             version_major, version_minor, version_micro, port_number,
             thread_pool_size, thread_count, max_threads,
             threads_high_water_mark, service_request_count, jvm_info)

        """
        return (
            self.component_code,
            self.packet_version,
            dbclass.Timestamp(*self.send_time),
            self.ip_address,
            self.container_id,
            self.container_type,
            self.event_type,
            self.services,
            self.uptime,
            self.version_major,
            self.version_minor,
            self.version_micro,
            self.port_number,
            self.thread_pool_size,
            self.thread_count,
            self.threads_max,
            self.threads_high_watermark,
            self.service_requests_count,
            self.jvm_info)
