Html/Javascript widget

Wednesday, 20 July 2022

Dependency Injection

In software engineering, dependency injection is a design pattern in which an object or function receives other objects or functions that it depends on. A form of inversion of control, dependency injection aims to separate the concerns of constructing objects and using them, leading to loosely coupled programs. The pattern ensures that an object or function which wants to use a given service should not have to know how to construct those services. Instead, the receiving 'client' (object or function) is provided with its dependencies by external code (an 'injector'), which it is not aware of. Dependency injection helps by making implicit dependencies explicit and helps solve the following problems: How can a class be independent from the creation of the objects it depends on? How can an application, and the objects it uses support different configurations? How can the behavior of a piece of code be changed without editing it directly? Fundamentally, dependency injection consists of passing parameters to a method. Because the client does not build or find the service itself, it typically only needs to declare the interfaces of the services it uses, rather than their concrete implementations. This makes it easier to change which services are actually used at runtime, especially in statically-typed languages where changing the underlying objects would otherwise require re-compiling the source code.

Monday, 18 July 2022

Multithreading

In computer architecture, multithreading is the ability of a central processing unit (CPU) (or a single core in a multi-core processor) to provide multiple threads of execution concurrently, which differs from multiprocessing. Threads use the memory of the process they belong to. Inter-process communication is slow as processes have different memory addresses. Inter-thread communication can be faster than inter-process communication because threads of the same process share memory with the process they belong to.

 Where multiprocessing systems include multiple complete processing units in one or more cores, multithreading aims to increase utilization of a single core by using thread-level parallelism, as well as instruction-level parallelism. As the two techniques are complementary, they are combined in nearly all modern systems architectures with multiple multithreading CPUs and with CPUs with multiple multithreading cores.

If a thread gets a lot of cache misses, the other threads can continue taking advantage of the unused computing resources, which may lead to faster overall execution, as these resources would have been idle if only a single thread were executed. Also, if a thread cannot use all the computing resources of the CPU (because instructions depend on each other's result), running another thread may prevent those resources from becoming idle.

Downside is that multiple threads can interfere with each other when sharing hardware resources such as caches or translation lookaside buffers (TLBs). As a result, execution times of a single thread are not improved and can be degraded, even when only one thread is executing, due to lower frequencies or additional pipeline stages that are necessary to accommodate thread-switching hardware.

shared resources (Tanenbaum):

among processes among threads
Address space; global variables; open files; child processes; pending alarms; signals and signal handlers; accounting info; programme counter; registers; stack; state;



From the software standpoint, hardware support for multithreading is more visible to software, requiring more changes to both application programs and operating systems than multiprocessing. Hardware techniques used to support multithreading often parallel the software techniques used for computer multitasking. Thread scheduling is also a major problem in multithreading. 

Threads use the memory of the process they belong to. Inter-process communication is slow as processes have different memory addresses. Inter-thread communication can be faster than inter-process communication because threads of the same process share memory with the process they belong to.



Monday, 4 July 2022

NPIV

 NPIV or N_Port ID Virtualisation is a Fibre Channel feature, consisting of several Fibre Channel node port (N_Port) IDs sharing a single physical N_Port, which lets multiple Fibre Channel initiators occupy a single physical port. This isolates the virtual server's storage from other virtual servers' storage. 

 

N_Port initialization with and without NPIV

Normally N_Port initialisation goes like this:

1- N_Port sends FLOGI to address 0xFFFFFE to obtain a valid address
2- N_Port sends PLOGI to address 0xFFFFFC to register this address with the name server
3- N_Port sends SCR to address 0xFFFFFD to register for state change notifications

When NPIV enters the picture it may proceed like this:

4- N_Port sends FDISC to address 0xFFFFFE to obtain an additional address
5- N_Port sends PLOGI to address 0xFFFFFC to register this additional address with the name server
6- N_Port sends SCR to address 0xFFFFFD to register for state change notifications.


7- ... (repeat FDISC/PLOGI/SCR for next address)

FDISC is an abbreviation for Fabric Discovery, or "Discover Fabric Service Parameters", which is a misnomer. It behaves just like FLOGI.

Sunday, 3 July 2022

Cohesion - a simple explanation

 Cohesion in Java is the Object-Oriented principle that a class should be designed with a single, well-defined purpose. The more focused a class is, the more is the cohesiveness of that class. Having high cohesion ensures that classes are easier to maintain, requiring fewer changes than low-cohesion classes. It also ensure a higher degree of reusability than classes with low cohesion.

example of class without cohesion:

public class Worker (){
String name;
double [] pay;

public void setName (String name){
this.name = name;
}

public String getName(){
return name;
}

public void setPay(double[] pay){
this.pay = pay;
}

public double calculateMeans(){
return means;
}

}

//

The same class with improved cohesion:

public class Worker (){
String name;
double [] pay;

public void setName (String name){
this.name = name;
}

public String getName(){
return name;
}

public class Pay {
Worker worker;
double [] pay;
public Pay (Worker, worker){
this.worker = worker;
}

}



public void setPay(double[] pay){
this.pay = pay;
}

public double calculateMeans(){
return means;
}

}

 

 

Notice the addition of the class Pay, improving the focus of the code and making it more cohesive. 

Monday, 27 June 2022

OAuth

 OAuth ("Open Authorization") is an open standard for access delegation, commonly used as a way for internet users to grant websites or applications access to their information on other websites but without giving them the passwords. This mechanism is used by companies such as Amazon,Google, Facebook, Microsoft, and Twitter to permit the users to share information about their accounts with third-party applications or websites.

Generally, OAuth provides clients a "secure delegated access" to server resources on behalf of a resource owner. It specifies a process for resource owners to authorize third-party access to their server resources without providing credentials. Designed specifically to work with Hypertext Transfer Protocol (HTTP), OAuth essentially allows access tokens to be issued to third-party clients by an authorization server, with the approval of the resource owner. The third party then uses the access token to access the protected resources hosted by the resource server. In particular, OAuth 2.0 provides specific authorization flows for web applications, desktop applications, mobile phones, and smart devices.


OAuth 2.0 has been analyzed using formal web protocol analysis. This analysis revealed that in setups with multiple authorization servers, one of which is behaving maliciously, clients can become confused about the authorization server to use and may forward secrets to the malicious authorization server (AS Mix-Up Attack).This prompted the creation of a new best current practice internet draft that sets out to define a new security standard for OAuth 2.0. Assuming a fix against the AS Mix-Up Attack in place, the security of OAuth 2.0 has been proven under strong attacker models using formal analysis.

Typescript

TypeScript is a programming language developed and maintained by Microsoft. It is a strict syntactical superset of JavaScript, adding optional static typing to the language. It is designed for the development of large applications and transpiles to JavaScript.
It may be used to develop JavaScript applications for both client-side and server-side execution (as with Node.js or Deno). Multiple options are available for transpilation. The default TypeScript Compiler can be used, or the Babel compiler can be invoked to convert TypeScript to JavaScript.

TypeScript provides static typing through type annotations to enable type checking at compile time. This is optional and can be ignored to use the regular dynamic typing of JavaScript.

function add(left: number, right: number): number {
    return left + right;
}

The annotations for the primitive types are number, boolean and string. Typescript also supports data types with the following annotations: Array, Enums and void.

Additional data types are: Tuple, Union, never and any. An array with predefined data types at each index is Tuple type. A variable that holds more than one type of data is Union type. When you are sure that something is never going to occur you use never type. Weakly- or dynamically-typed structures are of any type. 

Tuesday, 14 June 2022

understanding how JSON handles integers and strings

In JSON, values must be one of the following data types:

  • a string
  • a number
  • an object (JSON object)
  • an array
  • a boolean
  • null

JSON values cannot be one of the following data types:

  • a function
  • a date
  • undefined

 

Strings in JSON must always be in double quotes.
Example
{"name":"John"}


Numbers can be either an integer or a floating point.
Example
{"age":30}


JSON also supports objects, following the JSON syntax:

"obj name": {"field":value}

if value is a string, must be between double quotes.


"obj name": {"field":"string Value"}

 

The following is an example to illustrate both integers and strings

<!DOCTYPE html>
<html>
<body>
<p id="test"> </p>

<script>
var mytxt='{"emp":[' +
'{"name":"paul","surname":"smith","age":25,
"admission":2015},'+
'{"name":"maria","surname":"sheeva","age":20,
"admission":2016},'+
'{"name":"jon","surname":"shiv","age":27,
"admission":2012}]}';

var obj = eval ("(" + mytxt + ")");
document.getElementById("test").innerHTML =
obj.emp[1].name+ " " + obj.emp[1].surname + " " +
obj.emp[1].age + obj.emp[1].admission;
document.getElementById("test").innerHTML =
obj.emp[0].age + obj.emp[1].age + " " +
obj.emp[2].age;
document.getElementById("test").innerHTML =
obj.emp[2].admission + obj.emp[0].admission +
obj.emp[1].admission;


</script>

</body>


</html>


The output of the code above will be 6043, since document.getElementById receives whatever the containers obj.emp[2].admission, obj.emp[0].admission and obj.emp[1].admission hold, adding up the integer values passed in the parameters (2015+2016+2012)