text:
cd ../src/calculate_features/
python metabolic.py

cd ../..

redpajama_set_name: RedPajamaGithub


text:
from redmine.managers import ResourceBadMethodError

from pysflib import sfauth
from pysflib import sfredmine
from sfmigration.common import base
from sfmigration.common import utils

logger = utils.logger

def redmine_valid_api_method(resource):
    def decorator(f):
        def decorated(*args, **kwargs):
            try:
                f(*args, **kwargs)
            except NotImplementedError:
                log_msg = "Importing %s is not supported, skipping." % resource
                logger.warning(log_msg)
            except ResourceBadMethodError:
                log_msg = "Creating %s not supported by redmine API, skipping."
                logger.warning(log_msg % resource)
        return decorated
    return decorator

class SFRedmineMigrator(base.BaseRedmine):
    def __init__(self, username=None, password=None,
                 apikey=None, id=None, url=None, name=None,
                 sf_domain=None, versions_to_skip=None, issues_to_skip=None,
                 mapper=None, use_ssl=True, verify_ssl=False):
        self.sf_domain = sf_domain
        self.use_ssl = use_ssl
        self.verify_ssl = verify_ssl
        if self.use_ssl and str(url).startswith('http://'):
            logger.debug("migration url not using SSL, disabling SSL")
            self.use_ssl = False
            self.verify_ssl = False
        super(SFRedmineMigrator, self).__init__(username, password,
                                                apikey, id, url, name)
        if not self.id:
            projects = self.redmine.project.all()
            logger.debug("Fetched %i projects" % len(projects))
            id_candidates = [p.id for p in projects if p.name == self.name]
            try:
                self.id = id_candidates[0]
                logger.debug("Found project #%i" % self.id)
            except IndexError:
                log_msg = "Project %s not found on %s" % (self.name, self.url)
                log_msg += ", creating it"
                logger.warning(log_msg)
                p_identifier = "-".join(self.name.lower().split(' '))
                project = self.redmine.project.create(name=self.name,
                                                      identifier=p_identifier)
                self.id = project.id
        if not self.name:
            self.name = self.redmine.project.get(self.id)
        self.versions_to_update = {}
        self.versions_to_skip = versions_to_skip
        if not self.versions_to_skip:
            self.versions_to_skip = []
        self.issues_to_skip = issues_to_skip
        if not self.issues_to_skip:
            self.issues_to_skip = []
        if mapper:
            self.mapper = mapper
        else:
            self.mapper = utils.BaseMapper()

    def _create_connector(self):
        c = sfauth.get_cookie(self.sf_domain, self.username, self.password,
                              use_ssl=self.use_ssl, verify=self.verify_ssl)
        self.redmine = sfredmine.SFRedmine(self.url, auth_cookie=c,
                                           requests={'verify': False})

    def migrate(self, importer):
        self.migrate_trackers(importer)
        self.migrate_wiki(importer)
        self.migrate_issue_statuses(importer)
        self.migrate_versions(importer)
        self.migrate_issues(importer)
        self.cleanup(importer)

    @redmine_valid_api_method("trackers")
    def migrate_trackers(self, importer):
        for tracker in importer.fetch_trackers():
            self.redmine.tracker.create(**tracker)

    @redmine_valid_api_method("wiki pages")
    def migrate_wiki(self, importer):
        for article in importer.fetch_wiki():
            self.redmine.wiki_page.create(**article)

    @redmine_valid_api_method("issue statuses")
    def migrate_issue_statuses(self, importer):
        for status in importer.fetch_issue_statuses():
            self.redmine.issue_status.create(**status)

    @redmine_valid_api_method("versions")
    def migrate_versions(self, importer):
        for version in importer.fetch_versions():
            if str(version['source_id']) not in self.versions_to_skip:
                del version['source_id']
                logger.debug("Migrating version '%s' ..." % version['name'])
                version['project_id'] = self.id
                real_version = None
                if version['status'] != 'open':
                    # open the version so we can add issues to it
                    real_version = version['status']
                    version['status'] = 'open'
                new_version = self.redmine.version.create(**version)
                if real_version:
                    self.versions_to_update[new_version.id] = real_version
            else:
                logger.debug("Skipping version #%s" % version['source_id'])

    def migrate_issues(self, importer):
        for issue in importer.fetch_issues():
            if str(issue['source_id']) in self.issues_to_skip:
                logger.debug("Skipping issue #%s" % issue['source_id'])
            else:
                log_msg = "Migrating issue #%s: %s ..." % (issue['source_id'],
                                                           issue['subject'])
                logger.debug(log_msg)
                issue['project_id'] = self.id
                # check tracker
                if 'tracker_name' in issue:
                    mapped_tracker = self.mapper.map(issue['tracker_name'])
                    possible_trackers = [t.id
                                         for t in self.redmine.tracker.all()
                                         if t.name == mapped_tracker]
                    try:
                        issue['tracker_id'] = possible_trackers[0]
                    except IndexError:
                        log_msg = "Tracker %s not found, leaving empty"
                        logger.debug(log_msg % mapped_tracker)
                        del issue['tracker_id']
                # check issue status
                if 'status_name' in issue:
                    mapped_status = self.mapper.map(issue['status_name'])
                    possible_status = [t.id
                                       for t in self.redmine.issue_status.all()
                                       if t.name == mapped_status]
                    try:
                        issue['status_id'] = possible_status[0]
                    except IndexError:
                        log_msg = "Status %s not found, leaving empty"
                        logger.debug(log_msg % mapped_status)
                        if 'status_id' in issue:
                            del issue['status_id']
                # check version
                if 'version_name' in issue:
                    mapped_version = self.mapper.map(issue['version_name'])
                    possible_versions = [t.id for t in
                                         (self.redmine.version
                                          .filter(project_id=self.id))
                                         if t.name == mapped_version]
                    try:
                        issue['fixed_version_id'] = possible_versions[0]
                    except IndexError:
                        log_msg = "Version %s not found, leaving empty"
                        logger.debug(log_msg % mapped_version)
                        if 'fixed_version_id' in issue:
                            del issue['fixed_version_id']
                # check user
                if 'assigned_to_login' in issue:
                    mapped_user = self.mapper.map(issue['assigned_to_login'])
                    possible_users = [t.id for t in
                                      (self.redmine.user
                                       .filter(name=mapped_user))
                                      if t.login == mapped_user]
                    try:
                        log_msg = "User %s mapped to #%s"
                        logger.debug(log_msg % (issue['assigned_to_login'],
                                                possible_users[0]))
                        issue['assigned_to_id'] = possible_users[0]
                    except IndexError:
                        log_msg = "User %s not found, leaving empty"
                        logger.debug(log_msg % mapped_user)
                        if 'assigned_to_id' in issue:
                            del issue['assigned_to_id']
                # check priority
                if 'priority_name' in issue:
                    mapped_priority = self.mapper.map(issue['priority_name'])
                    possible_priority = [t.id for t in
                                         (self.redmine.enumeration
                                          .filter(resource='issue_priorities'))
                                         if t.name == mapped_priority]
                    try:
                        issue['priority_id'] = possible_priority[0]
                    except IndexError:
                        log_msg = ("Priority %s not found, "
                                   "setting default priority 1")
                        logger.debug(log_msg % mapped_version)
                        issue['priority_id'] = 1
                # clean unneeded values at this point
                for unneeded in ('tracker_name', 'status_name',
                                 'fixed_version_name', 'priority_name',
                                 'assigned_to_login', 'source_id'):
                    try:
                        del issue[unneeded]
                    except KeyError:
                        pass
                # create the issue finally!
                self.redmine.issue.create(**issue)

    def cleanup(self, *args, **kwargs):
        for version, status in self.versions_to_update.items():
            logger.debug("Re-setting correct status for version #%i" % version)
            self.redmine.version.update(version, status=status)


text:
using System;

namespace Azure.ResourceManager.DeviceProvisioningServices.Models
{
    /// <summary> The description of an X509 CA Certificate. </summary>
    public partial class DeviceProvisioningServicesCertificateProperties
    {
        /// <summary> Initializes a new instance of DeviceProvisioningServicesCertificateProperties. </summary>
        public DeviceProvisioningServicesCertificateProperties()
        {
        }

        /// <summary> Initializes a new instance of DeviceProvisioningServicesCertificateProperties. </summary>
        /// <param name="subject"> The certificate's subject name. </param>
        /// <param name="expireOn"> The certificate's expiration date and time. </param>
        /// <param name="thumbprint"> The certificate's thumbprint. </param>
        /// <param name="isVerified"> Determines whether certificate has been verified. </param>
        /// <param name="certificate"> base-64 representation of X509 certificate .cer file or just .pem file content. </param>
        /// <param name="createdOn"> The certificate's creation date and time. </param>
        /// <param name="updatedOn"> The certificate's last update date and time. </param>
        internal DeviceProvisioningServicesCertificateProperties(string subject, DateTimeOffset? expireOn, BinaryData thumbprint, bool? isVerified, BinaryData certificate, DateTimeOffset? createdOn, DateTimeOffset? updatedOn)
        {
            Subject = subject;
            ExpireOn = expireOn;
            Thumbprint = thumbprint;
            IsVerified = isVerified;
            Certificate = certificate;
            CreatedOn = createdOn;
            UpdatedOn = updatedOn;
        }

        /// <summary> The certificate's subject name. </summary>
        public string Subject { get; }
        /// <summary> The certificate's expiration date and time. </summary>
        public DateTimeOffset? ExpireOn { get; }
        /// <summary>
        /// The certificate's thumbprint.
        /// <para>
        /// To assign an object to this property use <see cref="BinaryData.FromObjectAsJson{T}(T, System.Text.Json.JsonSerializerOptions?)"/>.
        /// </para>
        /// <para>
        /// To assign an already formated json string to this property use <see cref="BinaryData.FromString(string)"/>.
        /// </para>
        /// <para>
        /// Examples:
        /// <list type="bullet">
        /// <item>
        /// <term>BinaryData.FromObjectAsJson("foo")</term>
        /// <description>Creates a payload of "foo".</description>
        /// </item>
        /// <item>
        /// <term>BinaryData.FromString("\"foo\"")</term>
        /// <description>Creates a payload of "foo".</description>
        /// </item>
        /// <item>
        /// <term>BinaryData.FromObjectAsJson(new { key = "value" })</term>
        /// <description>Creates a payload of { "key": "value" }.</description>
        /// </item>
        /// <item>
        /// <term>BinaryData.FromString("{\"key\": \"value\"}")</term>
        /// <description>Creates a payload of { "key": "value" }.</description>
        /// </item>
        /// </list>
        /// </para>
        /// </summary>
        public BinaryData Thumbprint { get; }
        /// <summary> Determines whether certificate has been verified. </summary>
        public bool? IsVerified { get; set; }
        /// <summary>
        /// base-64 representation of X509 certificate .cer file or just .pem file content.
        /// <para>
        /// To assign an object to this property use <see cref="BinaryData.FromObjectAsJson{T}(T, System.Text.Json.JsonSerializerOptions?)"/>.
        /// </para>
        /// <para>
        /// To assign an already formated json string to this property use <see cref="BinaryData.FromString(string)"/>.
        /// </para>
        /// <para>
        /// Examples:
        /// <list type="bullet">
        /// <item>
        /// <term>BinaryData.FromObjectAsJson("foo")</term>
        /// <description>Creates a payload of "foo".</description>
        /// </item>
        /// <item>
        /// <term>BinaryData.FromString("\"foo\"")</term>
        /// <description>Creates a payload of "foo".</description>
        /// </item>
        /// <item>
        /// <term>BinaryData.FromObjectAsJson(new { key = "value" })</term>
        /// <description>Creates a payload of { "key": "value" }.</description>
        /// </item>
        /// <item>
        /// <term>BinaryData.FromString("{\"key\": \"value\"}")</term>
        /// <description>Creates a payload of { "key": "value" }.</description>
        /// </item>
        /// </list>
        /// </para>
        /// </summary>
        public BinaryData Certificate { get; set; }
        /// <summary> The certificate's creation date and time. </summary>
        public DateTimeOffset? CreatedOn { get; }
        /// <summary> The certificate's last update date and time. </summary>
        public DateTimeOffset? UpdatedOn { get; }
    }
}


text:
Installation (do this first - really)
==
After cloning the SalesforceMobileSDK-iOS project from github, run the install script from the Terminal command line:

`./install.sh`
This pulls submodule dependencies from github, and builds all the library files you will need.  It also installs Xcode project templates in the default Xcode template location.
See the setup.md file for additional instructions. Xcode 4.2 or greater is a prerequisite for building the Salesforce Mobile SDK.  install.sh will check for this, and exit if the installed version of Xcode is incorrect. In addition, the Salesforce Mobile SDK requires iOS 5.0 or greater.  Building from the command line has been tested using ant 1.8.  Older versions may work, but we recommend using the latest version of ant.

**Users of Xcode earlier than 4.5 (iOS 6):** The iOS 6 development environment introduces a new processor architecture (armv7s), while removing support for another (armv6).  This makes libraries built for iOS 6 incompatible with earlier Xcode dev environments, without some further steps on the developer's part.

We have updated our libraries to support iOS 6.  If you do not want to update your Xcode environment to 4.5 yet, you'll need to remove support for armv6 (if it exists) in the SalesforceHybridSDK project (hybrid/SalesforceHybridSDK/SalesforceHybridSDK.xcodeproj), as well as your app's project configuration:

1. In Xcode, click on the Project at the top of the Project Navigator view.
2. Under the Project header, click the project/app name.
3. Select the Build Settings tab.
4. In the Architectures section at the top, if **armv6** is not listed in Valid Architectures, there are no further actions on your part.  Otherwise, double-click the "armv6 armv7" option in Valid Architectures.
5. Click on the armv6 item in the list, then click the '-' button at the bottom of the list.

You should now be compatible with the iOS 6 library builds.  Note that you're not losing anything by removing support for armv6 anyway, as that class of devices was never compatible with iOS 5, our baseline supported iOS SDK in the Salesforce Mobile SDK.

**Xcode 4.3 Users:** If you have not used the `xcode-select` tool to choose your version of Xcode at the command line, you may encounter the following error when running the install script:

```
xcode-select: Error: No Xcode folder is set. Run xcode-select -switch <xcode_folder_path> to set the path to the Xcode folder.
```

If you run `sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer` at the command line, you should be able to run the install script without issues.  This assumes you installed Xcode in the Applications folder.

**Note:** When using the app templates to create your application, **make sure the "Use Automatic Reference Counting checkbox is NOT selected.**

If you have problems building any of the projects, take a look at the online [FAQ](https://github.com/forcedotcom/SalesforceMobileSDK-iOS/wiki/FAQ) for troubleshooting tips.

Introduction
==

__What's New in 1.4__

**Updated iOS SDK to Cordova 2.2**
Make sure to update to the latest cordova.js and associated SDK JS plugin files when you upgrade.  These can either be taken from the repo, or from a newly-generated hybrid template app.

**API Versioning**
Cordova JavaScript libraries are now versioned to benefit hybrid apps that deploy multiple versions at the same time.
See "Versioning and Javascript Library Compatibility" in the "Introduction to Hybrid Development" chapter of the Mobile SDK Developer Guide.

**Reactive Session Management for Hybrid Apps**
Developers get more control over managing web sessions inside the hybrid container. Apps that previously relied on proactive session management will require some modification. Please upgrade with caution. (forcetk.js clients should not be affected.)

**Passcode Reset**
Added option for users to logout from the passcode screen

**Miscellaneous**
* SmartStore for native apps Xcode template
* Mobile Components for Visualforce updated to provide session management

__Version 1.3__

**Cordova Library Updates**
The Salesforce Hybrid SDK has updated its PhoneGap/Cordova support to Cordova 1.8.1.  All plugins and infrastructure have been updated to Cordova patterns.

**Default SmartStore Encryption**
If an app does not implement user passcodes, its SmartStore data will be protected with a default encryption scheme.

**Better Offline Support for Authentication in Native Apps**
Native apps can now function offline if the user has authenticated at some point.  Once the device comes back online, the app will reconnect with the user's existing credentials.  (Hybrid apps already have this capability.)

__Version 1.2__

**Connected Apps Pilot**
* Apps implemented with the Mobile SDK will now respect Connected Apps policies.  Rules defined by administrators for PIN code protection and session timeout intervals will now be enforced by native and hybrid app implementations. (This feature requires the Connected Apps Pilot be turned on.)

**SmartStore Enhancements**
* Upsert records based on external id. SmartStore can now determine record uniqueness and perform the proper updates based on an id defined by the developer. This design is reflects the Salesforce REST API, making it easier to implement data synchronization.
* Mock SmartStore Implementation. Developers can build and test SmartStore apps directly in the desktop browser.
* Option to self-encrypt the SmartStore databases, which can be securely backed-up.

__Version 1.1__

**Secure Offline API**
Store sensitive business data directly on a device with enterprise-class encryption for offline access. The Salesforce SDK provides a robust API for storing, retrieving, and querying  data without internet connectivity.

**Flexible OAuth2 authentication flow**
For hybrid apps, you now have the flexibility to configure whether or not your app needs to authenticate immediately when the app starts, or whether you'd prefer to defer authentication to a more convenient time in your app's lifecycle.

**Blocks APIs for iOS**
For native iOS developers, the REST API libraries have been updated to support a blocks-based callback interface, to make responding to asynchronous REST calls much easier.

__Version 1.0__
This is the first generally available release of Salesforce Mobile SDK for iOS that can be used to develop native and hybrid applications. The public facing APIs have been finalized. Due to the rapid pace of innovation of mobile operating systems, some of the APIs and modules may change in their implementation details, but should not have a direct impact on the application logic. All updates will be clearly communicated in advanced using github.
Check out [Developer Force](http://developer.force.com/mobilesdk) for additional articles and tutorials.

__Native Applications__
The Salesforce Mobile SDK provides the essential libraries for quickly building native mobile apps that interact with the Salesforce cloud platform. The OAuth2 library abstracts away the complexity of securely storing the refresh token or fetching a new session id when it expires. The SDK also provides Objective-C wrappers for the Salesforce REST API, making it easy to retrieve and manipulate data.

__Hybrid Applications__
HTML5 is quickly emerging as a powerful technology for developing cross-platform mobile applications. While developers can create sophisticated apps with HTML5 and JavaScript alone, some vital limitations remain, specifically: session management, access to native device functionality like the camera, calendar and address book. The Salesforce Mobile Container (based on the industry leading PhoneGap implementation) makes it possible to embed HTML5 apps stored on the device or delivered via Visualforce inside a thin native container, producing a hybrid application.

Application Templates
==
The Mobile SDK provides Xcode templates for quickly constructing the foundation of native and hybrid applications with configurable Settings bundles for allowing the user to log-out of the app or switch between production and Sandbox orgs.

__Native App Template__
For native apps that need to accesses the Salesforce REST API, use the native template that includes a default AppDelegate implementation that you can customize to perform any app-specific interaction.

__Hybrid App Template__
To create hybrid apps that use the Salesforce REST API or access Visualforce pages, start with the hybrid app template. By providing a SalesforceOAuthPlugin for the container based on PhoneGap, HTML5 applications can quickly leverage OAuth tokens directly from JavaScript calls.

Using the Native REST SDK (in a _new_ project)
==

Create a new "Native Force.com REST App" project (Command-Shift-N in Xcode). These parameters are required:

1. **Consumer Public Key**: The consumer key from your remote access provider.
1. **OAuth Redirect URL**: The URL used by OAuth to handle the callback.
1. **Company Identifier**: Something like com.mycompany.foo -- should correspond with an App ID you created in your Apple iOS dev center account.

You should then be able to compile and run the sample project. It's a simple project which logs you into
a salesforce instance via oauth, issues a 'select Name from Account' query and displays the result in a UITableView.

Note that the default native app template uses some default test values for the Consumer Public Key and the OAuth Redirect URL.  Before you publish your app to the iTunes App Store, you _MUST_ override these values with values from your own Remote Access object from a Production org.

Using the Native REST SDK (in an _existing_ project)
==

You can also use the SDK in an existing project:

1. Drag the folder `native/dependencies` into your project (check `Create groups for any added folders`)

2. Open the Build Settings tab for the project.

  * Set __Other Linker Flags__ to `-ObjC -all_load`.

3. Open the Build Phases tab for the project main target and link against the following required frameworks:

	1. **CFNetwork.framework**
	1. **CoreData.framework**
	1. **MobileCoreServices.framework**
	1. **SystemConfiguration.framework**
	1. **Security.framework**
	1. **libxml2.dylib**
	1. **libsqlite3.dylib**
	1. **libz.dylib**

4. Import the SalesforceSDK header via ``#import "SFRestAPI.h"``.

5. Build the project to verify that the installation is successful.

6. Refer to the [SFRestAPI documentation](http://forcedotcom.github.com/SalesforceMobileSDK-iOS/Documentation/SalesforceSDK/Classes/SFRestAPI.html) for some sample code to login into a salesforce instance and issue a REST API call.

Using the Hybrid app  SDK (in a _new_ project)
==

Create a new "Hybrid Force.com App" project (Command-Shift-N in Xcode). These parameters are required:

1. **Consumer Public Key**: The consumer key from your remote access provider.
1. **OAuth Redirect URL**: The URL used by OAuth to handle the callback.
1. **Company Identifier**: Something like com.mycompany.foo -- should correspond with an App ID you created in your Apple iOS dev center account.

You should then be able to compile and run the sample project, which is very similar to the ContactExplorer sample app.

Note that the default hybrid app template uses some default test values for the Consumer Public Key and the OAuth Redirect URL.  Before you publish your app to the iTunes App Store, you _MUST_ override these values with values from your own Remote Access object from a Production org.

Working with the hybrid sample apps
==

The sample applications contained under the hybrid/ folder are designed around the [PhoneGap SDK](http://www.phonegap.com/), also known as [callback-ios](https://github.com/callback/callback-ios).

Before you can work with those applications, you will need to ensure that you've updated the submodules for the SalesforceSDK project.  The default install.sh script sets up these submodules for you so that you need not install PhoneGap separately.

You can find more detailed documentation for working with the PhoneGap SDK in the [PhoneGap Getting Started Guide](http://www.phonegap.com/start).

**Note:** The hybrid sample applications are configured to look for the PhoneGap iOS Framework in their dependencies folder. To find out if the PhoneGap framework is properly linked in a sample project, take the following action:

1. Open the project in Xcode.
2. In Project Navigator, expand the dependencies folder.
3. If PhoneGap.framework is listed among the dependencies , your project should be fine, and no further action should be necessary.

If you do not see the PhoneGap framework, or otherwise get compilation errors related to the PhoneGap Framework not being found (e.g. 'Undefined symbols for architecture i386: "\_OBJC\_METACLASS\_$\_PhoneGapDelegate"'), you will need to either re-run the install script or add the PhoneGap Framework to the sample project:

1. Open the Xcode project of the sample application.
2. In the Project Navigator, right-click or control-click the Frameworks folder, and select 'Add files to "_Project Name_..."'.
3. Navigate to the Phonegap.framework folder (the default location is project/dependencies/PhoneGap.framework), and click "Add".

The sample application project should now build and run cleanly.

**Note:** _The Salesforce Mobile SDK requires iOS version 5.0 or later and has eliminated the direct use of SBJson._  If your app relies on SBJson, you might consider using SFJsonUtils instead or using the NSJSONSerialization class provided in iOS 5.0 directly.  (SFJsonUtils simply provides a convenience API for those who are porting SBJson-based code to iOS 5.0+.)

Documentation
==

* [Salesforce Native SDK](http://forcedotcom.github.com/SalesforceMobileSDK-iOS/Documentation/SalesforceSDK/index.html)
* [Salesforce OAuth](http://forcedotcom.github.com/SalesforceMobileSDK-iOS/Documentation/SalesforceOAuth/index.html)
* [Salesforce Hybrid SDK](http://forcedotcom.github.com/SalesforceMobileSDK-iOS/Documentation/HybridContainer/index.html)

Discussion
==

If you would like to make suggestions, have questions, or encounter any issues, we'd love to hear from you. Post any feedback you have to the [Mobile Community Discussion Board](http://boards.developerforce.com/t5/Mobile/bd-p/mobile) on developerforce.com.


text:
package monitor

import (
	"crypto/tls"
	"io"
	"strings"
	"sync"

	"github.com/TheThingsNetwork/go-utils/grpc/restartstream"
	"github.com/TheThingsNetwork/go-utils/grpc/ttnctx"
	"github.com/TheThingsNetwork/go-utils/log"
	"github.com/TheThingsNetwork/ttn/api"
	"github.com/TheThingsNetwork/ttn/api/broker"
	"github.com/TheThingsNetwork/ttn/api/gateway"
	"github.com/TheThingsNetwork/ttn/api/handler"
	"github.com/TheThingsNetwork/ttn/api/networkserver"
	"github.com/TheThingsNetwork/ttn/api/pool"
	"github.com/TheThingsNetwork/ttn/api/router"
	"github.com/TheThingsNetwork/ttn/utils"
	"github.com/golang/protobuf/ptypes/empty"
	"golang.org/x/net/context"
	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
)

// GenericStream is used for sending anything to the monitor.
// Depending on the context, this can be
// - *router.UplinkMessage
// - *router.DownlinkMessage
// - *gateway.Status
// - *broker.DeduplicatedUplinkMessage
// - *broker.DownlinkMessage
type GenericStream interface {
	Send(interface{})
	Close()
}

// ClientConfig for monitor Client
type ClientConfig struct {
	BackgroundContext context.Context
	BufferSize        int
}

// DefaultClientConfig for monitor Client
var DefaultClientConfig = ClientConfig{
	BackgroundContext: context.Background(),
	BufferSize:        10,
}

// TLSConfig to use
var TLSConfig *tls.Config

// NewClient creates a new Client with the given configuration
func NewClient(config ClientConfig) *Client {
	ctx, cancel := context.WithCancel(config.BackgroundContext)

	return &Client{
		log:    log.Get(),
		ctx:    ctx,
		cancel: cancel,

		config: config,
	}
}

// Client for monitor
type Client struct {
	log    log.Interface
	ctx    context.Context
	cancel context.CancelFunc

	config      ClientConfig
	serverConns []*serverConn
}

// AddServer adds a new monitor server. Supplying DialOptions overrides the default dial options.
// If the default DialOptions are used, TLS will be used to connect to monitors with a "-tls" suffix in their name.
// This function should not be called after streams have been started
func (c *Client) AddServer(name, address string) {
	log := c.log.WithFields(log.Fields{"Monitor": name, "Address": address})
	log.Info("Adding Monitor server")

	s := &serverConn{
		ctx:   log,
		name:  name,
		ready: make(chan struct{}),
	}
	c.serverConns = append(c.serverConns, s)

	go func() {
		var err error
		if strings.HasSuffix(name, "-tls") {
			s.conn, err = pool.Global.DialSecure(address, nil)
		} else {
			s.conn, err = pool.Global.DialInsecure(address)
		}
		if err != nil {
			log.WithError(err).Error("Could not connect to Monitor server")
		}
		close(s.ready)
	}()
}

// AddConn adds a new monitor server on an existing connection
// This function should not be called after streams have been started
func (c *Client) AddConn(name string, conn *grpc.ClientConn) {
	log := c.log.WithFields(log.Fields{"Monitor": name})
	log.Info("Adding Monitor connection")
	c.serverConns = append(c.serverConns, &serverConn{
		ctx:  log,
		name: name,
		conn: conn,
	})
}

// Close the client and all its connections
func (c *Client) Close() {
	c.cancel()
	for _, server := range c.serverConns {
		server.Close()
	}
}

type serverConn struct {
	ctx  log.Interface
	name string

	ready chan struct{}
	conn  *grpc.ClientConn
}

func (c *serverConn) Close() {
	if c.ready != nil {
		<-c.ready
	}
	if c.conn != nil {
		c.conn.Close()
	}
}

type gatewayStreams struct {
	log    log.Interface
	ctx    context.Context
	cancel context.CancelFunc

	mu       sync.RWMutex
	uplink   map[string]chan *router.UplinkMessage
	downlink map[string]chan *router.DownlinkMessage
	status   map[string]chan *gateway.Status
}

func (s *gatewayStreams) Send(msg interface{}) {
	if msg == nil {
		return
	}
	s.mu.RLock()
	defer s.mu.RUnlock()
	switch msg := msg.(type) {
	case *router.UplinkMessage:
		s.log.WithField("Monitors", len(s.uplink)).Debug("Sending UplinkMessage to monitor")
		for serverName, ch := range s.uplink {
			select {
			case ch <- msg:
			default:
				s.log.WithField("Monitor", serverName).Warn("UplinkMessage buffer full")
			}
		}
	case *router.DeviceActivationRequest:
		s.log.WithField("Monitors", len(s.uplink)).Debug("Sending DeviceActivationRequest->UplinkMessage to monitor")
		for serverName, ch := range s.uplink {
			select {
			case ch <- &router.UplinkMessage{
				Payload:          msg.Payload,
				Message:          msg.Message,
				ProtocolMetadata: msg.ProtocolMetadata,
				GatewayMetadata:  msg.GatewayMetadata,
				Trace:            msg.Trace,
			}:
			default:
				s.log.WithField("Monitor", serverName).Warn("UplinkMessage buffer full")
			}
		}
	case *router.DownlinkMessage:
		s.log.WithField("Monitors", len(s.downlink)).Debug("Sending DownlinkMessage to monitor")
		for serverName, ch := range s.downlink {
			select {
			case ch <- msg:
			default:
				s.log.WithField("Monitor", serverName).Warn("DownlinkMessage buffer full")
			}
		}
	case *gateway.Status:
		s.log.WithField("Monitors", len(s.status)).Debug("Sending Status to monitor")
		for serverName, ch := range s.status {
			select {
			case ch <- msg:
			default:
				s.log.WithField("Monitor", serverName).Warn("GatewayStatus buffer full")
			}
		}
	}
}

func (s *gatewayStreams) Close() {
	s.cancel()
}

// NewGatewayStreams returns new streams using the given gateway ID and token
func (c *Client) NewGatewayStreams(id string, token string) GenericStream {
	log := c.log.WithField("GatewayID", id)
	ctx, cancel := context.WithCancel(c.ctx)
	ctx = ttnctx.OutgoingContextWithID(ctx, id)
	ctx = ttnctx.OutgoingContextWithToken(ctx, token)
	s := &gatewayStreams{
		log:    log,
		ctx:    ctx,
		cancel: cancel,

		uplink:   make(map[string]chan *router.UplinkMessage),
		downlink: make(map[string]chan *router.DownlinkMessage),
		status:   make(map[string]chan *gateway.Status),
	}

	var wg utils.WaitGroup

	// Hook up the monitor servers
	for _, server := range c.serverConns {
		wg.Add(1)
		go func(server *serverConn) {
			if server.ready != nil {
				select {
				case <-ctx.Done():
					return
				case <-server.ready:
				}
			}
			if server.conn == nil {
				return
			}
			log := log.WithField("Monitor", server.name)
			cli := NewMonitorClient(server.conn)

			chUplink := make(chan *router.UplinkMessage, c.config.BufferSize)
			chDownlink := make(chan *router.DownlinkMessage, c.config.BufferSize)
			chStatus := make(chan *gateway.Status, c.config.BufferSize)

			defer func() {
				s.mu.Lock()
				defer s.mu.Unlock()
				delete(s.uplink, server.name)
				delete(s.downlink, server.name)
				delete(s.status, server.name)
				close(chUplink)
				close(chDownlink)
				close(chStatus)
			}()

			// Uplink stream
			uplink, err := cli.GatewayUplink(ctx)
			if err != nil {
				log.WithError(err).Warn("Could not set up GatewayUplink stream")
			} else {
				s.mu.Lock()
				s.uplink[server.name] = chUplink
				s.mu.Unlock()
				go func() {
					monitorStream(log, "GatewayUplink", uplink)
					s.mu.Lock()
					defer s.mu.Unlock()
					delete(s.uplink, server.name)
				}()
			}

			// Downlink stream
			downlink, err := cli.GatewayDownlink(ctx)
			if err != nil {
				log.WithError(err).Warn("Could not set up GatewayDownlink stream")
			} else {
				s.mu.Lock()
				s.downlink[server.name] = chDownlink
				s.mu.Unlock()
				go func() {
					monitorStream(log, "GatewayDownlink", downlink)
					s.mu.Lock()
					defer s.mu.Unlock()
					delete(s.downlink, server.name)
				}()
			}

			// Status stream
			status, err := cli.GatewayStatus(ctx)
			if err != nil {
				log.WithError(err).Warn("Could not set up GatewayStatus stream")
			} else {
				s.mu.Lock()
				s.status[server.name] = chStatus
				s.mu.Unlock()
				go func() {
					monitorStream(log, "GatewayStatus", status)
					s.mu.Lock()
					defer s.mu.Unlock()
					delete(s.status, server.name)
				}()
			}

			wg.Done()
			log.Debug("Start handling Gateway streams")
			defer log.Debug("Done handling Gateway streams")
			for {
				select {
				case <-ctx.Done():
					return
				case msg := <-chStatus:
					if err := status.Send(msg); err != nil {
						log.WithError(err).Warn("Could not send GatewayStatus to monitor")
						if err == restartstream.ErrStreamClosed {
							return
						}
					}
				case msg := <-chUplink:
					if err := uplink.Send(msg); err != nil {
						log.WithError(err).Warn("Could not send UplinkMessage to monitor")
						if err == restartstream.ErrStreamClosed {
							return
						}
					}
				case msg := <-chDownlink:
					if err := downlink.Send(msg); err != nil {
						log.WithError(err).Warn("Could not send DownlinkMessage to monitor")
						if err == restartstream.ErrStreamClosed {
							return
						}
					}
				}
			}
		}(server)
	}

	if api.WaitForStreams > 0 {
		wg.WaitForMax(api.WaitForStreams)
	}

	return s
}

type routerStreams struct {
	log    log.Interface
	ctx    context.Context
	cancel context.CancelFunc

	mu     sync.RWMutex
	status map[string]chan *router.Status
}

func (s *routerStreams) Send(msg interface{}) {
	if msg == nil {
		return
	}
	s.mu.RLock()
	defer s.mu.RUnlock()
	switch msg := msg.(type) {
	case *router.Status:
		s.log.WithField("Monitors", len(s.status)).Debug("Sending Status to monitor")
		for serverName, ch := range s.status {
			select {
			case ch <- msg:
			default:
				s.log.WithField("Monitor", serverName).Warn("Status buffer full")
			}
		}
	}
}

func (s *routerStreams) Close() {
	s.cancel()
}

// NewRouterStreams returns new streams using the given router ID and token
func (c *Client) NewRouterStreams(id string, token string) GenericStream {
	log := c.log
	ctx, cancel := context.WithCancel(c.ctx)
	ctx = ttnctx.OutgoingContextWithID(ctx, id)
	ctx = ttnctx.OutgoingContextWithToken(ctx, token)
	s := &routerStreams{
		log:    log,
		ctx:    ctx,
		cancel: cancel,

		status: make(map[string]chan *router.Status),
	}

	// Hook up the monitor servers
	for _, server := range c.serverConns {
		go func(server *serverConn) {
			if server.ready != nil {
				select {
				case <-ctx.Done():
					return
				case <-server.ready:
				}
			}
			if server.conn == nil {
				return
			}

			log := log.WithField("Monitor", server.name)
			cli := NewMonitorClient(server.conn)

			chStatus := make(chan *router.Status, c.config.BufferSize)

			defer func() {
				s.mu.Lock()
				defer s.mu.Unlock()
				delete(s.status, server.name)
				close(chStatus)
			}()

			// Status stream
			status, err := cli.RouterStatus(ctx)
			if err != nil {
				log.WithError(err).Warn("Could not set up RouterStatus stream")
			} else {
				s.mu.Lock()
				s.status[server.name] = chStatus
				s.mu.Unlock()
				go func() {
					monitorStream(log, "RouterStatus", status)
					s.mu.Lock()
					defer s.mu.Unlock()
					delete(s.status, server.name)
				}()
			}

			log.Debug("Start handling Router streams")
			defer log.Debug("Done handling Router streams")
			for {
				select {
				case <-ctx.Done():
					return
				case msg := <-chStatus:
					if err := status.Send(msg); err != nil {
						log.WithError(err).Warn("Could not send Status to monitor")
						if err == restartstream.ErrStreamClosed {
							return
						}
					}
				}
			}

		}(server)
	}

	return s
}

type brokerStreams struct {
	log    log.Interface
	ctx    context.Context
	cancel context.CancelFunc

	mu       sync.RWMutex
	uplink   map[string]chan *broker.DeduplicatedUplinkMessage
	downlink map[string]chan *broker.DownlinkMessage
	status   map[string]chan *broker.Status
}

func (s *brokerStreams) Send(msg interface{}) {
	if msg == nil {
		return
	}
	s.mu.RLock()
	defer s.mu.RUnlock()
	switch msg := msg.(type) {
	case *broker.DeduplicatedUplinkMessage:
		s.log.WithField("Monitors", len(s.uplink)).Debug("Sending DeduplicatedUplinkMessage to monitor")
		for serverName, ch := range s.uplink {
			select {
			case ch <- msg:
			default:
				s.log.WithField("Monitor", serverName).Warn("DeduplicatedUplinkMessage buffer full")
			}
		}
	case *broker.DeduplicatedDeviceActivationRequest:
		s.log.WithField("Monitors", len(s.uplink)).Debug("Sending DeduplicatedDeviceActivationRequest->DeduplicatedUplinkMessage to monitor")
		for serverName, ch := range s.uplink {
			select {
			case ch <- &broker.DeduplicatedUplinkMessage{
				Payload:          msg.Payload,
				Message:          msg.Message,
				DevEui:           msg.DevEui,
				AppEui:           msg.AppEui,
				AppId:            msg.AppId,
				DevId:            msg.DevId,
				ProtocolMetadata: msg.ProtocolMetadata,
				GatewayMetadata:  msg.GatewayMetadata,
				ServerTime:       msg.ServerTime,
				Trace:            msg.Trace,
			}:
			default:
				s.log.WithField("Monitor", serverName).Warn("DeduplicatedUplinkMessage buffer full")
			}
		}
	case *broker.DownlinkMessage:
		s.log.WithField("Monitors", len(s.downlink)).Debug("Sending DownlinkMessage to monitor")
		for serverName, ch := range s.downlink {
			select {
			case ch <- msg:
			default:
				s.log.WithField("Monitor", serverName).Warn("DownlinkMessage buffer full")
			}
		}
	case *broker.Status:
		s.log.WithField("Monitors", len(s.status)).Debug("Sending Status to monitor")
		for serverName, ch := range s.status {
			select {
			case ch <- msg:
			default:
				s.log.WithField("Monitor", serverName).Warn("Status buffer full")
			}
		}
	}
}

func (s *brokerStreams) Close() {
	s.cancel()
}

// NewBrokerStreams returns new streams using the given broker ID and token
func (c *Client) NewBrokerStreams(id string, token string) GenericStream {
	log := c.log
	ctx, cancel := context.WithCancel(c.ctx)
	ctx = ttnctx.OutgoingContextWithID(ctx, id)
	ctx = ttnctx.OutgoingContextWithToken(ctx, token)
	s := &brokerStreams{
		log:    log,
		ctx:    ctx,
		cancel: cancel,

		uplink:   make(map[string]chan *broker.DeduplicatedUplinkMessage),
		downlink: make(map[string]chan *broker.DownlinkMessage),
		status:   make(map[string]chan *broker.Status),
	}

	// Hook up the monitor servers
	for _, server := range c.serverConns {
		go func(server *serverConn) {
			if server.ready != nil {
				select {
				case <-ctx.Done():
					return
				case <-server.ready:
				}
			}
			if server.conn == nil {
				return
			}

			log := log.WithField("Monitor", server.name)
			cli := NewMonitorClient(server.conn)

			chUplink := make(chan *broker.DeduplicatedUplinkMessage, c.config.BufferSize)
			chDownlink := make(chan *broker.DownlinkMessage, c.config.BufferSize)
			chStatus := make(chan *broker.Status, c.config.BufferSize)

			defer func() {
				s.mu.Lock()
				defer s.mu.Unlock()
				delete(s.uplink, server.name)
				delete(s.downlink, server.name)
				delete(s.status, server.name)
				close(chUplink)
				close(chDownlink)
				close(chStatus)
			}()

			// Uplink stream
			uplink, err := cli.BrokerUplink(ctx)
			if err != nil {
				log.WithError(err).Warn("Could not set up BrokerUplink stream")
			} else {
				s.mu.Lock()
				s.uplink[server.name] = chUplink
				s.mu.Unlock()
				go func() {
					monitorStream(log, "BrokerUplink", uplink)
					s.mu.Lock()
					defer s.mu.Unlock()
					delete(s.uplink, server.name)
				}()
			}

			// Downlink stream
			downlink, err := cli.BrokerDownlink(ctx)
			if err != nil {
				log.WithError(err).Warn("Could not set up BrokerDownlink stream")
			} else {
				s.mu.Lock()
				s.downlink[server.name] = chDownlink
				s.mu.Unlock()
				go func() {
					monitorStream(log, "BrokerDownlink", downlink)
					s.mu.Lock()
					defer s.mu.Unlock()
					delete(s.downlink, server.name)
				}()
			}

			// Status stream
			status, err := cli.BrokerStatus(ctx)
			if err != nil {
				log.WithError(err).Warn("Could not set up BrokerStatus stream")
			} else {
				s.mu.Lock()
				s.status[server.name] = chStatus
				s.mu.Unlock()
				go func() {
					monitorStream(log, "BrokerStatus", status)
					s.mu.Lock()
					defer s.mu.Unlock()
					delete(s.status, server.name)
				}()
			}

			log.Debug("Start handling Broker streams")
			defer log.Debug("Done handling Broker streams")
			for {
				select {
				case <-ctx.Done():
					return
				case msg := <-chUplink:
					if err := uplink.Send(msg); err != nil {
						log.WithError(err).Warn("Could not send UplinkMessage to monitor")
						if err == restartstream.ErrStreamClosed {
							return
						}
					}
				case msg := <-chDownlink:
					if err := downlink.Send(msg); err != nil {
						log.WithError(err).Warn("Could not send DownlinkMessage to monitor")
						if err == restartstream.ErrStreamClosed {
							return
						}
					}
				case msg := <-chStatus:
					if err := status.Send(msg); err != nil {
						log.WithError(err).Warn("Could not send Status to monitor")
						if err == restartstream.ErrStreamClosed {
							return
						}
					}
				}
			}

		}(server)
	}

	return s
}

type networkServerStreams struct {
	log    log.Interface
	ctx    context.Context
	cancel context.CancelFunc

	mu     sync.RWMutex
	status map[string]chan *networkserver.Status
}

func (s *networkServerStreams) Send(msg interface{}) {
	if msg == nil {
		return
	}
	s.mu.RLock()
	defer s.mu.RUnlock()
	switch msg := msg.(type) {
	case *networkserver.Status:
		s.log.WithField("Monitors", len(s.status)).Debug("Sending Status to monitor")
		for serverName, ch := range s.status {
			select {
			case ch <- msg:
			default:
				s.log.WithField("Monitor", serverName).Warn("Status buffer full")
			}
		}
	}
}

func (s *networkServerStreams) Close() {
	s.cancel()
}

// NewNetworkServerStreams returns new streams using the given networkServer ID and token
func (c *Client) NewNetworkServerStreams(id string, token string) GenericStream {
	log := c.log
	ctx, cancel := context.WithCancel(c.ctx)
	ctx = ttnctx.OutgoingContextWithID(ctx, id)
	ctx = ttnctx.OutgoingContextWithToken(ctx, token)
	s := &networkServerStreams{
		log:    log,
		ctx:    ctx,
		cancel: cancel,

		status: make(map[string]chan *networkserver.Status),
	}

	// Hook up the monitor servers
	for _, server := range c.serverConns {
		go func(server *serverConn) {
			if server.ready != nil {
				select {
				case <-ctx.Done():
					return
				case <-server.ready:
				}
			}
			if server.conn == nil {
				return
			}

			log := log.WithField("Monitor", server.name)
			cli := NewMonitorClient(server.conn)

			chStatus := make(chan *networkserver.Status, c.config.BufferSize)

			defer func() {
				s.mu.Lock()
				defer s.mu.Unlock()
				delete(s.status, server.name)
				close(chStatus)
			}()

			// Status stream
			status, err := cli.NetworkServerStatus(ctx)
			if err != nil {
				log.WithError(err).Warn("Could not set up NetworkServerStatus stream")
			} else {
				s.mu.Lock()
				s.status[server.name] = chStatus
				s.mu.Unlock()
				go func() {
					monitorStream(log, "NetworkServerStatus", status)
					s.mu.Lock()
					defer s.mu.Unlock()
					delete(s.status, server.name)
				}()
			}

			log.Debug("Start handling NetworkServer streams")
			defer log.Debug("Done handling NetworkServer streams")
			for {
				select {
				case <-ctx.Done():
					return
				case msg := <-chStatus:
					if err := status.Send(msg); err != nil {
						log.WithError(err).Warn("Could not send Status to monitor")
						if err == restartstream.ErrStreamClosed {
							return
						}
					}
				}
			}

		}(server)
	}

	return s
}

type handlerStreams struct {
	log    log.Interface
	ctx    context.Context
	cancel context.CancelFunc

	mu       sync.RWMutex
	uplink   map[string]chan *broker.DeduplicatedUplinkMessage
	downlink map[string]chan *broker.DownlinkMessage
	status   map[string]chan *handler.Status
}

func (s *handlerStreams) Send(msg interface{}) {
	if msg == nil {
		return
	}
	s.mu.RLock()
	defer s.mu.RUnlock()
	switch msg := msg.(type) {
	case *broker.DeduplicatedUplinkMessage:
		s.log.Debug("Sending DeduplicatedUplinkMessage to monitor")
		for serverName, ch := range s.uplink {
			select {
			case ch <- msg:
			default:
				s.log.WithField("Monitor", serverName).Warn("DeduplicatedUplinkMessage buffer full")
			}
		}
	case *broker.DeduplicatedDeviceActivationRequest:
		s.log.WithField("Monitors", len(s.uplink)).Debug("Sending DeduplicatedDeviceActivationRequest->DeduplicatedUplinkMessage to monitor")
		for serverName, ch := range s.uplink {
			select {
			case ch <- &broker.DeduplicatedUplinkMessage{
				Payload:          msg.Payload,
				Message:          msg.Message,
				DevEui:           msg.DevEui,
				AppEui:           msg.AppEui,
				AppId:            msg.AppId,
				DevId:            msg.DevId,
				ProtocolMetadata: msg.ProtocolMetadata,
				GatewayMetadata:  msg.GatewayMetadata,
				ServerTime:       msg.ServerTime,
				Trace:            msg.Trace,
			}:
			default:
				s.log.WithField("Monitor", serverName).Warn("DeduplicatedUplinkMessage buffer full")
			}
		}
	case *broker.DownlinkMessage:
		s.log.Debug("Sending DownlinkMessage to monitor")
		for serverName, ch := range s.downlink {
			select {
			case ch <- msg:
			default:
				s.log.WithField("Monitor", serverName).Warn("DownlinkMessage buffer full")
			}
		}
	case *handler.Status:
		s.log.Debug("Sending Status to monitor")
		for serverName, ch := range s.status {
			select {
			case ch <- msg:
			default:
				s.log.WithField("Monitor", serverName).Warn("Status buffer full")
			}
		}
	}
}

func (s *handlerStreams) Close() {
	s.cancel()
}

// NewHandlerStreams returns new streams using the given handler ID and token
func (c *Client) NewHandlerStreams(id string, token string) GenericStream {
	log := c.log
	ctx, cancel := context.WithCancel(c.ctx)
	ctx = ttnctx.OutgoingContextWithID(ctx, id)
	ctx = ttnctx.OutgoingContextWithToken(ctx, token)
	s := &handlerStreams{
		log:    log,
		ctx:    ctx,
		cancel: cancel,

		uplink:   make(map[string]chan *broker.DeduplicatedUplinkMessage),
		downlink: make(map[string]chan *broker.DownlinkMessage),
		status:   make(map[string]chan *handler.Status),
	}

	// Hook up the monitor servers
	for _, server := range c.serverConns {
		go func(server *serverConn) {
			if server.ready != nil {
				select {
				case <-ctx.Done():
					return
				case <-server.ready:
				}
			}
			if server.conn == nil {
				return
			}

			log := log.WithField("Monitor", server.name)
			cli := NewMonitorClient(server.conn)

			chUplink := make(chan *broker.DeduplicatedUplinkMessage, c.config.BufferSize)
			chDownlink := make(chan *broker.DownlinkMessage, c.config.BufferSize)
			chStatus := make(chan *handler.Status, c.config.BufferSize)

			defer func() {
				s.mu.Lock()
				defer s.mu.Unlock()
				delete(s.uplink, server.name)
				delete(s.downlink, server.name)
				delete(s.status, server.name)
				close(chUplink)
				close(chDownlink)
				close(chStatus)
			}()

			// Uplink stream
			uplink, err := cli.HandlerUplink(ctx)
			if err != nil {
				log.WithError(err).Warn("Could not set up HandlerUplink stream")
			} else {
				s.mu.Lock()
				s.uplink[server.name] = chUplink
				s.mu.Unlock()
				go func() {
					monitorStream(log, "HandlerUplink", uplink)
					s.mu.Lock()
					defer s.mu.Unlock()
					delete(s.uplink, server.name)
				}()
			}

			// Downlink stream
			downlink, err := cli.HandlerDownlink(ctx)
			if err != nil {
				log.WithError(err).Warn("Could not set up HandlerDownlink stream")
			} else {
				s.mu.Lock()
				s.downlink[server.name] = chDownlink
				s.mu.Unlock()
				go func() {
					monitorStream(log, "HandlerDownlink", downlink)
					s.mu.Lock()
					defer s.mu.Unlock()
					delete(s.downlink, server.name)
				}()
			}

			// Status stream
			status, err := cli.HandlerStatus(ctx)
			if err != nil {
				log.WithError(err).Warn("Could not set up HandlerStatus stream")
			} else {
				s.mu.Lock()
				s.status[server.name] = chStatus
				s.mu.Unlock()
				go func() {
					monitorStream(log, "HandlerStatus", status)
					s.mu.Lock()
					defer s.mu.Unlock()
					delete(s.status, server.name)
				}()
			}

			log.Debug("Start handling Handler streams")
			defer log.Debug("Done handling Handler streams")
			for {
				select {
				case <-ctx.Done():
					return
				case msg := <-chUplink:
					if err := uplink.Send(msg); err != nil {
						log.WithError(err).Warn("Could not send UplinkMessage to monitor")
						if err == restartstream.ErrStreamClosed {
							return
						}
					}
				case msg := <-chDownlink:
					if err := downlink.Send(msg); err != nil {
						log.WithError(err).Warn("Could not send DownlinkMessage to monitor")
						if err == restartstream.ErrStreamClosed {
							return
						}
					}
				case msg := <-chStatus:
					if err := status.Send(msg); err != nil {
						log.WithError(err).Warn("Could not send Status to monitor")
						if err == restartstream.ErrStreamClosed {
							return
						}
					}
				}
			}

		}(server)
	}

	return s
}

func monitorStream(log log.Interface, streamName string, stream grpc.ClientStream) {
	err := stream.RecvMsg(new(empty.Empty))
	switch {
	case err == nil:
		log.Debugf("%s stream closed", streamName)
	case err == io.EOF:
		log.WithError(err).Debugf("%s stream ended", streamName)
	case err == context.Canceled || grpc.Code(err) == codes.Canceled:
		log.WithError(err).Debugf("%s stream canceled", streamName)
	case err == context.DeadlineExceeded || grpc.Code(err) == codes.DeadlineExceeded:
		log.WithError(err).Debugf("%s stream deadline exceeded", streamName)
	case grpc.ErrorDesc(err) == grpc.ErrClientConnClosing.Error():
		log.WithError(err).Debugf("%s stream connection closed", streamName)
	default:
		log.WithError(err).Warnf("%s stream closed unexpectedly", streamName)
	}
}


text:
@interface PodsDummy_Pods_HurryPorter_Example : NSObject
@end
@implementation PodsDummy_Pods_HurryPorter_Example
@end


text:
using System.Collections.Generic;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Citrina
{
    public class UsersUserSettingsXtr
    {
        public UsersUserConnections Connections { get; set; }

        /// <summary>
        /// User's date of birth.
        /// </summary>
        public string Bdate { get; set; }

        /// <summary>
        /// Information whether user's birthdate are hidden.
        /// </summary>
        public int? BdateVisibility { get; set; }

        public BaseCity City { get; set; }

        public BaseCountry Country { get; set; }

        /// <summary>
        /// User first name.
        /// </summary>
        public string FirstName { get; set; }

        /// <summary>
        /// User's hometown.
        /// </summary>
        public string HomeTown { get; set; }

        /// <summary>
        /// User last name.
        /// </summary>
        public string LastName { get; set; }

        /// <summary>
        /// User maiden name.
        /// </summary>
        public string MaidenName { get; set; }

        public AccountNameRequest NameRequest { get; set; }

        public UsersPersonal Personal { get; set; }

        /// <summary>
        /// User phone number with some hidden digits.
        /// </summary>
        public string Phone { get; set; }

        /// <summary>
        /// User relationship status.
        /// </summary>
        public int? Relation { get; set; }

        public UsersUserMin RelationPartner { get; set; }

        /// <summary>
        /// Information whether relation status is pending.
        /// </summary>
        public bool? RelationPending { get; set; }

        public IEnumerable<UsersUserMin> RelationRequests { get; set; }

        /// <summary>
        /// Domain name of the user's page.
        /// </summary>
        public string ScreenName { get; set; }

        /// <summary>
        /// User sex.
        /// </summary>
        public int? Sex { get; set; }

        /// <summary>
        /// User status.
        /// </summary>
        public string Status { get; set; }

        public AudioAudio StatusAudio { get; set; }

        public AccountUserSettingsInterests Interests { get; set; }

        public IEnumerable<string> Languages { get; set; }
    }
}


text:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
<script src="${base}/res/common/js/jquery.js" type="text/javascript"></script>
<script src="${base}/res/common/js/jquery-ui.js" type="text/javascript"></script>
<script src="${base}/res/common/js/jquery.ext.js" type="text/javascript"></script>
<script type="text/javascript">
<#if error??>
window.parent.plugExist("${error}");
<#else>
parent.document.getElementById("plugPath").value="${plugPath!}";
</#if>
</script>
</head>
<body>
</body>
</html>


text:
"""The Kostal Plenticore Solar Inverter integration."""
import logging

from kostal.plenticore import PlenticoreApiException

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant

from .const import DOMAIN
from .helper import Plenticore

_LOGGER = logging.getLogger(__name__)

PLATFORMS = ["select", "sensor", "switch"]

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
    """Set up Kostal Plenticore Solar Inverter from a config entry."""
    hass.data.setdefault(DOMAIN, {})

    plenticore = Plenticore(hass, entry)

    if not await plenticore.async_setup():
        return False

    hass.data[DOMAIN][entry.entry_id] = plenticore

    hass.config_entries.async_setup_platforms(entry, PLATFORMS)

    return True

async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
    """Unload a config entry."""
    unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
    if unload_ok:
        # remove API object
        plenticore = hass.data[DOMAIN].pop(entry.entry_id)
        try:
            await plenticore.async_unload()
        except PlenticoreApiException as err:
            _LOGGER.error("Error logging out from inverter: %s", err)

    return unload_ok


text:
package org.hsqldb.persist;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;

import org.hsqldb.error.Error;
import org.hsqldb.error.ErrorCode;
import org.hsqldb.lib.ArrayUtil;
import org.hsqldb.lib.FileAccess;
import org.hsqldb.lib.FileUtil;
import org.hsqldb.lib.HashMap;
import org.hsqldb.lib.java.JavaSystem;
import org.hsqldb.store.ValuePool;

/**
 * Wrapper for java.util.Properties to limit values to Specific types and
 * allow saving and loading.

 *
 * @author Fred Toussi (fredt@users dot sourceforge.net)
 * @version 1.9.0
 * @since 1.7.0
 */
public class HsqlProperties {

    //
    public static final int NO_VALUE_FOR_KEY = 1;
    protected String        fileName;
    protected Properties    stringProps;
    protected int[]         errorCodes = ValuePool.emptyIntArray;
    protected String[]      errorKeys  = ValuePool.emptyStringArray;
    protected boolean       resource   = false;
    protected FileAccess    fa;
    protected HashMap       metaData;

    public HsqlProperties() {
        stringProps = new Properties();
        fileName    = null;
    }

    public HsqlProperties(String fileName) {

        stringProps   = new Properties();
        this.fileName = fileName;
        fa            = FileUtil.getFileUtil();
    }

    public HsqlProperties(HashMap meta, String fileName, FileAccess accessor,
                          boolean b) {

        stringProps   = new Properties();
        this.fileName = fileName;
        fa            = accessor;
        metaData      = meta;
    }

    public HsqlProperties(Properties props) {
        stringProps = props;
    }

    public void setFileName(String name) {
        fileName = name;
    }

    public String setProperty(String key, int value) {
        return setProperty(key, Integer.toString(value));
    }

    public String setProperty(String key, boolean value) {
        return setProperty(key, String.valueOf(value));
    }

    public String setProperty(String key, String value) {
        return (String) stringProps.put(key, value);
    }

    public String setPropertyIfNotExists(String key, String value) {

        value = getProperty(key, value);

        return setProperty(key, value);
    }

    public Properties getProperties() {
        return stringProps;
    }

    public String getProperty(String key) {
        return stringProps.getProperty(key);
    }

    public String getProperty(String key, String defaultValue) {
        return stringProps.getProperty(key, defaultValue);
    }

    public int getIntegerProperty(String key, int defaultValue) {

        String prop = getProperty(key);

        try {
            if (prop != null) {
                defaultValue = Integer.parseInt(prop);
            }
        } catch (NumberFormatException e) {}

        return defaultValue;
    }

    public boolean isPropertyTrue(String key) {
        return isPropertyTrue(key, false);
    }

    public boolean isPropertyTrue(String key, boolean defaultValue) {

        String value = stringProps.getProperty(key);

        if (value == null) {
            return defaultValue;
        }

        return value.toLowerCase().equals("true");
    }

    public void removeProperty(String key) {
        stringProps.remove(key);
    }

    public void addProperties(Properties props) {

        if (props == null) {
            return;
        }

        Enumeration keys = props.propertyNames();

        while (keys.hasMoreElements()) {
            String key = (String) keys.nextElement();
            String value = props.getProperty(key);
            this.stringProps.put(key, value);
        }
    }

    public void addProperties(HsqlProperties props) {

        if (props == null) {
            return;
        }

        addProperties(props.stringProps);
    }

// oj@openoffice.org
    public boolean propertiesFileExists() {

        String propFilename = fileName + ".properties";

        return fa.isStreamElement(propFilename);
    }

    public boolean load() throws Exception {

        if (!propertiesFileExists()) {
            return false;
        }

        if (fileName == null || fileName.length() == 0) {
            throw new FileNotFoundException(
                Error.getMessage(ErrorCode.M_HsqlProperties_load));
        }

        InputStream fis           = null;
        String      propsFilename = fileName + ".properties";

// oj@openoffice.org
        try {
            fis = fa.openInputStreamElement(propsFilename);

            stringProps.load(fis);
        } finally {
            if (fis != null) {
                fis.close();
            }
        }

        return true;
    }

    /**
     *  Saves the properties.
     */
    public void save() throws Exception {

        if (fileName == null || fileName.length() == 0) {
            throw new java.io.FileNotFoundException(
                Error.getMessage(ErrorCode.M_HsqlProperties_load));
        }

        String filestring = fileName + ".properties";

        save(filestring);
    }

    /**
     *  Saves the properties using JDK2 method if present, otherwise JDK1.
     */
    public void save(String fileString) throws Exception {

// oj@openoffice.org
        fa.createParentDirs(fileString);

        OutputStream        fos = fa.openOutputStreamElement(fileString);
        FileAccess.FileSync outDescriptor = fa.getFileSync(fos);

        JavaSystem.saveProperties(
            stringProps,
            HsqlDatabaseProperties.PRODUCT_NAME + " "
            + HsqlDatabaseProperties.THIS_FULL_VERSION, fos);
        fos.flush();
        outDescriptor.sync();
        fos.close();

        return;
    }

    /**
     * Adds the error code and the key to the list of errors. This list
     * is populated during construction or addition of elements and is used
     * outside this class to act upon the errors.
     */
    private void addError(int code, String key) {

        errorCodes = (int[]) ArrayUtil.resizeArray(errorCodes,
                errorCodes.length + 1);
        errorKeys = (String[]) ArrayUtil.resizeArray(errorKeys,
                errorKeys.length + 1);
        errorCodes[errorCodes.length - 1] = code;
        errorKeys[errorKeys.length - 1]   = key;
    }

    /**
     * Creates and populates an HsqlProperties Object from the arguments
     * array of a Main method. Properties are in the form of "-key value"
     * pairs. Each key is prefixed with the type argument and a dot before
     * being inserted into the properties Object.

     *
     * "--help" is treated as a key with no value and not inserted.
     */
    public static HsqlProperties argArrayToProps(String[] arg, String type) {

        HsqlProperties props = new HsqlProperties();

        for (int i = 0; i < arg.length; i++) {
            String p = arg[i];

            if (p.equals("--help") || p.equals("-help")) {
                props.addError(NO_VALUE_FOR_KEY, p.substring(1));
            } else if (p.startsWith("--")) {
                String value = i + 1 < arg.length ? arg[i + 1]
                                                  : "";

                props.setProperty(type + "." + p.substring(2), value);

                i++;
            } else if (p.charAt(0) == '-') {
                String value = i + 1 < arg.length ? arg[i + 1]
                                                  : "";

                props.setProperty(type + "." + p.substring(1), value);

                i++;
            }
        }

        return props;
    }

    /**
     * Creates and populates a new HsqlProperties Object using a string
     * such as "key1=value1;key2=value2".

     *
     * The string that represents the = sign above is specified as pairsep
     * and the one that represents the semicolon is specified as delimiter,
     * allowing any string to be used for either.

     *
     * Leading / trailing spaces around the keys and values are discarded.

     *
     * The string is parsed by (1) subdividing into segments by delimiter
     * (2) subdividing each segment in two by finding the first instance of
     * the pairsep (3) trimming each pair of segments from step 2 and
     * inserting into the properties object.

     *
     * Each key is prefixed with the type argument and a dot before being
     * inserted.

     *
     * Any key without a value is added to the list of errors.
     */
    public static HsqlProperties delimitedArgPairsToProps(String s,
            String pairsep, String dlimiter, String type) {

        HsqlProperties props       = new HsqlProperties();
        int            currentpair = 0;

        while (true) {
            int nextpair = s.indexOf(dlimiter, currentpair);

            if (nextpair == -1) {
                nextpair = s.length();
            }

            // find value within the segment
            int valindex = s.substring(0, nextpair).indexOf(pairsep,
                                       currentpair);

            if (valindex == -1) {
                props.addError(NO_VALUE_FOR_KEY,
                               s.substring(currentpair, nextpair).trim());
            } else {
                String key = s.substring(currentpair, valindex).trim();
                String value = s.substring(valindex + pairsep.length(),
                                           nextpair).trim();

                if (type != null) {
                    key = type + "." + key;
                }

                props.setProperty(key, value);
            }

            if (nextpair == s.length()) {
                break;
            }

            currentpair = nextpair + dlimiter.length();
        }

        return props;
    }

    public Enumeration propertyNames() {
        return stringProps.propertyNames();
    }

    public boolean isEmpty() {
        return stringProps.isEmpty();
    }

    public String[] getErrorKeys() {
        return errorKeys;
    }

    // column number mappings
    public static final int indexName         = 0;
    public static final int indexType         = 1;
    public static final int indexClass        = 2;
    public static final int indexIsRange      = 3;
    public static final int indexDefaultValue = 4;
    public static final int indexRangeLow     = 5;
    public static final int indexRangeHigh    = 6;
    public static final int indexValues       = 7;
    public static final int indexLimit        = 9;

    public static Object[] getMeta(String name, int type,
                                   String defaultValue) {

        Object[] row = new Object[indexLimit];

        row[indexName]         = name;
        row[indexType]         = ValuePool.getInt(type);
        row[indexClass]        = "String";
        row[indexDefaultValue] = defaultValue;

        return row;
    }

    public static Object[] getMeta(String name, int type,
                                   boolean defaultValue) {

        Object[] row = new Object[indexLimit];

        row[indexName]         = name;
        row[indexType]         = ValuePool.getInt(type);
        row[indexClass]        = "Boolean";
        row[indexDefaultValue] = defaultValue ? Boolean.TRUE
                                              : Boolean.FALSE;

        return row;
    }

    public static Object[] getMeta(String name, int type, int defaultValue,
                                   int[] values) {

        Object[] row = new Object[indexLimit];

        row[indexName]         = name;
        row[indexType]         = ValuePool.getInt(type);
        row[indexClass]        = "Integer";
        row[indexDefaultValue] = ValuePool.getInt(defaultValue);
        row[indexValues]       = values;

        return row;
    }

    public static Object[] getMeta(String name, int type, int defaultValue,
                                   int rangeLow, int rangeHigh) {

        Object[] row = new Object[indexLimit];

        row[indexName]         = name;
        row[indexType]         = ValuePool.getInt(type);
        row[indexClass]        = "Integer";
        row[indexDefaultValue] = ValuePool.getInt(defaultValue);
        row[indexIsRange]      = Boolean.TRUE;
        row[indexRangeLow]     = ValuePool.getInt(rangeLow);
        row[indexRangeHigh]    = ValuePool.getInt(rangeHigh);

        return row;
    }

    /**
     * Perfoms any range checking for property and return an error message
     */
    public static String validateProperty(String key, String value,
                                          Object[] meta) {

        if (meta[indexClass].equals("Boolean")) {
            value = value.toLowerCase();

            if (value.equals("true") || value.equals("false")) {
                return null;
            }

            return "invalid boolean value for property: " + key;
        }

        if (meta[indexClass].equals("String")) {
            return null;
        }

        if (meta[indexClass].equals("Integer")) {
            int number = Integer.parseInt(value);

            if (Boolean.TRUE.equals(meta[indexIsRange])) {
                int low  = ((Integer) meta[indexRangeLow]).intValue();
                int high = ((Integer) meta[indexRangeHigh]).intValue();

                if (number < low || high < number) {
                    return "value outside range for property: " + key;
                }
            }

            if (meta[indexValues] != null) {
                int[] values = (int[]) meta[indexValues];

                if (ArrayUtil.find(values, number) == -1) {
                    return "value not supported for property: " + key;
                }
            }

            return null;
        }

        return null;
    }

    public String toString() {

        StringBuffer sb;

        sb = new StringBuffer();

        sb.append('[');

        int         len = stringProps.size();
        Enumeration en  = stringProps.propertyNames();

        for (int i = 0; i < len; i++) {
            String key = (String) en.nextElement();

            sb.append(key);
            sb.append('=');
            sb.append(stringProps.get(key));

            if (i + 1 < len) {
                sb.append(',');
                sb.append(' ');
            }

            sb.append(']');
        }

        return sb.toString();
    }
}


text:
require File.join(File.dirname(__FILE__), '/../../spec_helper')

module  DataShift

  describe 'Transformation Remove' do
    include_context 'ClearThenManageProject'

    context 'Column removals' do
      it 'should process options to remove unwanted columns' do
        headers = [:a, :b, :c, :d, :e, :f]

        DataShift::Configuration.configure do |config|
          config.remove_columns = [:b, :f]
        end

        DataShift::Transformation::Remove.new.unwanted_columns(headers )

        expect(headers).to_not include [:b, :f]
      end

      it 'should process options to remove unwant5ed columns' do
        headers = [:a, :id, :c, :d, :e, :created_on, :f, :updated_on]

        DataShift::Configuration.configure do |config|
          config.remove_rails = true
        end

        DataShift::Transformation::Remove.new.unwanted_columns(headers)

        expect(headers).to_not include [:id, :created_on, :updated_on]
      end

    end

  end

end


text:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Graficos extends CI_Controller {
  public function __construct(){
    parent::__construct();
    $this->load->helper('url');

    // session
    $this->load->library('session');
    if (!$this->session->userdata('id')) {
      redirect('/');
    }

    $this->load->model('planillas');
    $this->load->model('planilla_datos');
  }

  public function index($sector_url = '') {
    $planta_id = $this->session->userdata('planta_id');

    $all_sector_data = $this->planillas->get_all_sector($planta_id);

    if (empty($sector_url)) {
      $sector_url = $all_sector_data[0]->url;
    }

    $siguiente_url = '';
    for ($i = 0, $sectorLength = sizeof($all_sector_data); $i < $sectorLength; ++$i) {
      if ($sector_url == $all_sector_data[$i]->url) {
        if ($i == $sectorLength - 1) {
          $i = -1;
        }

        $siguiente_url = $all_sector_data[$i + 1]->url;
        break;
      }
    }

    $sector_data = $this->planillas->get_sector_data_by_url($sector_url, $planta_id);

    $planilla_data = $this->planillas->get_by_sector_url($sector_url, $planta_id, 1);
    if (sizeof($planilla_data)) {
      $planilla_data = $planilla_data[0];
      $fecha = date('Y-m-d', strtotime($planilla_data->tiempo_inicio));

      $planilla_datos = $this->planilla_datos->get_by_planilla($planilla_data->id);

      $datos = array();
      for ($i = 0, $planillaDatosLength = sizeof($planilla_datos), $maquinaId; $i < $planillaDatosLength; ++$i) {
        $maquinaId = $planilla_datos[$i]->maquina_id;

        if (!isset($datos[$maquinaId]['min'])) {
          $maquina_dato = $this->planillas->get_maquina_by_id($maquinaId);
          $datos[$maquinaId]['nombre'] = $maquina_dato->nombre;
          $datos[$maquinaId]['min'] = $maquina_dato->min;
          $datos[$maquinaId]['max'] = $maquina_dato->max;
          $datos[$maquinaId]['unidad'] = $maquina_dato->unidad;
          $datos[$maquinaId]['tiempo'] = array();
          $datos[$maquinaId]['valor'] = array();
        }

        array_push($datos[$maquinaId]['tiempo'], $planilla_datos[$i]->tiempo);
        array_push($datos[$maquinaId]['valor'], $planilla_datos[$i]->valor);
      }
    } else {
      $fecha = 'Sin planilla';
      $datos = array();
    }

    $this->load->view('header');
    $this->load->view('operador/grafico', array(
      'titulo' => $sector_data->medida,
      'fecha' => $fecha,
      'datos' => $datos,
      'siguiente_url' => 'graficos/' . $siguiente_url
    ));
  }
}


text:
<?xml version="1.0" encoding="UTF-8"?>
<ApexPage xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>33.0</apiVersion>
    <availableInTouch>false</availableInTouch>
    <confirmationTokenRequired>false</confirmationTokenRequired>
    <description>Default Force.com Limit Exceeded page</description>
    <label>BandwidthExceeded</label>
    <packageVersions>
        <majorNumber>1</majorNumber>
        <minorNumber>6</minorNumber>
        <namespace>sf_com_apps</namespace>
    </packageVersions>
</ApexPage>


text:
; a PE with DllCharacteristics set to NO_SEH, but using a Vectored Exception Handler

; Ange Albertini, BSD LICENCE 2013

%include 'consts.inc'

IMAGEBASE equ 400000h
org IMAGEBASE
bits 32

SECTIONALIGN equ 1000h
FILEALIGN equ 200h

istruc IMAGE_DOS_HEADER
    at IMAGE_DOS_HEADER.e_magic,  db 'MZ'
    at IMAGE_DOS_HEADER.e_lfanew, dd NT_Headers - IMAGEBASE
iend

NT_Headers:
istruc IMAGE_NT_HEADERS
    at IMAGE_NT_HEADERS.Signature, db 'PE', 0, 0
iend
istruc IMAGE_FILE_HEADER
    at IMAGE_FILE_HEADER.Machine,              dw IMAGE_FILE_MACHINE_I386
    at IMAGE_FILE_HEADER.NumberOfSections,     dw NUMBEROFSECTIONS
    at IMAGE_FILE_HEADER.SizeOfOptionalHeader, dw SIZEOFOPTIONALHEADER
    at IMAGE_FILE_HEADER.Characteristics,      dw IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_32BIT_MACHINE
iend

OptionalHeader:
istruc IMAGE_OPTIONAL_HEADER32
    at IMAGE_OPTIONAL_HEADER32.Magic,                 dw IMAGE_NT_OPTIONAL_HDR32_MAGIC
    at IMAGE_OPTIONAL_HEADER32.AddressOfEntryPoint,   dd EntryPoint - IMAGEBASE
    at IMAGE_OPTIONAL_HEADER32.ImageBase,             dd IMAGEBASE
    at IMAGE_OPTIONAL_HEADER32.SectionAlignment,      dd SECTIONALIGN
    at IMAGE_OPTIONAL_HEADER32.FileAlignment,         dd FILEALIGN
    at IMAGE_OPTIONAL_HEADER32.MajorSubsystemVersion, dw 4
    at IMAGE_OPTIONAL_HEADER32.SizeOfImage,           dd 2 * SECTIONALIGN
    at IMAGE_OPTIONAL_HEADER32.SizeOfHeaders,         dd SIZEOFHEADERS
    at IMAGE_OPTIONAL_HEADER32.Subsystem,             dw IMAGE_SUBSYSTEM_WINDOWS_CUI
    at IMAGE_OPTIONAL_HEADER32.DllCharacteristics,    dw IMAGE_DLLCHARACTERISTICS_NO_SEH ; <===
    at IMAGE_OPTIONAL_HEADER32.NumberOfRvaAndSizes,   dd 16
iend

%include 'dd_imports.inc'

%include 'section_1fa.inc'

EntryPoint: ;*******************************************************************
    push Msg
    call [__imp__printf]
    add esp, 1 * 4

    push VEHHandler
    push -1
    call [__imp__AddVectoredExceptionHandler]

    push SEHHandler
    push dword [fs:0]
    mov [fs:0], esp

    int3
_c

SEHHandler:
    push SEHMsg
    call [__imp__printf]
    add esp, 1 * 4
_
    push 1
    call [__imp__ExitProcess]
_c

VEHHandler:
    push VEHMsg
    call [__imp__printf]
    add esp, 1 * 4
_
    push 2
    call [__imp__ExitProcess]
_c

SEHMsg db "  # Structured Exception handler triggered", 0ah, 0
VEHMsg db "  # Vectored Exception handler triggered", 0ah, 0
Msg db " * a PE with DllCharacteristics set to NO_SEH, but using a Vectored Exception Handler", 0ah, 0
_d

Import_Descriptor: ;************************************************************
istruc IMAGE_IMPORT_DESCRIPTOR
    at IMAGE_IMPORT_DESCRIPTOR.OriginalFirstThunk, dd kernel32.dll_hintnames - IMAGEBASE
    at IMAGE_IMPORT_DESCRIPTOR.Name1,              dd kernel32.dll - IMAGEBASE
    at IMAGE_IMPORT_DESCRIPTOR.FirstThunk,         dd kernel32.dll_iat - IMAGEBASE
iend
istruc IMAGE_IMPORT_DESCRIPTOR
    at IMAGE_IMPORT_DESCRIPTOR.OriginalFirstThunk, dd msvcrt.dll_hintnames - IMAGEBASE
    at IMAGE_IMPORT_DESCRIPTOR.Name1,              dd msvcrt.dll - IMAGEBASE
    at IMAGE_IMPORT_DESCRIPTOR.FirstThunk,         dd msvcrt.dll_iat - IMAGEBASE
iend
istruc IMAGE_IMPORT_DESCRIPTOR
iend
_d

kernel32.dll_hintnames dd hnExitProcess - IMAGEBASE, hnAddVectoredExceptionHandler - IMAGEBASE, 0
msvcrt.dll_hintnames   dd hnprintf - IMAGEBASE, 0
_d

hnExitProcess                 _IMAGE_IMPORT_BY_NAME 'ExitProcess'
hnAddVectoredExceptionHandler _IMAGE_IMPORT_BY_NAME 'AddVectoredExceptionHandler'
hnprintf                      _IMAGE_IMPORT_BY_NAME 'printf'
_d

kernel32.dll_iat:
__imp__ExitProcess                 dd hnExitProcess - IMAGEBASE
__imp__AddVectoredExceptionHandler dd hnAddVectoredExceptionHandler - IMAGEBASE
    dd 0

msvcrt.dll_iat:
__imp__printf dd hnprintf - IMAGEBASE
    dd 0
_d

kernel32.dll db 'kernel32.dll', 0
msvcrt.dll db 'msvcrt.dll', 0
_d

align FILEALIGN, db 0


text:
import axios from "axios";
import Immutable from "immutable";
import {map} from "ramda";
import UUID from "uuid-js";

import {WRITE_API_ENDPOINT} from "lib/config";

export const MONITORING_CHART_TYPE = "monitoring";

export const ADD_MORE_DATA = "ADD_MORE_DATA";
export const CHANGE_Y_AXIS_VALUES = "CHANGE_Y_AXIS_VALUES";
export const FAVORITE_INSERTION_SUCCESS = "FAVORITE_INSERTION_SUCCESS";
export const RESET_Y_AXIS_VALUES = "RESET_Y_AXIS_VALUES";
export const SAVE_CHART_CONFIG = "SAVE_CHART_CONFIG";
export const SELECT_CHART_TYPE = "SELECT_CHART_TYPE";
export const SELECT_FAVORITE_CHART = "SELECT_FAVORITE_CHART";
export const SELECT_PERIOD = "SELECT_PERIOD";
export const SELECT_SENSORS_TO_DRAW = "SELECT_SENSORS_TO_DRAW";
export const SELECT_TIME_INTERVAL = "SELECT_TIME_INTERVAL";
export const TOGGLE_COMPARISON_CHART = "TOGGLE_COMPARISON_CHART";

function getBasicObject (type, payload) {
    return {
        type: type,
        payload: payload
    };
}

export const addMoreData = (backward) => getBasicObject(ADD_MORE_DATA, backward);

export const addToFavorite = (state, name, userId) => {
    return dispatch => {
        dispatch({
            type: "ADDING_TO_FAVORITE"
        });
        state.config = null;
        var endpoint = "http://" + WRITE_API_ENDPOINT + "/favorite-charts";
        let favorite = {
            id: UUID.create().hex,
            name: name,
            owner: userId,
            type: MONITORING_CHART_TYPE,
            state: state
        };
        axios.post(endpoint, favorite)
            .then(() => dispatch({
                type: FAVORITE_INSERTION_SUCCESS,
                payload: favorite
            }))
            .catch(() => dispatch({
                type: "FAVORITE_INSERTION_FAIL"
            }));
    };

};

export const changeYAxisValues = (values, xAxisPeriod) => {
    return {
        type: CHANGE_Y_AXIS_VALUES,
        yAxisValues: values,
        xAxisPeriod: xAxisPeriod
    };
};

export const resetYAxisValues = () => getBasicObject(RESET_Y_AXIS_VALUES);

export const saveChartConfig = (config, yAxisDisabled) =>  {
    return {
        type: SAVE_CHART_CONFIG,
        config: config,
        yAxisDisabled: yAxisDisabled
    };
};

export const selectChartType = (chartType, xAxisPeriod) => {
    return {
        type: SELECT_CHART_TYPE,
        chartType: chartType,
        xAxisPeriod: xAxisPeriod
    };
};

export function selectFavoriteChart (favoriteChart) {
    let state = favoriteChart.get("state").toJS();
    state.sensorsToDraw = map(sensor => typeof sensor === "string" ? sensor : Immutable.fromJS(sensor), state.sensorsToDraw);
    return getBasicObject(SELECT_FAVORITE_CHART, state);
}

export const selectPeriod = (xAxisPeriod) => getBasicObject(SELECT_PERIOD, xAxisPeriod);

export function selectSensorsToDraw (sensors) {
    let sensorsArray = (Array.isArray(sensors) ? sensors : [sensors]);
    return getBasicObject(SELECT_SENSORS_TO_DRAW, sensorsArray);
}

export const toggleComparisonChart = (comparisonChart, xAxisPeriod) => {
    return {
        type: TOGGLE_COMPARISON_CHART,
        comparisonChart: comparisonChart,
        xAxisPeriod: xAxisPeriod
    };
};

export const selectTimeInterval = (timeInterval) => getBasicObject(SELECT_TIME_INTERVAL, timeInterval);


text:
/*************************************************************************/
/*  node_3d.h                                                            */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md).   */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/

#ifndef NODE_3D_H
#define NODE_3D_H

#include "scene/main/node.h"

class Node3DGizmo : public RefCounted {
	GDCLASS(Node3DGizmo, RefCounted);

public:
	virtual void create() = 0;
	virtual void transform() = 0;
	virtual void clear() = 0;
	virtual void redraw() = 0;
	virtual void free() = 0;

	Node3DGizmo();
	virtual ~Node3DGizmo() {}
};

class Node3D : public Node {
	GDCLASS(Node3D, Node);
	OBJ_CATEGORY("3D");

public:
	enum RotationEditMode {
		ROTATION_EDIT_MODE_EULER,
		ROTATION_EDIT_MODE_QUATERNION,
		ROTATION_EDIT_MODE_BASIS,
	};

	enum RotationOrder {
		ROTATION_ORDER_XYZ,
		ROTATION_ORDER_XZY,
		ROTATION_ORDER_YXZ,
		ROTATION_ORDER_YZX,
		ROTATION_ORDER_ZXY,
		ROTATION_ORDER_ZYX
	};

private:
	enum TransformDirty {
		DIRTY_NONE = 0,
		DIRTY_VECTORS = 1,
		DIRTY_LOCAL = 2,
		DIRTY_GLOBAL = 4
	};

	mutable SelfList<Node> xform_change;

	struct Data {
		mutable Transform3D global_transform;
		mutable Transform3D local_transform;
		mutable Basis::EulerOrder rotation_order = Basis::EULER_ORDER_YXZ;
		mutable Vector3 rotation;
		mutable Vector3 scale = Vector3(1, 1, 1);
		mutable RotationEditMode rotation_edit_mode = ROTATION_EDIT_MODE_EULER;

		mutable int dirty = DIRTY_NONE;

		Viewport *viewport = nullptr;

		bool top_level_active = false;
		bool top_level = false;
		bool inside_world = false;

		RID visibility_parent;

		int children_lock = 0;
		Node3D *parent = nullptr;
		List<Node3D *> children;
		List<Node3D *>::Element *C = nullptr;

		bool ignore_notification = false;
		bool notify_local_transform = false;
		bool notify_transform = false;

		bool visible = true;
		bool disable_scale = false;

#ifdef TOOLS_ENABLED
		Vector<Ref<Node3DGizmo>> gizmos;
		bool gizmos_disabled = false;
		bool gizmos_dirty = false;
		bool transform_gizmo_visible = true;
#endif

	} data;

	NodePath visibility_parent_path;

	void _update_gizmos();
	void _notify_dirty();
	void _propagate_transform_changed(Node3D *p_origin);

	void _propagate_visibility_changed();

	void _propagate_visibility_parent();
	void _update_visibility_parent(bool p_update_root);

protected:
	_FORCE_INLINE_ void set_ignore_transform_notification(bool p_ignore) { data.ignore_notification = p_ignore; }

	_FORCE_INLINE_ void _update_local_transform() const;

	void _notification(int p_what);
	static void _bind_methods();

	virtual void _validate_property(PropertyInfo &property) const override;

	bool property_can_revert(const String &p_name);
	Variant property_get_revert(const String &p_name);

public:
	enum {
		NOTIFICATION_TRANSFORM_CHANGED = SceneTree::NOTIFICATION_TRANSFORM_CHANGED,
		NOTIFICATION_ENTER_WORLD = 41,
		NOTIFICATION_EXIT_WORLD = 42,
		NOTIFICATION_VISIBILITY_CHANGED = 43,
		NOTIFICATION_LOCAL_TRANSFORM_CHANGED = 44,
	};

	Node3D *get_parent_node_3d() const;

	Ref<World3D> get_world_3d() const;

	void set_position(const Vector3 &p_position);

	void set_rotation_edit_mode(RotationEditMode p_mode);
	RotationEditMode get_rotation_edit_mode() const;

	void set_rotation_order(RotationOrder p_order);
	void set_rotation(const Vector3 &p_euler_rad);
	void set_scale(const Vector3 &p_scale);

	Vector3 get_position() const;

	RotationOrder get_rotation_order() const;
	Vector3 get_rotation() const;
	Vector3 get_scale() const;

	void set_transform(const Transform3D &p_transform);
	void set_basis(const Basis &p_basis);
	void set_quaternion(const Quaternion &p_quaternion);
	void set_global_transform(const Transform3D &p_transform);

	Transform3D get_transform() const;
	Basis get_basis() const;
	Quaternion get_quaternion() const;
	Transform3D get_global_transform() const;

#ifdef TOOLS_ENABLED
	virtual Transform3D get_global_gizmo_transform() const;
	virtual Transform3D get_local_gizmo_transform() const;
	virtual void set_transform_gizmo_visible(bool p_enabled) { data.transform_gizmo_visible = p_enabled; };
	virtual bool is_transform_gizmo_visible() const { return data.transform_gizmo_visible; };
#endif

	void set_disable_gizmos(bool p_enabled);
	void update_gizmos();
	void set_subgizmo_selection(Ref<Node3DGizmo> p_gizmo, int p_id, Transform3D p_transform = Transform3D());
	void clear_subgizmo_selection();
	Vector<Ref<Node3DGizmo>> get_gizmos() const;
	Array get_gizmos_bind() const;
	void add_gizmo(Ref<Node3DGizmo> p_gizmo);
	void remove_gizmo(Ref<Node3DGizmo> p_gizmo);
	void clear_gizmos();

	void set_as_top_level(bool p_enabled);
	bool is_set_as_top_level() const;

	void set_disable_scale(bool p_enabled);
	bool is_scale_disabled() const;

	_FORCE_INLINE_ bool is_inside_world() const { return data.inside_world; }

	Transform3D get_relative_transform(const Node *p_parent) const;

	void rotate(const Vector3 &p_axis, real_t p_angle);
	void rotate_x(real_t p_angle);
	void rotate_y(real_t p_angle);
	void rotate_z(real_t p_angle);
	void translate(const Vector3 &p_offset);
	void scale(const Vector3 &p_ratio);

	void rotate_object_local(const Vector3 &p_axis, real_t p_angle);
	void scale_object_local(const Vector3 &p_scale);
	void translate_object_local(const Vector3 &p_offset);

	void global_rotate(const Vector3 &p_axis, real_t p_angle);
	void global_scale(const Vector3 &p_scale);
	void global_translate(const Vector3 &p_offset);

	void look_at(const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0));
	void look_at_from_position(const Vector3 &p_pos, const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0));

	Vector3 to_local(Vector3 p_global) const;
	Vector3 to_global(Vector3 p_local) const;

	void set_notify_transform(bool p_enabled);
	bool is_transform_notification_enabled() const;

	void set_notify_local_transform(bool p_enabled);
	bool is_local_transform_notification_enabled() const;

	void orthonormalize();
	void set_identity();

	void set_visible(bool p_visible);
	void show();
	void hide();
	bool is_visible() const;
	bool is_visible_in_tree() const;

	void force_update_transform();

	void set_visibility_parent(const NodePath &p_path);
	NodePath get_visibility_parent() const;

	Node3D();
};

VARIANT_ENUM_CAST(Node3D::RotationEditMode)
VARIANT_ENUM_CAST(Node3D::RotationOrder)

#endif // NODE_3D_H


text:
exports.buildController = function (params) {
  var logPrefix = params.controllerName + '.controller';
  if (process.appParams.color) logPrefix = logPrefix.yellow;
  return function (request, reqParams = {}, response) {
    request.logToConsole(logPrefix, reqParams);

    // make sure an admin is logged, or return an error page
    reqParams.loggedUser = request.getUser();
    if (params.adminOnly && !request.checkAdmin(response)) {
      console.log(logPrefix, 'must be logged as admin');
      return;
    }

    function render(res) {
      if (!res) {
        response.badRequest();
        console.log(logPrefix, '=> bad request');
      } else if (res.tsv) {
        response.legacyRender(res.tsv, null, { 'content-type': 'text/tsv' });
        console.log(logPrefix, '=> returned TSV');
      } else if (res.csv) {
        response.legacyRender(res.csv, null, { 'content-type': 'text/csv' });
        console.log(logPrefix, '=> returned CSV');
      } else if (res.html) {
        response.renderHTML(res.html);
        console.log(logPrefix, '=> returned HTML');
      } else if (res.json) {
        if (request.headers['user-agent']) {
          response.legacyRender(JSON.stringify(res.json, null, 2));
          console.log(logPrefix, '=> returned stringified JSON');
        } else {
          response.renderJSON(res.json);
          console.log(logPrefix, '=> returned JSON');
        }
      } else {
        response.legacyRender(res);
        console.log(logPrefix, '=> returned plain text');
      }
    }

    var processor = params.actions[reqParams.action];

    if (processor) {
      console.log(logPrefix, 'action: ' + reqParams.action + '...');
      processor(reqParams, render);
    } else
      render({
        html: [
          '
invalid action
',
          '
available actions:
',
          '

',
          Object.keys(params.actions).join('

'),
          '

',
        ].join('\n'),
      });
  };
};


text:
function hh = pez_freq(b,a,n,dum,Fs)
%
%  pez_freq()::Does Z-Plane freq response butchered from freqz()
%  for PeZ v3.0b last Rev 11/10/97  -- No Modifications without author's consent
%  (type 'pez' in MATLAB to run)
%  Craig Ulmer / GRiMACE@ee.gatech.edu

a = a(:).';
b = b(:).';
na = max(size(a));
nb = max(size(b));
nn = max(size(n));
%	Frequency vector specified.  Use Horner's method of polynomial
%	evaluation at the frequency points and divide the numerator
%	by the denominator.
    a = [a zeros(1,nb-na)];  % Make sure a and b have the same length
    b = [b zeros(1,na-nb)];
        w = n;
        s = exp(sqrt(-1)*w);
    h = polyval(b,s) ./ polyval(a,s);

    hh = h;


text: require('./kirbi').login();


text:
from sys import maxsize
import re

def simplify_phone(s):
    return re.sub("\(*.0*.\)|[\- ()]", "", s).replace("+41", "0")

def merge_phones_like_on_home_page(contact):
    return "\n".join(
        filter(None, map(
            lambda x: simplify_phone(x), filter(
                None, [contact.home_phone, contact.mobile_phone,
                       contact.work_phone, contact.secondary_phone]))))

def merge_emails_like_on_home_page(contact):
    return "\n".join(filter(None, [contact.email, contact.email2, contact.email3]))

def model_to_view(c):
    return Contact(
        id=c.id,
        last_name=" ".join(c.last_name.split()),
        first_name=" ".join(c.first_name.split()),
        address='\n'.join([_.strip() for _ in re.sub(' +', ' ', c.address).split('\n')]),
        all_emails_from_home_page=merge_emails_like_on_home_page(c),
        all_phones_from_home_page=merge_phones_like_on_home_page(c)
    )

class Contact:

    def __init__(self, first_name=None, middle_name=None, last_name=None, nickname=None, title=None, company=None,
                 address=None, home_phone=None, mobile_phone=None, work_phone=None, fax=None, email=None, email2=None,
                 email3=None, homepage=None, address2=None, secondary_phone=None, notes=None, id=None,
                 all_phones_from_home_page=None, all_emails_from_home_page=None):
        self.first_name = first_name
        self.last_name = last_name
        self.home_phone = home_phone
        self.mobile_phone = mobile_phone
        self.work_phone = work_phone
        self.secondary_phone = secondary_phone
        self.notes = notes
        self.address = address
        self.address2 = address2
        self.homepage = homepage
        self.email3 = email3
        self.email2 = email2
        self.email = email
        self.fax = fax
        self.company = company
        self.title = title
        self.nickname = nickname
        self.middle_name = middle_name
        self.all_phones_from_home_page = all_phones_from_home_page
        self.all_emails_from_home_page = all_emails_from_home_page
        self.id = id

    def __repr__(self):
        return "%s:%s;%s;%s;%s;%s" % (self.id, self.first_name, self.last_name, self.address,
                                   self.all_emails_from_home_page, self.all_phones_from_home_page)

    def __eq__(self, other):
        return (self.id is None or other.id is None or self.id == other.id) and (
            self.first_name == other.first_name
            and self.last_name == other.last_name
            and self.address == other.address
            and self.all_emails_from_home_page == other.all_emails_from_home_page
            and self.all_phones_from_home_page == other.all_phones_from_home_page)

    def id_or_max(self):
        if self.id:
            return int(self.id)
        else:
            return maxsize